码迷,mamicode.com
首页 > 其他好文 > 详细

创建虚拟机自动化脚本

时间:2019-09-14 22:28:32      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:ltm   mod   please   prefix   master   private   pre   current   boot   

1、模版xml

<!--
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、后端镜像文件

3、创建脚本

[root@localhost OMC]# cat /home/jy/kvm/cmd/clonevm.sh
#/bin/bash
#参数接收
#虚拟机器名字
DomName=$1

if [ -z $DomName ];then
  echo "please input vm name!"
  exit 9
fi

UUID=$(uuidgen)
#设置存储池位置
IMGDIR="/var/lib/libvirt/images"

# 设置后端镜像目录
IMG_BK_DIR="/var/lib/libvirt/images/bkimg"

#vmXML模板路径
VM_XMLPATH="/home/jy/kvm/cmd/xmltmpl/centos7.xml"

#vm config path
VM_XML_SYSPATH="/etc/libvirt/qemu"

#虚拟机序号存储文件目录
VM_CODEFILE="/home/jy/kvm/cmd/xmltmpl/vmcode.txt"
STARTNUM=`cat $VM_CODEFILE`
#获取当前要创建机器的编码01-09,10-19,20-29,30-39
TO_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"
sed -i "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

#创建前端镜像,以后备os7为模版
qemu-img create  -f qcow2 -b $IMG_BK_DIR/$LINUX_BACK_IMG $IMGDIR/$DomName.qcow2 25G &>/dev/null
sleep 3

cp /mnt/tmplvm.xml $VM_XML_SYSPATH/$DomName.xml
virsh define $VM_XML_SYSPATH/$DomName.xml

echo $TO_CREATE_VMNO > $VM_CODEFILE
echo "create vm success!"
virt-manager


#复制模版XML,修改名称,UUID,网卡MAC(机器编码xx:xx:xx:xx:01(第一台机器,02第二台机器):xx)

 

创建虚拟机自动化脚本

标签:ltm   mod   please   prefix   master   private   pre   current   boot   

原文地址:https://www.cnblogs.com/justart/p/11520335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!