Shrink a VM

Alessandro 123

Well-Known Member
May 22, 2016
653
24
58
40
I'm migrating some VMs from XenServer to PVE.
Now, one of these VM, has to be shrinked, if possible, in example, moving from 100GB to about 40GB.

Is this possible ?
 
since we don't know what is on the disk, you need to do this manually (first inside the VM - shrink everything that needs to be shrunk, then on the host by shrinking the file/volume/... depending on storage and format)
 
For all who might need a way to shrink an existing virtual disk in proxmox, I did the following:

I had a RAW image disk file as part of a Windows SBS 2011 Server that used 1000 GB of space on my storage, eventhough only 150 GB were used (storage was ext4 on a 2 TB HDD). I wantet to move it to a much faster RAID Z of 3x 500 GB SSDs. If you have a qcow2 image file, you might find more convinient ways to shrink that or you can convert it to raw (in newer versions of proxmox that can be done in the gui by moving a disk to another storage and converting it at the same time. When you chose this way, you already have a backup of your image file (as long as you don't set 'delete source' in the move disk dialoge) and can follow this guide . So here are the steps I took:

- get to know the partitions in your Windows VM.
- An evening before the migration, I started a disk defragmentation job on the Windows Server (inside the VM). That will save you time later on
- Next day, I grabbed a live .ISO of gparted and set it as boot device in Proxmox for the VM.
- Shutdown the VM
- MAKE A BACKUP OF YOUR VM!!!!!
- REALLY, MAKE A BACKUP!!!!!
- Boot the VM, using the gparted ISO as bootdevice
-from here on, I'm writing this based on how i remind it. Buttons might be labeled a bit different but I hope you get the idea ;-)
- gparted will boot up, chose your favorite language or hit enter twice when asked for action to set english as default language and start the gui.
- select your keyboard layout when asked to.
- once the desktop is loaded, you'll find a gparted icon. Start gparted.
- once gparted is loaded, select the right disk (upper right corner, identify it by size/partitions as you know it from windows[see first step])
- now click on the colored ribbbon that symbolizes your disk and its partitions. Select resize/move from the menu.
- Now click on the far right side of the ribbon and reduce the size of the partition by dragging the rectangle to the left until you reach the desired size. Take note of the total disk size and the now becoming free size (unallocated space).
- click 'apply'
- make coffee and wait. You can see the progress of the operation by clicking the small triangles at the bottom of the window in order to expand everything. At the bottom you'll find a percentage counter. Don't panic if doesn't move for several minutes or when it seems to be very slow or when you check the disk activity in proxmox console and it seems to be very slow. It will speed up later on. NEVER EVER STOP THAT PROCESS AS IT WILL KILL YOUR DATA!! Wait, be patient!
If you took the defragmentation step earlier on, this will be a bit quicker.
- When the prcess is finished close the window, close gparted and click the 'Exit' Button on the Desktop and chose to shutdown the computer (do not reboot yet!).
- Open the Proxmox Console either by using putty/ssh or the GUI. Navigate to the path where your raw image file is located. Lets assume it is called 'vm-100-disk1.raw'

cd /path/to/your/raw/image/file

- now look at the note you took earlier and remember the amount of space that was freed (that is now unallocated). Lets say you had a 1000 gb disk with 150 GB data on it and used the slider in gparted to remove 800 gb so you got a 200 gb partition with 150 gb of data in it. In the next step, we will shrink the actual raw disk file. The new partition is 200 GB in size but we do not know the exact size it was before. To be sure, when we shrink the raw image file, we will leave a litte space between the end of the partition and the end of the image file so we don't accidently cut away a piece of the partition. The command we will be using expects an amount of space to cut from the end of the raw image file. So just to be sure, we wont cut the whole 800 GB of our disk from the end, but just 790 GB for example. So we will be on the safe side and we can allocate that extra space to the vm later on. Use the following command to cut XXX GB of space from the end of the raw disk image file:

qemu-img resize -f raw vm-100-disk1.raw -XXXG

or for our example:

qemu-img resize -f raw vm-100-disk1.raw -790G

This might take a minute or two and you might see some warnings or not. At the end, you should see something like 'resize finished' or similar.
Now type

ls -lah

and you should see your raw image file has 210 GB now.

Now you can start your VM. In most cases, it will run a chkdsk on the first boot, but apart from that, it should boot normally.
 
You missed a step: Verify that the backup actually works after a restore. ;-)
 
I'm not sure if you have to update the configuration file under /etc/pve/qemu-server/ to reflect the new size.
For me, I had to do this before it was reflected in the hardware section for the VM.
To be safe, I just did it anyways. Maybe it works without it
I did this for a ZFS vm resize.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!