LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Laptop and Netbook (https://www.linuxquestions.org/questions/linux-laptop-and-netbook-25/)
-   -   Best way to mount remote shares automatically? (https://www.linuxquestions.org/questions/linux-laptop-and-netbook-25/best-way-to-mount-remote-shares-automatically-4175491733/)

gallard 01-18-2014 02:18 PM

Best way to mount remote shares automatically?
 
When I boot my laptop, my remote shares are mounted automatically by fstab.
But I rarely reboot my laptop.
When I leave my local network area, shares are unmounted automatically.
When I come back, I reconnect using a manual mount command.
That works fine but I would like to know if there is a way to automatically reconnect the shares?
Thanks
Using Mageia3 with KDE

dolphin_oracle 01-19-2014 07:23 PM

can your network manager run a script upon connection to a network? Might be an option (I think wicd has this sort of feature).

18Googol2 01-20-2014 12:48 AM

pam_mount can be what you looking for :)

Tadaen 01-21-2014 03:01 PM

I'm very new to scripting but maybe a script that checks if the shares are there or not, if they are it mounts them, if not then it just exits. Put it on roots crontab every... 5 minutes?

Tadaen 01-21-2014 03:30 PM

First function I've made with a bit of googling to find the command to use.

Code:

#!/bin/bash
#Check if mounted & mount if not from /etc/fstab
#Tadaen Sylvermane
#Created 2014/1/21

if mountpoint -q /path/to/mount ; then
        exit 1
else
        mount -a
        exit 1
fi

Now that I've made this I will likely use it in the near future here and just have it run as a root crontab every couple mins.

gallard 01-24-2014 12:07 PM

Variation on a theme
 
Thanks Tadaen
mountpoint is a good avenue.
Your solution works fine but it may fill your log with useless messages when you're not connected to server.
I propose:
Code:

SRV=tatata
MNT=mount_point
if mountpoint -q $MNT; then
  exit 1; # already mounted
elif ping -c 1 $SRV >/dev/null; then
  mount $MNT # we assume it is defined in fstab
  exit 2; # mount
else
  exit 3; # server unreachable
fi

or, if you prefer brevity:
Code:

SRV=tatata
MNT=mount_point
if ! mountpoint -q $MNT && ping -c 1 $SRV>/dev/null; then mount $MNT; fi


Tadaen 01-25-2014 11:49 AM

I am very new to scripting. I like your idea better. As far as the log is concerned I could probably alter mine to run once with a while statement and just have it sleep 300 or so. Shouldn't fill the log up then I would imagine. I don't know enough though. I will test it. Yours is more elegant though from what little I do know.

Tadaen 01-25-2014 12:01 PM

Code:

#!/bin/bash
#Check if mounted & mount if not from /etc/fstab
#Add to boot list as root.
#Tadaen Sylvermane
#Created 2014/1/25

while true; do
                if mountpoint -q /path/to/mountpoint ; then
                sleep 60
        else
                mount -a
                sleep 60
        fi
done

This one works and doesn't fill the log. Set to run at boot and let it go I guess.

*EDIT* Actually looking at how easy this was to set up as a while loop I will be changing some of my other scripts so that they also don't load the log with nonsense as I have 2 that run every 30 mins and 1 that runs every 10 to backup my ramdisk.

Tadaen 01-26-2014 10:33 PM

Better still. Will run every 5 minutes. This requires the shares to be in the /etc/fstab.

Code:

#!/bin/bash
#Runtime Script
#Tadaen Sylvermane
#Last Edit 2014/1/26

#Variables#

MNTPT=/path/to/mount

#####Begin Script#####

mountshares() {
        if mountpoint -q $MNTPT ; then
                return 0
        else
                mount -a
        fi
}

while true
do
        mountshares
        sleep 300
done


gallard 01-27-2014 01:09 PM

Hi Tadaen
OK! Let's suppose you're out of the office and your server is not reachable.
Every 5 minutes, you will attempt a mount and an error message will be logged.
That's what I meant by "fill with useless message".
Would be better to check for reachabibity before you call mount. You can do it with "ping -c 1".

Next, suppose you have 2 servers, possibly on two different networks (or in 2 offices).
One being reachable does not mean the other is.
So your "mount -a" will generate useless messages for the unreachable server.

Conclusions
- do a ping before you attempt mounting of remote ressources. Only attempt mounting if server is reachable.
- do it "per server" and do not use "mount -a"
As an exercise you may try to write a function with 2 arguments (server_address and mount_point).

Tadaen 01-27-2014 04:14 PM

i got it backwards you are correct. looking into it now.

*EDIT* Ok for samba, and mounting as user not root. Lines like this in the /etc/fstab. I'm guessing this could be modified for just a certain user so no one else can mount them but I'm done googling for now.

Code:

//ServerIP/Share  /mount/point  cifs  credentials=/home/youruser/creds,noauto,user  0  0
Then this... I don't know if it's sloppy or what but I do know that it works. Runs every 5 minutes. Does not spam log (unless it logs pings at which point I give up).

Code:

#!/bin/bash
#Automount Network Shares
#Tadaen Sylvermane
#Last Edit 2014/1/27

#Variables#

##Server 1

SRV1IP=ip
SRV1MNT=mountpoint
SRV1=//ip/share

#####Begin Script#####

automount()
{       
##Server 1
        if ping -c 1 $SRV1IP ; then
                if mountpoint -q $SRV1MNT ; then
                        return 0
                else
                        mount $SRV1
                fi
        else                       
                return 0
        fi
}

while true
do
        automount
        sleep 300
done

#####End Script#####

Copy paste and change variables accordingly to add servers.

*EDIT* Thank you for the suggestions and somewhat challenge. I am trying to learn as fast as I can.

gallard 01-28-2014 07:49 AM

Variation on a theme
 
Hi!
when a share is declared in /etc/fstab then mount can be called either with the mount_point or the share_path
This enable you to reduce the number of args to 2

Code:

# mymount()
# 1st arg: server address (name or ip)
# 2nd arg: local mount point
mymount() {
  if ! mountpoint -q $2 then
    if ping -c 1 $1 >/dev/null; then
      mount $2
    fi
  fi
}

while true
do
  mymount ip1 mountpoint1
  mymount ip2 mountpoint2

  sleep 300
done

The 300 seconds delay can be reduced to a low value since there's nomore useless messages in log (and, by the way, ping do not write into system log)
Also, I did put the mountpoint call in front of ping because mountpoint is a lighter and faster system call. But it would also work the other way around.

Tadaen 01-28-2014 08:16 AM

And now I think I understand positional parameters. I learn by example. Thank you much.

Also didn't know until now that you could mount an /etc/fstab entry by the mountpoint and not the device / location you are mounting from.


All times are GMT -5. The time now is 09:35 PM.