Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: HOWTO: Automatically mount and unmount network shares

  1. #11
    Join Date
    Dec 2006
    Beans
    678

    Re: HOWTO: Automatically mount and unmount network shares

    Quote Originally Posted by Darwin Award Winner View Post
    Your fstab-parsing code has a few errors. Most notably, parts of an fstab entry can be separated by any whitespace, not just tabs.
    Yes; however, it is possible for the 1st, 2nd, and 4th fields to contain a space (as \40, which python translates when it reads it). With lots of effort, one could still correctly identify the name and mount point, but it would add significant code. Or one could assume no spaces except between fields. Or, as I did, one could assume that since Ubuntu formats it with tabs and most people will follow that, chances are the whitespace is a tab. I wanted to allow for spaces in the fields because it is targeted toward CIFS mounts, and Windows users might well have spaces somewhere. I may post a second version that implements your approach; in the meantime I have made an explicit note on the need for tabs.

    Quote Originally Posted by Darwin Award Winner View Post
    Also, I've developed so preliminary UNTESTED code to support sshfs shares as well.
    I don't have a simple means for testing this; if it seems to be working for you over the course of a few days, I can incorporate it.

  2. #12
    Join Date
    Feb 2007
    Beans
    77

    Re: HOWTO: Automatically mount and unmount network shares

    How do you put spaces in fstab fields? You actually put a literal '\40' in the file?

    As for my needs, I have actually implemented my own solution using scripts in the /etc/network/if-(up|down).d directories. It mounts all shares when the network comes up, and unmounts them when the network goes down. The assumption in that approach is, of course, that my own network connection is the only one that ever goes up or down. This script could certainly be added to my setup, but the connections to my shares are stable enough that it would never have occasion to mount or unmount anything.

    So, if anyone else wants to test it, they can.

  3. #13
    Join Date
    Dec 2006
    Beans
    678

    Re: HOWTO: Automatically mount and unmount network shares

    Quote Originally Posted by Darwin Award Winner View Post
    How do you put spaces in fstab fields? You actually put a literal '\40' in the file?
    Yes. At least, according to a couple of resources I've found. I've never gotten around to actually testing it directly, though I have a case where it would make sense.

    Quote Originally Posted by Darwin Award Winner View Post
    As for my needs, I have actually implemented my own solution using scripts in the /etc/network/if-(up|down).d directories. It mounts all shares when the network comes up, and unmounts them when the network goes down. The assumption in that approach is, of course, that my own network connection is the only one that ever goes up or down. This script could certainly be added to my setup, but the connections to my shares are stable enough that it would never have occasion to mount or unmount anything.

    So, if anyone else wants to test it, they can.
    Sounds reasonable for your case - in mine, everything is dynamic.

  4. #14
    Join Date
    Feb 2007
    Beans
    77

    Re: HOWTO: Automatically mount and unmount network shares

    Good news, my HOWTO explaining my solution has just been approved. The two solutions are complimentary. This script can maintain the status of the shares as their respective servers become available or unavailable, and my script and immediately mount and unmount them when your own network comes up or down (for example, if your laptop has just connected to a wireless network). Useful if you are so impatient that you can't wait up to 120 seconds for the next iteration of this script to come around, or if you want to set this script's idle time much higher but still have instant access when you first connect. My script is here. The crux of the automation is a pair of scripts in the /etc/network/if-(up|down).d directories that get executed whenever the network comes up or down.

    The simplest adaptation of this script to that system would be to create the following files and make them executable:

    /etc/network/if-up.d/netfs-automount-start
    Code:
    #!/bin/sh
    
    # start the network automount script in the background
    automount &
    /etc/network/if-down.d/netfs-automount-stop
    Code:
    #!/bin/sh
    
    # stop the autmount script
    killall automount
    
    # unmount all cifs shares
    umount -a -l -t cifs
    
    # unmount all sshfs shares (only if you're using that feature)
    umount -l `grep '^sshfs#' /etc/mtab | awk '{print $2}'`
    That setup should ensure that the automount script is only running while your computer is connected to the network, and that any shares that are available when the network first comes up are mounted ASAP.

  5. #15
    Join Date
    Dec 2006
    Beans
    678

    Re: HOWTO: Automatically mount and unmount network shares

    Quote Originally Posted by Darwin Award Winner View Post
    Good news, my HOWTO explaining my solution has just been approved. The two solutions are complimentary. This script can maintain the status of the shares as their respective servers become available or unavailable, and my script and immediately mount and unmount them when your own network comes up or down (for example, if your laptop has just connected to a wireless network). Useful if you are so impatient that you can't wait up to 120 seconds for the next iteration of this script to come around, or if you want to set this script's idle time much higher but still have instant access when you first connect. My script is here. The crux of the automation is a pair of scripts in the /etc/network/if-(up|down).d directories that get executed whenever the network comes up or down.
    I agree - very complimentary. In fact, if I'm feeling creative, I may take your approach and modify it so that it only activates automount on my laptop when I'm actually using a particular network device, as I don't really need to be running automount unless I'm on the right network. In my case, that means using wireless with a static IP, so I can uniquely identify it.

  6. #16
    Join Date
    Dec 2006
    Beans
    678

    Re: HOWTO: Automatically mount and unmount network shares

    I have posted a completely new version/approach, which has many advantages and is simpler to set up. The new post is http://ubuntuforums.org/showthread.php?t=637258, but I've left this one because the new one doesn't completely replace the utility of this one.
    Last edited by tweedledee; December 21st, 2007 at 01:35 PM.

Page 2 of 2 FirstFirst 12

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
  •