为了能够快速的实现虚拟机的创建分配,我们可以准备一个虚拟机镜像模板,作为基础镜像使用。
实现思路:
1、使用virt-install ,构建一个模板机器,安装好基础软件。virt-install会在/etc/libvirtd/qemu/name.xml 和目标位置生成镜像文件name.img。
2、copy xml文件和img文件,替换 xml文件中的,uuid、file、 mac地址
<domain type='kvm'>
<name>templatename</name>
<uuid>f31e63a7-9f90-471e-a31f-b80ec60cc36a</uuid>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/mnt/mfs/templates.img'/>
</disk>
....
<interface type='bridge'>
<mac address='52:54:00:56:13:e6'/>
</interface>
</domain>
3、使用virt-copy-in更新新机器的ip信息,主机名等。
生成好目标机器的网卡配置信息,主机名,copy到镜像文件中。
sudo virt-copy-in ifcfg-eth0 -a $IMG_PATH/$VMIP.sys.img /etc/sysconfig/network-scripts/
sudo virt-copy-in hostname -a $IMG_PATH/$VMIP.sys.img /etc/
4、使用virsh命令,define 虚拟机,并启动
virsh define name.xml
virsh start vmname