How to shrink a virtual disk
XCP-ng doesn't support VDI (Virtual Disk Image) shrinking, only growing. However, there are solutions to achieve the same result, albeit with some manual steps.
Windows VMs
There are two options to do this. The most reliable is the Clonezilla method, but the XenConvert route might also work.
XenConvert
- Attach a disk of the desired size to the VM.
- Within the VM: Format this new disk in NTFS (quick format).
- Install and start XenConvert: select the system disk (probably C:) and set the newly formatted disk as the destination.
- Acknowledge the warning that the destination disk is smaller than the source.
- Wait for the copy process to complete.
- Using the Windows Disk Management tool, set the partition of the new disk to "Active" status.
- Shut down the VM.
- Detach both disks, then reattach the new disk so that it is in position 1.
- Start the VM.
CloneZilla
Use the i686 version of Clonezilla to avoid boot issues on the ISO while using a Windows template.
Pre-requisites
It seems that doing a dism /Online /Set-ReservedStorageState /State:Disabled
first (before anything else) is required on Windows to get a succesful partition shrink and avoiding any data loss.
- Identify the partition you want to move to the new disk and then shut down the VM.
- Create a new drive of the smaller size you want in this VM.
- Switch the VM to BIOS mode.
- Get the Clonezilla ISO available in your ISO SR and insert it.
- Check the boot order to ensure it's CD drive and then HDD.
Steps
- Boot the VM.
- Select "Start CloneZilla."
- Choose "device-device," meaning CloneZilla will copy from one disk to another, which is our goal.
- Select "Expert Mode."
- Choose "Disk to local disk" mode.
- Select the origin disk (the bigger one).
- Then select the destination disk (the smaller one).
- In the advanced options, make sure to unselect "-g auto."
- Also select "Don't check the source filesystem."
- Use the "-k1" option (Create the partition table proportionally).
- Finally, select "-p poweroff" once the copy is done.
- Start the cloning process!
When it's done:
- Eject the Clonezilla CD.
- Switch back the VM to UEFI.
- Detach the bigger disk (keep it as a backup in case something goes wrong).
- Boot the VM.
Linux VMs
You can use the same Clonezilla method. However, since "everything is a file" in Linux/BSDs, you can also use rsync
to migrate the files from the original system to the new one.