10

I'm using Ubuntu 18.04 for development on VM VirtualBox on windows 10 pro host I've compacted the virtual hard drive previously by following the steps from here: https://www.howtogeek.com/312883/how-to-shrink-a-virtualbox-virtual-machine-and-free-up-disk-space/

apart from the usual updates - I have not messed around with it, (recently just had installed the update for virtualbox gues additions)

previously, I only had one /dev/sda1 showing up - today I had three and when I attempt to run zerofree on any of those, I get that the device is mounted in rw

enter image description here

I have followed some instructions here as well to stop some services and try to remount- but it didnt work

https://unix.stackexchange.com/questions/42015/mount-is-busy-when-trying-to-mount-as-read-only-so-that-i-can-run-zerofree

it still says mount point is busy

I'm unsure if this happened because of a recent update ? I also tested by uninstalling the vmware guest additions - but it didn't work

Suggestions ?

  • I don't understand what you're asking, but I see only one /dev/sda1 and do not see three. There is a /dev/sda1 which is your / (or system files), a /dev/sda2 for you boot (or /boot/ files, kernel & with more limited encryption [if used]) and /dev/sda3 for user files /home/ - but this to me is a pretty standard setup which can be chosen at install. Either way I do not see three /dev/sda1 mentions. – guiverc Nov 14 '18 at 12:21
4

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
  • 1
    That's what I have done, from GRUB choose root, and on /dev/sda1 when I try to run zerofree on /dev/sda1 - it says it's mounted in rw – Danish Nov 15 '18 at 10:33
  • is there a way, from GRUB as root - to remount the /dev/sda1 as readonly ? - have already tried - mount -n -o remount,ro -t ext2 /dev/sda1 / - it comes back with / mount is busy I have actually done this 3-4 times before and it used to work - this time it just says that /dev/sda1 is mounted in rw mode - not sure what changed, the updates ? – Danish Nov 15 '18 at 12:39
  • 1
    If it were me, I would boot from an ISO like the Ubuntu installation disk, and choose 'Try Ubuntu'. Then, since I am running from a disk other than my normal disk, I could install zerofree and run that on the partitions of the disk. – Charles Green Nov 15 '18 at 13:45
  • Question is, any idea how do I run a boot ISO externally ? my ubuntu is a virtual box :;/ – Danish Nov 16 '18 at 10:44
  • 1
    There is an option in VirtualBox, to mount an ISO as a CD, and then boot from that CD. Some of these links are older, but the procedure will be very similar. https://forums.virtualbox.org/viewtopic.php?t=55617 or https://www.dedoimedo.com/computers/virtualbox-cdrom.html or https://www.howtogeek.com/187721/how-to-boot-from-a-usb-drive-in-virtualbox/ – Charles Green Nov 16 '18 at 15:30
10

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 stil 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

mount -n -o remount,ro -t ext2 /dev/sda1 /

Then finally run zerofree

zerofree /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
  • Excellent suggestions : systemctl stop + swapoff. They were they keys to my locks – Marvin Aug 1 '19 at 10:38
  • I kept getting mount: / is busy after the mount command, and stopping the other services mentioned in this other question resolved it for me (unix.stackexchange.com/questions/42015/…) (service rsyslog stop && killall dhclient) – chiliNUT Jan 12 at 8:24
3

I'd add a comment to Charles Green answer but I don't have enough reputation.

When running zerofree command, you may get an error saying that the /dev/sda1 partition is mounted as read-write. In this case you have to remount such partiton as read-only:

mount -n -o remount,ro -t ext2 /dev/sda1 /

and re-run the zerofree command, as described here

3

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

  • I had 'haveged' installed and running due to an entropy issue we experienced once, had to stop that service as well. Adding the additional line systemctl stop haveged.service in addition to these four systemctl stops did it for me. – Mahn Dec 14 '19 at 17:46
2

I used Emmanuel's solution and it worked. But for me the service and socket go back too quickly as he mentioned, so after failing a few times I decided to use a chained command to do all the steps in 1 line:

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

And it worked finally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.