sh scripting: how to mount a remote filesystem if it is not mounted?

In a bourne shell script (#!/bin/sh) how can check to see if a remote NFS share is mounted and, if it is not, mount it? I've got an ugly set of cat, greps and ifs using the output of 'mount' at the moment but it doesn't seem to be doing a reliable job.

Share

Improve this question

Follow

edited Jul 15, 2009 at 6:58

pgs's user avatar

pgs

3,67111 gold badge2121 silver badges1919 bronze badges

asked Jul 15, 2009 at 6:20

DrStalker's user avatar

DrStalker

7,3162424 gold badges8484 silver badges109109 bronze badges

  • 2

    It would be helpful to see your own effort in solving this. If you have a code that's unreliable, show it here and help us make it reliable. 

    – Karolis T.

     CommentedJul 15, 2009 at 6:27

Add a comment

9 Answers

Sorted by:

                                              Highest score (default)                                                                   Date modified (newest first)                                                                   Date created (oldest first)                              

5

If possible, setting up automount ( autofs ) would be the standard way to do this. It might already be in your distribution (comes with CentOS / Redhat default install ). Here is a tutorial.

Why use Automount?

Automounting is the process where mounting and unmounting of certain filesystems is done automatically by a daemon. If the filesystem is unmounted, and a user attempts to access it, it will be automatically (re)mounted. This is especially useful in large networked environments and for crossmounting filesystems between a few machines (especially ones which are not always online).

Share

Improve this answer

Follow

answered Jul 15, 2009 at 11:30

Kyle Brandt's user avatar

Kyle Brandt

85.8k7777 gold badges318318 silver badges463463 bronze badges

  • 1

    /facepalm. Now that you mention it automount is such an obvious way to approach this! 

    – DrStalker

     CommentedJul 16, 2009 at 6:02

Add a comment

Report this ad

3

Can you grep /etc/mtab for the device? grep -c '/mnt/foo' /etc/mtab if grep outputs '1' then /mnt/foo is mounted.

Share

Improve this answer

Follow

answered Jul 15, 2009 at 6:47

beggs's user avatar

beggs

39622 silver badges99 bronze badges

  • That'd be basically the same as what he/she is currently doing, grepping mount's output. 

    – derobert

     CommentedJul 15, 2009 at 6:49
  • 2

    Ok, but why is it not reliable? Bug in the script or problem with mount's output? 

    – beggs

     CommentedJul 15, 2009 at 6:53
  • 1

    what about greping /proc/mounts? on many sytems this is more up to date than /etc/mtab 

    – user12096

     CommentedMay 21, 2010 at 22:22

Add a comment

3

Use mountpoint.

mountpoint -q /path/to/nfs/share || mount -t nfs server:/nfs/share /path/to/nfs/share

(I don't know how widespread or portable mountpoint is; it's provided by the initscripts package on my Debian server.)

Share

Improve this answer

Follow

answered Jul 15, 2009 at 12:45

Josh Kelley's user avatar

Josh Kelley

99211 gold badge77 silver badges1717 bronze badges

  • At least it exists in fedora 17 - and was unaware of it. Thanks! 

    – David Ramirez

     CommentedSep 7, 2012 at 16:49

Add a comment

Report this ad

2

In solaris

If your checking that the system where the script is running has a remote filesystem mounted then

ISMOUNTED=`/usr/sbin/mount | grep "^/path/to/mount "`
if [ "$ISMOUNTED" = "" ]
then
    mountcommand*
fi

*mountcommand could be /usr/sbin/mount /path/to/mount if there is a corresponding entry in the /etc/vfstab or /usr/sbin/mount remotehost:/remote/path /path/to/mount

Share

Improve this answer

Follow

edited Jul 15, 2009 at 12:14

community wiki

2 revs
Iain

Add a comment

1

You may be able to do something with stat. The "device" field will be different across different filesystems. So, assuming you want to see if /mnt/foo is mounted, you'd compare the output of stat -c%d /mnt/ to stat -c%d /mnt/foo/. If the device is different, something is mounted there.

if [ `stat -c%d /mnt/` -eq `stat -c%d /mnt/foo/` ]; then
    mount /mnt/foo
fi

Share

Improve this answer

Follow

answered Jul 15, 2009 at 6:37

derobert's user avatar

derobert

1,3281212 silver badges2323 bronze badges

Add a comment

1

When it comes down to it, shell programming is about plugging together small discrete tools using pipes to produce some kind of compound utility. A utility that did what you're asking for in a "smart" way wouldn't really match the Unix philosophy.

If you want to do it more intelligently, you might want to look at doing this in Perl or Python or C, where you can use the library functions to talk to the portmapper to get information about mounted filesystems as a data structure. You can then intelligently perform the tasks to change the current state to the state you want.

Share

Improve this answer

Follow

answered Jul 15, 2009 at 8:15

James F's user avatar

James F

6,91911 gold badge2828 silver badges2424 bronze badges

Add a comment

1

Just to throw another idea out there, the df command can tell you the mounted filesystem of a directory. If you throw in the -l option, you get a pretty easy test to see if a directory is on a local filesystem or not.

$ cd /net/remoteshare
$ df -l .
df: no file systems processed
$ echo $?
1

Share

Improve this answer

Follow

answered Jul 15, 2009 at 13:30

Chad Huneycutt's user avatar

Chad Huneycutt

2,15611 gold badge1616 silver badges1414 bronze badges

Add a comment

0

A few suggestions:

  • If your distribution has an RC script to handle NFS mounts then it would be prudent to use or it check it's status. You can run into trouble if you incorrectly assume that services such as portmap and statd are already started.

  • It's often more reliable to use /proc/mounts in favour of output from mount or the possibly out-of-date content of /etc/mtab.

  • Use grep -qs and check the return code to determine whether a mount is present or not.

  • Assuming that all of the NFS mounts listed in /etc/fstab should be mounted then you can mount them universally with mount -a -t nfs,nfs4. Anything already mounted will be ignored.

Share

Improve this answer

Follow

answered Jul 15, 2009 at 8:37

Dan Carley's user avatar

Dan Carley

26.1k55 gold badges5656 silver badges7171 bronze badges

Add a comment

0

#!/bin/bash
mountpoint='your mountpoint'
mobileno=""
smsurl=""
mntcmd='/bin/mount -a'
mntchkcmd='/bin/mount'   
###Check mount point available or not IF not alert to service own
${mntchkcmd} | /bin/grep ${mountpoint} > /dev/null 2>&1
if [ $? != 0 ]
        then
    ${mntcmd}
    sleep 5
    ${mntchkcmd} | /bin/grep ${mountpoint} > /dev/null 2>&1
    if [ $? != 0 ]
        then
        echo "issue with mount point"
        exit
    fi
fi
###If mount point is available then check it for utilisation.
warn=90
critical=95
disksize=$(/bin/df -k ${mountpoint} | /bin/grep ${mountpoint} | /bin/awk -F% '{print $1}' | /bin/awk '{print $4}')
if [ ${disksize} -gt ${warn} ]
    then
        echo 'warn'
    elif [ ${disksize} -gt ${critical} ] 
    then
        echo 'critical' 
fi
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值