Linux 快速生成虚拟机 shell脚本

通过COW写时复制原理 实现快速生成虚拟机脚本

#!/bin/bash
#IMG 为镜像文件目录
#YUAN 为模版虚拟机镜像文件
#NAME 为生成虚拟机镜像文件名头
#XML 为xml文件存放位置
#IP 为当前用户的IP 我取来是为了生成mac地址用的
XML=/etc/libvirt/qemu/
IMG=/var/lib/libvirt/images/
YUAN=rh7_template
NAME=test
UUID=`uuidgen`
#IP=`ifconfig | sed -rn "2p" | sed -r 's/(inet)(.*)(netmask)(.*)/\2/'`
IP=`ifconfig |sed -n '2p' | awk '{print $2}'`
#将IP4,IP3设置为mac地址第三四位
#IP4=`echo $IP | sed -r 's/(.*)([0-9]*)[.]([0-9]*$)/\3/'` 这是我没有学awk的时候写的
IP4=`echo $IP | awk  -F. '{print $4}'`
if [ $IP4 -ge 100 ];then
    IP4=$[IP4%100]
elif [ $IP4 -le 9 ];then
    IP4=0$IP4
fi
#IP3=`echo $IP | sed -r 's/(.*)[.]([0-9]*)[.]([0-9]*$)/\2/'`这也是我没有学awk的时候写的
IP3=`echo $IP | awk  -F. '{print $3}'`
if [ $IP3 -ge 100 ];then
    IP3=$[IP3%100]
elif [ $IP3 -le 9 ];then
    IP3=0$IP3
fi

read -p '请输入要创建的数字: ' NUM
if [ -z $NUM ];then
    echo '输入不能为空'
    exit
elif [ -z `echo $NUM | sed 's/[^0-9]*//'` ];then
    echo '请输入数字'
    exit
elif [ $NUM -ge 100 -o $NUM -lt 1 ];then
    echo '请输入在1-100以内的数字' 
    exit
fi

if [ $NUM -le 9 ];then
    NUM=0$NUM
fi
NAME=${NAME}${NUM}
if [ -z $1 ];then
    if [ -e ${IMG}${NAME}.img ];then
        echo '你要创建的号码已经存在'
        exit
    fi
    echo -en "正在创建虚拟机镜像文件和......\t"
    #通过镜像文件,快速生成相应镜像文件
    qemu-img create -f qcow2 -b ${IMG}.${YUAN}.img ${IMG}${NAME}.img &>/dev/null
    echo -e "\e[32;1m[OK]\e[0m"
    echo -en "正在生成XML配置文件......\t"
    #MYXML 为生成的xml配置文件目录
    MYXML=${XML}${NAME}.xml
    cp $IMG.rhel7.xml ${MYXML}
    #使用sed对镜像文件进行修改
    sed -i "/<name>${YUAN}/s/${YUAN}/${NAME}/" ${MYXML}
    sed -i "/<uuid>/s/<uuid>.*</<uuid>$UUID</" ${MYXML}
    sed -i "/${YUAN}\.img/s/${YUAN}/${NAME}/" ${MYXML}
    for i in {a..d}
    do
        sed -i "/mac/s/${i}1/${IP3}/" ${MYXML}
        sed -i "/mac/s/${i}2/${IP4}/" ${MYXML}
        sed -i "/mac/s/${i}3/${NUM}/" ${MYXML}  
    done 
    echo -e "\e[32;1m[OK]\e[0m"
    echo -en "正在生成虚拟机......\t\t" 
    #通过XML文件生成虚拟机
    virsh define ${MYXML} &> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\e[32;1m[OK]\e[0m"
    else
        rm -rf ${MYXML}
        rm -rf ${IMG}${NAME}.img
        echo -e "\e[31;1m[Failed]\e[0m"
    fi
#-d将用次方法生成的虚拟机删除 当时是为了测试方便写的
elif [ $1 == "-d" ];then
    rm -rf ${MYXML} 
    rm -rf ${IMG}${NAME}.img
    virsh undefine ${NAME} &> /dev/null 
    echo "成功删除${NAME}虚拟机"
fi

这里我会提供模版XML 文件
模版虚拟机镜像文件 使用KVM生成一个新的rhel7虚拟机 做为原始盘
可以参考 (http://blog.51cto.com/13558754/2057088)

XML 模版

<domain type='kvm'>
    <name>rh7_template</name>
    <uuid>e783146f-d26f-448d-951f-5c739b136d2f</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>
    </os>
    <features>
        <acpi/>
        <apic/>
    </features>
        <cpu mode='host-model'>
            <model fallback='allow'/>
    </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/rh7_template.img'/>
            <target dev='vda' bus='virtio'/>
            <boot order='1'/>
            <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
        </disk>
        <disk type='file' device='cdrom'>
            <driver name='qemu' type='raw'/>
            <target dev='hda' bus='ide'/>
            <readonly/>
            <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='74:52:a1:a2:a3:01'/>
            <source network='private1'/>
            <model type='virtio'/>
            <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
        </interface>
        <interface type='network'>
            <mac address='74:52:b1:b2:b3:02'/>
            <source network='private2'/>
            <model type='virtio'/>
            <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
        </interface>
        <interface type='network'>
            <mac address='74:52:c1:c2:c3:03'/>
            <source network='public1'/>
            <model type='virtio'/>
            <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
        </interface>
        <interface type='network'>
            <mac address='74:52:d1:d2:d3:04'/>
            <source network='public2'/>
            <model type='virtio'/>
            <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/>
        </interface>
        <serial type='pty'>
            <target port='0'/>
        </serial>
        <console type='pty'>
            <target type='serial' port='0'/>
        </console>
        <input type='tablet' bus='usb'/>
        <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='0x08' function='0x0'/>
        </memballoon>
    </devices>
</domain>









本文转自 Xuenqlve 51CTO博客,原文链接:http://blog.51cto.com/13558754/2057094,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值