首页 > 技术文章 > qemu-img convert转换磁盘格式

weihua2020 2020-09-24 00:02 原文

老规矩,man 查看 qemu-img convert相关选项,如下:

 简单翻译如下:

convert [-c] [-p] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-B backing_file] [-o options] [-s snapshot_id_or_name] [-l snapshot_param] [-m num_coroutines] [-W] [-S sparse_size] filename [filename2 [...]] output_filename

Convert the disk image filename or a snapshot snapshot_param(snapshot_id_or_name is deprecated) to disk image output_filename using format output_fmt. It can be optionally compressed ("-c" option) or use any format specific options like encryption ("-o" option).

Only the formats "qcow" and "qcow2" support compression. The compression is read-only. It means that if a compressed sector is rewritten, then it is rewritten as uncompressed data.

Image conversion is also useful to get smaller image when using a growable format such as "qcow": the empty sectors are detected and suppressed from the destination image.

sparse_size indicates the consecutive number of bytes (defaults to 4k) that must contain only zeros for qemu-img to create a sparse image during conversion. If sparse_size is 0, the source will not be scanned for unallocated or zero sectors, and the destination image will always be fully allocated.

You can use the backing_file option to force the output image to be created as a copy on write image of the specified base image; the backing_file should have the same content as the input's base image, however the path,image format, etc may differ.

If a relative path name is given, the backing file is looked up relative to the directory containing
output_filename.

If the "-n" option is specified, the target volume creation will be skipped. This is useful for formats such as "rbd" if the target volume has already been created with site specific options that cannot be supplied through qemu-img.

Out of order writes can be enabled with "-W" to improve performance. This is only recommended for preallocated devices like host devices or other raw block devices. Out of order write does not work in combination with creating compressed images.

num_coroutines specifies how many coroutines work in parallel during the convert process (defaults to 8).

convert动作是将filename(可以同时指定多个)的disk image,或者snapshot snapshot_param(snapshot_id_or_name选项被遗弃)转换为output_filename,照这个意思,就是可以将多个input disk image合并为一个ouput disk image,可以配合使用-c 压缩选项或者-o 加密选项,当然加密选项需要特定的文件格式支持。

只有qcow和qcow2支持压缩,并且压缩是只读的,也就是说当一个压缩的扇区重写,那么该重写扇区就会变为非压缩数据
convert实际也可以用于压缩disk image大小,比如使用可增长式 格式qcow,,空扇区会被探测并压缩到目标disk image

SPARSE_SIZE表示转换期间qemu-img创建稀疏映像时必须仅包含零的连续字节数(默认值为4k)。如果稀疏大小为0,则不会扫描源是否有未分配的扇区或零个扇区,而目标映像将始终完全分配。

可以使用-B 参数来创建基于input disk image(base file)的out disk image
使用-B backing_file选项来指定后端镜像,让生成的文件是copy-on-write的增量文件,这时必须让转换命令中指定的后端镜像与输入文件的后端镜像的内容是相同的,尽管它们各自后端镜像的目录、格式可能不同。

 

相对路径??

-n参数如果存在,那么target 卷创建将会跳过,这在创建rbd格式转换时有用

-W表示乱序写,可以提高性能,推荐在主机host device或者raw block device中使用,注意乱序写不能和compressed压缩同时使用

-m num_coroutines 指定多进程协同处理convert转换,默认为8

 -f表示输入disk image的格式

-O 表示输出disk image的格式

 

根据man qemu-img中最后内容提示,支持的文件类型有:除了raw qcow2,还有以下格式:

 

 格式说明:vmdk  ,vdi vhd,vhdx 大家想必很熟悉了,raw是通用格式,qcow2是qemu主推的格式,qemu推荐使用raw和qcow2以获取更好的performance

 

留坑:-B参数如何使用???

推荐阅读