抽空学学KVM(七):虚拟机快照和克隆

前几天学写了KVM中qume-info命令的使用,今天学学在虚拟化里面用处广泛的快照和克隆功能,snapshot和virt-clone。对于snapshot命令的使用其实很简单。进入virsh界面以后可以查看对对应的命令查看详细的参数使用介绍。

virsh # snapshot-                        #snapshot命令主要是这10个命令
snapshot-create     snapshot-dumpxml    snapshot-parent
snapshot-create-as  snapshot-edit       snapshot-revert
snapshot-current    snapshot-info       
snapshot-delete     snapshot-list       
virsh # snapshot-
#snapshot-create - Create a snapshot from XML #从XML文件里面创建一个快照
#snapshot-create-as - Create a snapshot from a set of args;Create a snapshot (disk and RAM) from arguments;#从一个argument(内容提要)里面创建一个快照
#snapshot-current - Get or set the current snapshot        #获取当前快照
#snapshot-delete - Delete a domain snapshot                #删除快照
#snapshot-dumpxml - Dump XML for a domain snapshot                #备份某一实例的快照当前的xml文件
#snapshot-edit - edit XML for a snapshot,Returns basic information about a snapshot.#编辑某个快照的xml文件
#snapshot-list - List snapshots for a domain              #快照列表
#snapshot-parent - Get the name of the parent of a snapshot;Extract the snapshot's parent, if any #提取某个快照的跟
#snapshot-revert - Revert a domain to a snapshot;Revert domain to snapshot; #将实例恢复到快照的某一状态

下面简单看看常见的命令使用吧

virsh # list --all
 Id    Name                           State
----------------------------------------------------
 1     test-kvm                       running

virsh # snapshot-create test-kvm
Domain snapshot 1603521065 created          #为什么这里创建的快照的名字时这一串数值呢?
virsh # snapshot-list test-kvm
Name Creation Time State
------------------------------------------------------------
1603521065 2020-10-24 14:31:05 +0800 running   #快照的名称其实是与我创建的快照时的北京时间相关的。这个数字的叫做格林威治时间,也叫作Unix时间戳,说明1970.1.1到现在过了1603521065秒
virsh #

[root@KVM03-10 opt]# ll -h
total 6.7G
-rw-------. 1 root root 10G Oct 11 12:14 centos2.1.raw
-rw-r--r--. 1 qemu qemu 1.5G Oct 24 14:40 centos2.1.raw.qcow2
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 193K Oct 11 11:55 raw-to-qcow2.qcow2
-rw-r--r--. 1 root root 1.0G Oct 11 11:35 resize.raw
-rw-r--r--. 1 root root 5.4K Oct 11 12:38 test-kvm.xml
-rw-r--r--. 1 root root 257K Oct 11 11:36 test.qcow2
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw

[root@KVM03-10 opt]# virsh snapshot-list test-kvm
Name Creation Time State
------------------------------------------------------------
1603521065 2020-10-24 14:31:05 +0800 running
1603521844 2020-10-24 14:44:04 +0800 running

[root@KVM03-10 opt]# ll -h
total 7.7G
-rw-------. 1 root root 10G Oct 11 12:14 centos2.1.raw
-rw-r--r--. 1 qemu qemu 1.7G Oct 24 14:44 centos2.1.raw.qcow2    #发现做一次快照,虚机使用的磁盘就会增加一部分,说明其实我的快照文件是放在这个虚机的磁盘文件中的。
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 193K Oct 11 11:55 raw-to-qcow2.qcow2
-rw-r--r--. 1 root root 1.0G Oct 11 11:35 resize.raw
-rw-r--r--. 1 root root 5.4K Oct 11 12:38 test-kvm.xml
-rw-r--r--. 1 root root 257K Oct 11 11:36 test.qcow2
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]#
virsh # snapshot-delete test-kvm --current        #删除虚机当前快照,发现虚机的磁盘文件并未缩小。
Domain snapshot 1603521844 deleted
root@KVM03-10 opt]# ll -h
total 6.7G
-rw-------. 1 root root 10G Oct 11 12:14 centos2.1.raw
-rw-r--r--. 1 qemu qemu 1.7G Oct 24 15:00 centos2.1.raw.qcow2    #这是因为当前虚机所有磁盘格式为写时复制,也就是说虚机的磁盘会越来越大
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 193K Oct 11 11:55 raw-to-qcow2.qcow2
-rw-r--r--. 1 root root 1.0G Oct 11 11:35 resize.raw
-rw-r--r--. 1 root root 5.4K Oct 11 12:38 test-kvm.xml
-rw-r--r--. 1 root root 257K Oct 11 11:36 test.qcow2
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]#

下面再来试一下依照快照恢复,做这个操作前,首先确认虚机是否已经在宿主机上上做了快照。然后进入虚机内部,删系统文件rm -rf /*

virsh # snapshot-revert test-kvm --current      #通过快照恢复系统

virsh # reboot --domain test-kvm           #重启虚机后,虚机正常
Domain test-kvm is being rebooted

快照功能就这么多,其实在生产中很少会到命令行的形式去做快照,一般是在前台界面操作。下面在学习一下虚机的克隆命令哈。

[root@KVM03-10 opt]# virt-clone --help
usage: virt-clone --original [NAME] ...
Duplicate a virtual machine, changing all the unique host side configuration like MAC address, name, etc.
The VM contents are NOT altered: virt-clone does not change anything _inside_ the guest OS, it only duplicates disks and does host side changes. So things like changing passwords, changing static IP address, etc are outside the scope of this tool. For these types of changes, please see virt-sysprep(1).

简单介绍一下virt-clone的命令描述吧。virt-clone是用来复制一台虚拟机并改变所有主机侧唯一的信息,包括Mac地址、主机名(这个主机名应该是KVM平台看到的主机名,而不是虚机系统内部的主机名)等

虚机内容并不会被改变,virt-clone命令不会改变用户操作系统内的任何信息,它仅仅是拷贝磁盘和完成主机名的修改。所以像修改用户名(local-host),修改静态IP地址等不属于该命令的范围,如果要做此类修改,请参加virt-sysprep。

常用的命令

General Options:
  -o ORIGINAL_GUEST, --original ORIGINAL_GUEST Name of the original guest; The status must be shut off or paused.    #被克隆的虚机
  --original-xml ORIGINAL_XML    XML file to use as the original guest.
  --auto-clone          Auto generate clone name and storage paths from the original guest configuration.
  -n NEW_NAME, --name NEW_NAME Name for the new guest          #克隆后的名字
  --reflink             use btrfs COW lightweight copy
[root@KVM03-10 opt]# virt-clone -o test-kvm -n test-kvm-clone --auto-clone
WARNING  Setting the graphics device port to autoport, in order to avoid conflicting.  #提示,为了避免冲突,把克隆出来的虚机的VNC端口设置设置为了自动
WARNING  The requested volume capacity will exceed the available pool space when the volume is fully allocated. (10240 M requested capacity > 8768 M available)
Allocating 'centos2.1.raw-clone.qcow2'                           |  10 GB  00:00:09

Clone 'test-kvm-clone' created successfully.

查看虚机的xml文件如下:

  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>            #该虚机磁盘文件类型为QCOW2,因为克隆前的磁盘文件也是,所以不会修改
      <source file='/opt/centos2.1.raw-clone.qcow2'/>    #该虚机使用的磁盘文件路径在此,因为克隆命令的选项为auto-clone。此时虚机的磁盘文件路径与原机器一样
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

再冲突的对比一下克隆前后的两台机器之间xml文件的区别,检查发现和virt-clone解释内容一致。对比虚机系统内部的用户名是没有修改的,因为IP地址为DHCP,所以会有变化

[root@KVM03-10 qemu]# diff test-kvm.xml test-kvm-clone.xml
4c4
<   virsh edit test-kvm
---
>   virsh edit test-kvm-clone
9,10c9,10
<   <name>test-kvm</name>
<   <uuid>a9c5fb0f-9f5f-4f0b-87ca-06f4b6dfc62d</uuid>      #克隆前虚机的名字和UUID
---
>   <name>test-kvm-clone</name>
>   <uuid>e6598719-4944-49cd-83f6-85cfe431883f</uuid>    #克隆后虚机的名字和UUID
52c52
<       <source file='/opt/centos2.1.raw.qcow2'/>        #磁盘文件路径
---
>       <source file='/opt/centos2.1.raw-clone.qcow2'/>
76a77
>     <controller type='pci' index='0' model='pci-root'/>
83d83
<     <controller type='pci' index='0' model='pci-root'/>
85c85
<       <mac address='52:54:00:db:81:f8'/>          #克隆前虚机的Mac地址
---
>       <mac address='52:54:00:c3:44:a1'/>          #克隆后虚机的Mac地址
122a123
>   <seclabel type='dynamic' model='dac' relabel='yes'/>
[root@KVM03-10 qemu]#

宿主机的VNC端口多了5901,因为VNC自动分配

[root@KVM03-10 opt]# netstat -tlunp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN      2123/qemu-kvm
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN      4434/qemu-kvm

被克隆虚机的快照在克隆过程中不会被一并克隆

[root@KVM03-10 opt]# ll -h
total 7.9G
-rw-------. 1 root root  10G Oct 11 12:14 centos2.1.raw
-rw-------. 1 qemu qemu 1.2G Oct 24 16:05 centos2.1.raw-clone.qcow2   #克隆后的虚机磁盘文件不会包含克隆前虚机的快照
-rw-r--r--. 1 qemu qemu 1.7G Oct 24 16:05 centos2.1.raw.qcow2
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 193K Oct 11 11:55 raw-to-qcow2.qcow2
-rw-r--r--. 1 root root 1.0G Oct 11 11:35 resize.raw
-rw-r--r--. 1 root root 5.4K Oct 11 12:38 test-kvm.xml
-rw-r--r--. 1 root root 257K Oct 11 11:36 test.qcow2
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]#

刚才操作的过程叫做完全克隆,是把虚机的磁盘文件完完整整的复制一份,对于生产中大磁盘虚机,速度可能比较慢,另外还有一种克隆方式叫连接克隆。其实对于这一区别,vmware workstations对这个做出了很好的解释:

 

 

下面就创建一个链接克隆玩一把。

[root@KVM03-10 opt]# qemu-img create -f qcow2 -b centos2.1.raw.qcow2 centos2.1.raw-clone-link.qcow2    
#-b选项可参见“rebase [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename”

#其实这条命令中也可以通过“-o”实现,具体可看一下
Formatting 'centos2.1.raw-clone-link.qcow2', fmt=qcow2 size=10737418240 backing_file='centos2.1.raw.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off [root@KVM03-10 opt]#

[root@KVM03-10 opt]# qemu-img create -f qcow2 -o ? centos2.1.raw-clone-link.qcow2
Supported options:

  1. size Virtual disk size
  2. compat Compatibility level (0.10 or 1.1)
  3. backing_file File name of a base image
  4. backing_fmt Image format of the base image
  5. encryption Encrypt the image
  6. cluster_size qcow2 cluster size
  7. preallocation Preallocation mode (allowed values: off, metadata, falloc, full)
  8. lazy_refcounts Postpone refcount updates

[root@KVM03-10 opt]#

[root@KVM03-10 opt]# qemu-img create -f qcow2 -o backing_file=centos2.1.raw.qcow2 centos2.1.raw-clone-link-o.qcow2
Formatting ‘centos2.1.raw-clone-link-o.qcow2′, fmt=qcow2 size=10737418240 backing_file=’centos2.1.raw.qcow2’ encryption=off cluster_size=65536 lazy_refcounts=off
[root@KVM03-10 opt]#

[root@KVM03-10 opt]# ll -h
total 7.9G
-rw——-. 1 root root 10G Oct 11 12:14 centos2.1.raw
-rw-r–r–. 1 root root 193K Oct 24 16:42 centos2.1.raw-clone-link-o.qcow2
-rw-r–r–. 1 root root 193K Oct 24 16:34 centos2.1.raw-clone-link.qcow2    #通过链接克隆做到的很小
-rw——-. 1 qemu qemu 1.2G Oct 24 16:31 centos2.1.raw-clone.qcow2

查看磁盘信息

[root@KVM03-10 opt]# qemu-img info centos2.1.raw-clone-link-o.qcow2
image: centos2.1.raw-clone-link-o.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
backing file: centos2.1.raw.qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@KVM03-10 opt]# qemu-img info centos2.1.raw-clone-link.qcow2
image: centos2.1.raw-clone-link.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
backing file: centos2.1.raw.qcow2   #该磁盘文件的后置配磁盘文件,所以的读写都是基于centos2.1.raw.qcow2进行。换句话说比如在centos2.1.raw.qcow2上安装了某软件,centos2.1.raw-clone-link.qcow2大小也不会发送变化
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@KVM03-10 opt]#

准备好虚机的磁盘文件以后,需要再搞一个虚机的配置文件,直接复制即可,不过需要修改一下配置文件中所使用到的磁盘文件目录和名字

[root@KVM03-10 qemu]# cp test-kvm.xml test-kvm-clone-link.xml
[root@KVM03-10 qemu]# ll
total 24
drwxr-xr-x. 2 root root   26 Oct 11 10:11 autostart
drwx------. 3 root root   42 Aug 26 09:58 networks
-rw-------. 1 root root 4577 Oct 24 16:57 test-kvm-clone-link.xml
-rw-------. 1 root root 4650 Oct 24 15:43 test-kvm-clone.xml
-rw-------. 1 root root 4577 Oct 24 15:12 test-kvm.xml

<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type=’file’ device=’disk’>
<driver name=’qemu’ type=’qcow2’/>
<source file=’/opt/centos2.1.raw-clone-link.qcow2’/>

<interface type=’network’>


<source network=’default’/>
<model type=’virtio’/>
<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x03′ function=’0x0’/>

[root@KVM03-10 qemu]# virsh define test-kvm-clone-link.xml    #导入成功
Domain test-kvm-clone-link defined from test-kvm-clone-link.xml

[root@KVM03-10 qemu]# virsh start test-kvm-clone-link
Domain test-kvm-clone-link started

[root@KVM03-10 qemu]# virsh list –all              #启动成功
Id Name State
—————————————————-
1 test-kvm running
2 test-kvm-clone running
3 test-kvm-clone-link running

[root@KVM03-10 qemu]#

[root@KVM03-10 qemu]# virsh console test-kvm-clone-link
Connected to domain test-kvm-clone-link
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-514.el7.x86_64 on an x86_64

localhost login: root
Password:
Last login: Sat Oct 24 15:14:11 on tty1
[root@localhost ~]#                    #系统正常

进入到虚机内部创建一个文件

[root@localhost ~]# dd if=/dev/zero of=/home/test-liuhuan.raw bs=200M count=4  #创建了一个800Mb的文件
4+0 records in
4+0 records out
838860800 bytes (839 MB) copied, 10.4435 s, 80.3 MB/s
[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  1.8G  8.3G  18% /
devtmpfs        487M     0  487M   0% /dev
tmpfs           497M     0  497M   0% /dev/shm
tmpfs           497M  6.6M  490M   2% /run
tmpfs           497M     0  497M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
[root@localhost ~]# cd /home/
[root@localhost home]# ll -h
total 801M
-rw-r--r--. 1 root root 4.0K Aug 31 22:28 grub2.cfg.bak
-rw-r--r--. 1 root root 4.0K Aug 31 22:30 grub.cfg.bak
-rw-r--r--. 1 root root 800M Oct 24 17:21 test-liuhuan.raw
[root@localhost home]#

[root@KVM03-10 opt]# ll -h
total 8.9G
-rw——-. 1 root root 10G Oct 11 12:14 centos2.1.raw
-rw-r–r–. 1 root root 193K Oct 24 16:42 centos2.1.raw-clone-link-o.qcow2
-rw-r–r–. 1 qemu qemu 807M Oct 24 17:23 centos2.1.raw-clone-link.qcow2   #通过链接创建出来的虚机的磁盘文件增加了800M,但是后置备的磁盘文件的目录并未增加
-rw——-. 1 qemu qemu 1.2G Oct 24 17:18 centos2.1.raw-clone.qcow2
-rw-r–r–. 1 qemu qemu 1.7G Oct 24 17:03 centos2.1.raw.qcow2
[root@KVM03-10 opt]#

 

[root@localhost ~]# dd if=/dev/zero of=/home/test-liuhuan.raw bs=300M count=2 #在test-kvm,即通过链接方式被克隆的虚机,创建一个600M的文件
2+0 records in
2+0 records out
629145600 bytes (629 MB) copied, 9.02812 s, 69.7 MB/s
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 1.6G 8.5G 16% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.6M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /run/user/0
[root@localhost ~]#

[root@KVM03-10 opt]# ll -h
total 9.4G
-rw——-. 1 root root 10G Oct 11 12:14 centos2.1.raw
-rw-r–r–. 1 root root 193K Oct 24 16:42 centos2.1.raw-clone-link-o.qcow2
-rw-r–r–. 1 qemu qemu 807M Oct 24 17:37 centos2.1.raw-clone-link.qcow2
-rw——-. 1 qemu qemu 1.2G Oct 24 17:39 centos2.1.raw-clone.qcow2
-rw-r–r–. 1 qemu qemu 2.0G Oct 24 17:39 centos2.1.raw.qcow2        #test-kvm的虚机磁盘文件也随之增加,但链接克隆出来的虚机的磁盘文件不会增加
-rw-r–r–. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r–r–. 1 root root 193K Oct 11 11:55 raw-to-qcow2.qcow2
-rw-r–r–. 1 root root 1.0G Oct 11 11:35 resize.raw
-rw-r–r–. 1 root root 257K Oct 11 11:36 test.qcow2
-rw-r–r–. 1 root root 5.0G Oct 11 10:50 test.raw

#也就是可以这样理解,因为qcow2文件类型的特点为写时复制,通过链接方式克隆出来的虚机运行过程使用的系统与被克隆虚机为同一个。但是后面再次增加其他文件则写到自己虚机的磁盘文件中。但是如果删除了原虚机的磁盘文件,链接克隆出来的虚机将无法正常运行。

 

 

 

Tags: