4、虚拟机单机、集群的克隆、删除脚本(以初始化好的虚拟机为模板)
虚拟机克隆、删除脚本
[root@ubunt ~]# cat clone.sh
#!/bin/bash
#
#. /etc/init.d/functions (如果是ubuntu,注释此行)
Red="\e[1;31m"
Purple="\e[1;35m"
Green="\e[1;32m"
Blue="\e[1;36m"
Yellow="\e[1;33m"
End="\e[0m"
#克隆集群
clone() {
lists=`virsh list --all |awk '/.*template.*/{print $2}'`
echo "模板清单:"
echo "<-------------------->"
for x in $lists;do
echo -e "$Purple$x$End"
done
echo "<-------------------->"
read -p "请输入克隆模板: " template
#read -p "请输入克隆模板ip: " ip
read -p "请输入集群名称: " name
read -p "请输入节点数: " node
read -p "请输入ip起点x(y.y.y.x): " num
for ((i=0;i<$node;i++)) ;do
let n=$i+1
arg[$i]="$name-0$n"
virt-clone -o $template -n ${arg[$i]} -f /var/lib/libvirt/images/${arg[$i]}.qcow2 || continue
virsh start ${arg[$i]} || continue
sleep 30
echo $num ${arg[$i]}
if [[ $template =~ "ubuntu" ]];then
ssh root@10.0.0.100 "sed -i -r 's@(^[[:space:]]+- 10.0.0.)[0-9]+@\1$num@' /etc/netplan/00-installer-config.yaml;hostnamectl set-hostname ${arg[$i]} ;reboot"
else
ssh root@192.168.122.100 "sed -i -r 's@(^IPADDR=).*@\1192.168.122.$num@' /etc/sysconfig/network-scripts/ifcfg-eth0;hostnamectl set-hostname ${arg[$i]} ;reboot"
fi
let num=$num+1
done
}
#删除集群
delete(){
lists1=`virsh list --all | awk 'NR!=1{print $2}'|sed -nr "s/^(.*)-[0-9]+$/\1/p" |uniq`
echo "<-------------------->"
for n in $lists1 ;do
echo -e "$Purple$n$End"
done
echo "<-------------------->"
dname=1
echo $dname
read -p "请输入集群名称:" dname
echo $danme
for i in `virsh list --all| awk -v dname=$dname '$0 ~ dname{print $2}'`;do
echo -e "$Red$i$End"
if [[ $i =~ "template" ]]; then
continue;
elif [[ $dname == "" ]];then
action "删除失败" false
exit
else
virsh destroy $i &> /dev/null
virsh undefine --remove-all-storage $i &> /dev/null && action "删除成功" true || action "删除失败" false
fi
done
}
#单节点启动
nodestart(){
echo "<-------------------->"
for m in `virsh list --all | egrep ".*off$"|awk '{print $2}'` ;do
echo -e "$Purple$m$End"
done
echo "<-------------------->"
read -p "请输入节点名称:" startnode
virsh start $startnode &> /dev/null && action "$startnode 节点启动成功" true || action "$startnode 节点启动失败" false
}
#单节点重启
noderestart(){
echo "<-------------------->"
for o in `virsh list --all | egrep ".*running$"|awk '{print $2}'` ;do
echo -e "$Purple$o$End"
done
echo "<-------------------->"
read -p "请输入节点名称:" restartnode
virsh reboot $restartnode &> /dev/null && action "$restartnode 节点重启成功" true || action "$restartnode 节点重启失败" false
}
#单节点关机
nodestop(){
echo "<-------------------->"
for p in `virsh list --all | egrep ".*running$"|awk '{print $2}'` ;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入节点名称:" stopnode
virsh shutdown $stopnode &> /dev/null && action "$stopnode 节点关机成功" true || action "$stopnode 节点关机失败" false
}
#启动集群
clusterstart(){
lists2=`virsh list --all | awk 'NR!=1{print $2}'|sed -nr "s/^(.*)-[0-9]+$/\1/p"|sort -nr|uniq`
echo "<-------------------->"
for q in $lists2 ;do
echo -e "$Purple$q$End"
done
echo "<-------------------->"
read -p "请输入集群名称:" dname
for i1 in `virsh list --all| awk -v dname=$dname '$0 ~ dname{print $2}'`;do
echo -e "$Red$i1$End"
virsh start $i1 &> /dev/null && action "$i1 节点启动成功" true || action "$i1 节点启动失败" false
done
}
#关闭集群
clusterstop(){
lists3=`virsh list --all | awk 'NR!=1{print $2}'|sed -nr "s/^(.*)-[0-9]+$/\1/p" |sort -nr|uniq`
echo "<-------------------->"
for r in $lists3 ;do
echo -e "$Purple$r$End"
done
echo "<-------------------->"
read -p "请输入集群名称:" dname
for i2 in `virsh list --all| awk -v dname=$dname '$0 ~ dname{print $2}'`;do
echo -e "$Red$i2$End"
virsh shutdown $i2 &> /dev/null && action "$i2 节点关闭成功" true || action "$i2 节点关闭失败" false
done
}
#集群重启
clusterrestart(){
lists4=`virsh list --all | awk 'NR!=1{print $2}'|sed -nr "s/^(.*)-[0-9]+$/\1/p" | sort -nr|uniq`
echo "<-------------------->"
for s in $lists4 ;do
echo -e "$Purple$s$End"
done
echo "<-------------------->"
read -p "请输入集群名称:" dname
for i3 in `virsh list --all| awk -v dname=$dname '$0 ~ dname{print $2}'`;do
echo -e "$Red$i3$End"
virsh reboot $i3 &> /dev/null && action "$i3 节点重启成功" true || action "$i3 节点重启失败" false
done
}
#单机克隆
clone_Single(){
echo "<-------------------->"
for p in `virsh list --all |awk '{print $2}'` ;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入克隆模板: " templatecs
#read -p "请输入克隆模板ip: " ip
read -p "请输入名称: " csname
read -p "请输入ipx(y.y.y.x): " num1
virt-clone -o $templatecs -n $csname -f /var/lib/libvirt/images/$csname.qcow2 || continue
virsh start $csname || continue
sleep 60
echo $csname
if [[ $templatecs =~ "ubuntu" ]];then
ssh root@10.0.0.100 "sed -i -r 's@(^[[:space:]]+- 10.0.0.)[0-9]+@\1$num1@' /etc/netplan/00-installer-config.yaml;hostnamectl set-hostname $csname ;reboot"
else
ssh root@192.168.100.10 "sed -i -r 's@(^IPADDR=).*@\1192.168.10.$num1@' /etc/sysconfig/network-scripts/ifcfg-eth0;hostnamectl set-hostname $csname ;reboot"
fi
}
#单机删除
delete_Single(){
echo "<-------------------->"
for p in `virsh list --all |awk '{print $2}'` ;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入机器名称:" dsname
echo $dsname
if [[ $i =~ "template" ]]; then
exit;
elif [[ $dsname == "" ]];then
action "删除失败" false
exit
else
virsh destroy $dsname &> /dev/null
virsh undefine --remove-all-storage $dsname &> /dev/null && action "删除成功" true || action "删除失败" false
fi
}
#创建快照
create_snapshot(){
echo "<-------------------->"
for p in `virsh list --all |awk '{print $2}'` ;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请选择你要创建快照的机器:" shname
virsh snapshot-list $shname
read -p "请输入创建的快照名称:" createsnap
virsh snapshot-create-as $shname $createsnap && echo -e "$Blue 创建成功 $End" || echo -e "$Red 创建失败 $End"
virsh snapshot-list $shname
}
#删除快照
delete_snapshot(){
echo "<-------------------->"
for p in `virsh list --all |awk '{print $2}'` ;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入你要删除快照的机器:" dshname
virsh snapshot-list $dshname
read -p "请输入删除的快照名称:" deletesnap
virsh snapshot-delete $dshname $deletesnap && echo -e "$Blue 删除成功 $End" || echo -e "$Red 删除失败 $End"
virsh snapshot-list $dshname
}
#恢复快照
recover_snapshot(){
echo "<-------------------->"
for p in `virsh list --all |awk '{print $2}'` ;do
echo -e "$Purple$p$End"
done
echo "<-------------------->"
read -p "请输入你要恢复快照的机器:" rshname
virsh snapshot-list $rshname
read -p "请输入恢复的快照名称:" recoversnap
virsh snapshot-revert $rshname $recoversnap && echo -e "$Blue 恢复成功 $End" || echo -e "$Red 恢复失败 $End"
virsh snapshot-list $rshname
}
virsh list --all
PS3="请按要求输入操作选项:"
select lists in 克隆集群 删除集群 启动集群 关闭集群 集群重启 单节点启动 单节点关机 单节点重启 克隆单机 单机删除 创建快照 删除快照 恢复快照 退出;do
case $REPLY in # 1 2 3 4 5 6 7 8 9 10 11 12 13
1)
echo -e "$Blue$lists$End"
clone
;;
2)
echo -e "$Red$lists$End"
delete
;;
3)
echo -e "$Green$lists$End"
clusterstart
;;
4)
echo -e "$Red$lists$End"
clusterstop
;;
5)
echo -e "$Yellow$lists$End"
clusterrestart
;;
6)
echo -e "$Green$lists$End"
nodestart
;;
7)
echo -e "$Red$lists$End"
nodestop
;;
8)
echo -e "$Yellow$lists$End"
noderestart
;;
9)
echo -e "$Blue$lists$End"
clone_Single
;;
10)
echo -e "$Blue$lists$End"
delete_Single
;;
11)
echo -e "$Blue$lists$End"
create_snapshot
;;
12)
echo -e "$Blue$lists$End"
delete_snapshot
;;
13)
echo -e "$Blue$lists$End"
recover_snapshot
;;
14)
echo -e "$Blue$lists$End"
exit
;;
*)
echo -e "$Red输入错误$End"
;;
esac
done