Friday, May 3, 2024
35
rated 0 times [  35] [ 0]  / answers: 1 / hits: 62696  / 2 Years ago, mon, july 4, 2022, 2:51:14

My dynamically growing virtual disks (VDI) from various Ubuntu installations in Virtual Box are actually steadily "growing" indeed.



Deleting of files in the virtual machine does not free the space my VDI file took from the host hard disk space.



How do I shrink these files to give the unused hard disk space back to the host?


More From » partitioning

 Answers
2

A dynamically growing Virtual Box virtual hard drive file is capable of growing on demand of the virtual machine up to the maximum size we defined in setting up this file. It does not however free the space of files we deleted in the guest OS.



In case the disk had grown too much we can compact it again, provided the unused space is filled with zeros, and the drive is in VDI format.



For an ext2 to ext 4 filesystem this can be done from the Ubuntu guest with the command line utility zerofree Install zerofree * .



zerofree /dev/sdxX


This needs the drive to not be in use, and to be unmounted. We therefore may have to bind it temporarily to another VM we had created for this purpose, or we need to boot an Ubuntu live environment on this VM.



Replace /dev/sdxX with the ext2, ext3 or ext4 formatted partition in question.



On more than one partition we my have to repeat zerofree for each partition. Keep in mind that by filling with zeros the virtual file will temporarily grow up to it's maximum size.



Once all unused drive space is filled with zeros we then shrink the drive with



VBoxManage modifyhd <name>.vdi --compact


This will considerably decrease the file size of our VDI file.



* In a Windows guest we can replace unused space with the utility sdelete.


[#26154] Tuesday, July 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
inglehare

Total Points: 330
Total Questions: 111
Total Answers: 95

Location: Sint Maarten
Member since Tue, Mar 29, 2022
2 Years ago
;