1

Usually, I mount some directories to chroot environtment like this:

mount -t proc /proc /mnt/chroot/proc
mount --rbind /dev /mnt/chroot/dev
mount --rbind /sys /mnt/chroot/sys
mount --rbind /run /mnt/chroot/run

But, after finished, I can't umount those mounted directories and there's a message to get a list of running process that use the dirs which give me a bunch lines of processes list. So, how to properly unmount those directories?

  • Does fuser /mnt/chroot/proc /mnt/chroot/dev /mnt/chroot/sys /mnt/chroot/run produce any output? – Mark Plotnick May 4 '15 at 15:09
  • The only one that producing output is /mnt/chroot/proc: 1927 2357 2380. That's right after I chroot inside it and exit immediately – Mas Bagol May 4 '15 at 16:03
  • Run ps -p 1927 2357 2380. Maybe those processes are running in the background and don't immediately exit when you exit the chroot. – Mark Plotnick May 4 '15 at 16:05
  • Sorry, what should I do then? – Mas Bagol May 4 '15 at 16:10
  • Normally, the choice is to either terminate the processes or arrange that they close any files they have open in the filesystem you want to unmount. Run lsof -p 1927 and see what things under /mnt/chroot/proc it has open. – Mark Plotnick May 4 '15 at 21:37
1

The normal way to find what is preventing a filesystem from being unmounted is to list the processes that have a file open on it (or a file descriptor, or their current directory, etc.):

lsof /path/to/mount/point
fuser -m /path/to/mount/point

Review the list of processes and kill them if warranted.

There are also a few ways in which the kernel itself can have something going on that prevents the unmounting, for example if there is another mount point beneath it (e.g. you can't unmount /mnt/chroot while /mnt/chroot/proc is mounted).

When you have bind mounts, commands such as the one above list files open via any of the paths to the filesystem. So for example fuser -m /mnt/chroot/run lists processes that have files open on that filesystem, whether they're accessed via /run or via /mnt/chroot/run.

To find what is using the mount point, list the processes and the paths via which they have files open, and do some filtering on the paths. For example:

lsof /mnt/chroot/run | grep /mnt/chroot

or to get them all

lsof | grep /mnt/chroot

or you can access /proc directly:

ls -l /proc/[0-9]*/fd/* | grep /mnt/chroot

For automated processing (assuming a recent sed that supports -z for null delimiters):

find /proc/[0-9]*/fd -type l -printf '%p %l/\0' |
sed -nz 's!^/proc/\([0-9]*\)/fd/[0-9]* /mnt/chroot/.*!\1!p' |
sort -nu

If you really can't find what's holding a filesystem mounted but you need to get back the mount point, you can move the mount point out of the way by creating an empty directory somewhere and running mount --move /current/mount/point /empty/directory/out/of/the/way. You can also do a lazy unmount (umount -l), which causes the filesystem to have no mount point at all (so files on it can't be opened anymore) but still be mounted as long as there are open files or other references on it.

1

If there are processes running "inside" the chroot (even after your shell exited it), you are likely to be unable to umount the filesystems (say, you can't just pull the filesystem from under those processes feet).

In such case, reenter the chroot (in a shell), and stop the services, or just kill them (even from "outside" the chroot), if you don't care about their state, etc..

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.