批量创建虚拟机 先要创建模板
!/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}.xml
newuuid=uuidgen
newmac=openssl rand -hex 3 | sed -r 's/..\B/&:/g'
cp -v newxml
cp -v newimg
sed -ri "/^[ \t]<name>/c\ <name>newxml
sed -ri "/^[ \t]<uuid>/c\ <uuid>newxml
sed -ri "/^[ \t]<memory/c\ <memory unit='KiB'>newxml
sed -ri "/^[ \t]<currentMemory/c\ <currentMemory unit='KiB'>newxml
sed -ri "/^[ \t]<vcpu/c\ <vcpu placement='static'>newxml
sed -ri "/^[ \t]<source file/c\ <source file='newxml
sed -ri "/^[ \t]*<mac/c\ <mac address='52:54:00:newxml
virsh define kvm 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}.xml
newuuid=uuidgen
newmac=openssl rand -hex 3 | sed -r 's/..\B/&:/g'
cp -v newimg
cp -v newxml
sed -ri "/^[ /t]<name>/c\ <name>newxml
sed -ri "/^[ /t]<uuid>/c\ <uuid>newxml
sed -ri "/^[ /t]<memory/c\ <memory unit='KiB'>1500000</memory>" newxml
sed -ri "/^[ /t]<vcpu/c\ <vcpu placement='static'>1</vcpu>" {newimg}'/>" newmac'/>" $newxml
virsh define $newxml
virsh list --all
done
;;
read -p "请输入要删除的虚拟机名称:" newname
newimg=/var/lib/libvirt/images/{newname}.xml
virsh undefine $newname
rm -rf newxml
echo "已删除!"
;;
exit
;;
*)
;;
esac
done
网友评论