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
配置若有遗漏或错误,请评论留言。