###### #准备镜像文件. rhel7_template.img, description file. Rhel7.xml placed inside/var/lib/libvirt/images/
###### #记得把文件改成隐藏文件
#!/bin/bash
Exit code:
User Input Nothing
User input is not a number
User input out of range
VM Disk image exists
Img_dir=/var/lib/libvirt/images
Basevm=rh7_template # # #镜像文件模版名
Room=sed -n "1p" /etc/hostname | sed -r ‘s/(room)([0-9]{1,})(.*)/\2/‘
If [$ROOM-le 9];then
Room=0$room
Fi
ip=sed -n "1p" /etc/hostname | sed -r ‘s/(.*)([0-9]+)(.*)/\2/‘
Read-p "Enter VM number:" Vmnum
If [$VMNUM-le 9];then
Vmnum=0$vmnum
Fi
If [-Z "${vmnum}"]; Then
echo "You must input a number."
Exit 65
elif [$ (echo ${vmnum}*1 | BC) = 0]; Then
echo "You must input a number."
Exit 66
elif [${vmnum}-lt 1-o ${vmnum}-gt 99]; Then
echo "Input out of range"
Exit 67
Fi
Newvm=rh7_node${vmnum}
If [-e $IMG _dir/${newvm}.img]; Then
echo "File exists."
Exit 68
Fi
Echo-en "Creating Virtual machine Disk image......\t"
Qemu-img create-f qcow2-b $IMG _dir/.${basevm}.img $IMG _dir/${newvm}.img &>/dev/null
Echo-e "\e[32;1m[ok]\e[0m"
#virsh dumpxml ${BASEVM} >/tmp/myvm.xml
Cat/var/lib/libvirt/images/.rhel7.xml >/tmp/myvm.xml ##.rhel7.xml description file
Sed-i "/<name>${basevm}/s/${basevm}/${newvm}/"/tmp/myvm.xml
Sed-i "/uuid/s/<uuid>.*<\/uuid>/<uuid>$ (Uuidgen) <\/uuid>/"/tmp/myvm.xml
Sed-i "/${basevm}.img/s/${basevm}/${newvm}/"/tmp/myvm.xml
Sed-i "/mac/s/a1/${room}/"/tmp/myvm.xml
Sed-i "/mac/s/a2/${ip}/"/tmp/myvm.xml
Sed-i "/mac/s/a3/${vmnum}/"/tmp/myvm.xml
Sed-i "/mac/s/b1/${room}/"/tmp/myvm.xml
Sed-i "/mac/s/b2/${ip}/"/tmp/myvm.xml
Sed-i "/mac/s/b3/${vmnum}/"/tmp/myvm.xml
Sed-i "/mac/s/c1/${room}/"/tmp/myvm.xml
Sed-i "/mac/s/c2/${ip}/"/tmp/myvm.xml
Sed-i "/mac/s/c3/${vmnum}/"/tmp/myvm.xml
Sed-i "/mac/s/d1/${room}/"/tmp/myvm.xml
Sed-i "/mac/s/d2/${ip}/"/tmp/myvm.xml
Sed-i "/mac/s/d3/${vmnum}/"/tmp/myvm.xml
Echo-en "Defining new Virtual machine......\t\t"
Virsh Define/tmp/myvm.xml &>/dev/null
Echo-e "\e[32;1m[ok]\e[0m"
Linux scripts quickly clone virtual machines (multiple virtual network cards)