12

When entering a chroot is sometimes necessary to mount /sys and /dev using -rbind instead of -bind in order to insure everything is in it's right place when somebody goes looking.

The problem comes when unmounting.

A simple umount always fails; with the children being mounted as well it appears to be in use:

$ umount /mnt/chroot/sys
umount: /mnt/chroot/sys: device is busy.
    (In some cases useful info about processes that use
     the device is found by lsof(8) or fuser(1))

Another possible solution is to list the mounts from proc, and umount each of those like so:

$ grep /mnt/chroot/sys /proc/mounts | cut -f2 -d" " | sort -r | xargs umount

However this fails as well because the recursive mounts are not actually registered in the mtab:

/mnt/chroot/sys/kernel/security is not mounted (according to mtab)

Perhaps the solution is to perform a lazy umount, but this seems pretty dangerous to me.

Is there a better way to do this that I've missed?

  • 1
    Are you sure that /mnt/chroot/sys/kernel/security is mounted at that point? What is the output of grep /sys/kernel/security /proc/mounts? umount doesn't need its argument to be listed in /etc/mtab. If you pass it -n, it won't open the file at all. – Gilles Mar 21 '14 at 23:15
  • If you look closely at my grep command for creating the xargs for umount, I'm only sending it the mounts listed in /proc/mounts – natecornell Mar 22 '14 at 6:17
9

This worked for me correctly -- https://unix.stackexchange.com/a/264488/4319:

mount --rbind /dev /mnt/test
mount --make-rslave /mnt/test
umount -R /mnt/test

It was important to have the two first commands as two separate commands: do not combine --rbind and --make-rslave in one invocation of mount.

Without --make-rslave, the behavior was unwanted (and not successful):

  • umount -l would affect the original old mountpoints, too,
  • and umount -R would be affected by the busy (open) files under the original old mountpoints. (Very unexpected...)
  • Don't know if this has been fixed in a recent version of mount, but it's perfectly ok to combine --rbind and --make-rslave in the same mount invocation: mount --rbind --make-rslave /dev /mnt/test – Javi Merino Aug 14 '17 at 9:29
  • 1
    While you can combine the two arguments, it makes it do a non-recursive mount instead. So it doesn't actually work as intended. – Miral Jul 11 at 23:31
9

The credit goes to Gilles for this answer; Gilles noted in the question comments that the '-n' switch ignores the mtab and unmounts anything listed in /proc/mounts.

From the manpage:

-n     Unmount without writing in /etc/mtab.

So to answer my question of how to unravel a --rbind mount, this is the full command that worked for me:

grep /mnt/chroot/sys /proc/mounts | cut -f2 -d" " | sort -r | xargs umount -n

Merci, Gilles!

  • 1
    Trying mount --rbind / /mnt && umount -n /mnt/dev/shm (or pts), I get umount: /mnt/dev/shm: target is busy. umount -l /mnt kills the system (e.g. sudo fails saying stdin isn't a tty). This is on an installed Fedora system. I guess it's an old problem of mine: unix.stackexchange.com/questions/269695/… – sourcejedi Apr 15 '16 at 12:34
2

Since util-linux v2.23 (25-Apr-2013) the umount command supports the -R, --recursive option.

Here is what the man page says:

Recursively unmount each specified directory. Recursion for each directory will stop if any unmount operation in the chain fails for any reason. The relationship between mountpoints is determined by /proc/self/mountinfo entries. The filesystem must be specified by mountpoint path; a recursive unmount by device name (or UUID) is unsupported.

0

Thanks for that. I use this in my scripts to unmount the entire chroot-tree: (Make sure to set $MNT accordingly)

for dir in $(grep "$MNT" /proc/mounts | cut -f2 -d" " | sort -r)
do
    umount $dir 2> /dev/null
    (( $? )) && umount -n $dir
done

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.