LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Laptop and Netbook
User Name
Password
Linux - Laptop and Netbook Having a problem installing or configuring Linux on your laptop? Need help running Linux on your netbook? This forum is for you. This forum is for any topics relating to Linux and either traditional laptops or netbooks (such as the Asus EEE PC, Everex CloudBook or MSI Wind).

Notices


Reply
  Search this Thread
Old 01-18-2014, 02:18 PM   #1
gallard
LQ Newbie
 
Registered: May 2008
Posts: 11

Rep: Reputation: 3
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
 
Old 01-19-2014, 07:23 PM   #2
dolphin_oracle
MX Linux
 
Registered: Dec 2013
Posts: 402

Rep: Reputation: Disabled
can your network manager run a script upon connection to a network? Might be an option (I think wicd has this sort of feature).
 
Old 01-20-2014, 12:48 AM   #3
18Googol2
Member
 
Registered: Oct 2008
Distribution: FreeBSD, RedHat, Centos, Solaris, AIX
Posts: 34

Rep: Reputation: 17
pam_mount can be what you looking for
 
Old 01-21-2014, 03:01 PM   #4
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
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?
 
Old 01-21-2014, 03:30 PM   #5
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
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.
 
1 members found this post helpful.
Old 01-24-2014, 12:07 PM   #6
gallard
LQ Newbie
 
Registered: May 2008
Posts: 11

Original Poster
Rep: Reputation: 3
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
 
1 members found this post helpful.
Old 01-25-2014, 11:49 AM   #7
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
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.
 
Old 01-25-2014, 12:01 PM   #8
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
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.

Last edited by Tadaen; 01-25-2014 at 12:12 PM.
 
Old 01-26-2014, 10:33 PM   #9
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
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
 
Old 01-27-2014, 01:09 PM   #10
gallard
LQ Newbie
 
Registered: May 2008
Posts: 11

Original Poster
Rep: Reputation: 3
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).
 
1 members found this post helpful.
Old 01-27-2014, 04:14 PM   #11
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
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.

Last edited by Tadaen; 01-27-2014 at 06:06 PM.
 
Old 01-28-2014, 07:49 AM   #12
gallard
LQ Newbie
 
Registered: May 2008
Posts: 11

Original Poster
Rep: Reputation: 3
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.
 
1 members found this post helpful.
Old 01-28-2014, 08:16 AM   #13
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
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.

Last edited by Tadaen; 01-28-2014 at 09:13 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm working with symlinks with mount command to mount remote shares lhorace Linux - Newbie 1 11-21-2009 04:05 PM
How to mount SAMBA shares in /etc/fstab automatically? Micro420 Linux - General 7 06-16-2006 05:24 AM
Automatically re-establishing remote windows shares fraz Linux - Networking 2 04-26-2006 04:59 PM
automatically mount NFS shares turbo_acura Linux - Networking 1 10-28-2004 05:33 PM
Automatically mount file shares? odious1 Linux - Newbie 1 07-03-2003 01:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Laptop and Netbook

All times are GMT -5. The time now is 02:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration