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
- Virtual disk image format: There are mainly two virtual disk image formats for KVM virtual machines
raw
: Raw disk image format (default). This can be the fastest file-based format. Useqemu-img info
to obtain the real size used by the image orls -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).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.
- Command line tool:
qemu-img
. You can useqemu-img --help
orman qemu-img
for detailed information about this tool. This command may requireroot
user privilege. - 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.
- 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
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.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 mywin10_test.qcow2
image, I can useqemu-img resize win10_test.qcow2 +20G
If I want to shrink the virtual image
deepin_test.qcow2
by10GB
, I can useqemu-img resize deepin_test.qcow2 --shrink -10G
Note: When shrinking images, the
--shrink
option must be given. This informsqemu-img
that the user acknowledges all loss of data beyond the truncated image’s end.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: