Create a simple script to test if the NAS is mounted:
Code: Select all
#check to see if NAS mounted, if not mounted then mount it
#if mounted then umount it.
#https://www.cyberciti.biz/faq/howto-check-if-a-directory-exists-in-a-bash-shellscript/
#for a simple if test to the check the existence of a folder
#note spaces (or the lack of them) are important in bash
mountpt="/home/andy/nasmount" #folder where the NAS mounts
myNAS="/home/andy/nasmount/backups" #the folder we are going to test for
if [ -d $myNAS ]; then
echo "Folder exists so NAS is mounted."
else
echo "Error: folder does not exist so NAS not mounted."
fi
exit
In bash,
myvar=something
assigns something to the variable myvar, then
$myvar
means the contents of. Specify your mount point and the folder on the NAS you are going to check. I've set this up so I can check it works connecting to my synology NAS, you will need to change as appropriate.
Save and set the script executable - in your file manager right click on it and permissions tab and check the 'allow executing...' box. I saved it as mountNAS, save it as whatever you want.
Open a terminal in the folder where you have the script and execute it with:
./
means the current folder. you can later move it to a folder that is in
PATH
so it can be executed by just specifying the filename.
Test the script by running it, with you manually mounting the NFS share and unmounting it.
Now elaborate, add your mount commands to the script:
Code: Select all
#check to see if NAS mounted, if not mounted then mount it
#if mounted then umount it.
#https://www.cyberciti.biz/faq/howto-check-if-a-directory-exists-in-a-bash-shellscript/
#for a simple if test to the check the existence of a folder
#note spaces (or the lack of them) are important in bash
mountpt="/home/andy/nasmount" #folder where the NAS mounts
myNAS="/home/andy/nasmount/backup" #the folder we are going to test for
if [ -d $myNAS ]; then
echo "Folder exists so NAS is mounted."
sudo umount -vvv $mountpt
else
echo "Error: folder does not exist so NAS not mounted."
sudo mount -t nfs -vvv diskstation.local:/volume1/homes/ $mountpt -o vers=3,nofail,rw,user
fi
exit
and test again. I've got the vv options on mount/umount to get verbose output. Now it is asking for your pwd.
Now lets make it a little prettier, replace the echo commands with notify-send. This will give you a notification on your desktop telling you what it has done.
Code: Select all
#check to see if NAS mounted, if not mounted then mount it
#if mounted then umount it.
#https://www.cyberciti.biz/faq/howto-check-if-a-directory-exists-in-a-bash-shellscript/
#for a simple if test to the check the existence of a folder
#note spaces (or the lack of them) are important in bash
mountpt="/home/andy/nasmount" #folder where the NAS mounts
myNAS="/home/andy/nasmount/backup" #the folder we are going to test for
if [ -d $myNAS ]; then
notify-send "This is the title" "NAS has been unmounted"
sudo umount -vvv $mountpt
else
notify-send "This is the title" "NAS has been mounted"
sudo mount -t nfs -vvvv diskstation.local:/volume1/homes/ $mountpt -o vers=3,nofail,rw,user
fi
exit
test again.
Now we need to get rid of the requirement to enter your password, replace sudo with pkexec.
Code: Select all
#check to see if NAS mounted, if not mounted then mount it
#if mounted then umount it.
#https://www.cyberciti.biz/faq/howto-check-if-a-directory-exists-in-a-bash-shellscript/
#for a simple if test to the check the existence of a folder
#note spaces (or the lack of them) are important in bash
mountpt="/home/andy/nasmount" #folder where the NAS mounts
myNAS="/home/andy/nasmount/backup" #the folder we are going to test for
if [ -d $myNAS ]; then
notify-send "This is the title" "NAS has been unmounted"
pkexec umount -vvv $mountpt
else
notify-send "This is the title" "NAS has been mounted"
pkexec mount -t nfs -vvvv diskstation.local:/volume1/homes/ $mountpt -o vers=3,nofail,rw,user
fi
exit
test again, it's still asking for a password, that's because we need to define a policy for mount and umount that says we don't need one.
Create a file in
/usr/share/polkit-1/actions
with the following content (you need to be root to do this):
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<vendor>My NAS Project</vendor>
<vendor_url>http://whateveryouwant.org/</vendor_url>
<icon_name>mount</icon_name>
<action id="com.mynas.mount">
<description gettext-domain="mount">Run mount as root</description>
<message gettext-domain="gparted">Authentication is required to run mount as root</message>
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/mount</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">false</annotate>
</action>
<icon_name>umount</icon_name>
<action id="com.mynas.umount">
<description gettext-domain="umount">Run umount as root</description>
<message gettext-domain="foxclone">Authentication is required to run umount as root</message>
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/umount</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">false</annotate>
</action>
</policyconfig>
Save it as whatever you want, I tend to follow the naming convention for other files in the folder, e.g.
com.mynas.mount.policy
.
There are better ways of doing this by creating a local policy, but don't know how to do this. I learnt how to do it by building my own isos.
Important - the polkit assumes you have been usrmerged*. If you have installed LM20.2 from scratch you have, if you have have installed an earlier version (and maybe upgraded) you probably haven't. Check in your file manager, if bin is a link then the above is okay:
If not, then change /usr/bin/mount
to /bin/mount
in the polkit above, repeat for umount.
Test again, you should not be prompted for a password. If you get permission issues trying to view your nas via the file manager the problem probably lies with the NFS settings on the NAS - you are on your own for that one.
If it all works, move the script into a folder in
PATH
(if you create the folder
/home/you/bin
, it is automatically added to
PATH
) so it can be executed with just the filename. You can now add it to your startup applications and/or create a launcher on the desktop pointing at it.
Note - in your original post you said you didn't want any help with fstab options because mounting via fstab was 'flaky'. This could be as simple as the network not being properly up at the time fstab is executed. Did you try any of the options here to delay the mount:
https://unix.stackexchange.com/question ... or-network
might remove the need for all of the above.
* https://www.freedesktop.org/wiki/Softwa ... eUsrMerge/
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0