Usually, I mount some directories to chroot environtment like this:mount -t proc /proc /mnt/chroot/procmount --rbind /dev /mnt/chroot/devmount --rbind /sys /mnt/chroot/sysmount --rbind /run /mnt/chroot/runBut, 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?
How to properly unmount mounted directories after doing chroot?
chroot
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/pointfuser -m /path/to/mount/pointReview 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/chrootor to get them alllsof | grep /mnt/chrootor you can access /proc directly:ls -l /proc/[0-9]*/fd/* | grep /mnt/chrootFor 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 -nuIf 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.