
A Step-by-Step Guide to Safely Shrinking Your KVM Virtual Machine Disk
Virtual machines are essential tools, but their virtual disks can grow over time, consuming significant storage on your host system. While expanding a disk is straightforward, shrinking one requires a more careful, multi-step process. Failing to follow the correct procedure can lead to data corruption and an unbootable VM.
This guide provides a clear and safe methodology for reducing the size of a KVM virtual disk, reclaiming valuable space on your hypervisor. We will cover the necessary preparations within the guest operating system and the final steps on the host machine.
The Golden Rule: Always Create a Backup First
Before you begin any operation that modifies a virtual disk’s structure, a backup is not optional—it’s essential. This process involves altering partition tables and filesystems, and a single mistake can result in total data loss.
Actionable Tip: The simplest way to back up your VM is to shut it down and make a direct copy of its virtual disk file (e.g., the .qcow2 or .raw file). Store this copy in a safe location. If anything goes wrong, you can simply restore this file to recover your virtual machine.
Step 1: Prepare the Guest Operating System
You cannot shrink a virtual disk file on the host if the partitions and filesystems inside it are still occupying the larger size. The first and most critical phase of this process happens entirely within the running virtual machine.
Clean Up Unnecessary Files: Begin by deleting any files, logs, old software packages, and temporary data you no longer need. The goal is to maximize the free space within the operating system.
Zero-Out Free Space: This is a crucial step that is often overlooked. When you delete a file, the data isn’t actually erased; the operating system simply marks the space as available for future use. To effectively shrink the disk file, the host needs to know which blocks are truly empty. We achieve this by writing zeros to all unused space.
For Linux Guests: Use the
ddcommand to create a large file of zeros until the disk is full, and then immediately delete it.dd if=/dev/zero of=/zerofile bs=4M; rm -f /zerofileFor a more robust solution, consider using
zerofreeon an unmounted filesystem orsfillfrom thesecure-deletetoolkit, which is designed for this purpose.For Windows Guests: Microsoft provides a dedicated tool called SDelete. Download it from the Sysinternals Suite, open a command prompt as an administrator, and run the following command, replacing
C:with the appropriate drive letter.
cmd
sdelete64.exe -z C:
Shrink the Partition: Now you must resize the actual partition within the guest OS to be smaller than its current size. You cannot shrink the virtual disk on the host to a size smaller than the end of the last partition inside it.
Using a GUI (Recommended for most users): Boot the VM from a live CD image, such as GParted Live. This provides a user-friendly graphical interface to safely resize your partitions. Shrink the main partition to your desired new size, leaving unallocated space at the end of the disk.
Using Command Line Tools (Advanced): For Linux, tools like
fdiskorpartedcan be used to delete and recreate the partition with a smaller size. This is a high-risk operation and should only be performed by experienced users. After resizing the partition, you must also resize the filesystem on it using a tool likeresize2fsfor ext4 filesystems. Note: Some filesystems, like XFS, cannot be shrunk.
Once the partitions are successfully resized, shut down the virtual machine completely.
Step 2: Reduce the Virtual Disk File on the Host
With the guest OS prepared and shut down, you can now proceed with shrinking the actual disk image file on your KVM host.
There are two primary methods for this.
Method A: The virt-sparsify Method (Recommended)
The safest and most efficient way to reclaim space is by using the virt-sparsify tool, which is part of the libguestfs-tools package. This utility intelligently creates a new, smaller copy of your virtual disk, automatically removing the zeroed-out free space.
First, ensure you have the tools installed:
# For Debian/Ubuntu
sudo apt-get install libguestfs-tools
# For RHEL/CentOS
sudo yum install libguestfs-tools
Next, run the command to create a new, smaller disk image.
virt-sparsify --in-place your-vm-disk.qcow2
The --in-place option modifies the disk directly, but for maximum safety, it’s often better to create a new file:
virt-sparsify your-original-disk.qcow2 new-smaller-disk.qcow2
After this command completes, you can replace the old disk file with the new, smaller one.
Method B: The qemu-img Conversion Method
If you cannot use virt-sparsify, an alternative is to use qemu-img to convert the disk image. This process effectively creates a new copy, and during the conversion, it will discard the empty (zeroed) blocks. This method works especially well for qcow2 files.
qemu-img convert -O qcow2 your-original-disk.qcow2 new-smaller-disk.qcow2
This command reads the original disk and writes a new qcow2 file, naturally making it sparse and smaller. Once complete, you can safely replace the original file with the newly created one.
Final Step: Verification
After replacing the old disk file with the new, shrunken version, it’s time to verify that everything works.
- Start your virtual machine.
- Ensure that it boots correctly and the operating system loads without errors.
- Log in and check the disk space using
df -hin Linux or Disk Management in Windows to confirm that the OS recognizes the smaller partition size.
If the VM boots and operates as expected, the process was a success. You can now safely delete the backup you created at the beginning.
Source: https://kifarunix.com/decrease-shrink-kvm-virtual-machine-disk-size/


