Linux bash if mount

How to test if a given path is a mount point

I discover that on my Fedora 7 there is a mountpoint command.

NAME mountpoint - see if a directory is a mountpoint SYNOPSIS /bin/mountpoint [-q] [-d] /path/to/directory /bin/mountpoint -x /dev/device 

Apparently it come with the sysvinit package, I don’t know if this command is available on other systems.

[root@myhost~]# rpm -qf $(which mountpoint) sysvinit-2.86-17 

The solutions that parse output /etc/mtab can provide the wrong answer: root file system could be mounted read-only, and /etc/mtab may not have been updated since last boot. mount(8) often simply prints the content of /etc/mtab, and will give wrong answer for the same reason.

Thanks for this. Also note that the command exits with status 1 if the directory is not a mountpoint, which is quite useful.

Note that the mountpoint command currently just compares the device number of stat(dir) with lstat(dir/..) (similar to other answer), and it fails if the mount point was mounted using mount —bind to another part of the same filesystem.

It’s there on ubuntu/debian. This is probably the better solution, although the other ideas subscribe perfectly the unix way: that there is more than one.

Not relying on mount , /etc/mtab , /proc/mounts , etc.:

if [ `stat -c%d "$dir"` != `stat -c%d "$dir/.."` ]; then echo "$dir is mounted" else echo "$dir is not mounted" fi 

When $dir is a mount point, it has a different device number than its parent directory.

The benefit over the alternatives listed so far is that you don’t have to parse anything, and it does the right thing if dir=/some//path/../with///extra/components .

The downside is that it doesn’t mark / as a mountpoint. Well, that’s easy enough to special-case, but still.

+1. FYI I had to use ‘stat -c%d «/path»‘ instead of the options you described to compare device numbers. It might just be a different in our systems; I’m on RHEL 5.5. Also, for others see man stat(2) if you want to do this in a program.

This doesn’t appear to work with mount —bind mount points. mounted or not mounted the if is always false. Doesn’t work even with stat -c%d — this is on RHEL as well.

@ephemient : Had to read it twice. Was unclear the first time. Reading stat man page helped to understand what you are doing. I might have missed to notice this great answer. Thank you.

Your set of options compares the major device type, which will be the same in the example below. Instead, use Device Number. In the following code block, replace the semicolons with linefeeds to make it easier to read. mkdir -p /mnt/ramdisk/; mount -t tmpfs -o size=128M tmpfs /mnt/ramdisk; mkdir -p /mnt/ramdisk/ramdisk; mount -t tmpfs -o size=128M tmpfs /mnt/ramdisk/ramdisk; echo «Major Device type»; stat -c %t /mnt/ramdisk; stat -c %t /mnt/ramdisk/ramdisk; echo «Device Number»; stat -c %D /mnt/ramdisk; stat -c %D /mnt/ramdisk/ramdisk; umount /mnt/ramdisk/ramdisk; umount /mnt/ramdisk

Читайте также:  Manjaro linux восстановление системы

will give the device number of the directory. If it differs between the directory and its parent then you have a mount point.

Add /. onto the directory name if you want symlinks to different filesystems to count as mountpoints (you’ll always want it for the parent).

Disadvantages: uses GNU find so less portable

Advantages: Reports mount points not recorded in /etc/mtab.

if mount | cut -d ' ' -f 3 | grep '^/mnt/disk$' > /dev/null ; then . fi 

EDIT: Used Bombe’s idea to use cut.

@haggai_e: I hope you meant replace mount with a < /proc/mounts redirect. avoid useless cat suffering!

Unfortunately both mountpoint and stat will have the side-effect of MOUNTING the directory you are testing if you are using automount. Or at least it does for me on Debian using auto cifs to a WD MyBookLive networked disk. I ended up with a variant of the /proc/mounts made more complex because each POTENTIAL mount is already in /proc/mounts even if its not actually mounted!

Where 'disk' is the name of the server (networked disk) in /etc/hosts. '//disk/Public' is the cifs share name '/tmp/cifs' is where my automounts go (I have /tmp as RAM disk and / is read-only) '/tmp/cifs/disk' is a normal directory created when the server (called 'disk') is live. '/tmp/cifs/disk/Public' is the mount point for my 'Public' share.

Источник

Using bash to tell whether or not a drive with a given UUID is mounted

I am working on a bash script that is designed to tell whether or not a given drive is attached to the system, and if it is, to copy some data (backups that need to be taken off-site) to that drive. What I’d like to do is identify the drive/partition by UUID, so that I can have a script that frequently scans for the presence of that drive, and when it’s plugged in, copies data to it (via rsync, so I don’t particularly care if that part runs more than once, because the later runs are I/O-cheap). I want this to be as simple as possible so that I can hand the drive in question to a non-technical person and say «plug it in here, then unplug it later and take it with you.» I can’t seem to find any options in mount that quite do this, and grepping through /etc/fstab doesn’t tell me whether a drive is actually mounted or not. How can I tell, given a UUID, whether or not a drive with that UUID is mounted and what its mount point is?

4 Answers 4

A device can be mounted by its symlink. If this is the case, the mount command will refer to it by its symlink but you might have a reference by its real path, or worse another symlink.

This is compounded even more by the fact that multiple block device files can point to a single block device (!). This can be done by using the mknod command.

The only way I can find to accurately get this information is to compare the major and minor numbers of all entries in /proc/mounts with the major and minor number of the UUID you are looking for.

Читайте также:  Linux перераспределить размер дисков

Here is a function/script that will do this.

#!/bin/bash # $1: should be a drive uuid, such as that reported by blkid # return: return value of 0 if mounted, 1 if not mounted function is_mounted_by_uuid() < input_path=$(readlink -f /dev/disk/by-uuid/"$1") input_maj_min=$(stat -c '%T %t' "$input_path") cat /proc/mounts | cut -f-1 -d' ' | while read block_device; do if [ -b "$block_device" ]; then block_device_real=$(readlink -f "$block_device") blkdev_maj_min=$(stat -c '%T %t' "$block_device_real") if [ "$input_maj_min" == "$blkdev_maj_min" ]; then return 255 fi fi done if [ $? -eq 255 ]; then return 0 else return 1 fi >if is_mounted_by_uuid "$1"; then echo $(readlink -f /dev/disk/by-uuid/$1) is mounted else echo $(readlink -f /dev/disk/by-uuid/$1) is not mounted fi 

Источник

Bash Script — Check mounted devices

What I’m trying to do — In the end, I want a script that checks for 3 devices, an SD card, Backup1 & Backup2. They’ve all been set to auto-mount at their respective mountpoints. The script should check for SD card first, if this fails, then a warning should be sent, and nothing more. If SD is okay, but only one backup is mounted, then ask for confirmation to go ahead with rsync to mounted backup. If all devices are mounted, then rsync from SD card to both backups. Currently I’m just trying to get the device check nailed, using echo commands. Here’s what I have (after multiple attempts) —

if ! mount | grep /media/card >/dev/null then echo "ERROR: SD card not mounted, aborting" else if ! mount | grep /media/backup >/dev/null then if ! mount | grep /media/backup2 >/dev/null then echo "ERROR: No backup devices" else echo "CAUTION: Backup_2 missing, Backup_1 OKAY" fi else if ! mount | grep /media/backup2 /dev/null then echo "CAUTION: Backup_1 missing, Backup_2 OKAY" else echo "SUCCESS: All devices OKAY" fi fi fi 

Which isn’t working. I’m getting confused by all the nested ‘if’s and surely there’s a simpler way? Perhaps something that checks each device independently, then returns values that equate to the various combinations (0=no devices, 1=sd only, 2=sd & backup1, 3=sd & backup 2, 4 = all okay) which then is read and decides next part of script to run? If not, where has this way gone wrong? Any help is appreciated

Источник

Check if directory mounted with bash

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.

Linux Solutions

Solution 1 — Linux

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 

Solution 2 — Linux

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 .

Читайте также:  Расшарить папку linux nfs

Solution 3 — Linux

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 
mkdir -p /tmp/foo/ 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" 

Solution 4 — Linux

is_mount() < path=$(readlink -f $1) grep -q "$path" /proc/mounts > 
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 .

Solution 5 — Linux

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" END ' /proc/mounts # and you can use it in and if like so: if awk -v status=1 '$2 == "/opt" END ' /proc/mounts; then echo "yes" else echo "no" fi 

Solution 6 — Linux

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

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

Solution 7 — Linux

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.

Solution 8 — Linux

In my .bashrc, I made the following alias:

alias disk-list="sudo fdisk -l" 

Источник

Оцените статью
Adblock
detector