Check if directory mounted with bash

时光怂恿深爱的人放手 提交于 2019-11-27 19:48:36

问题


I am using

mount -o bind /some/directory/here /foo/bar

I want to check /foo/bar though with a bash script, and see if its been mounted? If not, then call the above mount command, else do something else. How can I do this?

CentOS is the operating system.


回答1:


Running the mount command without arguments will tell you the current mounts. From a shell script, you can check for the mount point with grep and an if-statement:

if mount | grep /mnt/md0 > /dev/null; then
    echo "yay"
else
    echo "nay"
fi

In my example, the if-statement is checking the exit code of grep, which indicates if there was a match. Since I don't want the output to be displayed when there is a match, I'm redirecting it to /dev/null.




回答2:


You didn't bother to mention an O/S.

Ubuntu Linux 11.10 (and probably most up-to-date flavors of Linux) have the mountpoint command.

Here's an example on one of my servers:

$ mountpoint /oracle
/oracle is a mountpoint
$ mountpoint /bin
/bin is not a mountpoint

Actually, in your case, you should be able to use the -q option, like this:

mountpoint -q /foo/bar || mount -o bind /some/directory/here /foo/bar

Hope that helps.




回答3:


The manual of mountpoint says that it:

checks whether the given directory or file is mentioned in the /proc/self/mountinfo file.

The manual of mount says that:

The listing mode is maintained for backward compatibility only. For more robust and customizable output use findmnt(8), especially in your scripts.

So the correct command to use is findmnt, which is itself part of the util-linux package and, according to the manual:

is able to search in /etc/fstab, /etc/mtab or /proc/self/mountinfo

So it actually searches more things than mountpoint. It also provides the convenient option:

-M, --mountpoint path

Explicitly define the mountpoint file or directory. See also --target.

In summary, to check whether a directory is mounted with bash, you can use:

if [[ $(findmnt -M "$FOLDER") ]]; then
    echo "Mounted"
else
    echo "Not mounted"
fi

Example:

mkdir -p /tmp/foo/{a,b}
cd /tmp/foo

sudo mount -o bind a b
touch a/file
ls b/ # should show file
rm -f b/file
ls a/ # should show nothing

[[ $(findmnt -M b) ]] && echo "Mounted"
sudo umount b
[[ $(findmnt -M b) ]] || echo "Unmounted"



回答4:


My solution:

is_mount() {
    path=$(readlink -f $1)
    grep -q "$path" /proc/mounts
}

Example:

is_mount /path/to/var/run/mydir/ || mount --bind /var/run/mydir/ /path/to/var/run/mydir/

For Mark J. Bobak's answer, mountpoint not work if mount with bind option in different filesystem.

For Christopher Neylan's answer, it's not need to redirect grep's output to /dev/null, just use grep -q instead.

The most important, canonicalize the path by using readlink -f $mypath:

  • If you check path such as /path/to/dir/ end with backslash, the path in /proc/mounts or mount output is /path/to/dir
  • In most linux release, /var/run/ is the symlink of /run/, so if you mount bind for /var/run/mypath and check if it mounted, it will display as /run/mypath in /proc/mounts.



回答5:


Another clean solution is like that:

$ mount | grep /dev/sdb1 > /dev/null && echo mounted || echo unmounted

For sure, 'echo something' can be substituted by whatever you need to do for each case.




回答6:


I like the answers that use /proc/mounts, but I don't like doing a simple grep. That can give you false positives. What you really want to know is "do any of the rows have this exact string for field number 2". So, ask that question. (in this case I'm checking /opt)

awk -v status=1 '$2 == "/opt" {status=0} END {exit status}' /proc/mounts

# and you can use it in and if like so:

if awk -v status=1 '$2 == "/opt" {status=0} END {exit status}' /proc/mounts; then
  echo "yes"
else
  echo "no"
fi



回答7:


The answers here are too complicated just check if the mount exists using:

cat /proc/mounts | tail -n 1

This only outputs the last mounted folder, if you want to see all of them just remove the tail command.




回答8:


In my .bashrc, I made the following alias:

alias disk-list="sudo fdisk -l"


来源:https://stackoverflow.com/questions/9422461/check-if-directory-mounted-with-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!