Change virtual disk image

In this post we talk about how to change the space of virtual disk images for KVM guest system.

Basic knowledge

  1. Virtual disk image format: There are mainly two virtual disk image formats for KVM virtual machines
    1. raw: Raw disk image format (default). This can be the fastest file-based format. Use qemu-img info to obtain the real size used by the image or ls -ls on Unix/Linux. Although Raw images give optimal performance, only very basic features are available with a Raw image (for example, no snapshots are available).
    2. qcow2: QEMU image format, the most versatile format with the best feature set. Use it to have optional AES encryption, zlib-based compression, support of multiple VM snapshots, and smaller images, which are useful on file systems that do not support holes (non-NTFS file systems on Windows). Note that this expansive feature set comes at the cost of performance.
  2. Command line tool: qemu-img. You can use qemu-img --help or man qemu-img for detailed information about this tool. This command may require root user privilege.
  3. Warning: Never use qemu-img to modify images in use by a running virtual machine or any other process; this may destroy the image. Also, be aware that querying an image that is being modified by another process may encounter inconsistent state.
  4. Imagine the virtual disk image in your host machine as a special physical hard drive to your guest system. If you increase it, you’ll need to use partition tools in your guest system to actually use the new space. If you want to shrink a disk image, you MUST use file system and partitioning tools inside the VM to reduce allocated file systems and partition sizes accordingly. Failure to do so will result in data loss!

Resize the disk image

  1. Locate your disk image file. By default it’s stored at /var/lib/libvirt/images/. You can check your VM’s XML file to locate the disk image files.

  2. Make sure you’ve done preparation in your VM, especially when you want to shrink the disk image. Then use qemu-img resize to extend/shrink the disk image.

        qemu-img resize filename [--shrink] [+|-]size[K|M|G|T]

    For example, if I want to add 20GB to my win10_test.qcow2 image, I can use

        qemu-img resize win10_test.qcow2 +20G

    If I want to shrink the virtual image deepin_test.qcow2 by 10GB, I can use

        qemu-img resize deepin_test.qcow2 --shrink -10G

    Note: When shrinking images, the --shrink option must be given. This informs qemu-img that the user acknowledges all loss of data beyond the truncated image’s end.

  3. You can use qemu-img info filename to check the image’s information. Add you should also check the disk information in your VM.

Useful reference

qemu-img is a useful tool. You can use it to check, compare, copy and create virtual disk images and then add them to your VMs. Below are some useful references:

Red Hat’s QEMU documentation about qemu-img

Red Hat’s documentation about using qemu-img

Chao Cheng
Chao Cheng

My research interests include applied statistics and machine learning.