56

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
1
  • 4
    The man page for zerofree suggests running telinit 1 and that solved the problem for me.
    – Alex Ryan
    Commented Dec 25, 2014 at 8:37

12 Answers 12

33

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.

2
  • 3
    The processes that have files open in write mode will have capital F in the access flags in the fuser output.
    – Jan Hudec
    Commented Feb 11, 2019 at 15:31
  • Appending a ` 2>&1 | grep F` to the fuser command would be a good inclusion to this answer, as suggested by @JanHudec. It just did the trick to me. Commented Aug 9, 2021 at 20:25
32

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
4
  • 5
    Captain Obvious wants to add: Make sure you install zerofree BEFORE you reboot in ro mode
    – Xosofox
    Commented Feb 4, 2017 at 21:00
  • using "Drop to Root Shell Prompt" and zerofree :filesystem mounted rw. But I had no problem in other VM Commented May 23, 2019 at 17:51
  • This only worked for me when the /dev/sda1 / ext2 ro 0 1 entry was at the top of the list. When I first put it as the last entry, I still had the mount point busy error.
    – afilina
    Commented Sep 26, 2019 at 12:45
  • This is the only thing that worked for me. Windows 10 host, Ubuntu 22 guest. THANK YOU!
    – Dan Barron
    Commented Feb 25, 2023 at 16:31
27

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}'
3
  • 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
    Commented May 20, 2015 at 8:43
  • I closed all processes that had a file open for reading, writing or both (u) and it still says busy.
    – Hubro
    Commented Mar 15, 2017 at 11:29
  • worked for me on debian 10, it told me to kill atop and rsyslog, i did, then i could mount it ro :)
    – hanshenrik
    Commented May 24, 2020 at 8:58
12

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 /
2
  • 1
    For me both units restart immediately.
    – rld.
    Commented Dec 26, 2017 at 23:47
  • worked for me in "Advanced Options > Kernel option with Recovery Mode > Drop to Root Shell Prompt" Commented May 23, 2019 at 17:53
9

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

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

Entering Un*x like GNU/Linux in Single User Mode

From Wikipedia:

Single-user mode is a mode in which a multiuser computer operating system boots into a single superuser. It is mainly used for maintenance of multi-user environments such as network servers. Some tasks may require exclusive access to shared resources, for example running fsck ...

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

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

  3. Now, trying to mount / in read-only mode:

    mount -o remount,ro /
    mount: / is busy
    

    If you seem unable to re-mount root filesytem in read-only mode politely, you could ask more severely:

  4. first using magic SysRq key keyboard kernel trap:

    • hold down AltGr, and maintain them 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 now, you could release AltGr
  5. Or by command line:

        echo s >/proc/sysrq-trigger
        echo u >/proc/sysrq-trigger
    

    I could check by testing to create a file:

        touch /dummyfile
        touch: cannot touch '/dummyfile': Read-only file system
    
  6. Then now, I could use tools on my root partition!

    fsck -fC0 /dev/sda2
    

    Like any other tools: zerofree, partclone, resize2fs (even to shrink partition, but for this, I had to use a loop device...), etc...

    Saving my whole root partition to a USB device, with zstd compression and creating a sha1sum controle file simultaneously:

    mount /dev/sdb1 /media/usb
    mkdir /media/usb/save-$HOSTNAME-$(date +%F)
    cd /media/usb/save-$HOSTNAME-$(date +%F) || exit 1
    partclone.ext4 -c -s /dev/sda2 -O >(
        zstd | tee sda2.ext.zst |
            sha1sum |
            sed 's/-$/sda2.ext.zst' >> sha1sums.txt
        ) -N -L sda2.log
    
  7. Then reboot!!.

    Don't ever try to return in multi-user mode without rebooting completely!!

4
  • 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... Commented Jan 5, 2018 at 19:43
  • My fsck doesn’t know -f; what would that do, zero unreferenced blocks? Commented Nov 16, 2018 at 14:21
  • 2
    @ᴠɪɴᴄᴇɴᴛ: I'ts ext2's fsck switch: -f Force checking even if the file system seems clean. Commented Nov 17, 2018 at 14:59
  • @ᴠɪɴᴄᴇɴᴛ No, zerofree is not fsck! I'd never used zerofree! I prefer to use partclone which save the partition without unreferenced blocks. (or tar or rsync, for saving only files, but excluding caches and other useless files)... Commented Sep 21, 2023 at 12:53
7

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.

2

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.

1
  • This sounded promising, but when I added the second VDI as SATA1 (after SATA0 for the original one) I had no extra entry in my client. blkid showed /dev/sda1 and /dev/sdb1 but with the same UUIDs.
    – bomben
    Commented Feb 18, 2021 at 12:31
1

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.

0

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 /
0

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.

0

The solution for me was to reboot the OS and choose "recovery mode" from GRUB's boot menu. From "recovery mode", there are fewer processes running, and remounting read-only works fine.

If the system has a swap file on the root file system (rather than a swap partition), it is also necessary to temporarily switch off the swap file with swapoff -a.

You must log in to answer this question.

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