This has been talked about before:
- Shrink Virtual Hard Disk Size
- The workstation vmdk gets bigger and bigger
- scripts to shrink vbox and kvm images
My proposal is to automate this
Overview
VirtualBox VDI disks can grow to take up a lot of space because free space is not automatically recovered on the host OS when the guest OS deletes files on its VDI disk. The free space can be recovered by the following steps:
- install the
zerofreepackage from Debian on the guest (zerofreeoverwrites free space with zeroes, needed for VirtualBox to reclaim free space) - delete/merge all VirtualBox snapshots (recommended) to have only one VDI disk per guest VM (booting from a snapshot will only recover space in the snapshotâs disk)
- delete unnecessary user-saved files on the guest
- purge unused packages, remove old kernels to free up space, delete cache files, etc
- make sure the disk is mounted read-only (needed for
zerofreeto run)- the easiest way is to reboot into the LIVE mode. If you have SYSMAINT, boot into LIVE SYSMAINT (required to use
sudo) - or edit the boot kernel command line and append
init=/bin/bashto thelinuxline
- the easiest way is to reboot into the LIVE mode. If you have SYSMAINT, boot into LIVE SYSMAINT (required to use
- run
sudo zerofree -v /dev/sdaXon theext4disk (/dev/sda3for Whonix 17.4.4.6) - shut down the guest
- if booted using
init=/bin/bash, first runexec initto finish booting
- if booted using
- on the host OS, run
VBoxManage modifymedium "path/to/guest/disk.vdi" ââcompact(--compactcauses all contiguous zero-filled space on guest VDIs to be reclaimed by the host) - check how much space has been recovered on the host disk
Automation
Whonix/Kicksecure could preinstall the zerofree package, and add an entry to the boot menu that performs steps 5-7. It could also add an entry to the GUI System Maintenance Panel that performs steps 4-7.
It may be possible to safely (by suspending all background/update processes?) remount the disk as read-only without rebooting with
echo s | sudo tee /proc/sysrq-trigger
sleep 2
echo u | sudo tee /proc/sysrq-trigger
(sync and remount all disks as read-only)
then
sudo zerofree -v /dev/sda3
(or sudo zerofree -v $(mount | grep "/dev/sda. on / type ext4" | awk '{print $1}'))
sudo mount -o remount,rw / &&
sudo mount -o remount,rw /boot/efi &&
shutdown -h now
Explanatory note presented to users:
- manually perform steps 1-3 (or 1-4)
- after guest VM shutdown, run
VBoxManage modifymedium "path/to/guest/disk.vdi" ââcompacton the host
more info on zerofree from the VirtualBox manual
Testing
Disk space usage can be tested by saving a file with random data on the guest and deleting it
dd if=/dev/urandom bs=1048576 count=500 of=$HOME/500MB-random-data.test
rm $HOME/500MB-random-data.test
After shutting down the guest, the VDI disk will have grown by 500 MB on the host. Check in VirtualBox disk manager and click Refresh. Sizes in VirtualBox are only updated when the VM is shut down. Running both commands multiple times will increase the size a bit more (for example, I ran both commands 8 times and got around 1.5 GB increase in disk usage)
Maintaining low disk usage
It could also be encouraged that to keep disk usage as low as possible, users should:
- set up the guest VM and install all user-desired software first
- upgrade everything, and follow steps 1-9 above
- create a (disposable) snapshot for daily use, use the VM from the snapshot
When you need to upgrade
- restore the snapshot (without creating a snapshot of the current machine state)
- delete the snapshot
- upgrade inside Whonix and perform steps 1-9 above post-upgrade
- create a new disposable snapshot for daily use