script for mounting NFS share

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
corkscrew22
Level 1
Level 1
Posts: 47
Joined: Fri Jul 04, 2014 4:02 am

script for mounting NFS share

Post by corkscrew22 »

My question (not sure if this is right forum)concerns mounting a NFS share on a network NAS.

My system info first
Kernel: 4.10.0-42-generic x86_64 (64 bit)
Desktop: Cinnamon 3.6.6 Distro: Linux Mint 18.3 Sylvia

Is it possible to write a script which will mount a NFS share and a script to unmount it?
I can manually mount the share by running this command

Code: Select all

sudo mount -t nfs 192.168.2.9:/TV /media/NASduo/NASTV
Please note I do not need any help /suggestions on mounting this via fstab entry I have tried that many different ways and the result is always the same it's "flaky" under mint 20 running Nemo 4.8.6.

So simple scripts on my desktop to mount and a script to umount is what I want
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
AndyMH
Level 21
Level 21
Posts: 13503
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: script for mounting NFS share

Post by AndyMH »

Just put your mount command in a script and wrap it in an if statement that tests for the existence of a file or folder on the mountpoint, if file present then share is mounted and unmount it, if not present then mount it. So you only need one script.

Don't want to have to type your password in every time? Then define a polkit for mount and umount and run it with

Code: Select all

pkexec mount -t nfs 192.168.2.9:/TV /media/NASduo/NASTV
You could even elaborate the script with notify-send so you get a notification on what it does each time you run it.

I have only played with nfs mounts (reverted to cifs) but didn't have any issues with mounting via fstab. A timing issue?
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
corkscrew22
Level 1
Level 1
Posts: 47
Joined: Fri Jul 04, 2014 4:02 am

Re: script for mounting NFS share

Post by corkscrew22 »

hi thanks for that,
i am a complete newbie when it comes to writing scripts, how do i do this? i have googled it a lot but i am overwhelmed with the amount of info....can you please help with this
User avatar
AndyMH
Level 21
Level 21
Posts: 13503
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: script for mounting NFS share

Post by AndyMH »

Yes, but it won't be until this evening my time, earliest.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
AndyMH
Level 21
Level 21
Posts: 13503
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: script for mounting NFS share

Post by AndyMH »

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:

Code: Select all

./mountNAS
./ 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:
Screenshot from 2021-10-18 16-23-53.png
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
corkscrew22
Level 1
Level 1
Posts: 47
Joined: Fri Jul 04, 2014 4:02 am

Re: script for mounting NFS share

Post by corkscrew22 »

AndyMH wrote: Sun Oct 17, 2021 4:45 am Just put your mount command in a script and wrap it in an if statement that tests for the existence of a file or folder on the mountpoint, if file present then share is mounted and unmount it, if not present then mount it. So you only need one script.

Don't want to have to type your password in every time? Then define a polkit for mount and umount and run it with

Code: Select all

pkexec mount -t nfs 192.168.2.9:/TV /media/NASduo/NASTV
You could even elaborate the script with notify-send so you get a notification on what it does each time you run it.

I have only played with nfs mounts (reverted to cifs) but didn't have any issues with mounting via fstab. A timing issue?

Hi
thanks for the time you have spent on this will take me some time to digest and figure out, since i have never written a script in my life.
to answer your query on fstab i used this because of timing issues

Code: Select all

 nfs	noauto,x-systemd.automount,x-systemd.mount-timeout=10s 
User avatar
AndyMH
Level 21
Level 21
Posts: 13503
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: script for mounting NFS share

Post by AndyMH »

You might try x-systemd.after=network-online.target instead of x-systemd.mount-timeout=10s and see if it makes any difference, simpler than the script.
since i have never written a script in my life.
Thats why I did it in stages, you will probably make some syntax errors along the way.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
corkscrew22
Level 1
Level 1
Posts: 47
Joined: Fri Jul 04, 2014 4:02 am

Re: script for mounting NFS share

Post by corkscrew22 »

AndyMH wrote: Mon Oct 18, 2021 6:08 pm You might try x-systemd.after=network-online.target instead of x-systemd.mount-timeout=10s and see if it makes any difference, simpler than the script.
since i have never written a script in my life.
Thats why I did it in stages, you will probably make some syntax errors along the way.
you got me thinking again i found this, pretty much exact same scenario and NAS as me will give it a whorl tonight
Locked

Return to “Scripts & Bash”