kvm从模板创建虚拟机
来源:原创
时间:2019-09-07
作者:脚本小站
分类:Linux
创建模板
安装需要的工具:
需要用到 virt-sysprep 这个命令,安装下面的包引入该工具。关于这个工具的文档详见:https://www.mankier.com/1/virt-sysprep
yum install libguestfs-tools -y
关闭虚拟机:
virsh shutdown vm-name
导出xml文件作为新虚拟机的配置:
virsh dumpxml vm-name > template.xml
修改xml配置文件:
<disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/home/template.qcow2'/> # 修改磁盘位置 <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </disk>
复制虚拟机作为模板:
cd /var/lib/libvirt/images/ cp centos7.qcow2 template.qcow2
初始化模板文件:
重置镜像并移除SSH kesy, 为网卡创建新的MAC地址,修改udev persistent net rules 清除log文件等
virt-sysprep -a template.qcow2
压缩镜像:
qcow2:
qemu-img convert -c -O qcow2 old.qcow2 new.qcow2
raw:
qemu-img convert -c -f raw -O qcow2 old.raw new.qcow2
从模板创建新虚拟机
virt-clone --connect qemu:///system --original-xml template.xml --name new-vmname --file newdisk.qcow2
--file -f 新虚拟机存放的位置
--name 新虚拟机名称
--original 模板文件,要被复制的虚拟机
重命名虚拟机
修改名称:
virsh dumpxml sample virsh undefine sample vim sample.xml virsh define sample.xml
kvm开启嵌套虚拟化
只支持3.0以后内核版本,centos6以下是需要升级内核。
查看是否开启:
cat /sys/module/kvm_intel/parameters/nested N
启用nested
echo 'options kvm_intel nested=1' >/etc/modprobe.d/kvm-nested.conf
卸载模块:
modprobe -r kvm_intel
加载模块:
modprobe kvm_intel
查看:
cat /sys/module/kvm_intel/parameters/nested Y