Qemu-img is a QEMU Disk Management tool that compiles qemu-img binary files by default after compiling QEMU-KVM source code. Qemu-img is also a more important tool in the use of QEMU/KVM, and this section introduces its usage and practice usage.
The basic use of the command line for the Qemu-img tool is as follows:
qemu-img command [command Options]
The commands it supports are divided into the following types:
(1 ) Check [-F fmt] filename
Check the disk image file for consistency check, look for errors in the image file, currently only support for "Qcow2", "QED", "VDI" format files. Among them, Qcow2 is the image file format introduced by the QEMU 0.8.3 version, and is the most widely used format in the present. QED (qemu enhanced disk) is an enhanced disk file format that has been added since QEMU version 0.14, to avoid some of the drawbacks of the QCOW2 format and to improve performance, but it is not yet mature enough. and VDI (virtual Disk Image) is the storage format in Oracle's VirtualBox virtual machines. The parameter-F fmt is the format of the specified file, and if the format is not specified qemu-img is automatically detected,filename is the name of the disk image file (including the path).
The following command line demonstrates how to use the qemu-img Check command.
[Email protected] kvm_demo]# qemu-img check Rhel6u3.qcow2
No errors were found on the image.
(2 Create [-F fmt] [- o options] filename [size]
Create an image file formatted with the fmt size file named filename . Depending on the file format fmt , you can also add one or more optionsto attach various feature settings to the file, using "-O?" To query for a format file that supports those options, separated by commas for each option in the "-o" option.
If the Backing_file option is used in the "-o" option to specify a back-end image file, the created image file only records the differences from the back-end image file. The back-end image files are not modified unless you use the "commit" command in QEMU Monitor or use the "qemu-img commit" command to manually commit the changes. In this case, the size parameter is not required, and its value defaults to the back-end image file. In addition, the direct use of the "-B backfile" parameter is the same as the "-O backing_file=backfile" effect.
The size option specifies how large the image file is, the default unit is bytes (bytes), or K (or K), M, G, T to indicate the size of KB, MB, GB, and TB, respectively. In addition, the size of the image file is not necessarily written at the end of the command, it can also be written in the "-o" option as one of the options.
The demo for the Create command is shown below, including options supported for querying the QCOW2 format, creating image files with Backing_file qcow2 format, and creating image files in qcow2 format that do not have a backing_file 10GB size.
[Email protected] kvm_demo]# qemu-img create-f qcow2-o? Temp.qcow
Supported options:
Size Virtual Disk size
Compat compatibility level (0.10 or 1.1)
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)
[Email protected] kvm_demo]# qemu-img create-f qcow2-b rhel6u3.img rhel6u3.qcow2
Formatting ' rhel6u3.qcow2′, fmt=qcow2 size=8589934592 backing_file= ' rhel6u3.img ' Encryption=off cluster_size=65536
[Email protected] kvm_demo]# qemu-img create-f qcow2-o backing_file=rhel6u3.img rhel6u3-1.qcow2
Formatting ' rhel6u3-1.qcow2′, fmt=qcow2 size=8589934592 backing_file= ' rhel6u3.img ' Encryption=off cluster_size=65536
[Email protected] kvm_demo]# qemu-img create-f qcow2-o backing_file=rhel6u3.img,size=20g rhel6u3-2.qcow2
Formatting ' rhel6u3-2.qcow2′, fmt=qcow2 size=21474836480 backing_file= ' rhel6u3.img ' Encryption=off cluster_size= 65536
[Email protected] kvm_demo]# qemu-img create-f qcow2 ubuntu.qcow2 10G
Formatting ' ubuntu.qcow2′, Fmt=qcow2 size=10737418240 encryption=off cluster_size=65536
(3 commit [-F fmt] filename
Commit the changes in the filename file to the backend support image file (specified by Backing_file when created).
(4 ) Convert [-c] [-F fmt] [-o output_fmt] [-O options] filename [filename2 [...]] output_filename
Convert the filename image file in fmt format to the format output_fmt named Output_ according to the options option the image file for filename. It supports conversion between image files in different formats, such as the ability to convert a vmdk file from VMware to a qcow2 file, which is useful for users moving from other virtualization scenarios to KVM. In general, the input file format FMT is automatically detected by the Qemu-img tool, and the output file format output_fmt according to its own needs to specify, The default is converted to the raw file format (and is stored by default using sparse files to save storage space).
Where the "-C" parameter compresses the output image file, only the image files in the Qcow2 and Qcow formats support compression, and the compression is read-only, and if the compressed sector is overridden, it is overwritten with uncompressed data. You can also use "-O options" to specify options such as back-end mirroring, file size, encryption, and so on. Using the Backing_file option to specify a back-end image, so that the resulting file is a copy-on-write delta file, you must make the backend image specified in the conversion command the same as the content of the back-end mirror of the input file, although the directories and formats of their respective back-end mirrors may be different.
If you use Qcow2, Qcow, cow, and so on as the output file format to convert the RAW format image file (non-sparse file format), the mirror transform can also be used to transform the image file into a smaller mirror, because it can remove the empty sector so that it does not exist in the generated output file.
The following command line demonstrates two transformations: convert a VMware VMDK format image to a qcow2 image that KVM can use, and transform a raw image file into a qcow2-formatted image.
[Email protected] kvm_demo]# qemu-img convert my-vmware.vmdk my-kvm.img
(There is no actual vmdk file present, only the command-line action shown)
[Email protected] kvm_demo]# qemu-img convert-o qcow2 rhel6u3.img rhel6u3-a.img
(5 ) Info [-F fmt] filename
Displays information about the filename image file. If the file is stored using a sparse file, it will also show the size of its original allocation and the amount of disk space actually occupied. If the file contains a snapshot of the client, the snapshot information is displayed. The following command line shows the input and output file information for the previous file conversion.
[Email protected] kvm_demo]# qemu-img info rhel6u3.img
Image:rhel6u3.img
File format: raw
Virtual size:8.0g (8589934592 bytes)
Disk size:8.0g
[Email protected] kvm_demo]# qemu-img info rhel6u3-a.img
Image:rhel6u3-a.img
File format: qcow2
Virtual size:8.0g (8589934592 bytes)
Disk size:6.8g
cluster_size:65536
(6 ) Snapshot [-l |-a snapshot |-c Snapshot |-d snapshot] filename
The "-l" option is to query and list all snapshots in the image file, "-a snapshot" is to have the image file use a snapshot, "-C Snapshot" is to create a snapshot, "-D" is to delete a snapshot.
(7 ) Rebase [-F fmt] [-t Cache] [-P] [-u]-b backing_file [-F backing_fmt] C19>filename
Change the back-end image file of the image file, only the QCOW2 and QED formats support the rebase command. Using the file specified in "-B backing_file" as the back-end mirror, the back-end image is also converted to the back-end image format specified in "-F backing_fmt".
It can work in two modes, one is Safe mode is also the default mode, QEMU-IMG will compare the original back-end image and the current back-end image of the difference between the reasonable processing, and the other is non-safe mode, is through the "-U" Parameters to specify, this mode is mainly used to rename the back-end image or move the location after the front-end image file repair processing, by the user to ensure the consistency of the back-end image.
(8 ) Resize filename [+ |-]size
Changes the size of the image file so that it differs from the size at the time of creation. The "+" and "-" respectively indicate the size of the image file is increased and decreased, and size is supported for use in units such as K, M, G, T, and so on. To reduce the size of the image, you need to ensure that the file system in the client has free space, otherwise the data will be lost, in addition, the QCOW2 format file does not support the operation of narrowing the image. After increasing the size of the image file, it is also necessary to start the client into the application of the "Fdisk", "parted" and other partition tools to do the corresponding operation in order to really allow the client to use the increased mirror space. However, you need to be careful when using the resize command (preferably a backup), and if it fails, it can result in data loss due to the image file not being used properly.
The following command line shows the size change of two mirrors: add 2GB of space to a 8GB qcow2 mirror, and reduce 1GB space by a 8GB size RAW image.
[Email protected] kvm_demo]# qemu-img resize rhel6u3-a.img +2g
Image resized.
[Email protected] kvm_demo]# qemu-img info rhel6u3-a.img
Image:rhel6u3-a.img
File Format:qcow2
Virtual size: 10G (10737418240 bytes)
Disk size:6.8g
cluster_size:65536
[Email protected] kvm_demo]# qemu-img resize rhel6u3-b.img-1g
Image resized.
[Email protected] kvm_demo]# qemu-img info rhel6u3-b.img
Image:rhel6u3-b.img
File Format:raw
Virtual size: 7.0G (7516192768 bytes)
Disk size:6.5g
QEMU-IMG command Explanation