Virtualbox
Option 1: Shrink VDI disk using dd
- Start the VM and delete anything you don't need in the VM.
- Then create a file with just zeros.
- Run
syncand delete the file.
dd if=/dev/zero of=junk
sync
rm junk
- Poweroff the VM and navigate to the folder containing the disk on the host and run the following command.
VBoxManage modifymedium disk ./diskname.vdi -compact
Option 2: Shrink VDI disk using zerofree
First step is to install zerofree. On archlinux it can be installed from the
AUR: yay -S zerofree.
- Start the VM and delete anything you don't need in the VM.
- Make sure you are allowed to edit command line parameters on boot.
- When starting the VM, hit
Escrepeatedly (grub) or holdspacefor the systemd boot menu to show up. - Press
eto edit the parameters and append aninit=/bin/bashto the command line arguments. Continue boot. You'll land in the bash shell. - Remount the root filesystem as read-only:
mount -n -o remount,ro -t ext4 /dev/sda1 / - Finally run
zerofree -v /dev/sda1 - When finished, shutdown with
halt
In the last step we run the VBoxManage command to shrink the VDI disk.
VBoxManage modifymedium disk ./diskname.vdi -compact