Discussion:
fstab won't automount shared NAS folders
(too old to reply)
RobH
2021-01-16 12:13:11 UTC
Permalink
I haven't really noticed this before as I don't reboot my desktop very
often, and it was a while since I last did it.
When I checked my shared NAS folders on my desktop after a recent
restart, due to the electric being switched off, I saw that none of my
media files were mounted.
So I did a manual mount with:
sudo mount -a

and then the folders appeared.

Out of curiosity I tried 2 other ubuntu 18.04 machines, and it was
exactly the same thing, no automount.

I then googled this problem, but all I could find was how to mount a NAS
folder and how to edit the fstab file.

There has been a round of updates recently, and I wondered if it was
anything to do with that, as the NAS folders used to automount, but not
recently
Jonathan N. Little
2021-01-16 13:59:57 UTC
Permalink
Post by RobH
I haven't really noticed this before as I don't reboot my desktop very
often, and it was a while since I last did it.
When I checked my shared NAS folders on my desktop after a recent
restart, due to the electric being switched off, I saw that none of my
media files were mounted.
sudo mount -a
and then the folders appeared.
Out of curiosity I tried 2 other ubuntu 18.04 machines, and it was
exactly the same thing, no automount.
I then googled this problem, but all I could find was how to mount a NAS
folder and how to edit the fstab file.
There has been a round of updates recently, and I wondered if it was
anything to do with that, as the NAS folders used to automount, but not
recently
It can all be a matter of timing. If you desktop boots and brings up
your network before you NAS then the share will not be available. What
happen next depends on what options you used for your share in fstab. If
memory serves me you are using smb and not nfs. With nfs I use the
_netdev option but for smb shares what I did was create a shell script
that checked if the share was mounted, if not then try mounting it. I
ran it on a cronjob, for this one client I did it hourly.

The script was if you want to downloaded it at:
<http://www.littleworksstudio.com/temp/usenet/check-network-mount.sh>

else here it is, but be mindful of the wordwrap:

#!/bin/bash
# check-network-mount.sh
#+ Check if network share in fstab is mounted, if not try and mount it
#+ v0.2 Use parameter substitution to prevent TERM error when cron job
#+ v0.3 Add delay between mount attempts to prevent 'mount error(16):
Device or resource busy'

TITLE='Check Network Mounted Share'
VER=0.3
SELF=$([[ $0 = /* ]] && echo "$0" || echo "$PWD/${0#./}")
SCRIPT=$(basename ${SELF%.*})
# Prevent 'TERM environment variable not set.' error when run by cron
export TERM=${TERM:-linux}

# My standard exit codes
declare -ir SUCCESS=0
declare -ir E_FATAL=1
declare -ir E_NOT_ROOT=64
declare -ir E_BAD_ARGS=67
declare -ir E_NO_CONNECT=73

#====================== Subroutines ======================================#
version()
{
echo "$(basename $0) v$VER"
exit $SUCCESS
}

# $1 exit code
usage()
{
cat <<-EOM

$TITLE v$VER
Usage: $(basename $0) token [tries]|OPTION

$(basename $0) is a script that checks if a network share defined in
fstab
is currently mounted, if not tries to mount it.

token - a string identifier to locate share in fstab and mtab
e.g., RemoteServer:/remote/folder /mnt/MyShare 'RemoteServer'
or 'MyShare' would be a good choice
tries - optional number of attempts to mount share before quiting
default is 3
OPTION
-h, --help display this help and exit
-v, --version output version information and exit
EOM
exit $1
}

# $1 ERR_NO, $2 message
fatal_error()
{
echo -e "Error #$1: $2" >&2
usage $1
}

check_tries()
{
re='^[0-9]+$'
[[ $1 =~ $re ]] && TRIES=$1 || fatal_error $E_BAD_ARGS 'tries arg
MUST be an integer'
}

try_mounting()
{
i=1

while [ -z "$(mount | grep $TOKEN)" ]
do
echo "Try mounting network share $TOKEN"
mount -a
sleep 5
i=$((i+1))
(( i > $TRIES )) && fatal_error $E_NO_CONNECT "Mounting network
share $TOKEN failed"
done
echo "Mounted $TOKEN successfully"
}

clear
# Parse command line
case $# in
0) fatal_error $E_BAD_ARGS "Missing required argument";;
1) [[ $1 == '-h' || $1 == '--help' ]] && usage $SUCCESS
[[ $1 == '-v' || $1 == '--version' ]] && version
TOKEN=$1; TRIES=3
;;
2) TOKEN=$1; check_tries $2
;;
*) fatal_error $E_BAD_ARGS "Invalid number of arguments";;
esac

# Need to be root for this script to work
[ "$(id -u)" == "0" ] || fatal_error $E_NOT_ROOT "Must be root to run
script use:\nsudo $0\n"

# 1st make sure token finds share defined in fstab
grep $TOKEN /etc/fstab > /dev/null || fatal_error $E_BAD_ARGS "Network
share not found in /etc/fstab matching token: $TOKEN"

# 2nd see if share is mounted else try remounting
[ -n "$(mount | grep $TOKEN)" ] && echo "Network share $TOKEN is
currently mounted" || try_mounting
--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
RobH
2021-01-16 17:16:33 UTC
Permalink
Post by Jonathan N. Little
Post by RobH
I haven't really noticed this before as I don't reboot my desktop very
often, and it was a while since I last did it.
When I checked my shared NAS folders on my desktop after a recent
restart, due to the electric being switched off, I saw that none of my
media files were mounted.
sudo mount -a
and then the folders appeared.
Out of curiosity I tried 2 other ubuntu 18.04 machines, and it was
exactly the same thing, no automount.
I then googled this problem, but all I could find was how to mount a NAS
folder and how to edit the fstab file.
There has been a round of updates recently, and I wondered if it was
anything to do with that, as the NAS folders used to automount, but not
recently
It can all be a matter of timing. If you desktop boots and brings up
your network before you NAS then the share will not be available. What
happen next depends on what options you used for your share in fstab. If
memory serves me you are using smb and not nfs. With nfs I use the
_netdev option but for smb shares what I did was create a shell script
that checked if the share was mounted, if not then try mounting it. I
ran it on a cronjob, for this one client I did it hourly.
<http://www.littleworksstudio.com/temp/usenet/check-network-mount.sh>
#!/bin/bash
# check-network-mount.sh
#+ Check if network share in fstab is mounted, if not try and mount it
#+ v0.2 Use parameter substitution to prevent TERM error when cron job
Device or resource busy'
TITLE='Check Network Mounted Share'
VER=0.3
SELF=$([[ $0 = /* ]] && echo "$0" || echo "$PWD/${0#./}")
SCRIPT=$(basename ${SELF%.*})
# Prevent 'TERM environment variable not set.' error when run by cron
export TERM=${TERM:-linux}
# My standard exit codes
declare -ir SUCCESS=0
declare -ir E_FATAL=1
declare -ir E_NOT_ROOT=64
declare -ir E_BAD_ARGS=67
declare -ir E_NO_CONNECT=73
#====================== Subroutines ======================================#
version()
{
echo "$(basename $0) v$VER"
exit $SUCCESS
}
# $1 exit code
usage()
{
cat <<-EOM
$TITLE v$VER
Usage: $(basename $0) token [tries]|OPTION
$(basename $0) is a script that checks if a network share defined in
fstab
is currently mounted, if not tries to mount it.
token - a string identifier to locate share in fstab and mtab
e.g., RemoteServer:/remote/folder /mnt/MyShare 'RemoteServer'
or 'MyShare' would be a good choice
tries - optional number of attempts to mount share before quiting
default is 3
OPTION
-h, --help display this help and exit
-v, --version output version information and exit
EOM
exit $1
}
# $1 ERR_NO, $2 message
fatal_error()
{
echo -e "Error #$1: $2" >&2
usage $1
}
check_tries()
{
re='^[0-9]+$'
[[ $1 =~ $re ]] && TRIES=$1 || fatal_error $E_BAD_ARGS 'tries arg
MUST be an integer'
}
try_mounting()
{
i=1
while [ -z "$(mount | grep $TOKEN)" ]
do
echo "Try mounting network share $TOKEN"
mount -a
sleep 5
i=$((i+1))
(( i > $TRIES )) && fatal_error $E_NO_CONNECT "Mounting network
share $TOKEN failed"
done
echo "Mounted $TOKEN successfully"
}
clear
# Parse command line
case $# in
0) fatal_error $E_BAD_ARGS "Missing required argument";;
1) [[ $1 == '-h' || $1 == '--help' ]] && usage $SUCCESS
[[ $1 == '-v' || $1 == '--version' ]] && version
TOKEN=$1; TRIES=3
;;
2) TOKEN=$1; check_tries $2
;;
*) fatal_error $E_BAD_ARGS "Invalid number of arguments";;
esac
# Need to be root for this script to work
[ "$(id -u)" == "0" ] || fatal_error $E_NOT_ROOT "Must be root to run
script use:\nsudo $0\n"
# 1st make sure token finds share defined in fstab
grep $TOKEN /etc/fstab > /dev/null || fatal_error $E_BAD_ARGS "Network
share not found in /etc/fstab matching token: $TOKEN"
# 2nd see if share is mounted else try remounting
[ -n "$(mount | grep $TOKEN)" ] && echo "Network share $TOKEN is
currently mounted" || try_mounting
After a further check and reboot of 2 ubuntu machines whilst the NAS
server was up and running, both machine did mount the NAS shared folders.
So fstab does automount them and I was mistaken. This was because as you
suggested the NAS server was not up and running first on the day in
question.
Must make a mental note in future to check the NAS server is running
before I start up the other machines after being powered off.

Thanks
Jonathan N. Little
2021-01-16 23:11:06 UTC
Permalink
Post by RobH
After a further check and reboot of 2 ubuntu machines whilst the NAS
server was up and running, both machine did mount the NAS shared folders.
So fstab does automount them and I was mistaken. This was because as you
suggested the NAS server was not up and running first on the day in
question.
Must make a mental note in future to check the NAS server is running
before I start up the other machines after being powered off.
That why I used the script. If the share is not up, the script will
mount it automatically once the server is online.
--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Chris Elvidge
2021-01-16 15:02:57 UTC
Permalink
Post by RobH
I haven't really noticed this before as I don't reboot my desktop very
often, and it was a while since I last did it.
When I checked my shared NAS folders on my desktop after a recent
restart, due to the electric being switched off, I saw that none of my
media files were mounted.
sudo mount -a
and then the folders appeared.
Out of curiosity I tried 2 other ubuntu 18.04 machines, and it was
exactly the same thing, no automount.
I then googled this problem, but all I could find was how to mount a NAS
folder and how to edit the fstab file.
There has been a round of updates recently, and I wondered if it was
anything to do with that, as the NAS folders used to automount, but not
recently
Have you tried using the _netdev parameter in the 4th field of fstab
(fs_mntops)?
--
Chris Elvidge
England
RobH
2021-01-16 16:03:55 UTC
Permalink
Post by Chris Elvidge
Post by RobH
I haven't really noticed this before as I don't reboot my desktop very
often, and it was a while since I last did it.
When I checked my shared NAS folders on my desktop after a recent
restart, due to the electric being switched off, I saw that none of my
media files were mounted.
sudo mount -a
and then the folders appeared.
Out of curiosity I tried 2 other ubuntu 18.04 machines, and it was
exactly the same thing, no automount.
I then googled this problem, but all I could find was how to mount a
NAS folder and how to edit the fstab file.
There has been a round of updates recently, and I wondered if it was
anything to do with that, as the NAS folders used to automount, but
not recently
Have you tried using the _netdev parameter in the 4th field of fstab
(fs_mntops)?
No I haven't. Does _netdev go at the end of the fstab line:

...........cifs credentials=/home/rob/.smbclient,uid=rob 0 0 <<<< here?
Jonathan N. Little
2021-01-16 23:13:43 UTC
Permalink
Post by RobH
Post by Chris Elvidge
Post by RobH
I haven't really noticed this before as I don't reboot my desktop
very often, and it was a while since I last did it.
When I checked my shared NAS folders on my desktop after a recent
restart, due to the electric being switched off, I saw that none of
my media files were mounted.
sudo mount -a
and then the folders appeared.
Out of curiosity I tried 2 other ubuntu 18.04 machines, and it was
exactly the same thing, no automount.
I then googled this problem, but all I could find was how to mount a
NAS folder and how to edit the fstab file.
There has been a round of updates recently, and I wondered if it was
anything to do with that, as the NAS folders used to automount, but
not recently
Have you tried using the _netdev parameter in the 4th field of fstab
(fs_mntops)?
...........cifs credentials=/home/rob/.smbclient,uid=rob 0 0 <<<< here?
No, it goes in the OPTIONS section but anyway it is not applicable for
samba mounts only for NFS not CIFS
--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
Loading...