Shrink/reduce VirtualBox Linux guest VDI disk - Host: Windows, Guest: Ubuntu/Lubuntu
VirtualBox hard disks (VDI, VMDK or VHD) bloat over time. This is due to the fact that deleted files on a VBox hard disk (guest system) are not actually deleted, but only given the status “free”. This means that files “deleted” at OS level still occupy the memory of a VirtualBox hard disk.
However, there is a solution for this. VirtualBox provides a program with which the “free” status blocks can be removed again. However, these must first be rewritten to zero.
In this article, I will show you how to do this step by step so that you can reduce the size of your VDI hard disk.
1.0 The system cleanup - Step 1/3
1.1 Guest-System (Linux) clean up
To reduce the size of the VDI hard disk as much as possible, all unnecessary files/programs on the guest system should be deleted beforehand. Accordingly, delete all unnecessary files and programs on your guest system now.
Additionally, the following actions can be performed to clean the system.
1.2 Remove unnecessary kernels
This command removes unnecessary kernels:
$ sudo apt-get autoremove --purge
1.3 Cache leeren
Delete old installation files from the package cache (/var/cache/apt/archives):
$ apt-get clean
You can find more options for cleaning a Linux system here: Clean up Linux
2.0 Prepare hard drive with Zerofree - Step 2/3
2.1 Install Linux Tool Zerofree
Zerofree will be needed later in the boot menu/recovery mode. The program must therefore be installed *beforehand* under Linux (guest system) using the following command. Now install Zerofree on the Linux system (guest system) using the following command:
$ sudo apt-get install zerofree
2.2 Start Linux (guest system) in boot menu / recovery mode
Zerofree writes unused blocks (status "free") with zeros so that these blocks can be removed later. For this to work, Linux must be executed via the boot menu and the hard disk must be set to read only (ro) beforehand.
To do this, reboot the VBox (guest system) and open the boot menu. To do this, press the ESC key during the boot process for Ubuntu.
Now use the arrow keys to navigate to "Advanced options for Ubuntu" and press Enter. Select the latest kernel with the addition "(Recovery mode)" and press Enter again. In Lubuntu, the "Recovery mode" item may not be visible due to the lower resolution. Then select the second item. This should be "Recovery mode".
The recovery Menu opens. Depending on the Linux version, this should look like this (see image below). Now use the arrow keys to navigate to the sub-item "root": "Drop to root shell prompt" and press Enter:
Then press Enter again to enter the root command line (shell).
You are now in the recover mode shell. Now the hard drive must be set to read only.
2.3 Set hard disk to read only (ro)
The following command will now set the hard drive to read only (ro). This is necessary so that Zerofree can run successfully. To do this, run this command:
$ systemctl stop systemd-journald.socket && systemctl stop systemd-journald.service && sudo swapoff -a && mount -n -o remount,ro -t ext2 /dev/sda1
The following message should now appear:
The hard disk is now set to read only (ro) within a session. Experience shows that this status is automatically reset after about two minutes. Therefore, Zerofree must now be executed promptly.
2.4 Run Zerofree
Now run Zerofree with the following command:
$ zerofree -v /dev/sda1
If the message “zerofree: filesystem /dev/sda1 is mounted rw” appears at this point, then the status has been set back to read/write. In this case, simply execute the previous command again (point 2.3) and restart Zerofee promptly (point 2.4).
The zeros are now rewritten. This takes about 5 minutes for a hard drive size of 80-100gb (SSD). The result should look like this:
Now shut down the guest system with this command.
$ shutdown now
3.0 Perform VDI downsizing - Step 3/3
3.1 Effectively shrink your VDI hard drive with VBoxManage
In this section, the VBox hard drive (VDI file) is visibly reduced in size. This is carried out on the Windows host. The Linux guest system must be switched off during this time.
Open Windows PowerShell on the Windows host and navigate to the VDI file. Tip: Open the directory with Explorer, press Shift + right mouse button and go to the menu item "Open PowerShell window here". This will open the PowerShell in the selected directory.
Now run this command (with the & sign) in PowerShell. This will start VBoxManage.exe reducing/shrinking the VDI hard drive:
& 'C:\Program Files\Oracle\VirtualBox\VBoxManage.exe' modifymedium disk '.\Lubuntu 18.04 mini WB-v2-MNTN1-disk1.VDI' --compact
This process takes about 5 minutes for an 80-100gb VDI file/hard drive. The result then looks like this:
That's it, the VDI file has now been successfully shrunk or reduced.
Previously: 78,15 GB
Afterward: 74,73 GB
4.0 Possible error messages
4.1 Failed to lock media when compacting ...VDI
Message: "VBoxManage.exe: error: Failed to lock media when compacting...VDI" and "error: Failed to compact medium!"
Solution: In this case, the VBox virtual machine is still active and needs to be shut down.