Create a virtual machine automation scripts

1, xml template

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit node1
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>node1</name>
  <uuid>fb0d1a74-26c5-4734-87b9-3d8f87ea5eb4</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Haswell-noTSX</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/centos7.0.qcow2'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:ff:01:01'/>
      <source network='private1'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <interface type='network'>
      <mac address='52:54:00:7e:01:02'/>
      <source network='private2'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </interface>
    <interface type='network'>
      <mac address='52:54:00:b3:01:03'/>
      <source network='public1'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </interface>
    <interface type='network'>
      <mac address='52:54:00:62:01:04'/>
      <source network='public2'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='spicevmc'>
      <target type='virtio' name='com.redhat.spice.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='spice' autoport='yes'>
      <image compression='off'/>
    </graphics>
    <sound model='ich6'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <redirdev bus='usb' type='spicevmc'>
    </redirdev>
    <redirdev bus='usb' type='spicevmc'>
    </redirdev>
    <memballoon model='virtio'>
      <address type = 'PCI' = Domain '0x0000' Bus = '0x00' slot = '0x07' function = '0x0' />
    </ memballoon>
  </ Devices>
</ Domain>

2, the rear end of the image file

3, create a script

[root @ localhost OMC] # CAT /home/jy/kvm/cmd/clonevm.sh
# / bin / bash
# parameters received
# virtual machine name
DOMname = $ 1

IF [the -z $ DOMname]; the then
  echo "Please name the INPUT vm ! "
  Exit. 9
Fi

the UUID = $ (the uuidgen)
# storage pool set position
IMGDIR =" / var / lib / the libvirt / Images "

# rear mirror disposed directory
IMG_BK_DIR =" / var / lib / the libvirt / Images / bkimg "

#vmXML template path
VM_XMLPATH = "/ Home / JY / KVM / cmd / xmltmpl / centos7.xml"

#vm config path
VM_XML_SYSPATH = "/ etc / the libvirt / QEMU" # virtual machine number storage file directory VM_CODEFILE = "/ home / jy / kvm /cmd/xmltmpl/vmcode.txt " StartNum =` $ VM_CODEFILE` CAT # get the current coding machine 01-09,10-19,20-29 want to create,30-39TO_CREATE_VMNO=$[STARTNUM+1]tmpNum=0







if [ $TO_CREATE_VMNO -lt 10 ];then
  tmpNum="0$TO_CREATE_VMNO"
else
  tmpNum="$TO_CREATE_VMNO"
fi
MAC_START_PREFIX="52:54:00:ff:$tmpNum"

#linux tmpl name
LINUX_BACK_IMG="centos7.0.qcow2"

echo "start create vm...."
echo "current vm start mac is $MAC_START_PREFIX"

#检查是否存在
if [ -f "$IMGDIR/$DomName.qcow2" ];then
  echo "please check vm name,probably already exist!"
  exit 1
fi

cp $VM_XMLPATH  /mnt/tmplvm.xml
sed -i "s#<name>.*</name>#<name>$DomName</name>#" /mnt/tmplvm.xml
sed -i "s#<uuid>.*</uuid>#<uuid>$UUID</uuid>#" /mnt/tmplvm.xml

IMGNAME="\/var\/lib\/libvirt\/images\/$DomName.qcow2"
-i Sed "S / <Source File = '. * \. qcow2' \ /> / <Source File = '$ imgName' \ /> /" /mnt/tmplvm.xml
Sed -i "S / <MAC address = '\ (\ {2 \} \.): \ (* \.): \ (* \.): \ (* \.): \ (\ {2 \} \.): \ (. * \) '\ /> / <MAC address =' $ MAC_START_PREFIX: \. 6 '\ /> / "/mnt/tmplvm.xml

# create the front mirror, as a template to reserve os7
qemu-img create -f qcow2 -b $ IMG_BK_DIR / $ $ IMGDIR LINUX_BACK_IMG / $ DomName.qcow2 25G &> / dev / null
SLEEP 3

cp /mnt/tmplvm.xml $ VM_XML_SYSPATH / $ DomName.xml
virsh the DEFINE $ VM_XML_SYSPATH / $ DomName.xml

echo $ TO_CREATE_VMNO> VM_CODEFILE $
echo "the Create vm ! Success "
virt-Manager


# copy the template XML, modify the name, UUID, network card MAC (machine code xx: xx: xx: xx: 01 ( first machine, the second machine 02): xx)

 

Guess you like

Origin www.cnblogs.com/justart/p/11520335.html