How to shrink vdi virtual disk in VirtualBox to reduce filesize
Problem:
I use VirtualBox and I want to share my Virtual Machine (MV), the export file (.ova) is too big.
When I try to share in Google Drive (or any other cloud service), it takes many time to upload.
Solution:
You shouldn’t share the .ova file, you can share the virtual hard disk (.vdi) and then create a new MV with the same content.
Step by step:
-
Start you guest virtual machine with
root.Reboot to GRUB menu (Press
Escas Ubuntu is booting up).Advanced options for Ubuntu > Recovery Mode (choose highest version number)From the
Recovery MenuselectDrop to root shell prompt(root - Consola de superusuario)dfto get your partitions (to see the partition you want to shrink e.g. sda1, sda4 etc. usually its/dev/sda1)dfIMPORTANT! In next step, REVIEW and set the partition you want to shrink.
- In Terminal, execute:
systemctl stop systemd-journald.socket && systemctl stop systemd-journald.service && systemctl stop systemd-journald-dev-log.socket systemd-journald-audit.socket && sudo swapoff -a && mount -n -o remount,ro -t ext2 /dev/sda1 / && zerofree /dev/sda1 - Shutdown your guest in
VirtualBox.haltafter that clic in Machine -> Power off
- Finally, compact the
.vdito get space. In your host (in Linux) open aTerminaland type:VBoxManage modifymedium --compact '/path/to/thedisk.vdi'IMPORTANT! remember specify the absolute path to
.vdifile.If your host is Windows OS:
VBoxManage.exe modifymedium disk "C:\path\to\disk.vdi" --compactIf your host is MacOS (example, Ubuntu OS):
VBoxManage modifyhd /path/to/thedisk.vdi --compact
Source:
https://askubuntu.com/questions/1092812/zerofree-on-ubuntu-18-04
http://manpages.ubuntu.com/manpages/focal/en/man1/systemctl.1.html
https://www.howtogeek.com/312883/how-to-shrink-a-virtualbox-virtual-machine-and-free-up-disk-space/
https://wiki.ubuntu.com/RecoveryMode
https://gist.github.com/bitstarr/01e4946d9ab4ca368953300415cb934a
https://blog.lazy-evaluation.net/posts/reclaiming-vm-space-virtualbox.html
https://bbs.archlinux.org/viewtopic.php?id=281224
========
(bonus!) brief explanation:
First, stop all processes writing to the disk so you don’t get mount /: mount busy error
systemctl stop systemd-journald.socket
systemctl stop systemd-journald.service
systemctl stop systemd-journald-dev-log.socket
systemctl stop systemd-journald-audit.socket
Check if any swap partitions are enabled:
swapon -s
If enabled, then disable them:
sudo swapoff -a
Then finally you should be able to mount dev/sda1 as read-only. (Your filesystem type might be different from ext2. To find the filesystem, run df -T)
mount -n -o remount,ro -t ext2 /dev/sda1 /
Then finally run zerofree
zerofree -v /dev/sda1
Whens zerofree is done, shutdown Ubuntu
halt