Shrinking the root disk of a Proxmox VE virtual machine

Today I tried to shrink the root disk of one of my KVM virtual machines running in Proxmox VE, from 200 GB to 100 GB. Below are the process and steps that I followed.

Obviously, this is an extremely dangerous operation – your virtual disk could be damaged and you could permanently lose your data. Please experiment with testing VMs and make backups before operating on important ones. I am not responsible for any damage caused by following the steps below.

Preparation: GParted Live ISO

GParted (GNOME Partition Editor) will be used to edit partitions in the following stages. The Live ISO allows you to boot into a live graphical environment (independent of your original operating system) to use the software.

  1. Find the latest GParted Live ISO link here.
  2. Using the Proxmox VE web interface, navigate to “local” > “ISO Images” > “Download from URL”, and download the ISO file to the host’s storage.

Stage 1: Shrink the file system partitions inside the disk image

The first stage is to shrink the existing file system, so that all the existing partitions and data are moved to the first half of the disk image.

  1. Shut down the virtual machine and wait for it to fully stop.
  2. In the VM’s Hardware tab, add a “CD/DVD Drive” and choose the ISO file downloaded earlier. It should be added as “ide0” (or higher if ide0 is already occupied).
  3. Switch to Options tab, and edit the “Boot Order” option to check the newly added “ide0”, and move it to the top of the list.
  4. Start the VM to boot into GParted Live environment.
  5. Switch to the VM’s Console tab to enter the VNC graphical interface.
  6. You should see GParted Live being booted.
    • When prompted by the boot menu (to choose keyboard mapping and others), use your best judgement.
    • Eventually, you will enter a lightweight Linux desktop environment with GParted automatically launched.
  7. Shrink and/or move the partitions so that the total size is BELOW the desired new size, for example, 80%.
    • The buffer will ensure that no data is accidentally cut off during the disk image resizing step due to unit difference (GB vs. GiB) or any other reasons.
  8. Click the green checkmark button to apply your changes. It will take a while to move data blocks and shrink partitions.
  9. When all operations are completed, close GParted software and shutdown the VM.
Example of partition layout in GParted after shrinking

Stage 2: Shrink the disk image in Proxmox VE host

After you have successfully shrunk the partitions, it’s time to reduce the disk image size to reclaim the unused space at the end of the virtual “hard drive” in PVE host.

  1. Connect to PVE host’s terminal. You could either use SSH, or the web interface’s “Shell” tab at the host level.
  2. Depending on the type of your disk image, execute the appropriate commands.
    • For my ZFS volume setup, I first ran sudo zfs list to check the image’s volume name (which was “rpool/data/vm-<vmid>-disk-0”), and then used sudo zfs set volsize=100G rpool/data/vm-<vmid>-disk-0 to set its size.
    • For other setups such as LVM or plain “.qcow2” files, refer to this thread or other online resources to determine how to shrink them.
  3. Once the disk image’s size is successfully changed, run sudo qm rescan -vmid <vmid> to update the size tracked by Proxmox VE.

Stage 3: Repair the partition table and fill the unused buffer space

In the previous stage, you have cut off the last part of the virtual “hard drive”. If you are using GPT partition table, it would mean that the backup table at the drive’s end will be lost, and the primary table at the beginning will have a wrong size.

Also, we have left a 20% buffer space in Stage 1 within the new disk, which we could fill with one or more of our partitions.

  1. Start the VM and boot into GParted Live environment again.
  2. When GParted is automatically started, you might see two errors: “Invalid argument during seek…” and “The backup GPT table is corrupt…”. Temporarily click “Ignore” and “OK” respectively to skip the errors. Do NOT make any changes in GParted now!
  3. On the desktop of the live environment, open Terminal.
  4. Use gdisk to repair the GPT partition table.
    • First, open sudo gdisk /dev/sdX (replace X with your actual drive letter)
    • You should see some errors about GPT partition table being corrupt. Execute v command to diagnose.
    • Understand and carefully follow the recommended steps to fix the issues. For me, the fix was x (expert mode) and e (relocate backup structure).
    • Execute p to check the partition table, and w to write the changes and exit.
Example process of repairing the GPT partition table
  1. Switch back to GParted, execute “GParted > Refresh devices” to reload the repaired partition table.
  2. Fill the unused space as desired, apply the changes, and shutdown the VM once the changes are complete.
  3. Remove the CD/DVD Drive from VM Hardware tab, and revert the Boot Order settings.

Now you have completed all the steps to shrink both the virtual disk image and the partitions it contains. You can try to boot into the original VM operating system to confirm that everything is working.

References: Shrink disk size of VM : r/Proxmox

Thanks Bing AI’s DALL·E 3 for generating the featured image of this article.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注