Resize KVM Virtual Machine Disk Size
In this article
To resize a KVM virtual machine disk, you use the qemu-img resize command to change the disk image file, then extend the partition and filesystem inside the guest OS. The exact steps differ depending on whether you are growing or shrinking the disk, and whether the disk uses raw or qcow2 format.
Background: How KVM Disk Images Work
KVM (Kernel-based Virtual Machine) uses QEMU disk images as virtual hard drives. These images are files on the host that the guest OS sees as a full disk. The two most common formats are:
- qcow2 – Copy-on-write format that grows as data is written. It supports snapshots, compression, and encryption.
- raw – A flat, byte-for-byte image of the disk. It is simpler and often faster, but takes up full allocated space immediately.
Resizing involves two independent steps: changing the image file size on the host, and then making the guest OS aware of the new space. The first step is done with qemu-img, the second with partition tools like fdisk, parted, or growpart, and filesystem tools like resize2fs (for ext4) or xfs_growfs (for XFS).
Before You Start: Safety and Preparation
Resizing a disk is a low-risk operation if done correctly, but mistakes can corrupt data. Always take these precautions:
- Back up the disk image:
cp disk.qcow2 disk.qcow2.bakor useqemu-img convertto create a copy. - Shut down the VM cleanly before resizing. Do not resize a disk that is in use.
- Check current disk usage inside the guest with
df -hto know how much free space you have and how much you need. - For shrinking, you must first shrink the filesystem and partition inside the guest, then shrink the image. Shrinking is riskier and not supported for all filesystems.
How to Grow a KVM Disk (Step-by-Step)
Growing a disk is straightforward. Here is the general workflow for a qcow2 image:
- Shut down the VM.
- On the host, run:
qemu-img resize /path/to/disk.qcow2 +20Gto add 20 GB, orqemu-img resize /path/to/disk.qcow2 50Gto set the size to exactly 50 GB. - Start the VM and log in.
- Check the disk name with
lsblk(e.g.,vdafor virtio disks). - Extend the partition:
growpart /dev/vda 1(or usefdiskto delete and recreate the partition with the same start sector). - Resize the filesystem: for ext4 use
resize2fs /dev/vda1, for XFS usexfs_growfs /mountpoint. - Verify with
df -h.
For raw images, the same qemu-img resize command works, but you may need to use truncate -s instead if the image is raw and you want to add space without converting. However, qemu-img resize is the recommended method for both.
How to Shrink a KVM Disk (Risky, Not Always Possible)
Shrinking is more complex and dangerous. You cannot shrink a qcow2 image that has snapshots, and you cannot shrink a filesystem that is in use. The general steps are:
- Inside the guest, shrink the filesystem first. For ext4, use
resize2fs /dev/vda1 30G(to shrink to 30 GB). - Shrink the partition with
fdiskorpartedto match the new filesystem size. - Shut down the VM.
- On the host, run
qemu-img resize /path/to/disk.qcow2 30Gto set the image to the new size. - For qcow2, you may want to run
qemu-img convert -O qcow2 disk.qcow2 disk-shrunk.qcow2to reclaim free space.
Shrinking XFS is not supported at all. If you need to shrink an XFS filesystem, you must back up, recreate the partition, and restore.
Common Problems and How to Fix Them
Several issues can arise during resizing:
- Partition not recognized after grow – Run
partprobeor reboot the guest to refresh the partition table. - Filesystem resize fails – Ensure the partition is fully extended first. Use
lsblkto verify the partition size matches the disk size. - Disk is busy – If you get “Device or resource busy”, unmount the filesystem (for shrinking) or use a live CD to resize offline.
- qcow2 image won't shrink – Snapshots prevent shrinking. Delete snapshots with
qemu-img snapshot -dbefore resizing. - Guest OS is Windows – Windows uses NTFS. After growing the disk, open Disk Management (diskmgmt.msc) and use “Extend Volume”. Shrinking NTFS is possible but requires the built-in “Shrink Volume” tool, and you must shrink the partition before shrinking the image.
Choosing the Right Size and Format
When planning a resize, consider the following:
- Growth needs – How much additional space do you actually need? Check current usage with
du -sh /inside the guest. - Future growth – It is often easier to over-allocate now than to resize again later, but don't waste host storage.
- Thin provisioning – qcow2 images only use space as data is written, so you can set a large virtual size without immediately consuming host disk space.
- Raw vs qcow2 – If you need maximum performance and have plenty of storage, raw is fine. If you want snapshots and thin provisioning, use qcow2.
Alternatives to Manual Resizing
If you are using a virtualization management tool, you may not need to touch the command line:
- virt-manager – GUI tool that can resize disks through the “Show virtual hardware details” dialog.
- virsh blockresize – Command-line tool that resizes a disk attached to a running VM (for live growth).
- oVirt / Proxmox – Web-based management panels that offer disk resize options.
These tools still ultimately call qemu-img resize and then require the guest OS to recognize the new space.
Frequently Asked Questions
Can I resize a KVM disk while the VM is running?
Yes, for growing. You can use virsh blockresize to increase the disk size of a running VM, but the guest OS still needs to see the new space. For shrinking, the VM must be shut down.
What is the difference between qcow2 and raw for resizing?
qcow2 images support thin provisioning and snapshots, but shrinking requires converting the image to reclaim space. Raw images are simpler and faster, but growing a raw image with qemu-img resize adds space that may be sparse (not actually allocated).
How do I know the disk device name inside the guest?
Use lsblk or fdisk -l inside the guest. Virtio disks typically appear as vda, vdb, etc. SATA disks appear as sda, sdb.
Can I shrink a qcow2 image with snapshots?
No. Snapshots store the difference between the current state and the snapshot. Shrinking the disk would invalidate those differences. Delete all snapshots before attempting to shrink.
What filesystems can be resized online?
ext4 and XFS can be grown online. Shrinking ext4 can be done offline (unmounted), but XFS cannot be shrunk at all. For NTFS, Windows can grow and shrink volumes online.