一台虚拟机的核心就是一个磁盘镜像,这个镜像可以理解成虚拟机的磁盘,里面有虚拟机的操作系统和驱动等重要文件,qemu-img就是quemu创建管理磁盘镜像的工具。

upload successful

安装

安装好qemu-kvm后就能够使用qemu-img命令。

yum install -y qemu-kvm
或
apt-get install qemu-kvm

常用命令

接下来我们走一个真实流程,借此来熟悉了解qemu-img的常用命令

创建磁盘镜像

命令格式:

qemu-img create [-f fmt] [-o options] filename [size]

创建一个格式为fmt大小为size文件名为filename的镜像文件。根据文件格式fmt的不同,还可以添加一个或多个选项(options)来附加对该文件的各种功能设置,可以使用“-o ?”来查询某种格式文件支持那些选项,在“-o”选项中各个选项用逗号来分隔。

如果“-o”选项中使用了backing_file这个选项来指定其后端镜像文件,那么这个创建的镜像文件仅记录与后端镜像文件的差异部分。后端镜像文件不会被修改,除非在QEMU monitor中使用“commit”命令或者使用“qemu-img commit”命令去手动提交这些改动。这种情况下,size参数不是必须需的,其值默认为后端镜像文件的大小。另外,直接使用“-b backfile”参数也与“-o backing_file=backfile”效果相同。

size选项用于指定镜像文件的大小,其默认单位是字节(bytes),也可以支持k(或K)、M、G、T来分别表示KB、MB、GB、TB大小。另外,镜像文件的大小(size)也并非必须写在命令的最后,它也可以被写在“-o”选项中作为其中一个选项。

随后,我们会使用该命令的更多参数,目前我们需要创建一个qcow2格式4GB大小的镜像文件test.img,,命令如下:

qemu-img create -f qcow2 test.img 4GB
或
qemu-img create -f qcow2 -o size=4GB test.img

磁盘一致性检查

命令格式:

qemu-img check [-f fmt] filename

对磁盘镜像文件进行一致性检查,查找镜像文件中的错误,目前仅支持对“qcow2”、“qed”、“vdi”格式文件的检查。其中,qcow2是QEMU 0.8.3版本引入的镜像文件格式,也是目前使用最广泛的格式。qed(QEMU enhanced disk)是从QEMU 0.14版开始加入的增强磁盘文件格式,为了避免qcow2格式的一些缺点,也为了提高性能,不过目前还不够成熟。而vdi(Virtual Disk Image)是Oracle的VirtualBox虚拟机中的存储格式。参数-f fmt是指定文件的格式,如果不指定格式qemu-img会自动检测,filename是磁盘镜像文件的名称(包括路径)。

我们需要对刚创建的磁盘镜像文件做一个检查,命令如下:

qemu-img check test.img
或
qemu-img check -f qcow2 test.img

结果如下:

No errors were found on the image.

转换镜像格式

命令格式:

qemu-img convert [-c] [-f fmt] [-O output_fmt] [-o options] filename [filename2 […]] output_filename

将fmt格式的filename镜像文件根据options选项转换为格式为output_fmt的名为output_filename的镜像文件。它支持不同格式的镜像文件之间的转换,比如可以用VMware用的vmdk格式文件转换为qcow2文件,这对从其他虚拟化方案转移到KVM上的用户非常有用。一般来说,输入文件格式fmt由qemu-img工具自动检测到,而输出文件格式output_fmt根据自己需要来指定,默认会被转换为与raw文件格式(且默认使用稀疏文件的方式存储以节省存储空间)。

其中,“-c”参数是对输出的镜像文件进行压缩,不过只有qcow2和qcow格式的镜像文件才支持压缩,而且这种压缩是只读的,如果压缩的扇区被重写,则会被重写为未压缩的数据。同样可以使用“-o options”来指定各种选项,如:后端镜像、文件大小、是否加密等等。使用backing_file选项来指定后端镜像,让生成的文件是copy-on-write的增量文件,这时必须让转换命令中指定的后端镜像与输入文件的后端镜像的内容是相同的,尽管它们各自后端镜像的目录、格式可能不同。

如果使用qcow2、qcow、cow等作为输出文件格式来转换raw格式的镜像文件(非稀疏文件格式),镜像转换还可以起到将镜像文件转化为更小的镜像,因为它可以将空的扇区删除使之在生成的输出文件中并不存在。

我们之前创建了一个qcow2格式大小4G名为test.img的镜像文件,现在,我们想对这个镜像文件做扩容,所以需要先转换为raw格式文件。命令如下:

qemu-img convert -O raw test.img test_convert1.raw

查看镜像信息

命令格式:

qemu-img info [-f fmt] filename

展示filename镜像文件的信息。如果文件是使用稀疏文件的存储方式,也会显示出它的本来分配的大小以及实际已占用的磁盘空间大小。如果文件中存放有客户机快照,快照的信息也会被显示出来。下面的命令行演示了前面进行文件转换的输入、输出文件的信息。

我们把qcow2格式文件转成了raw格式文件,接下来看看raw格式文件的信息,命令如下:

qemu-img info test_convert1.raw

结果如下:

image: test_convert1.raw
file format: raw
virtual size: 4.0G (4268709120 bytes)
disk size: 0G

修改镜像大小

命令格式:

qemu-img resize filename [+ | -]size

改变镜像文件的大小,使其不同于创建之时的大小。“+”和“-”分别表示增加和减少镜像文件的大小,而size也是支持K、M、G、T等单位的使用。缩小镜像的大小之前,需要在客户机中保证里面的文件系统有空余空间,否则会数据丢失,另外,qcow2格式文件不支持缩小镜像的操作。在增加了镜像文件大小后,也需启动客户机到里面去应用“fdisk”、“parted”等分区工具进行相应的操作才能真正让客户机使用到增加后的镜像空间。不过使用resize命令时需要小心(最好做好备份),如果失败的话,可能会导致镜像文件无法正常使用而造成数据丢失。

接下来,我们增加1G容量到镜像中,命令如下:

qemu-img resize test_convert1.raw +1G

重新查看镜像信息,会发现镜像大小已经改变。

镜像的快照

命令格式:

qemu-img snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename

“-l” 选项是查询并列出镜像文件中的所有快照,“-a snapshot”是让镜像文件使用某个快照,“-c snapshot”是创建一个快照,“-d”是删除一个快照。

我们为test.img先创建一个名为booting的快照,然后再检查他,最后再删除这个快照,命令如下:

qemu-img snapshot -c booting test.img
qemu-img snapshot -l test.img
qemu-img snapshot -d booting test.img

派生镜像

在创建镜像中,有一个参数backing_file,它可以在一个镜像的基础上创建一个镜像,就像docker技术一样。

我们来根据test.img创建一个派生镜像test_pack1.img,具体操作如下:

qemu-img create -f qcow2 test_pack1.img -o backing_file=test.img 4G
或
qemu-img create -f qcow2 test_pack1.img -o backing_file=test.img,size=4G
或
qemu-img create -f qcow2 test_pack1.img -b test.img 4G

结合之前的格式转换,镜像大小修改,我们可以安全的修改一个镜像的大小了。

全部命令

qemu-img version 2.6.0 (qemu-kvm-ev-2.6.0-28.el7.10.1), Copyright (c) 2004-2008 Fabrice Bellard
usage: qemu-img command [command options]
QEMU disk image utility

Command syntax:
  check [-q] [--object objectdef] [--image-opts] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename
  create [-q] [--object objectdef] [--image-opts] [-f fmt] [-o options] filename [size]
  commit [-q] [--object objectdef] [--image-opts] [-f fmt] [-t cache] [-b base] [-d] [-p] filename
  compare [--object objectdef] [--image-opts] [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] filename1 filename2
  convert [--object objectdef] [--image-opts] [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_id_or_name] [-l snapshot_param] [-S sparse_size] filename [filename2 [...]] output_filename
  info [--object objectdef] [--image-opts] [-f fmt] [--output=ofmt] [--backing-chain] filename
  map [--object objectdef] [--image-opts] [-f fmt] [--output=ofmt] filename
  snapshot [--object objectdef] [--image-opts] [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename
  rebase [--object objectdef] [--image-opts] [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename
  resize [--object objectdef] [--image-opts] [-q] filename [+ | -]size
  amend [--object objectdef] [--image-opts] [-p] [-q] [-f fmt] [-t cache] -o options filename

Command parameters:
  'filename' is a disk image filename
  'objectdef' is a QEMU user creatable object definition. See the qemu(1)
    manual page for a description of the object properties. The most common
    object type is a 'secret', which is used to supply passwords and/or
    encryption keys.
  'fmt' is the disk image format. It is guessed automatically in most cases
  'cache' is the cache mode used to write the output disk image, the valid
    options are: 'none', 'writeback' (default, except for convert), 'writethrough',
    'directsync' and 'unsafe' (default for convert)
  'src_cache' is the cache mode used to read input disk images, the valid
    options are the same as for the 'cache' option
  'size' is the disk image size in bytes. Optional suffixes
    'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),
    'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P)  are
    supported. 'b' is ignored.
  'output_filename' is the destination disk image filename
  'output_fmt' is the destination format
  'options' is a comma separated list of format specific options in a
    name=value format. Use -o ? for an overview of the options supported by the
    used format
  'snapshot_param' is param used for internal snapshot, format
    is 'snapshot.id=[ID],snapshot.name=[NAME]', or
    '[ID_OR_NAME]'
  'snapshot_id_or_name' is deprecated, use 'snapshot_param'
    instead
  '-c' indicates that target image must be compressed (qcow format only)
  '-u' enables unsafe rebasing. It is assumed that old and new backing file
       match exactly. The image doesn't need a working backing file before
       rebasing in this case (useful for renaming the backing file)
  '-h' with or without a command shows this help and lists the supported formats
  '-p' show progress of command (only certain commands)
  '-q' use Quiet mode - do not print any output (except errors)
  '-S' 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 the number of bytes is 0, the source will not be scanned for
       unallocated or zero sectors, and the destination image will always be
       fully allocated
  '--output' takes the format in which the output must be done (human or json)
  '-n' skips the target volume creation (useful if the volume is created
       prior to running qemu-img)

Parameters to check subcommand:
  '-r' tries to repair any inconsistencies that are found during the check.
       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all
       kinds of errors, with a higher risk of choosing the wrong fix or
       hiding corruption that has already occurred.

Parameters to snapshot subcommand:
  'snapshot' is the name of the snapshot to create, apply or delete
  '-a' applies a snapshot (revert disk to saved state)
  '-c' creates a snapshot
  '-d' deletes a snapshot
  '-l' lists all snapshots in the given image

Parameters to compare subcommand:
  '-f' first image format
  '-F' second image format
  '-s' run in Strict mode - fail on different image size or sector allocation

Supported formats: cloop tftp null-aio ftp ftps null-co http file blkreplay raw parallels sheepdog vmdk vvfat vhdx vpc https ssh host_cdrom host_device nbd iscsi gluster bochs rbd blkdebug luks vdi qcow quorum qcow2 qed blkverify dmg

参考

标签: 虚拟化, Qemu, 磁盘镜像, qemu-img

添加新评论