Using QEMU-IMG to manage virtual machine disk mirroring (creating virtual machines, virtual machine snapshots)

Source: Internet
Author: User

The core of a virtual machine is a disk image, which can be understood as a disk of virtual machines with important files such as the operating system and drivers of virtual machines. This article mainly describes the general process of creating virtual machines.
To create a virtual machine image

It typically takes two steps to run a virtual machine on a host:

First step: Create Virtual machine Mirroring

qemu-img Create -F Raw/images/vm1.raw 8G

The mirror created by qmeu-img is a sparse file, which means that the file you just created does not have 8G, and it will increase slowly as the data increases until 8G

Step two: Start the virtual machine

KVM /imges/vm1.raw

Run Result: Because there is nothing in the mirror, the prompt cannot find a bootable device.

using qemu-img to manage mirrors qemu-img basic Commands

The previous section describes the use of qemu-img to create mirrors, which describes the powerful features of qemu-img in mirroring management.

Qemu-img has many commands, including the following commonly used, of course qemu-img-h you understand. Info

viewing information for mirrors

Create Mirror Check

Check Mirror convert

Transform the format of the Mirror (Raw,qcow ...). ) Snapshot

Manage snapshots of mirrors rebase

Create a new mirror based on an existing mirror resize

Increase or decrease the mirror size
creating mirrors

qemu-img Create -F <fmt>-o <options> <fname> <size>


qemu-img Create -F raw-o Size=4g/images/vm2.raw

Total 0-rw-r--r--1 Hzgatt hzgatt 4.0G  June 14:11 vm2.raw
00-rw-r--r-- 1 Hzgatt hzgatt 4.0G  June 14:11 Vm2.raw

qemu-img Info Vm2.raw 
file Format:raw
virtual size:4.0g (4294967296 bytes)
disk size:0

Although the size of the file in LS is 4G, the disk size is actually 0. That's the sparse file.


Converts a mirrored file into a different format, and the QEMU-IMG supported format can look at the last line of Qemu-img-h.

Supported FORMATS:VVFAT VPC VMDK VDI Sheepdog RBD Raw host_cdrom host_floppy host_device file QED qcow2 qcow parallels NB D DMG tftp ftps ftp https http cow cloop bochs blkverify blkdebug

Conversion command:

qemu-img convert-c - o - o fname Out_fname

-C: Using compression, only Qcow and QCOW2 support

-F: Source mirror format, it will automatically detect, so omit the

The format of the-o target Mirror

-O Other Select first

FName: Source Files

Out_fname: Converted Documents

See Example:

qemu-img convert -c-o qcow2 vm2.raw vm2.qcow2

LL- s
Total 136K
   0-rw-r--r--1 hzgatt hzgatt 5.0G  June 13:55 vm1.raw
136k-rw-r--r--1 hzgatt hzgatt 193K  June 2 9 14:22 vm2.qcow2
   0-rw-r--r--1 hzgatt hzgatt 4.0G  June 14:11 Vm2.raw
qemu-img Info vm2.qcow2 
file format:qcow2
virtual size:4.0g (4294967296 bytes)
disk size:136k

If you want to see what the-o option is supported by the format you want to convert, you can add-o at the end of the command.

qemu-img convert -c-o qcow2 vm2.raw vm2.qcow2- o?
Supported options:
size             Virtual disk size
backing_file     file name of a base image
Backing_fmt      Image format of the base image
encryption       Encrypt the image
cluster_size     qcow2 cluster size
Preallocation    preallocation mode (allowed Values:off, metadata)

Increase reduce mirror size

Note: Only mirrors in RAW format can change size

qemu-img Resize +2GB
hzgatt@hzgatt:~/images$ ll-s total
   0-rw-r--r--1 hzgatt hzgatt 5.0G  June 13:55 vm1.raw
1 36k-rw-r--r--1 Hzgatt hzgatt 193K  June 14:22 vm2.qcow2
   0-rw-r--r--1 hzgatt hzgatt 6.0G  June 14:28 vm2.ra W
hzgatt@hzgatt:~/images$ qemu-img info vm2.raw 
file format:raw
virtual size:6.0g ( 6442450944 bytes)
disk size:0


View Snapshots

qemu-img snapshot-l /images/vm2.qcow2

Note: only Qcow2 supports snapshots

Make a snapshot

qemu-img snapshot-c booting Vm2.qcow2


qemu-img Snapshot -c booting vm2.qcow2 
qemu-img snapshot-l vm2.qcow2 
Snapshot list:
id< C5/>tag                 vm SIZE                DATE       vm CLOCK
1         booting                   0 2012-06-29 14:35:04   00:00:00.000

Restore from Snapshot:

qemu-img snapshot-a 1/images/vm2.qcow2

Then boot the virtual machine from the KVM and find the virtual machine in a snapshot state

To delete a snapshot:

qemu-img snapshot-d 2/images/vm2.qcow

using derived mirrors (qcow2)

When you create more and more virtual machines, and you find that many virtual machines are of the same operating system, the difference is that the installation of software is not the same, then you will certainly want to take their public parts, only to save those with the public part of the different things, so that the image size down, more space, management is also convenient. A derived mirror is used to do this.

First look at a raw mirror

qemu-img Info vm3_base.raw 
file format:raw
virtual size:2.0g (2147483648 bytes)
disk size:2.0g

Now we create a new mirror, but derive from it

qemu-img create-f qcow2 vm3_5.qcow2-o backing_file= Vm3_base.raw 5G
Formatting ' vm3_5.qcow2 ', fmt=qcow2 size=5368709120 backing_file= ' Vm3_base.raw ' Encryption=off cluster_size=65536

ll-rw-r--r--1 hzgatt hzgatt 193K  June 15:00 vm3_5.qcow2
-rw-r--r--1 hzgatt hzgatt 2.0G  June 29 14: Wuyi Vm3_base.raw

qemu-img Info Vm3_5.qcow2 
file format:qcow2
virtual size:5.0g (5368709120 bytes)
disk size:136k
backing File:vm3_base.raw (actual path:vm3_base.raw)

^_^, this image is only 136K, enough to save it. Dry the eternal truth.

Now we have a lot of security patches on the vm3_5.qcow2 and then find that I want to derive a new virtual machine on Vm3_5.qcow2, O (∩∩) o ... Haha, what to do next.

qemu-img convert -o raw vm3_5.qcow2 Vm3_base2.raw

qemu-img Info vm3_base2.raw 
file format:raw
virtual size:5.0g (5368709120 bytes)
disk size:592m

This transformation will merge vm3_5 and base, generating new Vm3_base2.raw, and you can continue to carry on an endless journey of derivation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.