Script searches for and mounts all NFS shares

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/sh
# autonfs.sh
# Purpose: automatically mounts all shares on given servers.
# Arguments: currently none.
# BUGS: spaces in NFS-exported fs will break this script.
# kkatarn, 2015
# List of NFS servers
NFS_HOSTS="storage1 storage2"
MNT_PREFIX="/mnt/nfs"
if [ -f /boot/kernel/kernel ]; then
# FreeBSD
NFS_MOUNT="mount_nfs"
else
# FIXME: assuming linux
NFS_MOUNT="mount.nfs -o rw -o nolock"
fi
for SRV in ${NFS_HOSTS}; do
echo "$(basename $0): mounting all NFS exports on ${SRV}..."
EXPORTS=$(showmount -e $SRV)
for EXP in ${EXPORTS}; do
if [ $(echo $EXP | cut -c 1) != '/' ]; then
continue;
fi
FS="${SRV}:${EXP}"
MNT="${MNT_PREFIX}/${SRV}${EXP}"
if ! [ -d ${MNT} ]; then
mkdir -p ${MNT}
fi
${NFS_MOUNT} ${FS} ${MNT} && echo "$(basename $0): successfully mounted ${FS}"
done
done