Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: HOWTO: Automatically mount and unmount network shares

  1. #1
    Join Date
    Dec 2006
    Beans
    678

    HOWTO: Automatically mount and unmount network shares

    Here is a script you can run in the background that will automatically detect your network shares as they dynamically come and go from the network, and respond accordingly (i.e., mounting them when the networked connection is available, and unmount it when it disappears). I find this useful for my laptop (which travels) and my desktop (which doesn't, but connects to several other computers that may or may not be on/present at any moment).

    You may an alternative version of this approach more helpful for most applications: http://ubuntuforums.org/showthread.php?t=637258

    First requirement: working network connections. For SAMBA, see either of these links:
    http://doc.gwos.org/index.php/HowToM...resPermanently
    http://www.ubuntuforums.org/showthread.php?t=280473

    NOTE: you must have the connections in your fstab, and you MUST be using "cifs" in place of "smbfs" (I find cifs more reliable anyway - all you need to do is replace instances of "smbfs" with "cifs," they are installed together). Using smbfs will prevent you from being able to unmount. If you happen to be working in a pure *nix environment and can use NFS, this will probably work, but I've not tried it. This script will completely ignore shares not listed in /etc/fstab, meaning it will not interfere with anything else you happen to be doing. Your fstab line must include the "user" option, and should look something like the following:
    Code:
    //IPADDRESS	/media/MOUNT	cifs	auto,user,credentials=/etc/.credentials1,rw,uid=1000,umask=000	0	0
    Also note that if you use a credentials file, as I am here, it must be readable by your account (not just root, as suggested by the above links).
    Note that your fields in fstab must be separated by tabs, not spaces.

    Second requirement: mount.cifs & umount.cifs must be SUID:
    Code:
    sudo chmod +s /sbin/mount.cifs
    sudo chmod +s /sbin/umount.cifs
    Again, if using NFS, there is probably an equivalent requirement, but I've not tested.

    Third requirement: copy this script into a file (e.g., "automount"):
    Code:
    #!/usr/bin/python
    
    '''
    This script assumes that all the network shares in /etc/fstab are accessible
    to the current user.  In addition, the shares need to be CIFS (not SMBFS) in
    order to correctly allow unmounting by the user in the event the share is
    disconnected.  /sbin/mount.cifs & /sbin/umount.cifs must be suid root.
    
    This periodically reads mtab check the status of nework shares,
    and unmounts recently inactive shares.  All inactive shares are then issued a
    ping.  Responsive systems are then mounted.
    
    Only shares listed in fstab are handled; any others shown by mtab are ignored.
    '''
    
    Delay = 120 # number of seconds between checks
    
    import os
    import time
    
    def CheckStatus(Share):
        '''Check mtab to determine if a share is mounted.'''
        Mtab = os.popen('grep \'^//\' /etc/mtab', 'r').readlines() # get mounted shares
        for Line in Mtab:
            if Share in Line: return True
        return False
        
    Shares = {} # dictionary of IP/name : mountpoint
    
    # Read /etc/fstab to identify permanent shares
    fstab = open('/etc/fstab', 'r')
    for EachLine in fstab:
        Line = EachLine.strip()
        if Line[:2] == '//': # then reference to share
            Parts = Line.split('\t')
            Path = Parts[0].split('/')
            Shares[Path[2]] = Parts[1]# 3rd piece is name b/c starts with '//'
    fstab.close()
    
    # Check status of all shares, every Delay seconds
    while True:
        LastTime = time.time() # mark for next time around
        for ShareName, ShareMount in Shares.iteritems():
            if CheckStatus(ShareMount): # this share is mounted, check status
                if os.system('ping -c 1 %s' % (ShareName)) != 0: # ping failed
                    os.system('umount -l %s' % (ShareMount)) # unmount
            else: # share is not mounted, check to see if should mount
                if os.system('ping -c 1 %s' % (ShareName)) == 0: # ping responded
                    os.system('mount %s' % (ShareMount))
        NumSecs = max(0, Delay - (time.time() - LastTime)) # Delay minus elapsed time
        time.sleep(NumSecs) # sleep until next round of pings
    Then:
    Code:
    chmod 755 automount
    sudo cp automount /usr/local/bin
    You can now add "automount" to your programs that start upon login, and your network shares will dynamically appear and disappear.

    It is probably possible to do all this with a shell script, but I'm more comfortable with python for something this sophisticated; feel free to post any simpler alternatives. Also let me know if you encounter problems or get it working with NFS and I'll update accordingly.

    Final note about using cifs vs. smbfs: as per this bug (https://launchpad.net/ubuntu/+source...nit/+bug/42121, with duplicate and more information at https://launchpad.net/ubuntu/+source...it/+bug/49695), you may encounter a delay in shutting down while unmounting fails. I see this on some of my computers, but other than an extra 60 seconds in the shutdown, it's not a problem (and if nothing is mounted, it won't happen). (See post # 7 for the a way around this problem.) Also, you may encounter a problem (again, really just a delay) with mounting during boot, in which case you can just change your fstab entries to "noauto" (the script will mount the shares as soon as you log in anyway).
    Last edited by tweedledee; December 21st, 2007 at 01:34 PM. Reason: Updates

  2. #2
    Join Date
    Mar 2007
    Beans
    37

    Re: HOWTO: Automatically mount and unmount network shares

    Thanks; this seems to be working fine for me. BTW, I put "user" and "noauto" in my fstab line for the cifs share in order to use this script. What I can't figure out is why it didn't just work as expected without the "noauto." In that case, the boot process waits for a minute or two for the filesystem to mount, then goes on when it fails, though I haven't found any error messages to indicate why it failed. I'm guessing it's a name lookup failure or something, but without any error messages, it's impossible to know.

  3. #3
    Join Date
    Dec 2006
    Beans
    678

    Re: HOWTO: Automatically mount and unmount network shares

    Quote Originally Posted by JonathanRRogers View Post
    Thanks; this seems to be working fine for me. BTW, I put "user" and "noauto" in my fstab line for the cifs share in order to use this script. What I can't figure out is why it didn't just work as expected without the "noauto." In that case, the boot process waits for a minute or two for the filesystem to mount, then goes on when it fails, though I haven't found any error messages to indicate why it failed. I'm guessing it's a name lookup failure or something, but without any error messages, it's impossible to know.
    I'm glad it's working, and thanks for the feedback - "user" is essential for this to work, the "noauto" is possibly related to another bug with the cifs unmounting process during shutdown. I've updated my original post to include this information.

  4. #4
    Join Date
    Oct 2006
    Location
    India, Chennai, Mylapore
    Beans
    268
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: HOWTO: Automatically mount and unmount network shares

    Hi Tweedledee11
    I have some doubts on:
    "//IPADDRESS /media/MOUNT cifs auto,user,credentials=/etc/.credentials1,rw,uid=1000,umask=000 0 0"

    What does "rw,uid=1000,umask=000 0 0"" stand for?
    How does it operate and what are the other options?
    Could you please point me to a link where these details might be available?
    Thank you
    Sincerely Abhi Kalyan.
    Regards,
    Abhi Kalyan
    http://www.kgsplus.com

  5. #5
    Join Date
    Oct 2006
    Location
    India, Chennai, Mylapore
    Beans
    268
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: HOWTO: Automatically mount and unmount network shares

    My problem and how you helped solve it!
    Problem:
    I had remotely mounted folders with NTFS security installed on them.
    The major problem that i was facing was that when i create or modify a file the file permission set them to be owned by the "root", so i could not delete ot alter it from my account.

    This was the former lines on the fstab
    //server/home /media/remotedrive cifs credentials=/root/.pwfile

    After your HOWTO i had changed it to
    //server/home /media/remotedrive cifs credentials=/root/.pwfile,rw,uid=1000,umask=000 0 0

    And it did the job!!! "VIOLA"

    Now i have access right of th user defined in the password file.

    It would be great to know why it did not work initially and why it works now, That why i had asked for some link from where i could get the details regarding the change you had suggested,
    Thank you You have helped me a full Load.
    Thank you once again
    Sincerely Abhi Kalyan
    Regards,
    Abhi Kalyan
    http://www.kgsplus.com

  6. #6
    Join Date
    Dec 2006
    Beans
    678

    Re: HOWTO: Automatically mount and unmount network shares

    Quote Originally Posted by Abhi Kalyan View Post
    Hi Tweedledee11
    I have some doubts on:
    "//IPADDRESS /media/MOUNT cifs auto,user,credentials=/etc/.credentials1,rw,uid=1000,umask=000 0 0"

    What does "rw,uid=1000,umask=000 0 0"" stand for?
    How does it operate and what are the other options?
    Could you please point me to a link where these details might be available?
    Thank you
    Sincerely Abhi Kalyan.
    A very brief explanation can be found here: http://www.humbug.org.au/talks/fstab...structure.html. A more detailed one (that answers the "other options" question) is here: http://en.wikipedia.org/wiki/Fstab. An even shorter explanation:
    rw means the read/write access to mount the drive with (rw means full read & write).
    uid is the user ID, that should be yours (1000 is the uid of the 1st user in Ubuntu).
    umask refers to the permissions to create files with ("man umask" in a terminal for more details), 000 basically means no effect.
    The two 0 mean do not flag the system for backing up and don't check the drive with fsck (neither of which you probably want for a network drive).

    At a guess, I'd say it was probably the UID that solved your "root" problem, as it meant the drive was mounted as "you" and not "root."

  7. #7
    Join Date
    Dec 2006
    Beans
    678

    Re: HOWTO: Automatically mount and unmount network shares

    I've found a solution to the log delay in shutting down with CIFS (and NFS and others) shares mounted. Create a file ~/.xsession and add the following lines (on the off-chance you already have such a file, anything before the "gnome-session" line will run before gnome starts; anything after it will run when the user logs out):
    Code:
    gnome-session
    killall automount
    umount -l /SHARE
    Replacing automount with whatever name you actually gave the script, and adding as many umount lines as necessary for each share (in my case, I have 5 that are /media/computername). Updated original post to reflect this.

  8. #8
    Join Date
    Feb 2007
    Beans
    77

    Re: HOWTO: Automatically mount and unmount network shares

    This looks like an interesting script. I am interested in extending it to other network filesystems, specifically sshfs, but I need to know one thing: does this script rely on shares that lose their connection to automatically unmount? Because sshfs does not necessarily do that.

    Also, a suggestion: using df to get the list of mounted shares is kind of roundabout, and since df allows certain filesystem types to be filtered out of its output, using it is not guaranteed to work. It's probably better just to grep through /etc/mtab directly, something like this:
    Code:
    grep '^//' /etc/mtab
    The output of that should be all lines in mtab that begin with two slashes.

    So you could rewrite your CheckStatus function something like this:
    Code:
    def CheckStatus():
        Returns = [] 
        Pipe = os.popen('grep "^//" /etc/mtab', 'r') # issue grep
        for EachLine in Pipe.readlines():
            ... # no more need to filter lines in here
        Pipe.close() 
        return Returns

  9. #9
    Join Date
    Dec 2006
    Beans
    678

    Re: HOWTO: Automatically mount and unmount network shares

    Quote Originally Posted by Darwin Award Winner View Post
    This looks like an interesting script. I am interested in extending it to other network filesystems, specifically sshfs, but I need to know one thing: does this script rely on shares that lose their connection to automatically unmount? Because sshfs does not necessarily do that.
    Right now it decides whether or not to unmount based solely on the response to a ping. I/you could add another criterea if that's not quite what you have in mind.

    Quote Originally Posted by Darwin Award Winner View Post
    Also, a suggestion: using df to get the list of mounted shares is kind of roundabout, and since df allows certain filesystem types to be filtered out of its output, using it is not guaranteed to work. It's probably better just to grep through /etc/mtab directly, something like this:
    Code:
    grep '^//' /etc/mtab
    You are entirely correct. In addition, df has the disadvantage of being slow, so it's possible for the status to change between checks. I've been meaning to clean this up for a while, your message proved the kick I need. The considerably simpler and somewhat more reliable version is now up.

  10. #10
    Join Date
    Feb 2007
    Beans
    77

    Re: HOWTO: Automatically mount and unmount network shares

    Your fstab-parsing code has a few errors. Most notably, parts of an fstab entry can be separated by any whitespace, not just tabs. Try this:
    Code:
    for EachLine in fstab: 
        Line = EachLine.strip()
        if Line.startswith('//'): # then reference to share 
            Parts = Line[1:].split() 
            Shares[Parts[0]] = Parts[1]
    Also, I've developed so preliminary UNTESTED code to support sshfs shares as well. First change the grep command in CheckStatus:
    Code:
    Mtab = os.popen('grep \'^//\' /etc/mtab', 'r').readlines() # get mounted shares
    Then, add a stanza in the fstab-parsing loop to parse sshfs entries:
    Code:
    import re
    
    for EachLine in fstab: 
        Line = EachLine.strip()
        if Line.startswith('//'): # then reference to share 
            Parts = Line[1:].split() 
            Shares[Parts[0]] = Parts[1]
        elif Line.startswith('sshfs#'):
            Parts = Line.split() 
            hostmatch = re.search('[#@]([A-Za-z0-9\-.]+):', Parts[0])
            if not hostmatch:
                continue # skip bad entries
            host = hostmatch.group(0)
            Shares[host] = Parts[1]
    Since this code is untested, I make no guarantees, except that I guarantee that it will NOT work unless you have set up passwordless ssh logins.
    Last edited by Darwin Award Winner; May 3rd, 2007 at 03:34 AM. Reason: Adding sshfs code

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •