kvm批量创建虚拟机

批量创建虚拟机

准备:

镜像文件,如centos7.0.qcow2

配置文件,如vm2.xml

#!/bin/env bash
start=10
end=11
simages=/var/lib/libvirt/images/centos7.0.qcow2
sqemu=/etc/libvirt/qemu/vm2.xml
images=/data/kvm/images
qemu=/data/kvm/qemu

#复制img文件
for i in `seq $start $end`
do
{
cp $simages $images/vm-test${i}.img && echo "$i copy commplete..."
}&
done
wait

#复制修改xml文件
for i in `seq $start $end`
do
cp $sqemu $qemu/vm-test${i}.xml
uuid=`uuidgen`
mac1=`openssl rand -base64 8 |md5sum |cut -c1-2`
mac2=`openssl rand -base64 8 |md5sum |cut -c1-2`
sed -i "10c <uuid>$uuid</uuid>>" $qemu/vm-test${i}.xml
#磁盘名称
sed -i "s/centos7.0.qcow2/vm-test${i}.img/" $qemu/vm-test${i}.xml
sed -i "s/>vm1</>vm-test${i}</" $qemu/vm-test${i}.xml
sed -i "66c <mac address='52:54:00:af:$mac1:$mac2'/>" $qemu/vm-test${i}.xml
sed -i "s#/var/lib/libvirt/images#${images}#" $qemu/vm-test${i}.xml
virsh define $qemu/vm-test${i}.xml
virsh start vm-test${i}
done

posted @ 2021-08-06 17:25  破碎的屋檐  阅读(200)  评论(0编辑  收藏  举报