Shrinking a VM disk image: loss and recovery
A small experiment. I have a 2TB physical disk (SSD) with about 80GB of real data on it. I want to shrink that 2TB to a virtual disk that’s 150GB; 80GB of data and 70GB of free space. The disc has two GPT partitions, almost all of the space allocated to the second partition. That partition is formatted with BTRFS.
I managed to do this but it was kinda scary all the way, particularly the part where I broke everything and lost all my disk partitions. I did recover it in the end but I do not recommend the experience. I was playing around on a test VM so it wasn’t a catastrophe, but if this were a precious system I would have been very unhappy.
To cut to the chase: resizing the filesystem and the partition were easy. But shrinking the virtual disk with qemu-img caused big problems. The underlying problem is that GPT has two copies of the paritition table: a primary at the start of the disk and a secondary at the end. Changing the disk size screws up the secondary and Linux gets angry and won’t recognize the partitions. You can repair the GPT partition table manually with gdisk but it’s scary. This may not be an issue with old fashioned MBR partitions.
Battle plan
The simple way to do start is gparted’s GUI; it will resize a btrfs filesystem and partition for you. But I’m stubborn and using command line tools. The plan is roughly:
- Clone the physical disc to a QCOW2 image
- Clone the QCOW2 image with
qm import diskto whatever raw format Proxmox wants. - Build a VM around that Proxmox clone.
- Boot the VM
- Use
btrfs filesystem resizeto resize the filesystem inside the live VM. - Use
partedon the disk to shrink the partition for the filesystem we resized. - Reboot and test it’s basically working but the partition is now really smaller
- Shut down and use
qm resizeon Proxmox to make the disc smaller. This is the part that broke. - Repair the damage with gdisk
- Reboot and you’re done!
The first few steps are documented in other blog posts and are about moving a physical disc to a VM. The rest of this post is about resizing a Proxmox VM disk in QEMU raw format.
btrfs filesystem resize
The goal here is to make btrfs think the partition is smaller than it used to be, moving all the data to the front of it so you can then lop off the rest of the partition. It’s pretty straightforward and can (must) be done on a mounted disk.
# time btrfs filesystem resize 150G /
Resize device id 1 (/dev/vda2) from 1.82TiB to 150.00GiB
real 0m21.361s
# btrfs filesystem show /
Total devices 1 FS bytes used 72.25GiB
devid 1 size 150.00GiB used 95.02GiB path /dev/vda2
# df -h /
/dev/vda2 150G 74G 75G 50% /
The filesystem resize was quick and easy! As far as everything is concerned the filesystem is now 150GB big, not 2TB. However the partition is still 2TB.
Parted
Now I want to repartition the disk so nothing is using space beyond 150GB. I’m using parted to edit the partition table on the live disk, the one that is running the OS I’m running parted from. That’s not a good idea, it’d be safer to boot from rescue media. But I’m feeling spicy and this is a test VM.
It’s very important to separate “GB” (1,000,000,000 bytes) from “GiB” (1,073,741,824 bytes). btrfs filesystem is explicit about talking in GiB. parted understands both but seems to prefer the base 10 smaller numbers. You can change that with unit GiB in parted.
unit GiB print
Number Start End Size File system Name Flags
1 0.00GiB 1.05GiB 1.05GiB fat32 boot, esp
2 1.05GiB 1863GiB 1862GiB btrfs
(parted) resizepart 2 155GiB
Warning: Partition /dev/vda2 is being used. Are you sure you want to continue?
Yes/No? y
Warning: Shrinking a partition can cause data loss, are you sure you want to continue?
Yes/No? y
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 1863GiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 0.00GiB 1.05GiB 1.05GiB fat32 boot, esp
2 1.05GiB 155GiB 154GiB btrfs
Note I chickened out and made the new partition 154 GiB, a few GiB bigger than the filesystem. I don’t trust my math. The correct number for the end of the partition would be 1.05GiB + 150GiB. (But oh god does parted round down by a few bytes?)
I rebooted and verified everything worked. I now have a 2TB disk with only 155GiB of partitions on it.
Resizing the QEMU virtual disc
This is the part where I broke things. I wanted to make the actual VM disk smaller, to shrink it from 2TB to 150GiB (or a bit bigger)
Proxmox explicitly does not allow shrinking a desk with the GUI or qm command line tool. The docs just say “that’s dangerous and we won’t help you”.
However it’s still possible to do it with command line tools. I did this with qemu-img on a raw image. That linked page talks about LVM as well. Note that both qm and qemu-img seem to work in binary sizes. Ie: 1G is 1GiB, 1,073,741,824 bytes. I sure hope this is consistent everywhere since it’s not explicit.
First, I shut down the VM. I did some experiments on a scratch monkey while the VM was still up and bad things happened. The file size of the .raw changed on the Proxmox host, but even after rebooting the size looked like the old size inside the VM. It must only work to resize a disk if the VM is offline.
# ls -l disk.raw
-rw-r----- 1 root root 2000398934016 Dec 30 18:49 disk.raw
# qemu-img resize --shrink -f raw disk.raw 156G
Image resized.
# ls -l disk.raw
-rw-r----- 1 root root 167503724544 Dec 30 19:18 disk.raw
# qm rescan
# rescan volumes...
VM 100 (virtio0): size of disk 'local-btrfs:100/vm-100-disk-1.raw' updated from 1953514584K to 156G
Looks like it worked! Note the qm rescan is necessary.
But then disaster. Reboot failed. Getting to a rescue disk , blkid didn’t show any of my partitions. fdisk and parted on /dev/vda showed scary errors like “GPT PMBR size mismatch (3907029167 != 327155711) will be corrected by write.”
I was able to correct things somewhat by using qemu-img resize again to grow the disk back to 2TB, its original size (to the exact byte). Even then nothing was working. But now parted and fdisk reported “The backup GPT table is corrupt, but the primary appears OK so that will be used.” I used gdisk (a new-to-me program) to interactively repair things. It used the primary GPT table for the info and rewrote everything. And it worked! But this was all super scary. And I’m back to a 2TB disk.
I have a theory for what went wrong here. Apparently GPT writes two copies of the partition table; a primary at the front of the disk and a secondary at the back. When I shrunk the disk the second copy was removed, then when I made the disk bigger again it was “back” but now zeros instead of data. And so there was a minor complaint with the secondary and gdisk could work around it. Not a big deal to recover from but spooky.
But I really wanted the disk smaller. So I resized it again, booted to rescue, and used gdisk. And ran the v command. Look at all these errors!

Not only is it unhappy about the secondary partition table being wrong but also it is aware that the disk used to be bigger and stuff is inconsistent.
I used the suggested e command (or was it r?) as suggested. That basically says “trust the primary” and generate a new partition table. Then w to write the change. That all worked fine; both gdisk and parted no longer reported errors. Is it done?
Reader, the system rebooted with no problems. Ubuntu was saved!
Ideas for doing this more safely.
gdisk has options to back up GPT partition data to a file and restore it. I’d definitely use that before monkeying around with any of this.
I would use the gparted GUI tool for most of the work. It handles both the btrfs resize and the repartitioning. It’s not going to anticipate the GPT partition weirdness of a qemu-img shrunk disk, but maybe it has nice tools for recovering from the mess too.