I have a QNap NAS with a virtual Windows Server 2012 R2 Essentials on it. I use this virtual machine only as a backup. Recently I had a need to shrink, or compact, the virtual hard drive image file on it. I found this board post that described a way to do this. But it seemed overly complicated. And for me it would require a hard drive larger than 2 TB to do it. The only hard drive I had that large was in the NAS itself. So I needed a better way. This is the steps I used for my QNap virtual machine, which is based QEMU. The steps should work with any virtual machine, including Microsoft Hyper-V, Oracle VirtualBox, and Synology’s virtual machine. These steps should only be used if there is no other easy way to shrink the virtual hard drive image.

  1. Create a backup. The backup can be something as simple as making a copy of virtual hard drive.
  2. Boot your virtual machine into GParted. Shrink the virtual hard drive partition so that it is smaller than the size you want for new virtual drive to be. For example, if the virtual hard drive is 2000 GB and you want it to be 1500 GB, then shrink it down to 1950 GB, then 1900 GB, and so on until you get the correct size. If necessary, move all partitions after the boot partition so that the free space after the last partition is under the reduced size. The slow shrink is necessary to avoid errors on the partition.
  3. Exit GParted.
  4. Run your virtual operating system’s disk checking utility.
  5. Shut down the virtual operating system.
  6. Create a new virtual hard drive that will the target size you need. In our example above, that would 1500 GB, or 1.5 TB.
  7. Boot your virtual machine into Clonezilla.
  8. The options you need to use are Expert mode, device-to-device, then choose the old hard drive — which will show up as the original image size — as the source and the smaller drive as the destination. In our example above, the source drive would be 2 TB because we haven’t shrunk the virtual drive, just the partitions on the virtual drive.
  9. If this is a Windows virtual machine, uncheck the “-g auto Reinstall grub on target hard disk boot sector“. Check the options “-rescue Continue reading next one when disk block read errors” and “-icds Skip checking destination disk size before creating partition table“. Then press OK at the bottom.
  10. You can skip check/repairing source file system. You should use the partition table of the source disk. These are the next two screens.
  11. If necessary, boot back into GParted and move and resize the partitions to fill the new virtual disk size.
  12. Using your virtual machine’s console, remove the extra hard drive, choose the shrunk virtual drive image instead of the old larger one.
  13. Boot into your operating system and test. Test a lot. Maybe even a few days. If you used GParted again, run a disk check.
  14. If everything looks good, you can delete the old larger image and the backup.