'How to check if /tmp and /proc filesysystems are mounted in a chrooted environment?

I have already tried mounting the filesystems without checking like this:

sudo -- mount -t proc /proc $chroot_dir/proc
sudo -- mount --bind /tmp $chroot_dir/tmp

However, this would corrupt the parent OS session if already mounted, and I would have to restart the OS. I want to check if they're mounted beforehand.



Solution 1:[1]

You can see in /etc/mtab what's currently mounted:

if grep $chroot_dir/proc /etc/mtab; then
  echo already mounted
fi;

And analogously for tmp.

Solution 2:[2]

Or you can use the output of mount:

is_mounted() {
    local drives=`mount | grep "$1" | awk '{print $3}'`
    local arr=($drives)
    for d in $arr; do
        if [ "$d" == "$1" ]; then
            return 0
        fi
    done
    return 1
}

which returns 0 if the path $chroot_dir/proc is mounted. So after issuing:

sudo -- mount -t proc /proc $chroot_dir/proc

the above function will return 0:

is_mounted $chroot_dir/proc
echo $?

and 1 otherwise

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christian Fritz
Solution 2 Suraj Rao