How to shrink an ext4 disk with a GPT table without LVM using the qcow2 disk format in Proxmox

This guide explains how to shrink an ext4 disk with a GPT partition table without using LVM, specifically using the QCOW2 disk format in Proxmox.

BACKUP YOUR DATA FIRST BEFORE ATTEMPTING THIS. YOU HAVE BEEN WARNED.

1. Check Disk Usage in the VM

In the VM, run:

df -h

Note down the actual usage of the disk you want to shrink (e.g., 250GB).

2. Shutdown the VM

Ensure the virtual machine is powered off.

3. Boot with GParted Live ISO

Load the GParted Live ISO and start the VM using this ISO.

4. Resize the Partition with GParted

In the GParted GUI, resize the desired partition. Leave some headroom—for example, if you noted 250GB in step 1, shrink it to 300GB.

5. Shutdown the VM Again

After resizing, shut down the VM once more.

6. Check the Current QCOW2 File Size on the Host

On the Proxmox host, navigate to the image directory:

cd /var/lib/vz/images/
qemu-img info vm-204-disk-1.qcow2

Note the current size of the QCOW2 file.

7. Shrink the QCOW2 File

On the Proxmox host, execute:

qemu-img resize vm-204-disk-1.qcow2 --shrink -1500G

Replace the filename and the size value (-1500G in this example) with your desired values. This command shrinks the disk from 2000G to 500G.

8. Verify the QCOW2 File Size

Confirm the file has been shrunk:

qemu-img info vm-204-disk-1.qcow2

9. Update Proxmox with the New File Size

Run the following to let Proxmox rescan and update the disk size:

qm rescan

10. Attempt to Boot the VM

When you try to boot the VM, it may fail with an error similar to:

ALERT! /dev/disk/by-uuid/xxxxxxxxx does not exist. Dropping to a shell

This error indicates that the GPT table is broken.

11. Fix the GPT Table

Boot into the GParted Live CD again and run:

sudo sgdisk -e /dev/sda

Although it may throw some warnings and complain, it should (fingers crossed) automatically fix the GPT table.

Note to self: It might be possible to execute step 11 directly after step 4 to avoid booting into the live CD twice. I’ll see if this can be done in future attempts.

12. Resize the Partition to Include Headroom

Once the GPT table is fixed, the VM should boot without issues. However, the partition will not be fully resized because of the headroom. To incorporate the headroom, run:

sudo growpart /dev/sda 2
sudo resize2fs /dev/sda2

Done !

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top