How to Shrink Dynamic VDi file Foot Print?

This is for discussing general topics about how to use VirtualBox.
Post Reply
NaXal
Posts: 7
Joined: 30. Jan 2021, 20:27

How to Shrink Dynamic VDi file Foot Print?

Post by NaXal »

Hello,

I am using VirtualBox with following setup,

Host OS :- Windows 10 (Up to date)
Guest OS :- Ubuntu Server 20

I have two virtual Disc Attached with Ubuntu Server - 30GB / sda (Boot - with Default Ubuntu Install Partition), and 500GB / sdb (Data Storage with only one single partition) and I am running NextCloud on this VM for my home (family) photos and videos backup.

The 500 GB Dynamic Disc is growing as per the requirements but how do I shrink it when I am freeing up data from NextCloud, the Dymamic Disc isn't shrinking automatically. For manual attempt, I have tried to run following command,

Code: Select all

VBoxManage.exe modifymedium disk "my vdi path" --compact
However even after successful job completion, the actual vdi foot print on host hard drive remains the same.

Please suggest what do I need to do to reduce the foot print since its unnecessarily blocking a lot of HDD space on host drive when actual disc usage in side guest is far lower after data deletion.

Thanks.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to Shrink Dynamic VDi file Foot Print?

Post by mpack »

NaXal wrote:the Dymamic Disc isn't shrinking automatically
Not a surprise. The feature is called "dynamic allocation", not "dynamic sizing"! What gave you the idea that dynamic shrinking was a thing? File and deletion thereof is not a hardware feature, so there's no way VirtualBox can know how much of your disk you consider to be still in use. All VirtualBox knows is what sectors have been written to.

Actually, there is allegedly a way to do it that I've never tried. First tell VirtualBox that the drive is non-rotational (i.e. it's an SSD), then enable "trim" for that drive in the guest OS. Now the guest OS can tell the hardware when it's ok to deallocate parts of the drive image.

A short term one-off solution is to compact the drive: for that the easiest solution is CloneVDI. If the drive growth was caused by some kind of mistake this will correct it. Note however that if the drive size is not an error and instead reflects your natural usage pattern then compacting the drive is kinda pointless, as it will very quickly puff back up to the larger size.

The best solution is much easier: don't configure a virtual disk capacity that you can't afford.

You can constrain the drive usage after the fact by reducing the main partition size inside the guest (using the guests own partition management feature), then run CloneVDI to compact the drive - which will eliminate data already stored in now-unpartitioned areas. The guest OS won't write data to unpartitioned parts of the drive.
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: How to Shrink Dynamic VDi file Foot Print?

Post by fth0 »

mpack wrote:Actually, there is allegedly a way to do it that I've never tried. First tell VirtualBox that the drive is non-rotational (i.e. it's an SSD), then enable "trim" for that drive in the guest OS. Now the guest OS can tell the hardware when it's ok to deallocate parts of the drive image.
I'm using this method regularly, but there are some tricky details involved, and I never got to writing concise instructions. In short: (1) The drive must be set to non-rotational and to discard (the latter is not possible from inside the VirtualBox Manager, use VBoxManage for that). (2) The SATA controller must be configured to use the Host I/O Cache, otherwise it can get locked indefinitely when trimming is done. (3) Use sudo fstrim -av inside the Linux guest OS to trigger the trimming when the guest OS is idle.
NaXal
Posts: 7
Joined: 30. Jan 2021, 20:27

Re: How to Shrink Dynamic VDi file Foot Print?

Post by NaXal »

Hello,

Thank you for your replies.
mpack wrote:What gave you the idea that dynamic shrinking was a thing?
Pardon me for being a noob, but usually Dynamic in general means both way rather being only one way expanding in this case. I am not arguing about the word or how or what it means, rather I am searching for a solution regarding my issue.
The best solution is much easier: don't configure a virtual disk capacity that you can't afford.
Its not about affording, its about wasting.

If I may point to my use case and if you may take a look, My Ubuntu disk usage is under 10GB where as in host OS, the vdi file is consuming over 40GB
Disc Usage.JPG
Disc Usage.JPG (65.03 KiB) Viewed 3580 times
Disc Usage 01.JPG
Disc Usage 01.JPG (30.93 KiB) Viewed 3580 times
I am trying to find a way to shirk the disk foot print down to the actual size.
fth0 wrote:I'm using this method regularly, but there are some tricky details involved, and I never got to writing concise instructions.
It would be helpful for me if some one helps me out by pointing to some article or tutorial as how I may achieve this?

Thanks.
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to Shrink Dynamic VDi file Foot Print?

Post by mpack »

NaXal wrote:but usually Dynamic in general means both way rather being only one way expanding in this case.
"Dynamic" simply means to respond when needed (not before or much after). It doesn't imply any particular function, but the phrase used by the manual is "dynamic allocation", which I think makes the function pretty clear.
NaXal wrote:I am searching for a solution regarding my issue.
I mentioned several possible approaches to your problem in my previous reply, although some had to be wrapped in caveats since you hadn't made your intentions clear.
Post Reply