Decrease a VM Disk Size

Ok, I now succeeded: as a last step, one does indeed have to repair the partition table.

Here's a short writup on how I finally did it to shrink a VM hard drive from 120G (14% used by Ubuntu server 24.04 LTS) to 56G on a ZFS volume:

First of all, this is a risky operation, so backup your VM! This step can't be stressed enough!

Load a GParted live iso (or any other tool with a Debian/Ubuntu based terminal, I guess) into the CD-rom of the VM and change the boot order so that the VM boots from the CD-rom.

Once GParted has loaded, close the GParted window and open the terminal.

First, list the devices with the following command:

Code:
sudo fdisk -l

In my case, I wanted to shrink /dev/sda2

Check the file system:
Code:
df -hT
In my case, sda2 has an ext4 file system.

Then, check whether the partition table is GUID or MBR:
Code:
sudo parted -l
In my case, it is GUID (= GPT), so the steps below should be followed:

1. Shrink the file system

In the GParted terminal type:
Code:
sudo e2fsck -f /dev/sda2 && sudo resize2fs /dev/sda2 40G

2. Shrink the partition

In the GParted terminal, type:
Code:
sudo gdisk /dev/sda

We first request information about the partition we are about to manipulate using the i command:

Code:
Command (? for help): i
Partition number (1-2): 2
Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem)
Partition unique GUID: D8F77E4C-589B-4E33-A803-F06A6C343852
First sector: 1050624 (at 513.0 MiB)
Last sector: 84754431 (at 40.4 GiB)
Partition size: 83703808 sectors (39.9 GiB)
Attribute flags: 0000000000000000
Partition name: 'Linux filesystem'

Since we have to delete the partition as part of the resizing operation, we want to keep its current information at hand. One thing we may want to do is to apply the current partition unique GUID to the newly created one (by default a random one is generated). To delete the partition, we have to use the d command (don't worry, the tasks are only executed at the end, when we type w)

Code:
Command (? for help): d
Partition number (1-2): 2

At this point we recreate the partition with the n command, using the same partition number and starting point (in this case the default one), and provide the new size. Since in this case we want to use shrink the partition, we enter +55G to reduce it to 55 Gibibyte, corresponding to ~59 Gigabyte :

Code:
Command (? for help): n
Partition number (2-128, default 2): 2
First sector (34-15122398, default = 1026048) or {+-}size{KMGTP}: 1026048
Last sector (1026048-15122398, default = 15122398) or {+-}size{KMGTP}: +55G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

To change the unique GUID of the new partition and use the previous one, we should enter the “advanced operations” menu of gdisk, using the x command (notice how the prompt changes)

Code:
Command (? for help): x
Expert command (? for help):

The expert command we want to use is c. Once we enter it, We will be asked to provide the partition number and the new GUID for it. In this case we enter the one that was used for the “old” partition:

Code:
Expert command (? for help): c
Partition number (1-2): 2
Enter the partition's new unique GUID ('R' to randomize): D8F77E4C-589B-4E33-A803-F06A6C343852
New GUID is D8F77E4C-589B-4E33-A803-F06A6C343852

At this point we can save the changes to disk with the w command (if for some reasons we want to leave the “expert” menu and return to the main one, we can use m instead):

Code:
Expert command (? for help): w

3. Enlarge the file system to cover all the available space

We now have a much smaller partition than before, but as there is an even smaller file system on it, it should still be enlarged to cover all the available space on the partition.

In the GParted terminal, type:

Code:
sudo e2fsck -f /dev/sda2 && sudo resize2fs /dev/sda2

Now shutdown GParted.

4. Reduce the size of the VM hard disk on the zfs local-vmpool

Make sure the VM is shut down !

Now, the VM hard disk on the vm-pool (ZFS file system) should still be shrunk to 56 Gibibytes (= ~60 Gigabytes) and the information about the new size updated by the following commands in the PVE shell:

Code:
zfs set volsize=56G local-vmpool/vm-102-disk-1
qm rescan

5. Repair the partition table

Reboot the VM into GParted and in the GParted terminal, type:

Code:
sudo fdisk -l

It will prompt GPT PMBR mismatch... . Run:

Code:
sudo gdisk /dev/sda

1. Enter v to verify that the partition table is repaired;
2. Enter w to write to the partition table.

Verify there is no longer a mismatch by typing:

Code:
sudo fdisk -l

If everything is ok, shutdown GParted, unload the GParted iso from the CD-rom and change the boot order to the VM hard disk. It should now boot up fine, at least it did in my case. If not, well, you'll be up and running again in no time with the backup you made!

Credits go to the following sites:
https://linuxconfig.org/how-to-manipulate-gpt-partition-tables-with-gdisk-and-sgdisk-on-linux
https://dallas.lu/pve-reduce-ubuntu-vm-disk-size/
and of course to this very own thread where people already posted very useful info.
 
Last edited:
  • Like
Reactions: Onslow