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

As mentioned in one of the comments, you have three partitions mounted (sda1, sda2 and sda3) and several temporary filesystems used for other purposes.

The manual for zerofree indicates that if you want to run this on a filesystem, then that filesystem must be either mounted readonly, or unmounted. When you are running Ubuntu, it is certain that the root partition /, which is /dev/sda1 on your computer, is mounted thus the command will give a failure.

If you want to run zerofree on /dev/sda1 you will need to either boot from an alternate device, uch as a bootable USB or ISO image, or perform a chroot to an alternate linux image. The link that you reference has specific instructions for you, on how to run this program on your root partition. Please re-read their instructions on how to use this program in Linux:

  • Boot and access Grub before the OS loads
  • Choose 'Advanced Options' from the grub menu
  • Enter a recovery mode session
  • use the 'root' login
  • Identify the disk
  • Run zerofree
  • halt the machine
  • powerdown and restart the VM

2019-01-22

The current version Ubuntu makes running zerofree difficult from the recovery session - in this case, it is easier to run this program from a live USB:

  • Download a current Ubuntu iso file
  • Create a live USB / CD with the Ubuntu iso
  • Boot your device from the live media (this can also be done in VM's)
  • Install the program zerofree, if it is not already installed, with the command sudo apt install zerofree
  • Run the command
  • Reboot the machine

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. :)