创建虚拟机自动化脚本

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)

 

转载于:https://www.cnblogs.com/justart/p/11520335.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值