Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the qemu-img command

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the qemu-img command for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Qemu-img is the disk management tool of QEMU, and the binary qemu-img will be compiled by default after the qemu-kvm source code is compiled. Qemu-img is also an important tool in the process of using QEMU/KVM. This section introduces its usage and practical usage.

The basic command line usage of the qemu-img tool is as follows:

Qemu-img command [command options]

The commands it supports are as follows:

(1) check [- f fmt] filename

Check the consistency of disk image files to find errors in the image files. Currently, you can only check files in "qcow2", "qed" and "vdi" formats. Among them, qcow2 is the image file format introduced by QEMU 0.8.3, and it is also the most widely used format at present. Qed (QEMU enhanced disk) is an enhanced disk file format added since QEMU version 0.14, in order to avoid some of the shortcomings of the qcow2 format and to improve performance, but it is not yet mature enough. Vdi (Virtual Disk Image) is the storage format in Oracle's VirtualBox virtual machine. The parameter-f fmt is the format of the specified file, which is automatically detected if you do not specify the format qemu-img, and filename is the name of the disk image file (including the path).

The following command line demonstrates the use of qemu-img 's check command.

[root@jay-linux 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 with the format fmt and the size of the size file named filename. Depending on the file format fmt, you can add one or more options (options) to attach various functional settings for the file, using "- o?" To query which options are supported by files in a certain format, which are separated by commas in the "- o" option.

If backing_file is used in the "- o" option to specify its back-end image file, then the created image file records only the difference between the back-end image file and the back-end image file. The back-end image files will not be modified unless you use the "commit" command in QEMU monitor or the "qemu-img commit" command to manually commit these changes. In this case, the size parameter is not required, and its value defaults to the size of the backend mirror file. In addition, using the "- b backfile" parameter directly has the same effect as "- o backing_file=backfile".

The size option is used to specify the size of the image file, the default unit is bytes, or k ("% x8" K), M, G, T are supported to represent KB, MB, GB, and TB sizes, respectively. In addition, the size of the image file (size% F mirror size% 9 does not have to be written at the end of the command, it can also be written in the "- o" option as one of the options.

The demonstration of the c2eate command is shown below, which includes the options supported by the eAC query qcow2 format, the creation of an image file in qcow2 format with backing_file, and an image file in qcow2 format that creates% B without the 10GB size of e89backing_file.

[root@jay-linux 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)

[root@jay-linux kvm_demo] # qemu-img create-f qcow2-b rhel6u3.img r (el6u3.qcow2

Formatting 'rhel6u3.qcow2', fmt=qcow2 size=8589934592 backing_file='rhel6u3.img' encryption=off cluster_size=6u536

[root@jay-linux kvm_demo] # qemu-img create-f qcow2-o backing_file=rhel6u3.img rhel6u3-1.qcow2

Formatting 'rhel6u3-1.qcow2percent, fmt=qcow2 size=8589934592 backing_file='rhel6u3.img' encryption=off cluster_size=65536

[root@jay-linux kvm_demo] # qemu-img create-f qcow2-o backing_file=rhel6u3.img,size=20G rhel6u3-2.qcow2

Formatting 'rhel6u3-2.qcow2percent, fmt=qcow2 size=21474836480 backing_file='rhel6u3.img' encryption=off cluster_size=65536

[root@jay-linux 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] filenam% [filename2 [...]] Output_filename

Converts an filename image file in fmt format to an image file named output_filename in output_fmt format according to the options option. It supports the conversion of image files in different formats, such as using vmdk format files for VMware to qcow2 files, which is very useful for users who move from other virtualization schemes to KVM. Generally speaking, the input file format fmt is automatically detected by the qemu-img tool, while the output file format output_fmt is specified according to its own needs and will be converted to the raw file format by default (and sparse files are used by default to save storage space).

Where the "- c" parameter is to compress the output image file, but only qcow2 and qcow format image files support compression, and this compression is read-only, if the compressed sector is rewritten, it will be rewritten to uncompressed data. You can also use "- o options" to specify various options, such as: back-end image, file size, encryption, and so on. Use the backing_file option to specify the backend image so that the generated file is an incremental file of copy-on-write. In this case, you must make the content of the backend image specified in the conversion command the same as the backend image of the input file, although the directory and format of their respective backend images may be different.

If you use qcow2, qcow, cow, etc., as output file formats to convert raw format image files (non-sparse file formats), image conversion can also convert the image file to a smaller image because it removes empty sectors so that they do not exist in the generated output file.

The following command line demonstrates two transformations: to convert an vmdk format image of VMware to a qcow2 image that KVM can use, and to convert an raw image file into an qcow2 format image.

[root@jay-linux kvm_demo] # qemu-img convert my-vmware.vmdk my-kvm.img

(there is no actual vmdk file here, only a demonstration of its command line operation)

[root@jay-linux kvm_demo] # qemu-img convert-O qcow2 rhel6u3.img rhel6u3-a.img

(5) info [- f fmt] filename

Displays the information of the filename image file. If the file is stored using sparse files, it also shows the size of its original allocation and the actual amount of disk space it has occupied. If the client snapshot is stored in the file, the snapshot information will also be displayed. The following command line demonstrates the information about the input and output files that were previously converted.

[root@jay-linux kvm_demo] # qemu-img info rhel6u3.img

Image: rhel6u3.img

File format: raw

Virtual size: 8.0g (8589934592 bytes)

Disk size: 8.0G

[root@jay-linux 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 queries and lists all snapshots in the mirror file, "- a snapshot" causes the mirror file to use a snapshot, "- c snapshot" creates a snapshot, and "- d" deletes a snapshot.

(7) rebase [- f fmt] [- t cache] [- p] [- u]-b backing_file [- F backing_fmt] filename

Change the backend image file of the image file. Only the qcow2 and qed formats support the rebase command. The file specified in "- b backing_file" is used as the back-end image, and 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 the safe mode (Safe Mode), which is also the default mode. Qemu-img will compare the difference between the original backend image and the current backend image for reasonable processing. The other is the non-secure mode (Unsafe Mode), which is specified by the "- u" parameter. This mode is mainly used to rename the backend image or repair the front-end image file after the location has been moved, and it is up to the user to ensure the consistency of the backend image.

(8) resize filename [+ | -] size

Change the size of the image file to make it different from the size when it was created. "+" and "-" mean to increase and decrease the size of the image file, respectively, while size also supports the use of K, M, G, T and other units. Before reducing the size of the image, you need to ensure that there is free space in the file system in the client, otherwise data will be lost. In addition, qcow2 format files do not support the operation of reducing the image. After increasing the size of the image file, you also need to start the client to apply "fdisk", "parted" and other partition tools to do the corresponding operation in order to really let the client use the increased image space. However, you need to be careful when using the resize command (it is best to make a good backup). If it fails, it may cause the image file not to be used properly and result in data loss.

The following command line demonstrates the size change of two mirrors: increase the 2GB space for a 8GB qcow2 image and reduce the 1GB space for a 8GB-sized raw image.

[root@jay-linux kvm_demo] # qemu-img resize rhel6u3-a.img + 2G

Image resized.

[root@jay-linux 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

[root@jay-linux kvm_demo] # qemu-img resize rhel6u3-b.img-1G

Image resized.

[root@jay-linux 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

This is the end of this article on "how to use qemu-img commands". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report