zerofree on ubuntu 18.04

What worked for me (combining answers from several related questions and forums):

  • VirtualBox 5.2.10
  • Windows 10 as Host OS
  • Ubuntu 18.04 as Guest OS

First install zerofree:

sudo apt-get install zerofree

Create 0s to get space to be reclaimed(delete all files you don't need as well as empty the trash)

Reboot to GRUB menu (Press Esc as Ubuntu is booting up)

Advanced options for Ubuntu > Recovery Mode (choose highest version number)

From the Recovery Menu select Drop to root shell prompt

df to get your partitions(to see the partition you want to shrink e.g. sda1, sda 4 etc. usually its /dev/sda1)

df

Try to run zerofree

zerofree /dev/sda1

Partition is read-write, needs to be mounted as read-only for zerofree to run Error:

If you get an error from zerofree like /dev/sda1 is mounted as rw, then you need to mount it as read-only to continue.

To mount the /dev/sda1 or your desired partition as read-only to use zerofree on it:

Note 1: you should perform the following steps relatively quick to be able to mount the drive as ro before services and sockets go back into use and start writing to the disk again. Otherwise you may need to perform the systemctl step again to make sure writing to the disk has stopped.

Note 2: (Skip reading this until the steps following this fail).Depending on your system, you may have other services and sockets still writing to disk other than systemd-journald. If the below steps are not working try to see whats still writing to disk after you run the steps and then stop them too in the same way.

systemctl --type=service
systemctl --type=socket

Steps:

Update: User @Kevin gave an excellent suggestion to execute the steps ( see below) in a one liner command in case the services and sockets go back into use too fast:

systemctl stop systemd-journald.socket && systemctl stop systemd-journald.service && sudo swapoff -a && mount -n -o remount,ro -t ext2 /dev/sda1 / && zerofree /dev/sda1

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

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

Then finally, on the Host OS (in my case Windows 10), compact the vdi to reclaim space:

VBoxManage.exe modifymedium disk "C:\path\to\disk.vdi" --compact

I used Emmanuels' solution and it only worked after stopping two more sockets:

systemctl stop systemd-journald.socket 

new lines:

systemctl stop systemd-journald-dev-log.socket
systemctl stop systemd-journald-audit.socket
systemctl stop systemd-journald.service
sudo swapoff -a 
mount -n -o remount,ro -t ext4 /dev/sda1 / 
zerofree /dev/sda1

Note: I use Ubuntu 18.04.3 and EXT4

Shrunk my disk from 40 GB to 7 GB. Thanks. :)