加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

创建虚拟机自动化脚本

发布时间:2020-12-15 07:45:53 所属栏目:Java 来源:网络整理
导读: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 t

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、创建脚本

[[email?protected] OMC]# cat /home/jy/kvm/cmd/clonevm.sh#/bin/bash#参数接收#虚拟机器名字DomName=$1if [ -z $DomName ];then? echo "please input vm name!"? exit 9fiUUID=$(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 pathVM_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-39TO_CREATE_VMNO=$[STARTNUM+1]tmpNum=0if [ $TO_CREATE_VMNO -lt 10 ];then? tmpNum="0$TO_CREATE_VMNO"else? tmpNum="$TO_CREATE_VMNO"fiMAC_START_PREFIX="52:54:00:ff:$tmpNum"#linux tmpl nameLINUX_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 1ficp $VM_XMLPATH? /mnt/tmplvm.xmlsed -i "s#<name>.*</name>#<name>$DomName</name>#" /mnt/tmplvm.xmlsed -i "s#<uuid>.*</uuid>#<uuid>$UUID</uuid>#" /mnt/tmplvm.xmlIMGNAME="/var/lib/libvirt/images/$DomName.qcow2"sed -i "s/<source file=‘.*.qcow2‘/>/<source file=‘$IMGNAME‘ />/" /mnt/tmplvm.xmlsed -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/nullsleep 3cp /mnt/tmplvm.xml $VM_XML_SYSPATH/$DomName.xmlvirsh define $VM_XML_SYSPATH/$DomName.xmlecho $TO_CREATE_VMNO > $VM_CODEFILEecho "create vm success!"virt-manager#复制模版XML,修改名称,UUID,网卡MAC(机器编码xx:xx:xx:xx:01(第一台机器,02第二台机器):xx)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读