
KVM模板机backing file
1.生产环境中使用虚拟机模板部署虚拟机
通过镜像的方式来部署虚拟机,也可以称为通过模版方式部署虚拟机。
- 简单理解KVM虚拟机的硬盘格式
- raw格式:KVM虚拟机原始的硬盘镜像,性能好但功能简单,使用raw格式会占用大量的存储空间,比如创建了10G直接使用10G,不管安装的操作系统系统究竟使用多少。
- qcow2格式:新型的KVM虚拟机硬盘镜像,性能一般但功能多,在使用镜像部署虚拟机时使用到,使用qcow2格式减少空间,类似于精简置备,比如创建了10G,操作系统只使用了5G,那么实际使用空间就是5G,硬盘随使用动态增长。
2.什么是backing file
了解raw以及qcow2两种格式后还需要了解了一下KVM中一个特殊的backing file,可以翻译为后备镜像,后备镜像简单来说就是多台虚拟机共用一个镜像文件。我们以场景应用的方式来解释什么是backing file。
场景:前文已经安装好一台centos7虚拟机,使用raw格式10GB,假设实际使用5G,现在想快速再创建20台,注意,是快速创建,不是一台一台重新安装。
- 方案1:复制安装好的centos7虚拟机raw硬盘,修改虚拟机配置,20台虚拟机是200GB空间使用。
- 方案2:将centos7虚拟机raw硬盘作为backing flie,再创建20份qcow2格式的镜像,修改虚拟机配置文件,20台虚拟机原始使用10G空间,后续数据写入再增加空间。
很明显,使用方案1复制文件需要大量时间,使用方案2不需要大量复制数据就可以部署虚拟机,同时降低了空间的使用,这就是backing flie。
- 复制已安装Linux系统虚拟机硬盘镜像创建虚拟机
以centos7虚拟机作为模板,通过复制虚拟机硬盘镜像的方式来创建虚拟机。
第1步,使用命令“virsh list --all”查看KVM主机上的虚拟机信息,centos7虚拟机处于关闭状态。
root@kvm-server:/vm-linux# virsh list --all Id Name State 8 win2012 running 12 win7 running - centos7 shut off - centos7-02 shut off - centos7-03 shut off第2步,使用命令“ls”查看虚拟机硬盘镜像文件,使用的raw格式。
root@kvm-server:/vm-linux# ls centos7.raw第3步,使用命令“qemu-img create -f raw -b centos7.raw centos7-04.raw”复制虚拟机硬盘镜像文件创建新的虚拟机硬盘镜像,如果使用raw参数会出现错误提示,告诉backing flie不能使用raw格式。
root@kvm-server:/vm-linux# qemu-img create -f raw -b centos7.raw centos7-04.raw qemu-img: centos7-04.raw: Backing file not supported for file format 'raw'第4步,调整参数,将raw格式调整为qcow2格式,再使用命令“qemu-img create -f qcow2 -b centos7.raw centos7-04.raw” 复制虚拟机硬盘镜像文件创建新的虚拟机硬盘镜像。
root@kvm-server:/vm-linux# qemu-img create -f qcow2 -b centos7.raw centos7-04.raw Formatting 'centos7-04.raw', fmt=qcow2 size=10737418240 backing_file=centos7.raw cluster_size=65536 lazy_refcounts=off refcount_bits=16第5步,使用命令“ls”查看新创建的虚拟机硬盘镜像文件centos7-04.raw,注意硬盘镜像文件是raw格式。
root@kvm-server:/vm-linux# ls centos7-04.raw centos7.raw第6步,使用命令“qemu-img info centos7-04.raw”查看硬盘镜像文件信息,从文件格式可以看,新创建的硬盘镜像文件格式为qcow2。
root@kvm-server:/vm-linux# qemu-img info centos7-04.raw image: centos7-04.raw file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 196K cluster_size: 65536 backing file: centos7.raw Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false第7步,既然新创建的硬盘镜像文件格式为qcow2格式,在实际使用中可以直接创建,使用命令“rm -rf centos7-04.raw”删除创建的硬盘镜像文件后重新创建。
root@kvm-server:/vm-linux# rm -rf centos7-04.raw第8步,使用命令“qemu-img create -f qcow2 -b centos7.raw centos7-04.qcow2” 重新创建新的虚拟机硬盘镜像,注意格式为qcow2。
root@kvm-server:/vm-linux# qemu-img create -f qcow2 -b centos7.raw centos7-04.qcow2 Formatting 'centos7-04.qcow2', fmt=qcow2 size=10737418240 backing_file=centos7.raw cluster_size=65536 lazy_refcounts=off refcount_bits=16第9步,使用命令“ls”查看新创建的虚拟机硬盘镜像文件centos7-04.qcow2,注意硬盘镜像文件是qcow2格式。
root@kvm-server:/vm-linux# ls centos7-04.qcow2 centos7.raw第10步,使用命令“qemu-img info centos7-04.qcow2”查看硬盘镜像文件信息,后缀以及硬盘镜像文件格式为qcow2。
root@kvm-server:/vm-linux# qemu-img info centos7-04.qcow2 image: centos7-04.qcow2 file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 196K cluster_size: 65536 backing file: centos7.raw Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false至此,通过复制虚拟机硬盘镜像文件创建虚拟机硬盘镜像文件完成,但是虚拟机还不能使用,还需要对虚拟机配置文件进行一些处理,注册虚拟机才能。
第11步,使用命令“virsh list --all”查看虚拟机信息,并没有centos7-04虚拟机,因为还未注册该虚拟机。
root@kvm-server:/vm-linux# virsh list --all Id Name State 8 win2012 running 12 win7 running - centos7 shut off - centos7-02 shut off - centos7-03 shut off第12步,使用命令“ls /etc/libvirt/qemu/”查看现有的虚拟机配置文件,虚拟机配置文件就是xml文件。注意,KVM主机虚拟机配置文件位于/etc/libvirt/qemu目录。
root@kvm-server:/vm-linux# ls /etc/libvirt/qemu/ centos7-02.xml centos7-03.xml centos7.xml networks win2012.xml win7.xml第13步,使用命令“cp /etc/libvirt/qemu/centos7.xml /etc/libvirt/qemu/centos7-04.xml”复制原centos7虚拟机配置文件,重新命名为centos7-04.xml。
root@kvm-server:/vm-linux#cp /etc/libvirt/qemu/centos7.xml /etc/libvirt/qemu/centos7-04.xml第14步,使用命令“vi /etc/libvirt/qemu/centos7-04.xml”编辑配置文件,因为是复制的centos7配置文件,虚拟机uuid、硬盘镜像路径、MAC地址等需要修改,否则新创建的虚拟机无法启动,uuid以及MAC地址推荐删除重新生成。
root@kvm-server:/vm-linux# vi /etc/libvirt/qemu/centos7-04.xml <domain type='kvm'> <name>centos7</name> #虚拟机名 <uuid>632d4cc5-2146-4222-826e-08fae04d847b</uuid> #虚拟机uuid,推荐删除重新生成 <memory unit='KiB'>2097152</memory> #虚拟机内存 <currentMemory unit='KiB'>2097152</currentMemory> <vcpu placement='static'>1</vcpu> #虚拟机VCPU <devices> <emulator>/usr/bin/kvm-spice</emulator> <disk type='file' device='disk'> <driver name='qemu' type='raw'/> #虚拟机硬盘镜像格式,注意格式修改 <source file='/vm-ubuntu/ubuntu.raw'/> #虚拟机硬盘镜像路径,注意路径修改 <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </disk> <interface type='bridge'> <mac address='52:54:00:ad:3f:69'/> #虚拟机MAC地址,推荐删除重新生成 <source bridge='virbr0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>第15步,修改完成配置文件后,使用命令“virsh define /etc/libvirt/qemu/centos7-04.xml”注册虚拟机。
root@kvm-server:/vm-linux# virsh define /etc/libvirt/qemu/centos7-04.xml Domain centos7-04 defined from /etc/libvirt/qemu/centos7-04.xml第16步,注册完成后使用命令“virsh list --all”查看虚拟机是否注册成功,注册成功后centos7-04虚拟机处于关闭状态。
root@kvm-server:/vm-linux# virsh list --all Id Name State 8 win2012 running 12 win7 running - centos7 shut off - centos7-02 shut off - centos7-03 shut off - centos7-04 shut off第17步,使用命令“virsh start centos7-04”启动虚拟机。
root@kvm-server:/vm-linux# virsh start centos7-04 Domain centos7-04 started第18步,使用命令“virsh console centos7-04”连接到虚拟机控制台。
root@kvm-server:/vm-linux# virsh console centos7-04 Connected to domain centos7-04 Escape character is ^]第19步,如果虚拟机配置文件存在问题,虚拟机控制台不会有其他显示,请关闭虚拟机电源后检查配置文件。
[ 4.360721] type=1403 audit(1540821037.906:3): policy loaded auid=4294967295 ses=4294967295 [ 4.370052] systemd[1]: Successfully loaded SELinux policy in 349.408ms. [ 4.403450] ip_tables: (C) 2000-2006 Netfilter Core Team [ 4.405915] systemd[1]: Inserted module 'ip_tables' [ 4.430357] systemd[1]: Relabelled /dev and /run in 20.852ms. Welcome to CentOS Linux 7 (Core)! [ OK ] Stopped Switch Root. [ OK ] Stopped Journal Service. Starting Journal Service... [ OK ] Set up automount Arbitrary Executab...ats File System Automount Point. [ OK ] Listening on udev Control Socket. [ OK ] Created slice system-serial\x2dgetty.slice. [ OK ] Reached target Remote File Systems. Mounting Debug File System... [ OK ] Listening on /dev/initctl Compatibility Named Pipe. [ OK ] Created slice User and Session Slice. [ OK ] Reached target Slices. [ OK ] Created slice system-getty.slice. [ OK ] Listening on Delayed Shutdown Socket. Mounting Huge Pages File System... [ OK ] Stopped File System Check on Root Device. …… [ 7.990611] Netfilter messages via NETLINK v0.30. [ 7.998664] ip_set: protocol 6 CentOS Linux 7 (Core) Kernel 3.10.0-693.el7.x86_64 on an x86_64 kvm-centos login: root Password: Last login: Tue Oct 16 17:34:45 on tty1第20步,使用root用户名以及密码登录虚拟机,使用命令“ip addr”查看IP地址,可以看到MAC不同于原centos7虚拟机MAC地址,生成了新的MAC地址。
[root@kvm-centos ~]# ip addr第21步,使用命令“ping”检查网络的连通性,访问公网地址正常。
[root@kvm-centos ~]# ping 119.6.6.6第22步,使用命令“virsh list --all”查看虚拟机信息,centos7-04虚拟机处于运行状态。
root@kvm-server:/vm-linux# virsh list --all Id Name State 8 win2012 running 12 win7 running 19 centos7-04 running - centos7 shut off - centos7-02 shut off - centos7-03 shut off第23步,尝试一下启动centos7虚拟机,使用命令“virsh start centos7”后会出现报错提示,为什么不能启动?因为已将centos7虚拟机硬盘镜像作为backing file文件。
root@kvm-server:/vm-linux# virsh start centos7 error: Failed to start domain centos7 error: internal error: qemu unexpectedly closed the monitor: 2018-10-29T13:55:30.517655Z qemu-system-x86_64: -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x7,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1: Failed to get "write" lock Is another process using the image?第24步,使用命令“vi /etc/libvirt/qemu/centos7-04.xml”查看虚拟机新的配置文件,注意重新生成的uuid以及MAC地址。
root@kvm-server:/vm-linux#vi /etc/libvirt/qemu/centos7-04.xml <domain type='kvm'> <name>centos7-04</name> <uuid>1f56b567-d667-414f-a4cd-74fa9749e2e3</uuid> #重新生成的uuid <memory unit='KiB'>2097152</memory> <currentMemory unit='KiB'>2097152</currentMemory> <vcpu placement='static'>1</vcpu> <interface type='bridge'> <mac address='52:54:00:38:93:21'/> #重新生成的MAC地址 <source bridge='virbr0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>至此,通过复制已安装Linux系统虚拟机硬盘镜像创建虚拟机完成,整体来说是比较简单的,主要就是修改虚拟机配置文件。
- 复制已安装Windows系统虚拟机硬盘镜像创建虚拟机
通过复制已安装Windows系统虚拟机硬盘镜像创建虚拟机整体来说与Linux相同。
第1步,使用命令“qemu-img create -f qcow2 -b win7.raw win7-02.qcow2” 创建新的虚拟机硬盘镜像,注意格式为qcow2。
root@kvm-server:/vm-win# qemu-img create -f qcow2 -b win7.raw win7-02.qcow2 Formatting 'win7-02.qcow2', fmt=qcow2 size=21474836480 backing_file=win7.raw cluster_size=65536 lazy_refcounts=off refcount_bits=16第2步,使用命令“ls”查看新创建的虚拟机硬盘镜像文件win7-02.qcow2,注意硬盘镜像文件是qcow2格式。
root@kvm-server:/vm-win# ls win2012.raw win7-02.qcow2 win7.raw第3步,使用命令“qemu-img info win7-02.qcow2”查看硬盘镜像文件信息,后缀以及硬盘镜像文件格式为qcow2。
root@kvm-server:/vm-win# qemu-img info win7-02.qcow2 image: win7-02.qcow2 file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 196K cluster_size: 65536 backing file: win7.raw Format specific information: compat: 1.1 lazy refcounts: false refcount bits: 16 corrupt: false第4步,使用命令“cp /etc/libvirt/qemu/win7.xml /etc/libvirt/qemu/win7-02.xml”复制原win7虚拟机配置文件,重新命名为win7-02.xml。
root@kvm-server:/vm-win# cp /etc/libvirt/qemu/win7.xml /etc/libvirt/qemu/win7-02.xml第5步,使用命令“vi /etc/libvirt/qemu/win7-02.xml”编辑配置文件,与Linux虚拟机一样,因为是复制的win7配置文件,虚拟机uuid、硬盘镜像路径、MAC地址等需要修改,否则新创建的虚拟机无法启动,uuid以及MAC地址推荐删除重新生成。
root@kvm-server:/vm-win# vi /etc/libvirt/qemu/win7-02.xml <domain type='kvm'> <name>win7</name> <uuid>b052cc07-0a98-4102-9f7d-e1438725665b</uuid> #虚拟机uuid,推荐删除重新生成 <memory unit='KiB'>4194304</memory> <currentMemory unit='KiB'>4194304</currentMemory> <vcpu placement='static'>2</vcpu> <devices> <emulator>/usr/bin/kvm-spice</emulator> <disk type='file' device='disk'> <driver name='qemu' type='raw'/> #虚拟机硬盘镜像格式,注意格式修改 <source file='/vm-win/win7.raw'/> #虚拟机硬盘镜像路径,注意路径修改 <target dev='hda' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> <interface type='bridge'> <mac address='52:54:00:d2:ef:25'/> #虚拟机MAC地址,推荐删除重新生成 <source bridge='virbr0'/> <model type='rtl8139'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> <graphics type='vnc' port='5901' autoport='no' listen='0.0.0.0'> #虚拟机VNC连接端口 <listen type='address' address='0.0.0.0'/>第6步,修改完成配置文件后,使用命令“virsh define /etc/libvirt/qemu/win7-02.xml”注册虚拟机。
root@kvm-server:/vm-win# virsh define /etc/libvirt/qemu/win7-02.xml Domain win7-02 defined from /etc/libvirt/qemu/win7-02.xml第7步,注册完成后使用命令“virsh list --all”查看虚拟机是否注册成功,注册成功后win7-02虚拟机处于关闭状态。
root@kvm-server:/vm-win# virsh list --all Id Name State 8 win2012 running 19 centos7-04 running - centos7 shut off - centos7-02 shut off - centos7-03 shut off - win7 shut off - win7-02 shut off第8步,使用命令“virsh start win7-02”启动虚拟机。
root@kvm-server:/vm-win# virsh start win7-02 Domain win7-02 started第9步,使用命令“virsh list --all”查看虚拟机信息,win7-02虚拟机处于运行状态。
root@kvm-server:/vm-win# virsh list --all Id Name State 8 win2012 running 19 centos7-04 running 21 win7-02 running - centos7 shut off - centos7-02 shut off - centos7-03 shut off - win7 shut off第10步,尝试一下启动win7虚拟机,使用命令“virsh start win7”后会出现报错提示,为什么不能启动?因为已将win7虚拟机硬盘镜像作为backing file文件。
root@kvm-server:/vm-win# virsh start win7 error: Failed to start domain win7 error: internal error: process exited while connecting to monitor: 2018-10-29T14:47:48.923749Z qemu-system-x86_64: -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0: Failed to get "write" lock Is another process using the image?第11步,使用命令“vi /etc/libvirt/qemu/ win7-02.xml”查看虚拟机新的配置文件,注意重新生成的uuid以及MAC地址。
root@kvm-server:/vm-win# vi /etc/libvirt/qemu/win7-02.xml <domain type='kvm'> <name>win7-02</name> <uuid>386c0ba5-17b5-40fa-8d8c-f083903e26a3</uuid> #重新生成的uuid <memory unit='KiB'>4194304</memory> <currentMemory unit='KiB'>4194304</currentMemory> <vcpu placement='static'>4</vcpu> <interface type='bridge'> <mac address='52:54:00:06:bf:35'/> #重新生成的MAC地址 <source bridge='virbr0'/> <model type='rtl8139'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>至此,通过复制已安装Windows系统虚拟机硬盘镜像创建虚拟机完成,与Linux虚拟机一样,主要就是修改虚拟机配置文件,与Linux虚拟机有差异的是,Windows虚拟机不能通过console连接,需要使用vnc viewer进行连接。