linux:qemu-kvm:qemu-img

文書の過去の版を表示しています。


qemu-img による仮想ディスクの操作

$ sudo qemu-img create -f qcow2 filename.qcow2 256G
Formatting 'filename.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=274877906944 lazy_refcounts=off refcount_bits=16
$ sudo qemu-img info filename.qcow2
image: filename.qcow2
file format: qcow2
virtual size: 256 GiB (274877906944 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
$ sudo qemu-img resize filename.qcow2 +256G
Image resized.

仮想ディスクの容量確認

$ sudo qemu-img info filename.qcow2
image: filename.qcow2
file format: qcow2
virtual size: 512G (549755813888 bytes)
disk size: 188G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

仮想ディスクイメージの圧縮 (Shrink) とは、仮想ディスクイメージ内の使われなくなった領域を開放し、イメージサイズをコンパクトにして物理ディスク領域も開放する。

# ll
合計 30426336
-rw-------. 1 root root 274920112128 12月  2 16:18 Windows_10_Pro_x64.qcow2
# qemu-img info Windows_10_Pro_x64.qcow2
image: Windows_10_Pro_x64.qcow2
file format: qcow2
virtual size: 256 GiB (274877906944 bytes)
disk size: 10.7 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: true
    refcount bits: 16
    corrupt: false
    extended l2: false

スパースファイル (英語: sparse file) とはファイルの一種で、部分的に空のファイルを実際のファイルシステム上で少ない消費容量で効率的に保存する仕組みである。実際にディスク上に空の領域を確保せずに、領域が空であることを示すメタデータを書きこむことで実現される。
スパースファイル - Wikipedia

# pwd
/var/lib/libvirt/images
# ls -l --block-size=g
合計 13G
-rw-------. 1 root root 257G 12月  2 16:18 Windows_10_Pro_x64.qcow2
-rw-------. 1 root root 257G 12月  2 16:18 fedora35_minimal.qcow2
-rw-r--r--. 1 root root   1G 12月  3 00:11 filename.qcow2
# df -BG
ファイルシス   1G-ブロック  使用 使用可 使用% マウント位置
devtmpfs                8G    0G     8G    0% /dev
tmpfs                   8G    1G     8G    4% /dev/shm
tmpfs                   4G    1G     4G    1% /run
/dev/sda3             476G   48G   428G   10% /
/dev/sda3             476G   48G   428G   10% /home
/dev/sda2               1G    1G     1G   28% /boot
/dev/sda1               1G    1G     1G    3% /boot/efi
tmpfs                   8G    1G     8G    1% /tmp
tmpfs                   2G    1G     2G    1% /run/user/1000
# find . -type f -printf "%S\t%p\n" | gawk '$1 < 1.0 {print}'
0.0416002	./Windows_10_Pro_x64.qcow2
0.00872493	./fedora35_minimal.qcow2
# qemu-img info filename.qcow2 
image: filename.qcow2
file format: qcow2
virtual size: 256 GiB (274877906944 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
# qemu-img convert -O qcow2 Windows_10_Pro_x64.qcow2 Windows_10_Pro_x64.qcow2_shrink
# ll
合計 30426336
-rw-------. 1 root root 274920112128 12月  2 16:18 Windows_10_Pro_x64.qcow2
-rw-r--r--. 1 root root  11280121856 12月  3 12:59 Windows_10_Pro_x64.qcow2_shrink
# qemu-img convert -O qcow2 -c Windows_10_Pro_x64.qcow2 Windows_10_Pro_x64.qcow2_shrink-c
# ll
合計 30426336
-rw-------. 1 root root 274920112128 12月  2 16:18 Windows_10_Pro_x64.qcow2
-rw-r--r--. 1 root root  11280121856 12月  3 12:59 Windows_10_Pro_x64.qcow2_shrink
-rw-r--r--. 1 root root   6055723008 12月  3 13:22 Windows_10_Pro_x64.qcow2_shrink-c

VMware の vmdk から QEMU の qcow2 形式に変換するには、以下のコマンドを実行する。
※ qcow2 形式は必要に応じて膨らむので、実際に使われている容量のファイルができあがる。

$ sudo qemu-img convert -O qcow2 filename.vmdk filename.qcow2

vmdk から raw イメージに変換するには、以下のコマンドを実行する。
※ vmdk で定義されているディスク容量(使われていない領域を含む)の raw イメージができあがる。

$ sudo qemu-img convert -O raw filename.vmdk filename.img
  • linux/qemu-kvm/qemu-img.1639028209.txt.gz
  • 最終更新: 2021/12/09 14:36
  • by ともやん