[SOLVED] Reduce the size of QCOW2 images with Windows OS

virmix

Member
Sep 4, 2020
36
1
13
I try to do this : https://pve.proxmox.com/wiki/Shrink_Qcow2_Disk_Files and I see with these steps from Trims/Discards the spaces in server down, but the qcow2 still hace the same size.

The solution is clone disk, but how?

0. Backup VM
I suggest backup , you never know! But only include in backup the disk qcow2 you want to reduce space or go to shell for a copy

Exmaple of VM cp /var/lib/vz/images/100/vm-100-disk-1.qcow2 /var/lib/vz/images/100/bkvm-100-disk-1.qcow2


1. Proxmox Preparation

Enable discard option in disk and restart VM
1639840755150.png

2. Windows Preparation

Download SDdelete tool : https://download.sysinternals.com/files/SDelete.zip
Unzip into C:\Windows\System32

Run CMD as administrator

Enable Trim for Discard feature this :
Enter fsutil behavior set DisableDeleteNotify 0

Clean free space for Discard
Enter sdelete -z c:

1639841104277.png



Go to Windows Partition Disk or any Tool for Resize Partiton and move all partition to the left and resize. For example I have a qwow2 with 700Gb and I resize third partition (C:\) from 698GB to 157 GB

1639841561108.png

Tool Portable : https://mega.nz/file/od4wlJSS#Ol4en_Wt-21TcGlcaAtILsvKmQs-d10w0qqoK_KOTns

1639841244305.png

I recomend clean the partition unallocated

1639841952766.png
1639841988817.png

This is the same of sclean (filling with zero)

Now you can see the store and disk down


1639842179265.png

But qcow2 have the same size

1639842118454.png


3. QCOW2 Preparation

Now if go to shell and run this

cd /var/lib/vz/images/100 cp vm-100-disk-1.qcow2 vm-100-disk-1.bk qemu-img convert -O -p qcow2 vm-100-disk-1.bk NEWvm-100-disk-1.qcow2 rm vm-100-disk-1.bk

In teory now the new qcow2 (NEWvm-100-disk-1.qcow2) reduce the size but not for me.
If it is your case run this


qm stop 100 rm vm-100-disk-1.qcow2 mv NEWvm-100-disk-1.qcow2 vm-100-disk-1.qcow2 qm start 100

If not go to fourth step

4. Clone Preparation


Download and Upload ISOS to Proxmox
Drivers : https://fedorapeople.org/groups/vir...ve-virtio/virtio-win-0.1.208-1/virtio-win.iso
Clone APP : https://mega.nz/file/QYxyBLSC#LhpOM_kX_-EnTUyZ484M0BuMQTDbi5a3kVagyH4E0Go

Create new VM with low requirement :
2GB RAM
2 or 4 Core
Disk 160GB in my case (I want to reduce from 700GB to 160GB)
Add 2 CDROM (Drivers ISO,Clone APP ISO)

When was created change option boots
1639843099267.png


1639843221193.png

Copy backup to new VM 101
cp /var/lib/vz/images/100/NEWvm-100-disk-1.qcow2 /var/lib/vz/images/101/bkvm-101-disk-1.qcow2 qm rescan

Go to hardward and now see new disk. Click on EDIT to add this unused disk like this
1639840755150.png


Run NEW VM from CD ROM and see second post. In this post I upload the max screenshot per post
 
Last edited:
When open the APP go to menu for install drivers


1639844380025.png


1639844387412.png

Choose the version of drivers of qcow2 Windows OS


1639844398400.png



When open both files then click on Disk Clone or Particion Clone for rescan disk with new drivers

1639844459209.png


Select the disk

1639844525355.png

Select the target. My HD of 700GB I tranfer to 160Gb, because I want to delete 541Gb of unllocated disk

1639844543968.png

Not click for enabled sector clone and go to next and procedd.

When finish stop NEW VM
The move new disk from new VM to old VM

qm stop 100 mv /var/lib/vz/images/100/vm-100-disk-1.qcow2 /var/lib/vz/images/100/vm-100-disk-1.old mv /var/lib/vz/images/101/bkvm-101-disk-1.qcow2 /var/lib/vz/images/100/vm-100-disk-1.qcow2 qm start 100

From 700Gb to 160GB
1639844978168.png


5. UEFI DISK

If you have a UEFI disk it is possible show an error (0xc000000e) when try to start. You need to reset uefi setting.

Change CDROM (Clone ISO) to Windows Setup (Windows ISO).
Go to the step of select disk to load Drivers of Virtio , then end Setup Wizard
The last step go to Home CD Menu and click on run CMD

c:

If not go to c:\ do this:

diskpart list disk sel disk 1 list volum sel volum 1 (1 is my example, select your number partition) assign letter c exit

bootrec /fixmbr bootrec /fixboot bootrec /rebuildbcd exit

6. Rollback


If something go wrong and step 4 is done:

rm /var/lib/vz/images/100/vm-100-disk-1.qcow2 mv /var/lib/vz/images/100/vm-100-disk-1.old /var/lib/vz/images/100/vm-100-disk-1.qcow2

or restore proxmox backup from step 0
 
Last edited:
So I was having an issue regarding backup size reduction, and I know this is an old thread, but this may help someone so I thought I would chime in.

I noticed this week that my backup was taking up nearly 500G when only 30G was being used in the Windows VM. The disk was allocated for about 400G max (probably some difference with GiB etc accounting for the difference with 400 vs 500). At this point I only managed to reduce the backup size, but I plan on trying to reduce the actual disk size next and I will post back if I manage to reduce the VM disk size as well. Anyway my steps to reduce the backup size are below.

Using defraggler's optimize function (which essentially is doing the same as this step in the wiki page and overwrites the empty spaces with zeroes) I was able to reduce the size of the backup from 500G to just 30G with zstd compression.

If anyone else runs into this issue where their backups are larger than the data on the VM disk, I suggest either using SDelete per the wiki steps or running an optimize with defraggler.

I will update this post if I figure out how to reduce the VM disk size as well
 
Last edited:
I will update this post if I figure out how to reduce the VM disk size as well
With QCOW2, you cannot do that online, only offline by converting the file once. It is read, empty blocks omitted and only used blocks are written.

Best do pair this with a backend storage that supports trim, so that empty spaces are indeed just unmapped. ZFS is capable of doing this and I use this heavily.
 
With QCOW2, you cannot do that online, only offline by converting the file once. It is read, empty blocks omitted and only used blocks are written.

Best do pair this with a backend storage that supports trim, so that empty spaces are indeed just unmapped. ZFS is capable of doing this and I use this heavily.
When you say backend storage do you refer to where the VM disk is stored? I believe mine is currently partitioned as LVM and ext4 for the actual filesystem. I will maybe have to try ZFS if it automatically unmaps empty spaces. For backups I am using the provided 500GB from OVH as part of the dedicated server I have.
 
Last edited:
When you say backend storage do you refer to where the VM disk is stored? I believe mine is currently partitioned as LVM and ext4 for the actual filesystem.
Yes, that is what I meant. With LVM and ext4 you need the offline reducing by copying the image via qemu-img convert and moving it on top of the file. You can also compress the file if you want. This (german) page describes it pretty good.

I will maybe have to try ZFS if it automatically unmaps empty spaces.
Yes, this is one of the many advantages of ZFS over any other method.
 
  • Like
Reactions: Darkk
I agree. ZFS is very robust in storage systems as it offers dedupe, snapshots for backups and data integrity. Even you don't have additional drives to properly create a ZFS array you still can use it on a single drive. Just you won't have redundancy.
 
  • Like
Reactions: successtheman
>With QCOW2, you cannot do that online, only offline by converting the file once. It is read, empty blocks omitted and only used blocks are written.

why does it work with my qcow2 files then @LnxBil ?
 
Yes, that is what I meant. With LVM and ext4 you need the offline reducing by copying the image via qemu-img convert and moving it on top of the file. You can also compress the file if you want. This (german) page describes it pretty good.


Yes, this is one of the many advantages of ZFS over any other method.

Apologies for the late response, however we finally got Proxmox moved to a new installation and as you said ZFS cleared the unused space within the image files automatically. Thanks a bunch even though it took a while, this will save a ton of time that would otherwise be used trimming image files
 
>With QCOW2, you cannot do that online, only offline by converting the file once. It is read, empty blocks omitted and only used blocks are written.

why does it work with my qcow2 files then @LnxBil ?

I'm really not sure why it worked for you, I tried many many times and it would never work with the system online. Perhaps you maybe were already using ZFS or another filesystem that also automatically clears empty space?
 

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!