< 所有主题
Print

qemu-img

qemu-img

命令帮助

root@eve-ng:~ # qemu-img --help
qemu-img version 2.5.0 (Debian 1:2.5+dfsg-5ubuntu10.48), Copyright (c) 2004-2008 Fabrice Bellard
usage: qemu-img command [command options]
QEMU disk image utility

Command syntax:
  check [-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename
  create [-q] [-f fmt] [-o options] filename [size]
  commit [-q] [-f fmt] [-t cache] [-b base] [-d] [-p] filename
  compare [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] filename1 filename2
  convert [-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 [-f fmt] [--output=ofmt] [--backing-chain] filename
  map [-f fmt] [--output=ofmt] filename
  snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename
  rebase [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename
  resize [-q] filename [+ | -]size
  amend [-p] [-q] [-f fmt] [-t cache] -o options filename

Command parameters:
  'filename' is a disk image filename
  '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: rbd host_cdrom blkdebug qcow host_device vpc qcow2 cloop vdi sheepdog qed nbd 
tftp vvfat ftp ftps https dmg http vmdk parallels iscsi raw bochs quorum null-aio null-co vhdx blkverify file

创建qcow2虚拟磁盘

创建qcow2
root@eve-ng:~ # qemu-img create -f qcow2 virtioa.qcow2 10G
Formatting 'virtioa.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 
lazy_refcounts=off refcount_bits=16

查看新建的文件
root@eve-ng:~ # ll
total 44908
drwxr-xr-x  2 root root     4096 Mar  9 14:32 ./
drwxr-xr-x 96 root root     4096 Mar  9 14:28 ../
-rw-r--r--  1 root root   197120 Mar  9 14:32 virtioa.qcow2

查看新建的qcow2磁盘信息
root@eve-ng:~ # qemu-img info virtioa.qcow2 
image: virtioa.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes) #磁盘容量,这个磁盘文件的最大容量
disk size: 196K                       #磁盘当前占用的空间,这是占用主机磁盘空间的实际大小
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

转换虚拟磁盘格式

.img转为.qcow2

root@eve-ng:~ # ll    
total 410124
drwxr-xr-x  2 root root      4096 Mar  7 19:50 ./
drwxr-xr-x 95 root root      4096 Mar  7 19:47 ../
-rw-r--r--  1 root root 419955220 Mar  7 19:50 openwrt-x86-64-generic-squashfs-combined.img

指定被转换格式时,不是使用"img",应该使用"raw"
root@eve-ng:~ # qemu-img convert -f img -O qcow2 openwrt-x86-64-generic-squashfs-combined.img hda.qcow2
qemu-img: Could not open 'openwrt-x86-64-generic-squashfs-combined.img': Unknown driver 'img'
root@eve-ng:~ # qemu-img convert -f raw -O qcow2 openwrt-x86-64-generic-squashfs-combined.img hda.qcow2

root@eve-ng:~ # ll
total 456400
drwxr-xr-x  2 root root      4096 Mar  7 19:54 ./
drwxr-xr-x 95 root root      4096 Mar  7 19:47 ../
-rw-r--r--  1 root root  47448064 Mar  7 19:54 hda.qcow2
-rw-r--r--  1 root root 419955220 Mar  7 19:50 openwrt-x86-64-generic-squashfs-combined.img

在此例中,img虚拟磁盘文件大小约400MB,而qcow2虚拟磁盘文件约45MB。

root@eve-ng:~ # qemu-img info hda.qcow2 
image: hda.qcow2
file format: qcow2
virtual size: 401M (419955712 bytes)
disk size: 45M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

root@eve-ng:~ # qemu-img info openwrt-x86-64-generic-squashfs-combined.img 
image: openwrt-x86-64-generic-squashfs-combined.img
file format: raw
virtual size: 401M (419955712 bytes)
disk size: 401M

 

上一个 nmcli
目录