I am trying to run zerofree on Ubuntu 11.04 so that I can compact the VirtualBox vdi image using:

VBoxManage modifyhd Ubuntu.vdi --compact

In order to run zerofree the disk image has be mounted as read-only. I'm following these instructions which says to use this to remount as read-only from the recovery mode (Drop to root shell prompt):

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

But when I do this I get the error:

mount: / is busy

Any ideas on how to do this?

Follow up: Following Jari's answer and this post by running these commands resolves the issue.

service rsyslog stop
service network-manager stop
killall dhclient
  • 3
    The man page for zerofree suggests running telinit 1 and that solved the problem for me. – Alex Ryan Dec 25 '14 at 8:37

11 Answers 11

up vote 17 down vote accepted

Some processes are keeping files open for writing. These could be, for example, programs that write logs, like rsyslogd, networking tools, like dhclientor something else. Shutting these down one by one and trying the remount might work.

You can find processes that use certain files by using the program fuser. For example, fuser -v -m / will return a list of processes. However, I am not sure if it is one of these which keeps the file system busy.

You can only remount the filesystem read-only if there is no process that has a file open for writing. Run lsof / to see what processes have files open on the root filesystem. Files open for writing will be indicated in the FD column. You can filter these with

lsof / | awk '$4 ~ /[0-9].*w/'

To filter the process IDs automatically, parse the output of lsof -F pa:

lsof -F pa /home | awk '/^p/ {pid = substr($0, 2)} /^a.*w/ {print pid}'
  • 1
    Tried with this solution, still busy, might becoz some resource can't show with this way. After stop some running services, can remount the fs as read only. – Sam Liao May 20 '15 at 8:43
  • I closed all processes that had a file open for reading, writing or both (u) and it still says busy. – Hubro Mar 15 '17 at 11:29

Improving Peter answer:

I could not kill processes that uses my mount point. So, I did this:

  • Edit /etc/fstab to starts in read-only in next boot. Ex: /dev/sda1 / ext2 ro 0 1
  • Reboot and open a shell (Host Key + F2, for instance)
  • Run zerofree

To "recover" your system:

  • Mount with read-write option: $ mount -o remount,rw /dev/sda1
  • Edit /etc/fstab again, restoring your original value

Extra: if necessary open recovery mode in any step:

  • Reboot
  • Hold shift while VM is loading
  • Advanced Options > Kernel option with Recovery Mode > Drop to Root Shell Prompt
  • 1
    Captain Obvious wants to add: Make sure you install zerofree BEFORE you reboot in ro mode – Xosofox Feb 4 '17 at 21:00

There are sometimes so many processes keeping the mount busy, that it might be easier to simply reboot with the mount read-only.

Change the entry for the root filesystem in /etc/fstab, for example:

/dev/sda1 / ext2 relatime,rw,errors=remount-ro 0 1

becomes:

/dev/sda1 / ext2 noatime,ro 0 1

On reboot, the filesystem will be mounted read-only, so you can run zerofree on it.

When you're finished, remount the filesystem read/write again (mount -o remount,rw /) and undo your changes to /etc/fstab.

Inspired by https://wiki.debian.org/ReadonlyRoot#Enable_readonly_root

  • Great! FYI, I used just this /dev/sda1 / ext2 ro 0 1 and I could boot in readonly mode. – Topera Sep 17 '16 at 4:32

If you use systemd then disk writes are stopped by stopping systemd-journald.

# systemctl stop systemd-journald.socket
# systemctl stop systemd-journald.service
# mount -o ro,remount /
  • For me both units restart immediately. – rld. Dec 26 '17 at 23:47

For anyone else who's just looking for a quick fix to paste into your terminal...

(Before following the commands below, make sure you have completed the other steps like telinit 1.)

Found this:

fuser -mv /dev/sda1 > /tmp/sda1.pids
kill `cat /tmp/sda1.pids`

Commands may kill your session, but after you come back, your partition is ready to be remounted.

I assume you try to run the mount command in the VM. Anyway you probably have a different root partition than the author of the forum post.

Try:

mount -o ro,remount /

Which does not depend on the fs or mount option. If this does not work you can also try to determine your root fs with grep " / " /proc/mounts or cat /proc/cmdline. If your root filesystem is for example /dev/mapper/system-rootfs you can use:

mount -o ro,remount /dev/mapper/system-rootfs /

I had this issue which kept me from putting my machine in read only mode. Solving the problem took me a longer than id like to admit... I believe my issue was that I left Apache-Zeppelin running and forgot about it. Make sure that you are not doing the same with another hosting type service. To solve the problem I used fuser -kill / to terminate all of the processes that could be causing the issue and it allowed me to go back into ro mode.

If you still can not unmount or remount your device after stopping all services and processes with open files, then there may be a swap file or swap partition keeping your device busy. This will not show up with fuser or lsof. Turn off swapping with:

sudo swapoff -a

You could check beforehand and show a summary of any swap partitions or swap files with:

swapon -s

or:

cat /proc/swaps

As an alternative to using the command sudo swapoff -a, you might also be able to disable the swap by stopping a service or systemd unit. For example:

sudo systemctl stop dphys-swapfile

or:

sudo systemctl stop var-swap.swap

In my case, turning off swap was necessary, in addition to stopping any services and processes with files open for writing, so that I could remount my root partition as read only in order to run fsck on my root partition without rebooting. This was necessary on a Raspberry Pi running Raspbian Jessie.

Ok, there is what I've done

First, my goal was to reduce my / (ROOT) partition in order to add another one on my disk.

  • From running session once everything cleaned and backuped:

    init 1
    

    My desktop go away and I'm now on Linux console...

    ...
    Give root password for maintenance or press CTRL+D to continue
    TheRootPassword_SomethingLike1234
    
  • Now, trying to mount / in read-only mode:

    mount -o remount,ro /
    mount: / is busy
    
  • Ok, from now, I'm in theorically single-user mode, but ps ax show a lot of other process!!

    Killing them all tis not really possible, or dangerous... ( kill 1 is forbiden... I don't have time to play psdoom :-)

    The only thing I can do is a System Request, for this, I know two ways: (see Documentation/sysrq.txt file in kernel docs):

    1. first using magic SysRq key keyboard kernel trap:

      • hold down AltGr, then maintain then pressed,
      • hit PrtScn just once, but don't release AltGr,
      • hit s just once, this will send Emergency sync request to kernel and
      • hit u, this will send Umount all request, this will remount all mounted filesystems read-only,
      • then release AltGr
    2. Or by command line:

      echo s >/proc/sysrq-trigger
      echo u >/proc/sysrq-trigger
      
  • Then now, I could

    fsck -fC0 /dev/mapper/MyDisk-ROOT
    

    ....

...

  • Note: In fine, if my fsck was succesfully done, I've booted a Live USB in order to by able to run resize2fs... This feature is not already implemented... – F. Hauri Jan 5 at 19:43

Attach the vdi file as additional and non-system volume to another VirtualBox. There you can (re)mount it as needed and perform the zerofree.

Let us say you want to compact the VDI disk of VM1 (Virtual Machine 1). Then you need a second VM2. That is, :

  • Make sure that neither VM1 nor VM2 is running;
  • From VirtualBox Manager:

    • Select VM2,
    • "Settings"->"Storage"->"Add Hard Disk":"Select it as additional disk",

    • select the vdi file of VM1, which you want to compact. Hint: Make sure that the hard disk just added to VM2 goes second in boot-order after the original disk of VM2.

    • start VM2: now you can (re)mount the volume of VM1 as additional disk for VM2, because no process are using it in VM2. Hint: First, open the volume with the folebrowser of Linux, which automatically mounts the volume. Then, use mount -l to see which is the _Device_ in the command sudo mount -o remount,ro _Device_. In my case this was /dev/sdb1. So the command was (1) sudo mount -o ro,remount /dev/sdb1 and then (2)sudo zerofree /dev/sdb1.

Instead of VM2 one should be able to boot from and installation disk like and .iso image and achieve the same BUT I have not tried it.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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