38

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 ?

1
  • 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, 2018 at 12:21

10 Answers 10

42

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-journal* \
&& sudo swapoff -a && mount -n -o remount,ro -t ext2 /dev/sda1 / \
&& zerofree -v /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
8
  • Excellent suggestions : systemctl stop + swapoff. They were they keys to my locks
    – Marvin
    Aug 1, 2019 at 10:38
  • 1
    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, 2020 at 8:24
  • Hi Emmanuel, the last step of your solution "modifymedium", when applied to my case never runs and stays at 0% forever. I don't know why! What is the reason?
    – Rebel
    Jul 7, 2020 at 6:20
  • @Ash Could be a bug.. What version of VirtualBox are you using? Try using a different (newer) version of VirtualBox (earlier versions of VB use modifyhd or modifyvdi for example instead of modifymedium).
    – Emmanuel
    Jul 9, 2020 at 4:02
  • 3
    I executed the following on Ubuntu 20.04 LTS: systemctl stop systemd-journal* && swapon -s && sudo swapoff -a && mount -n -o remount,ro -t ext4 /dev/sda<your number> / ... I used only one stop command and it works: systemctl stop systemd-journal* ...
    – ofarouk
    Feb 21, 2022 at 8:38
24

install zerofree

sudo apt install zerofree

reboot into ubuntu recovery console, hold down [RIGHT SHIFT]

advanced options -> recovery mode -> root console

find the root directory

mount | grep "sda"

run zerofree

echo "u" > /proc/sysrq-trigger
mount /dev/mapper / -o remount,ro
zerofree -v /dev/sda1

reboot

shutdown -r now

compress the drive

VBoxManage modifyhd /path/to/VDI/VM.vdi --compact

source: cyplex from ubuntuforums.org

3
  • 2
    the echo "u" > /proc/sysrq-trigger seemed to made the difference in my case. every other tip I read with stopping various services, killing dhclients, disabling swap, was not sufficient Sep 6, 2021 at 10:26
  • 2
    this one actually works with 20.04
    – dawnslayer
    Sep 6, 2021 at 12:57
  • 1
    This one actually works with 21.04
    – endolith
    Jan 27, 2022 at 18:36
13

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

3
  • 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, 2019 at 17:46
  • 1
    It Works! Thanks a lot, your solution ends with 5 hrs of torture
    – Benjamin
    Jun 7, 2020 at 5:20
  • It worked for me from above solutions . Thanks a lot !
    – Jack
    Feb 18, 2022 at 8:05
9

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
12
  • 2
    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, 2018 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, 2018 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. Nov 15, 2018 at 13:45
  • 2
    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/ Nov 16, 2018 at 15:30
  • 2
    @GiacomoAlzetta My understanding is that the behavior of the recovery session has changed somewhat around the release of Ubuntu 18.10 - many people are reporting that they are unable to run fsck on '/' from recovery, for example. Thus, we are forced to use a live USB or CD to do this instead, as I mentioned about 3 comments ago... Jan 22, 2019 at 14:59
5

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

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.

2

Here's how I did it on Ubuntu 18.04 Server guest

  1. Put Ubuntu 18.04 Desktop Live CD in VM's optical drive (make sure boot sequence loads this first)
  2. Boot VM, when CD asks: click Try Ubuntu
  3. Once GUI is up, run terminal
  4. sudo apt install zerofree and let it install
  5. sudo fdisk -l and note system HD e.g. /dev/sda2
  6. sudo zerofree -v /dev/sda2 and let it finish
  7. halt
  8. ACPI shutdown VM
  9. Remove Ubuntu 18.04 Desktop Live CD from VM's optical drive before restarting
2

I succeeded in shrinking VBox VM Image size by using dd instead of zerofree.

  1. Normal log into Ubuntu (my VM is elementary OS 5.1)
  2. Run sudo dd if=/dev/zero of=/EMPTY bs=1M
  3. Run sudo rm -f /EMPTY
  4. Power off: sudo poweroff
  5. In host OS (windows 10) run VBoxManage.exe modifyhd vbox.vdi --compact.

My VM file is shrinked form 8GB to 6GB. NOTE: It is okay to ignore some warning outputs.

The following is full example of my input/output:

In VM:

erich@vbox:~$ sudo dd if=/dev/zero of=/EMPTY bs=1M status=progress
[sudo] password for erich:         
97889812480 bytes (98 GB, 91 GiB) copied, 415 s, 236 MB/s
dd: error writing '/EMPTY': No space left on device
93399+0 records in
93398+0 records out
97934905344 bytes (98 GB, 91 GiB) copied, 415.592 s, 236 MB/s
erich@vbox:~$ sudo rm -f /EMPTY
bash: cannot create temp file for here-document: No space left on device

In host OS:

cd <C:/path/to/folder/of/vbox.vdi>
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd eOS.vdi --compact
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
1
  • This will work, but there is a reason people want to use zerofree instead of dd on a VM: dd will fill the entire drive to its maximum capacity, whereas zerofree only zeroes unused sectors. So if you have a 2 TB virtual image and only ever used 2 GB of it and have 100 MB to reclaim, dd will grow the image to 2 TB, zerofill will only zero 100 MB and not grow the image. In some situations, it makes a huge difference - like when the amount of space on your host is less than the maximum size your disk may grow to.
    – msanford
    Jan 27, 2022 at 4:41
0

I am using Ubuntu 18.04 server VMs accessed remotely via phpvirtualbox. There seemed to be a lot of services running.

I tried the various tips mentioned above, but the method quoted by Teoman was the only way I could get the drive mounted ro, and so was able to run zerofree.

(As an aside, there's no point doing this if your VM is encrypted, as compacting the VM cannot work when its encrypted. Which mine are.)

0

This command worked for me. It's better if you add -v on zerofree because the terminal will otherwise look like it's not responding when executing the command. Add -v will display the percentage of the status. The OS is Ubuntu 18.04.4.

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

And after I finished the command below, I free the VDI image size from 160G to 25G.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .