kvm 一键部署

2019-04-23  本文已影响0人  强_b75e

批量创建虚拟机 先要创建模板

!/bin/bash

while :
do
cat << EOF
1.创建自定义配置单个虚拟机
2.批量创建自定义配置虚拟机
3.批量创建默认配置虚拟机
4.删除虚拟机
5.退出
EOF
read -p "请选择:" kvm
function wang () {
read -p "请输入新虚拟机名称:" newname
read -p "请输入新虚拟机mem大小(单位KiB):" newmem
read -p "请输入新虚拟机cpu核数:" newcpu
img=/var/lib/libvirt/images/kvm1.qcow2
xml=/etc/libvirt/qemu/kvm1.xml
newimg=/var/lib/libvirt/images/{newname}.qcow2 newxml=/etc/libvirt/qemu/{newname}.xml
newuuid=uuidgen
newmac=openssl rand -hex 3 | sed -r 's/..\B/&:/g'

cp -v xmlnewxml
cp -v imgnewimg

sed -ri "/^[ \t]<name>/c\ <name>newname\</name\>"newxml
sed -ri "/^[ \t]
<uuid>/c\ <uuid>newuuid\</uuid\>"newxml
sed -ri "/^[ \t]<memory/c\ <memory unit='KiB'>newmem\</memory\>"newxml
sed -ri "/^[ \t]
<currentMemory/c\ <currentMemory unit='KiB'>newmem\</currentMemory\>"newxml
sed -ri "/^[ \t]<vcpu/c\ <vcpu placement='static'>newcpu\</vcpu\>"newxml
sed -ri "/^[ \t]
<source file/c\ <source file='{newimg}'/>"newxml
sed -ri "/^[ \t]*<mac/c\ <mac address='52:54:00:newmac'/\>"newxml

virsh define newxml virsh list --all } casekvm in

wang
;;

read -p "请输入要安装几次虚拟机:" rr
for i in seq $rr
do
wang
done
;;

read -p "请输入要安装几次虚拟机:" tt
for i in seq $tt
do
newname=openssl rand -hex 3 | sed -r 's/..\B/&:/g'
img=/var/lib/libvirt/images/kvm1.qcow2
xml=/etc/libvirt/qemu/kvm1.xml
newimg=/var/lib/libvirt/images/{newname}.qcow2 newxml=/etc/libvirt/qemu/{newname}.xml
newuuid=uuidgen
newmac=openssl rand -hex 3 | sed -r 's/..\B/&:/g'

cp -v imgnewimg
cp -v xmlnewxml

sed -ri "/^[ /t]<name>/c\ <name>newname\</name\>"newxml
sed -ri "/^[ /t]
<uuid>/c\ <uuid>newuuid\</uuid\>"newxml
sed -ri "/^[ /t]<memory/c\ <memory unit='KiB'>1500000</memory>" newxml sed -ri "/^[ /t]*<currentMemory/c\ <currentMemory unit='KiB'\>1500000\</currentMemory\>"newxml
sed -ri "/^[ /t]
<vcpu/c\ <vcpu placement='static'>1</vcpu>" newxml sed -ri "/^[ \t]*<source file/c\ <source file='{newimg}'/>" newxml sed -ri "/^[ /t]*<mac/c\ <mac address='52:54:00:newmac'/>" $newxml

virsh define $newxml
virsh list --all
done
;;

read -p "请输入要删除的虚拟机名称:" newname
newimg=/var/lib/libvirt/images/{newname}.qcow2 newxml=/etc/libvirt/qemu/{newname}.xml

virsh undefine $newname

rm -rf newimg rm -rfnewxml

echo "已删除!"
;;

exit

;;

*)
;;

esac
done

上一篇下一篇

猜你喜欢

热点阅读