Operating System - HP-UX
1829102 Members
2501 Online
109986 Solutions
New Discussion

Test if there is a filesystem mounted

 
SOLVED
Go to solution
Pedro Cirne
Esteemed Contributor

Test if there is a filesystem mounted

Hi,

How can I test in a shell script if a filesystem is mounted on directory?

Thks,

Pedro
8 REPLIES 8
Luk Vandenbussche
Honored Contributor
Solution

Re: Test if there is a filesystem mounted

grep $dir /etc/mnttab
if [ $? -eq 0 ]
then
echo mounted
fi
Pedro Cirne
Esteemed Contributor

Re: Test if there is a filesystem mounted

Thks
MarkSyder
Honored Contributor

Re: Test if there is a filesystem mounted

I know you've already closed this, but I like to make commands shorter if I can!

if grep $dir /etc/mnttab
then

etc.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Dennis Handly
Acclaimed Contributor

Re: Test if there is a filesystem mounted

>Mark Syder: I like to make commands shorter

You should also use grep -q:
if grep -q $dir /etc/mnttab; then
VK2COT
Honored Contributor

Re: Test if there is a filesystem mounted

Hello,

Note that /etc/mnttab can get corrupt or
have missing record for a specific file system.

Do not rely on /etc/mnttab exclusively.

Here is a little snippet for /bin/sh and /bin/bash script:

#!/bin/sh
fsname="/opt"

if [ "`df $fsname | awk '! /awk/ && ! /Mounted on/ { if ( $NF == "'$fsname'" ) {print}}'`" ]
then
echo "PASS: $fsname mounted"
else
echo "FAIL: $fsname not mounted"
fi

Cheers,

VK2COT

PS. The thread is closed so do not worry about assigning points.
VK2COT - Dusan Baljevic
Dennis Handly
Acclaimed Contributor

Re: Test if there is a filesystem mounted

>VK2COT: Here is a little snippet for /bin/sh

I had a little trouble interpreting your script. First of all, you should have used bdf and not df, otherwise it won't work.

I don't understand why you want to exclude "awk"? ! /awk/

Also, you might want to use awk -v instead of something hard to understand with your stuttering quoting:

M=$(bdf $fsname | awk -v fsname=$fsname '! /Mounted on/ { if ($NF == fsname) print}')
if [ "$M" ]; then

Dennis Handly
Acclaimed Contributor

Re: Test if there is a filesystem mounted

I forgot to mention in my reply to VK2COT that using bdf to see if something is mounted may be expensive. Doing bdf on a NFS directory may take some time. It may be better to just trust mnttab and use syncer(1M) to keep it up to date, 30 seconds seems ok?
VK2COT
Honored Contributor

Re: Test if there is a filesystem mounted

Hello again,

Dennis, thanks for the comments.
I used df(1) as I cut-and-pasted from
my home Linux server :)

Of course, bdf(1) is more suitable
for HP-UX.

As far as NFS slowdown is concerned,
one can avoid checking it by using:

bdf -t vxfs
bdf -t hfs
and similar

If NFS is really a problem, then the
server is unresponsive anyway, so the
file system is acting almost like
being unmounted...

Alas, if /etc/mnttab is corrupt,
and if one has bad luck,
waiting 30 seconds for syncer(1) to
update the file can be too long.

# uname -a
HP-UX myserv B.11.11 U 9000/800 1448733058 unlimited-user license

# cat /etc/mnttab

# df -k

# bdf
Filesystem kbytes used avail %used Mounted on
/dev/vg00/lvol3 212992 137904 74560 65% /
/dev/vg00/lvol1 298928 56768 212264 21% /stand
/dev/vg00/lvol8 2621440 1416088 1198128 54% /var
/dev/vg00/lvol10 1048576 3388 981331 0% /var/tmp
/dev/vg00/lvol11 1048576 91944 896903 9% /var/opt/perf
/dev/vg00/lvignite 46612480 33730800 12781064 73% /var/opt/ignite
/dev/vg00/lvol12 1048576 160045 833055 16% /var/opt/OV
/dev/vg00/lvol9 4194304 1012790 2982812 25% /var/adm/crash
/dev/vg00/lvol7 2244608 1401872 836176 63% /usr
/dev/vg00/lvol4 524288 93240 427736 18% /tmp
/dev/vg00/lvol6 3276800 2529776 741224 77% /opt
/dev/vg00/lvol5 524288 5968 514592 1% /home

In other words, one might face a race
condition when tests fail because
/etc/mnttab is empty or corrupt.

Commands bdf(1), and mount(1) update
/etc/mnttab on HP-UX, but df(1) does not.
See real-life example above.

My point was not to trust single commands.
If something is critical double-check.

In my Perl OAT script, for example,
I check file systems in many ways. Once,
I found a very weird server that had
/stand unmounted. II was aksed to come to a specific customer and upgrade their server.
It was the first time ever I saw an HP-UX server running happily without /stand.

It is easy to replicate it (I just did it
on one server):

# umount /stand

# dmesg

Sep 6 21:13
Can't get kernel namelist

That is how I knew something was wrong :)

Cheers,

VK2COT
VK2COT - Dusan Baljevic