Do .vdi dynamic disks shrink ?

Discussions about using Windows guests in VirtualBox.
Locked
rothers
Posts: 4
Joined: 19. Feb 2023, 17:17

Do .vdi dynamic disks shrink ?

Post by rothers »

I have a 100 GB .vdi dynamic disk, Win7 host, WinXP Guest.

Does the actual size on the host shrink as space is freed up on the guest ?

After installing the guest OS and some apps actual size on host was about 20 GB. The actual size has expanded over time in sync with the space used on guest until it is now 99 GB. I've freed up about 50GB on the guest but the actual size on host remains at 99 GB. Done a defrag on the guest but made no difference.

Anyway to make the size of the .vdi file shrink back ?
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: Do .vdi dynamic disks shrink ?

Post by mpack »

rothers wrote: Does the actual size on the host shrink as space is freed up on the guest ?
No. If you think about it, what exactly does it mean to "free up space" on a disk? VirtualBox doesn't concern itself with the guest filesystem, all it cares about is whether data is stored in disk sectors, and deleting a file in the guest OS doesn't mean there isn't data in those sectors any more (*). Bear in mind that VirtualBox emulates the hardware, not the behaviour of the OS. So the only aspect relevant to VirtualBox is when you write to a drive sector for the first time, as that is when host space is allocated.

The only practical way to discard sectors which the guest OS is not using is to compact the drive. VirtualBox has a long winded way of doing this, but a more practical way is with my CloneVDI tool which can work because it does understand certain guest filesystems. You need to clone the drive with the options "Keep UUID" and "Compact".

Note also that after compaction the drive will immediately begin growing again, and performance will go down slightly because writes are slower. Dynamic drives usually reach an equilibrium size quite quickly, so if you are careful about not doing something that will write to a lot of new sectors then it's not true that you should expect linear growth. The equilibrium size of the drive reflects what you are doing in that PC, and if you keep exceeding 90GB then it argues that the drive is too small - or you need to modify your habits (don't accumulate junk, or at least copy it to a NAS or something).

(*) Actually, when VirtualBox is emulating an SSD then trim operations are supported, whereby the guest OS can nominate sectors to be replaced as part of the wear levelling features of an SSD. But the details of how that works depends on the guest OS, and I have not investigated the details.
rothers
Posts: 4
Joined: 19. Feb 2023, 17:17

Re: Do .vdi dynamic disks shrink ?

Post by rothers »

Thanks, CloneVDI with Keep UUID and Compact did the trick
Locked