Take time to learn KVM: virtual machine snapshots and clones

A few days ago, I learned how to use the qume info command in KVM. Today, I learn the snapshot and clone functions, snapshot and virt clone, which are widely used in virtualization. The use of the snapshot command is actually very simple. After entering the virsh interface, you can view the detailed parameter introduction of the corresponding command.

virsh # snapshot-                        #The snapshot command is mainly these 10 commands
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 #from XML Create a snapshot in the file
#snapshot-create-as - Create a snapshot from a set of args;Create a snapshot (disk and RAM) from arguments;#From one argument(Create a snapshot inside
#snapshot-current - Get or set the current snapshot        #Get current snapshot
#snapshot-delete - Delete a domain snapshot                #Delete snapshot
#snapshot-dumpxml - Dump XML for a domain snapshot                #Backup the current snapshot of an instance xml file
#snapshot-edit - edit XML for a snapshot,Returns basic information about a snapshot.#Edit the of a snapshot xml file
#snapshot-list - List snapshots for a domain              #Snapshot list
#snapshot-parent - Get the name of the parent of a snapshot;Extract the snapshot's parent, if any #To extract a snapshot
#snapshot-revert - Revert a domain to a snapshot;Revert domain to snapshot; #Restore the instance to a state of the snapshot

Let's take a brief look at the use of common commands

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

virsh # snapshot-create test-kvm
Domain snapshot 1603521065 created          #Why is the name of the snapshot created here a string of values?
virsh # snapshot-list test-kvm
Name Creation Time State
------------------------------------------------------------
1603521065 2020-10-24 14:31:05 +0800 running   #The name of the snapshot is actually related to Beijing time when I created the snapshot. This number is called Greenwich mean time, also known as Unix timestamp, indicating 19701.1 1603521065 seconds have passed
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    #It is found that if I take a snapshot, the disk used by the virtual machine will increase, indicating that in fact, my snapshot file is placed in the disk file of the virtual machine.
-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        #Delete the current snapshot of the virtual machine and find that the disk file of the virtual machine has not been reduced.
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    #This is because the current format of all disks of the virtual machine is copy on write, which means that the disks of the virtual machine will become larger and larger
-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]#

Let's try to restore according to the snapshot. Before doing this operation, first confirm whether the virtual machine has taken a snapshot on the host machine. Then enter the virtual machine and delete the system file rm -rf/*

virsh # snapshot-revert test-kvm --current      #Recovering the system from snapshots

virsh # reboot --domain test-kvm           #After restarting the virtual machine, the virtual machine is normal
Domain test-kvm is being rebooted

There are so many snapshot functions. In fact, snapshots are rarely taken in the form of command line in production. They are generally operated in the foreground interface. Let's learn the cloning command of virtual machine.

[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).

Briefly introduce the command description of virt clone. Virt clone is used to copy a virtual machine and change the unique information on all hosts, including Mac address, host name (this host name should be the host name seen by KVM platform, not the host name inside the virtual machine system), etc

The contents of the virtual machine will not be changed. The virt clone command will not change any information in the user's operating system. It only copies the disk and completes the modification of the host name. Therefore, modifying the user name (local host) and static IP address does not belong to the scope of this command. If you want to make such modifications, please participate in virt Sysprep.

Common commands

General Options:
  -o ORIGINAL_GUEST, --original ORIGINAL_GUEST Name of the original guest; The status must be shut off or paused.    #Cloned virtual machine
  --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          #Cloned name
  --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.  #Prompt: to avoid conflicts, set the VNC port of the cloned virtual machine to automatic
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.

View the xml file of the virtual machine as follows:

  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>            #The disk file type of the virtual machine is QCOW2. Because the disk file before cloning is also the same, it will not be modified
      <source file='/opt/centos2.1.raw-clone.qcow2'/>    #The disk file path used by the virtual machine is here because the option of the clone command is auto clone. At this time, the disk file path of the virtual machine is the same as that of the original machine
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

Compare the differences of xml files between the two machines before and after cloning, and the results are consistent with the interpretation of virt clone. Compared with the virtual machine, the user name inside the system has not been modified, because the IP address is DHCP, so it will change

[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>      #Name and UUID of virtual machine before cloning
---
>   <name>test-kvm-clone</name>
>   <uuid>e6598719-4944-49cd-83f6-85cfe431883f</uuid>    #Name and UUID of the cloned virtual machine
52c52
<       <source file='/opt/centos2.1.raw.qcow2'/>        #Disk file path
---
>       <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 address of virtual machine before cloning
---
>       <mac address='52:54:00:c3:44:a1'/>          #Mac address of virtual machine after cloning
122a123
>   <seclabel type='dynamic' model='dac' relabel='yes'/>
[root@KVM03-10 qemu]#

The VNC port of the host is 5901 more, because VNC is automatically allocated

[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

Snapshots of the cloned virtual machine will not be cloned during the cloning process

[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   #The cloned virtual machine disk file will not contain the snapshot of the virtual machine before cloning
-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]#

The operation process just now is called complete cloning, which is to completely copy the disk files of the virtual machine. For large disk virtual machines in production, the speed may be relatively slow. In addition, there is another cloning method called connection cloning. In fact, vmware workstations gives a good explanation for this difference:

 

 

Now create a link clone to play.

[root@KVM03-10 opt]# qemu-img create -f qcow2 -b centos2.1.raw.qcow2 centos2.1.raw-clone-link.qcow2    
#-b Options can be found in“ rebase [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename"

#In fact, this command can also be implemented through "- o". See the details
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    #Cloning through links does little
-rw-------. 1 qemu qemu 1.2G Oct 24 16:31 centos2.1.raw-clone.qcow2

View disk information

[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   #The disk file is configured after the disk file, so the reading and writing are based on CentOS 2 1.raw. Qcow2. In other words, like CentOS 2 1.raw. A software is installed on qcow2, centos2 1.raw-clone-link. The size of qcow2 will not change
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@KVM03-10 opt]#

After preparing the disk file of the virtual machine, you need to make another configuration file of the virtual machine and copy it directly. However, you need to modify the directory and name of the disk file used in the configuration file

[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    #Import succeeded
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              #Start successful
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 ~]#                    #The system is normal

Enter the virtual machine and create a file

[root@localhost ~]# dd if=/dev/zero of=/home/test-liuhuan.raw bs=200M count=4  #Created an 800Mb file
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   #The disk file of the virtual machine created through the link has increased by 800 M,However, the directory of the later prepared disk files has not been increased
-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 #stay test-kvm,That is, create a 600 virtual machine cloned by linking M Documents
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        #The virtual machine disk files of test KVM also increase, but the disk files of the virtual machine linked and cloned will not increase
-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

#That is to say, it can be understood in this way, because the qcow2 file type is characterized by copy on write. The virtual machine cloned by linking uses the same system as the cloned virtual machine. However, other files added later will be written to the disk file of your virtual machine. However, if the disk file of the original virtual machine is deleted, the linked cloned virtual machine will not work normally.

 

 

 

Tags: kvm

Posted by gunhoe86 on Tue, 10 May 2022 13:19:21 +0300