linux脚本快速克隆虚拟机(多虚拟网卡)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux脚本快速克隆虚拟机(多虚拟网卡)相关的知识,希望对你有一定的参考价值。

#######准备镜像文件.rhel7_template.img,描述文件.rhel7.xml分别放在/var/lib/libvirt/images/内
#######记得把文件改成隐藏文件

#!/bin/bash
exit code:
65 -> user input nothing
66 -> user input is not a number
67 -> user input out of range
68 -> vm disk image exists

IMG_DIR=/var/lib/libvirt/images
BASEVM=rh7_template ####镜像文件模版名
ROOM=sed -n "1p" /etc/hostname | sed -r ‘s/(room)([0-9]{1,})(.*)/\2/‘
if [ $ROOM -le 9 ];then
ROOM=0$ROOM
fi
IP=sed -n "1p" /etc/hostname | sed -r ‘s/(.*)([0-9]+)(.*)/\2/‘
read -p "Enter VM number: " VMNUM
if [ $VMNUM -le 9 ];then
VMNUM=0$VMNUM
fi

if [ -z "${VMNUM}" ]; then
echo "You must input a number."
exit 65
elif [ $(echo ${VMNUM}*1 | bc) = 0 ]; then
echo "You must input a number."
exit 66
elif [ ${VMNUM} -lt 1 -o ${VMNUM} -gt 99 ]; then
echo "Input out of range"
exit 67
fi

NEWVM=rh7_node${VMNUM}

if [ -e $IMG_DIR/${NEWVM}.img ]; then
echo "File exists."
exit 68
fi

echo -en "Creating Virtual Machine disk image......\t"
qemu-img create -f qcow2 -b $IMG_DIR/.${BASEVM}.img $IMG_DIR/${NEWVM}.img &> /dev/null
echo -e "\e[32;1m[OK]\e[0m"

#virsh dumpxml ${BASEVM} > /tmp/myvm.xml
cat /var/lib/libvirt/images/.rhel7.xml > /tmp/myvm.xml ##.rhel7.xml 描述文件
sed -i "/<name>${BASEVM}/s/${BASEVM}/${NEWVM}/" /tmp/myvm.xml
sed -i "/uuid/s/<uuid>.*<\/uuid>/<uuid>$(uuidgen)<\/uuid>/" /tmp/myvm.xml
sed -i "/${BASEVM}.img/s/${BASEVM}/${NEWVM}/" /tmp/myvm.xml

sed -i "/mac /s/a1/${ROOM}/" /tmp/myvm.xml
sed -i "/mac /s/a2/${IP}/" /tmp/myvm.xml
sed -i "/mac /s/a3/${VMNUM}/" /tmp/myvm.xml

sed -i "/mac /s/b1/${ROOM}/" /tmp/myvm.xml
sed -i "/mac /s/b2/${IP}/" /tmp/myvm.xml
sed -i "/mac /s/b3/${VMNUM}/" /tmp/myvm.xml

sed -i "/mac /s/c1/${ROOM}/" /tmp/myvm.xml
sed -i "/mac /s/c2/${IP}/" /tmp/myvm.xml
sed -i "/mac /s/c3/${VMNUM}/" /tmp/myvm.xml

sed -i "/mac /s/d1/${ROOM}/" /tmp/myvm.xml
sed -i "/mac /s/d2/${IP}/" /tmp/myvm.xml
sed -i "/mac /s/d3/${VMNUM}/" /tmp/myvm.xml

echo -en "Defining new virtual machine......\t\t"
virsh define /tmp/myvm.xml &> /dev/null
echo -e "\e[32;1m[OK]\e[0m"

以上是关于linux脚本快速克隆虚拟机(多虚拟网卡)的主要内容,如果未能解决你的问题,请参考以下文章

Linux_创建母版,快速克隆,如何移动虚拟机.ziw

克隆Linux系统之后的网卡问题

Linux操作-修改克隆虚拟机的网卡名

linux 虚拟机复制网卡不能启动或使用

vmware克隆Centos6.4虚拟机网卡无法启动问题

VMware克隆linux虚拟机后,克隆后的虚拟机如何把网卡eth1修改为eth0 ?