Document directory
-
- Qemu-IMG basic commands
- Increase or decrease the image size
- Snapshots
Qmeu-img create virtual machine (create virtual machine, virtual machine snapshot)
Use
Qmeu-img
Manage Virtual Machine Disk Images (create virtual machines and Virtual Machine snapshots)
The core of a virtual machine is a disk image, which can be understood as the disk of a virtual machine, which contains important files such as the operating system and driver of the virtual machine. This document describes how to create a VM.
Create a virtual machine image
Two steps are required to run a VM on a host:
Step 1: create a virtual machine image
qemu-img create -f raw /images/vm1.raw 8G
The image created by qmeu-img is a sparse file. That is to say, the newly created file does not have 8 GB, And it will gradually increase with the increase of data until 8 GB.
Step 2: Start the VM
kvm /imges/vm1.raw
Running result: the system prompts that the bootable device cannot be found because the image does not contain any content.
Use qemu-img to manage Images Qemu-img basic commands
The previous section describes how to use qemu-img to create an image. This section describes the powerful features of qemu-img in image management.
Qemu-img has many commands, including the following commonly used commands. Of course, you know qemu-img-h.
-
info
-
View image information
-
create
-
Create an image
-
check
-
Check image
-
convert
-
Format of the converted image, (raw, qcow ......)
-
snapshot
-
Manage image snapshots
-
rebase
-
Create a new image based on an existing image
-
resize
-
Increase or decrease the image size
Create an image
qemu-img create -f <fmt> -o <options> <fname> <size>
Example:
qemu-img create -f raw -o size=4G /images/vm2.raw
Hzgatt @ hzgatt :~ /Images $LlTotal 0-rw-r -- r -- 1 hzgatt 4.0g June 29 14:11 vm2.rawhzgatt @ hzgatt :~ /Images $Ll-STotal 00-RW-r -- 1 hzgatt 4.0g June 29 14:11 vm2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm2.raw image: vm2.rawfile format: rawvirtual size: 4.0G (4294967296 bytes)disk size: 0
Although the file size in LS is 4 GB, the disk size is actually 0. This is a sparse file.
Conversion
Convert an image file to another format. The formats supported by qemu-IMG can be viewed in 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 nbd dmg tftp ftps ftp https http cow cloop bochs blkverify blkdebug
Conversion command:
qemu-img convert -c -f fmt -O out_fmt -o options fname out_fname
-C: compression is adopted. Only qcow and qcow2 are supported.
-F: the format of the source image, which is automatically detected and therefore omitted.
-O Target Image Format
-O other options
Fname: source file
Out_fname: converted file
Example:
hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2
Hzgatt @ hzgatt :~ /Images $Ll-STotal 136 K 0-RW-r -- 1 hzgatt 5.0g June 29 13:55 vm1.raw136k-RW-r -- 1 hzgatt 193 K June 29 14:22 vm2.qcow2 0-RW-r -- r -- 1 hzgatt 4.0g June 29 14:11 vm2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm2.qcow2 image: vm2.qcow2file format: qcow2virtual size: 4.0G (4294967296 bytes)disk size: 136Kcluster_size: 65536
If you want to see which-o options are supported by the format to be converted, you can add-o at the end of the command?
hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2 -o ?Supported options:size Virtual disk sizebacking_file File name of a base imagebacking_fmt Image format of the base imageencryption Encrypt the imagecluster_size qcow2 cluster sizepreallocation Preallocation mode (allowed values: off, metadata)
Increase or decrease the image size
Note: Only raw images can be changed.
hzgatt@hzgatt:~/images$ qemu-img resize vm2.raw +2GB
Hzgatt @ hzgatt :~ /Images $Ll-stotal 136 K 0-rw-r -- 1 hzgatt 5.0G June 29 13:55 vm1.raw136K-rw-r -- 1 hzgatt 193 K June 29 14:22 vm2.qcow2 0-rw- r -- 1 hzgatt 6.0G June 29 14:28 vm2.rawhzgatt @ hzgatt: ~ /Images $ qemu-img info vm2.raw image: vm2.rawfile format: rawvirtual size: 6.0G (6442450944 bytes) disk size: 0
Snapshots
View snapshots
qemu-img snapshot -l /images/vm2.qcow2
Note: Only qcow2 supports snapshots.
Snapshot taking
qemu-img snapshot -c booting vm2.qcow2
Example:
hzgatt@hzgatt:~/images$ qemu-img snapshot -c booting vm2.qcow2 hzgatt@hzgatt:~/images$ qemu-img snapshot -l vm2.qcow2 Snapshot list:ID TAG VM SIZE DATE VM CLOCK1 booting 0 2012-06-29 14:35:04 00:00:00.000
Restore from snapshot:
qemu-img snapshot -a 1 /images/vm2.qcow2
Then, start the VM from kvm and you will find that the VM is in another State when a snapshot is taken.
Delete a snapshot:
qemu-img snapshot -d 2 /images/vm2.qcow
Use a derived image (qcow2)
When more and more virtual machines are created, and you find that many virtual machines are the same operating system, the difference is that the software installed is not the same, then you will certainly want to extract their public parts, only save things that are different from the public part, so the size of the image goes down, the space increases, and the management is convenient. A derived image is used to do this!
First, check an original image.
hzgatt@hzgatt:~/images$ qemu-img info vm3_base.raw image: vm3_base.rawfile format: rawvirtual size: 2.0G (2147483648 bytes)disk size: 2.0G
Now we create an image, but it is derived from
hzgatt@hzgatt:~/images$ qemu-img create -f qcow2 vm3_5.qcow2 -o backing_file=vm3_base.raw 5GFormatting 'vm3_5.qcow2', fmt=qcow2 size=5368709120 backing_file='vm3_base.raw' encryption=off cluster_size=65536
Hzgatt @ hzgatt :~ /Images $Ll-RW-r -- 1 hzgatt 193 K June 29 15:00 vm3_5.qcow2-rw-r -- r -- 1 hzgatt 2.0g June 29 14:51 vm3_base.raw
hzgatt@hzgatt:~/images$ qemu-img info vm3_5.qcow2 image: vm3_5.qcow2file format: qcow2virtual size: 5.0G (5368709120 bytes)disk size: 136Kcluster_size: 65536backing file: vm3_base.raw (actual path: vm3_base.raw)
^ _ ^, This image is only 136 K, enough to save. Dry is always the truth!
Now we have installed a lot of security patches on vm3_5.qcow2 and found that I want to create a new virtual machine on vm3_5.qcow2. What should I do next?
hzgatt@hzgatt:~/images$ qemu-img convert -O raw vm3_5.qcow2 vm3_base2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm3_base2.raw image: vm3_base2.rawfile format: rawvirtual size: 5.0G (5368709120 bytes)disk size: 592M
This conversion will merge vm3_5 and base to generate a new vm3_base2.raw, and then you can continue the endless derivation journey!
Original