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

446 Views Asked by At

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.

2

There are 2 best solutions below

1
Christian Fritz On BEST ANSWER

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.

0
user3483642 On

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