Linux scripts quickly clone virtual machines (multiple virtual network cards)

Source: Internet
Author: User
Tags uuid myvm

###### #准备镜像文件. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.