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

Thread: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no errors!

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Beans
    16

    Arrow [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no errors!

    Updated: 12/12/09
    I've found that the following works on Ubuntu 9.10 Karmic while using wireless and WPA2 on my laptop:

    After hours of beating my head against the wall trying to figure out how to get rid of those time-consuming CIFS VFS errors during shutdown, I cobbled together this (ugly) fix.

    I take no credit for this script. I found it here. I simply put two and two together, as I guessed this script may execute before Network Manager is killed. I just plugged in the umountnfs.sh portion.
    • Save the script somewhere safe. I saved it in ~/Scripts, and gave it root-only permissions.
    • sudo chmod +x /path/to/script/scriptname
    • Add the script to your Start-up Applications. (System > Preferences > Startup Applications)

    Code:
    #!/usr/bin/env python
    
    #Author: Seamus Phelan
    
    #This program runs a custom command/script just before gnome shuts 
    #down.  This is done the same way that gedit does it (listening for 
    #the 'save-yourself' event).  This is different to placing scipts 
    #in /etc/rc#.d/ as the script will be run before gnome exits.
    #If the custom script/command fails with a non-zero return code, a 
    #popup dialog box will appear offering the chance to cancel logout
    #
    #Usage: 1 - change the command in the 'subprocess.call' in 
    #           function 'session_save_yourself' below to be what ever
    #           you want to run at logout.
    #       2 - Run this program at every gnome login (add via menu System 
    #           -> Preferences -> Session)
    # 
    #
    
    import sys
    import subprocess
    import datetime
    
    import gnome
    import gnome.ui
    import gtk
    
    
    class Namespace: pass
    ns = Namespace()
    ns.dialog = None
    
    
    def main():
        prog = gnome.init ("gnome_save_yourself", "1.0", gnome.libgnome_module_info_get(), sys.argv, [])
        client = gnome.ui.master_client()
        #set up call back for when 'logout'/'Shutdown' button pressed
        client.connect("save-yourself", session_save_yourself)
        client.connect("shutdown-cancelled", shutdown_cancelled)
    
    
    def session_save_yourself( *args):
        #Unmount those CIFS shares!
        retcode = subprocess.call("sudo /etc/init.d/umountnfs.sh", shell=True)
        if retcode != 0:
            #command failed  
            show_error_dialog()
        return True
    
    def shutdown_cancelled( *args):
        if ns.dialog != None:
            ns.dialog.destroy()
        return True
    
    
    def show_error_dialog():
        ns.dialog = gtk.Dialog("There was a problem running your pre-shutdown script",
                               None,
                               gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                               ("There was a problem running your pre-shutdown script - continue logout", gtk.RESPONSE_ACCEPT))
        if ns.test_mode == True:
            response = ns.dialog.run()
            ns.dialog.destroy()
        else:
            #when in shutdown mode gnome will only allow you to open a window using master_client().save_any_dialog()
            #It also adds the 'Cancel logout' button
            gnome.ui.master_client().save_any_dialog(ns.dialog)
    
    
    
    #Find out if we are in test mode???
    if len(sys.argv) >=2 and sys.argv[1] == "test":
        ns.test_mode = True
    else:
        ns.test_mode = False
    
    if ns.test_mode == True:
        main()
        session_save_yourself()
    else:
        main()
        gtk.main()
    • Run in terminal:
      Code:
      sudo visudo
    • Add the following to the bottom of the file:
      Code:
      %admin ALL=NOPASSWD: /etc/init.d/umountnfs.sh


    • Press Ctrl-X to exit, press Y to save changes, and hit [Enter] to write the file.


    I hope this helps! I'm a linux noob, so apologies if my guide wasn't perfect.
    (Credit to stevo1982 for the sudo password workaround!)
    Last edited by ubradford; December 12th, 2009 at 12:13 PM. Reason: Added stevo1982's workaround.

  2. #2
    Join Date
    Jun 2009
    Location
    Leamington Spa, UK
    Beans
    32
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no error

    Thanks, ubradford. This worked for me!

    It should be possible to fix the security issue by having the script refer to a credentials file instead of putting your password in plain text in the script?

  3. #3
    Join Date
    Oct 2009
    Beans
    16

    Re: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no error

    Quote Originally Posted by Ian Ferguson View Post
    Thanks, ubradford. This worked for me!

    It should be possible to fix the security issue by having the script refer to a credentials file instead of putting your password in plain text in the script?
    I'll play around with it and try to figure out how to echo the contents of a file into sudo. I never thought of that, I'm just a noob. lol

  4. #4
    Join Date
    Jun 2009
    Location
    Leamington Spa, UK
    Beans
    32
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no error

    Just one curious thing. I tested this by clicking "Restart" in the shutdown menu, and my ststem shut down and restarted cleanly, but when I click "Shut Down" it gets as far as a blank screen with a flashing cursor, but doesn't power off as it used to.

    Also, it doesnt work when I change ownership of the script to root. I have to keep ownership of it myself.

  5. #5
    Join Date
    Oct 2009
    Beans
    16

    Re: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no error

    Quote Originally Posted by Ian Ferguson View Post
    Just one curious thing. I tested this by clicking "Restart" in the shutdown menu, and my ststem shut down and restarted cleanly, but when I click "Shut Down" it gets as far as a blank screen with a flashing cursor, but doesn't power off as it used to.

    Also, it doesnt work when I change ownership of the script to root. I have to keep ownership of it myself.
    The ownership and permissions of my script from "ls -la" are as follows:
    Code:
    -rwxr-xr-x  1 root     root       2496 2009-12-06 04:00 gnomelogout
    I've tried "Restart" and "Shut Down" from both gnome-panel and from the "Shut Down the Computer" dialogue box when pressing the power button on my machine. It is working cleanly in all situations. I do however notice that the shut-down splash screen will break from time to time and let a login prompt flash up on the screen for just a second before the machine powers off entirely.

    Maybe another script / program running during logout is conflicting somehow? Or have you played around with the order of script execution in /etc/rc0.d/ and /etc/rc6.d/ ? Maybe one of those could be causing a problem for you at shut-down? Both are just guesses, I'm by far not an expert.

    Also, it looks like shutting down from terminal with either:
    Code:
    sudo shutdown -r 0
    or
    Code:
    sudo shutdown -P 0
    will break the script. I'd have to guess that gnome never gets a chance to send out the "save-yourself" event the script is listening for when the computer is shut down in that manner.
    Last edited by ubradford; December 8th, 2009 at 06:49 AM.

  6. #6
    Join Date
    Jun 2009
    Location
    Leamington Spa, UK
    Beans
    32
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no error

    All working now. Turns out that in trying out different fixes, I'd messed up rc0.d, so that's why it wasn't shutting down properly.

    Just as well I'd already learned the lesson about making backups before editing system and config files!

    I've also found this script doesn't work when I shut down or restart from command line. I guess that must bypass the Gnome shutdown sequence.

    It occurs to me that this script would be useful if there are other tasks (e.g. backup or synchronize files) that you want to perform automatically on shutdown, but which need the network connection to still be up.

  7. #7
    Join Date
    Jan 2010
    Beans
    1

    Re: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no error

    Quote Originally Posted by ubradford View Post
    Updated: 12/12/09
    I've found that the following works on Ubuntu 9.10 Karmic while using wireless and WPA2 on my laptop:

    After hours of beating my head against the wall trying to figure out how to get rid of those time-consuming CIFS VFS errors during shutdown, I cobbled together this (ugly) fix.

    I take no credit for this script. I found it here. I simply put two and two together, as I guessed this script may execute before Network Manager is killed. I just plugged in the umountnfs.sh portion.
    • Save the script somewhere safe. I saved it in ~/Scripts, and gave it root-only permissions.
    • sudo chmod +x /path/to/script/scriptname
    • Add the script to your Start-up Applications. (System > Preferences > Startup Applications)

    Code:
    #!/usr/bin/env python
    
    #Author: Seamus Phelan
    
    #This program runs a custom command/script just before gnome shuts 
    #down.  This is done the same way that gedit does it (listening for 
    #the 'save-yourself' event).  This is different to placing scipts 
    #in /etc/rc#.d/ as the script will be run before gnome exits.
    #If the custom script/command fails with a non-zero return code, a 
    #popup dialog box will appear offering the chance to cancel logout
    #
    #Usage: 1 - change the command in the 'subprocess.call' in 
    #           function 'session_save_yourself' below to be what ever
    #           you want to run at logout.
    #       2 - Run this program at every gnome login (add via menu System 
    #           -> Preferences -> Session)
    # 
    #
    
    import sys
    import subprocess
    import datetime
    
    import gnome
    import gnome.ui
    import gtk
    
    
    class Namespace: pass
    ns = Namespace()
    ns.dialog = None
    
    
    def main():
        prog = gnome.init ("gnome_save_yourself", "1.0", gnome.libgnome_module_info_get(), sys.argv, [])
        client = gnome.ui.master_client()
        #set up call back for when 'logout'/'Shutdown' button pressed
        client.connect("save-yourself", session_save_yourself)
        client.connect("shutdown-cancelled", shutdown_cancelled)
    
    
    def session_save_yourself( *args):
        #Unmount those CIFS shares!
        retcode = subprocess.call("sudo /etc/init.d/umountnfs.sh", shell=True)
        if retcode != 0:
            #command failed  
            show_error_dialog()
        return True
    
    def shutdown_cancelled( *args):
        if ns.dialog != None:
            ns.dialog.destroy()
        return True
    
    
    def show_error_dialog():
        ns.dialog = gtk.Dialog("There was a problem running your pre-shutdown script",
                               None,
                               gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                               ("There was a problem running your pre-shutdown script - continue logout", gtk.RESPONSE_ACCEPT))
        if ns.test_mode == True:
            response = ns.dialog.run()
            ns.dialog.destroy()
        else:
            #when in shutdown mode gnome will only allow you to open a window using master_client().save_any_dialog()
            #It also adds the 'Cancel logout' button
            gnome.ui.master_client().save_any_dialog(ns.dialog)
    
    
    
    #Find out if we are in test mode???
    if len(sys.argv) >=2 and sys.argv[1] == "test":
        ns.test_mode = True
    else:
        ns.test_mode = False
    
    if ns.test_mode == True:
        main()
        session_save_yourself()
    else:
        main()
        gtk.main()
    • Run in terminal:
      Code:
      sudo visudo
    • Add the following to the bottom of the file:
      Code:
      %admin ALL=NOPASSWD: /etc/init.d/umountnfs.sh


    • Press Ctrl-X to exit, press Y to save changes, and hit [Enter] to write the file.


    I hope this helps! I'm a linux noob, so apologies if my guide wasn't perfect.
    (Credit to stevo1982 for the sudo password workaround!)
    Unfortunately this is solution still doesn't work for me :S, I'm using Ubuntu 9.10.

    You just added this script in the autostart application? You didn't add anything to /etc/rc0.d or rc6.d directory ??


    EDIT:
    It works if I shutdown from the gnome menu, it fail if I shutdown from th termina.
    Is there a solution for that issue?
    Last edited by impolo; January 11th, 2010 at 12:49 PM.

  8. #8
    Join Date
    Dec 2009
    Location
    Sydney, AU
    Beans
    13
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no error

    Thanks, this worked nicely for me
    Alienware M17X | QX9300 @ 3 GHz | GTX260M x2 SLI | 1920x1200 | 8GB 1333 Mhz | BD-ROM | Ubuntu 9.10 | 1TB 7K2 Raid 0

  9. #9
    Join Date
    Oct 2009
    Beans
    16

    Re: [Fix] Karmic: Automaticly unmount CIFS shares at shutdown & restart with no error

    Quote Originally Posted by impolo View Post
    Unfortunately this is solution still doesn't work for me :S, I'm using Ubuntu 9.10.

    You just added this script in the autostart application? You didn't add anything to /etc/rc0.d or rc6.d directory ??


    EDIT:
    It works if I shutdown from the gnome menu, it fail if I shutdown from th termina.
    Is there a solution for that issue?
    No solution I am aware of. See post #5 in this thread for an explanation.
    Last edited by ubradford; January 16th, 2010 at 07:44 AM.

  10. #10
    Join Date
    Oct 2008
    Location
    Cambridge, UK
    Beans
    223

    Re: [Fix] Karmic: Automatical unmount CIFS shares at shutdown & restart with no error

    Hi

    Sorry to butt into an old thread - especially as I am a complete noob! Found this thread linked to in another thread I was reading...

    Perhaps someone could correct me, if I am wrong, but couldn't a simple Upstart script provide a more elegant solution to this problem that applies to all more recent Ubuntu (>=10.04??) flavours (Xubuntu, LXDE, Kubuntu, etc.)??

    I'm (theoretically ) a big fan of the Upstart model - since it allows service dependencies to be expressed (so startup and shutdown can be asynchronous).

    Any comments?

    Bob

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
  •