Unable to compact VDI disk image of a RedHat Linux guest

Discussions related to using VirtualBox on Windows hosts.
Post Reply
1780yz
Posts: 7
Joined: 23. Mar 2021, 01:55

Unable to compact VDI disk image of a RedHat Linux guest

Post by 1780yz »

I have a RedHat Linux guest running on a Windows host. The VDI disk image is set to dynamic allocation, logic size 40GB, actual size 39.8 GB; and the file system of the guest OS is used for 15 GB. So, I tried to compact the VDI disk image following the below link:
https://www.itsfullofstars.de/2020/03/s ... isk-image/

I used the below commands to add and remove the big empty file, and I shut down the VM.

Code: Select all

dd if=/dev/zero of=/var/dummy bs=8126k
rm /var/dummy
Then I used tried the Windows commands as the screenshot below, to shrink the size of the VDI disk image, but none of them worked. The disk size kept the same as it was before.
Screenshot:

Code: Select all

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe modifymedium --compact C:\my\vbox\path\to\vm-disk1.vdi
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Program Files\Oracle\VirtualBox>VBoxManage.exe modifyhd "C:\my\vbox\path\to\vm-disk1.vdi" --compact
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Program Files\Oracle\VirtualBox>
Eventually, I tried to export the VM to an OVA file, then delete the original VM and import again. In this way, the disk image is reduced to 20GB. This is a workaround, not the formal way of zeroing and them compacting disk image.

I'm still wondering why the formal way didn't work in the first place? Any pointers will be highly appreciated.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Unable to compact VDI disk image of a RedHat Linux guest

Post by scottgus1 »

compact does not reduce the final disk size of the disk. It only removes empty sectors to reduce the size of the disk file on the host, if there are empty sectors available and the disk has not been encrypted.
1780yz wrote:The disk size kept the same as it was before.
Please define exactly what you mean by 'disk size'.

Also see https://www.virtualbox.org/manual/ch08. ... difymedium and Mpack's CloneVDI.
fth0
Volunteer
Posts: 5668
Joined: 14. Feb 2019, 03:06
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Linux, Windows 10, ...
Location: Germany

Re: Unable to compact VDI disk image of a RedHat Linux guest

Post by fth0 »

Your initial strategy can only work if the VM has no snapshots. Does it have snapshots?
scottgus1 wrote:It only removes empty sectors
To be more precise: It only removes whole 1 MB blocks (2048 consecutive sectors at an 1 MB boundary) filled with zeroes.
1780yz
Posts: 7
Joined: 23. Mar 2021, 01:55

Re: Unable to compact VDI disk image of a RedHat Linux guest

Post by 1780yz »

By 'disk size' here, I mean "Actual Size" of the disk image.
scottgus1 wrote:compact does not reduce the final disk size of the disk. It only removes empty sectors to reduce the size of the disk file on the host, if there are empty sectors available and the disk has not been encrypted.
1780yz wrote:The disk size kept the same as it was before.
Please define exactly what you mean by 'disk size'.

Also see https://www.virtualbox.org/manual/ch08. ... difymedium and Mpack's CloneVDI.
1780yz
Posts: 7
Joined: 23. Mar 2021, 01:55

Re: Unable to compact VDI disk image of a RedHat Linux guest

Post by 1780yz »

It doesn't have any snapshot.
fth0 wrote:Your initial strategy can only work if the VM has no snapshots. Does it have snapshots?
scottgus1 wrote:It only removes empty sectors
To be more precise: It only removes whole 1 MB blocks (2048 consecutive sectors at an 1 MB boundary) filled with zeroes.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Unable to compact VDI disk image of a RedHat Linux guest

Post by scottgus1 »

1780yz wrote:By 'disk size' here, I mean "Actual Size" of the disk image.
A disk image has two sizes. The file size on the host disk, and the size the disk presents to the VM.

Example: I can make a VM for XP and give it a 10GB size. On the host, the disk file starts off at a couple MB. I then install XP on the VM. On the host, the disk file rises to 1.6GB on the host. But it still presents 10GB in the VM.

What "size" did not change? The file size on the host drive or the size presented to the VM OS?
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Unable to compact VDI disk image of a RedHat Linux guest

Post by mpack »

scottgus1 wrote: A disk image has two sizes.
To prevent confusion I suggest that we stick to "capacity" when referring to the configured disk capacity, and reserve "size" to refer to the size of the file on the host.

@1780yz: compaction does not change the drive capacity, which may be what your "drive size" means. Nor will usage figures reported by the guest OS change. The only thing that will change is the size of the host VDI file, and even that only if zero blocks were present. As Fth0 points out, zeroing blocks from inside the guest will only be effective if snapshots are not used ("snapshots" includes related features such as linked clones). So, if zero filling was not effective then either you were zero filling the wrong VM, or the VM uses snapshots, or the VM was already as compacted as it can get.

I think that attempting to compact a fixed size disk would give you a VERR_NOT_SUPPORTED error, so I'm not listing that one.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Unable to compact VDI disk image of a RedHat Linux guest

Post by scottgus1 »

That sounds fine to me. (Now we have to get the posters to do it. :lol: )
Post Reply