Umount network drives with systemd before shutdown

13,042

Solution 1

After 3 days of searching and testing, I developed a working solution -- this is for a Debian-Jessie, Linux Mint, i386 (32-bit) installation. I have network cifs shares, that hang shutdown or reboot for 120 seconds if these are not umounted first. With this script, I don't have to manually umount the shares before reboot or shutdown.

Auto cifs umount script, runs on shutdown and reboot

  • create the following script as /etc/init.d/aaaumount (scripts are executed in number, and alpha order, that's why the aaa, so this runs first):

    #!/bin/sh
    
    #
    # aaaumount initscript
    #
    ### BEGIN INIT INFO
    # Provides:          aaaumount
    # Required-Start:    $local_fs $remote_fs
    # Required-Stop:     $remote_fs
    # Default-Start:     S
    # Default-Stop:      0 1 6
    # Short-Description: umounts cifs shares
    # Description:       This script unmounts cifs shares
    ### END INIT INFO
    
    case "$1" in
     stop)
                umount -t cifs -af
    esac
    
  • make it executable:

    sudo chmod 755 /etc/init.d/aaaumount
    
  • make symbolic link to the service in /lib/systemd/system/:

    cd /lib/systemd/system/
    sudo ln -s /dev/null aaaumount.service
    
  • make symbolic links in both /etc/rc0.d and /etc/rc6.d (0 and 6 are shutdown/reboot run levels):

    sudo ln -s /etc/init.d/aaaumount /etc/rc0.d/K01aaaumount 
    sudo ln -s /etc/init.d/aaaumount /etc/rc6.d/K01aaaumount 
    
  • activate it:

    sudo systemctl enable aaaumount.service
    

Solution 2

Add the _netdev mount option to the remote filesystems in /etc/fstab. After a systemctl daemon-reload this should make your network mounts dependencies of the remote-fs.target; check it with systemctl list-dependencies remote-fs.target. Such filesystems are unmounted before network is brought down.

Solution 3

After some trial and error, I figured wpa_supplicant.service, which is necessary for my system to use the Wi-Fi, is not really managed by systemd (and its systemd unit is "disabled") on Ubuntu 16.04. It seems to be started by something via DBus. I still don't know why, but it is shut down before network-online.target and network.target.

In the end, to be sure my drive is unmounted correctly, I added After=graphical.target to the mount unit. To do this, you can add x-systemd.requires=graphical.target to the options in your /etc/fstab file, like this:

Note that you need your mount utility (in my case, mount.cifs) to ignore options starting by x-, which is not the case of mount.cifs before version 6.5.

Share:
13,042

Related videos on Youtube

pandita
Author by

pandita

Updated on September 18, 2022

Comments

  • pandita
    pandita over 1 year

    My shutdown takes quite a long time (I'm on Debian 8.1) and I found out that it can be fixed by unmounting the network drive before shutting down the system. Apparently the network gets disconnected before all the drives are unmounted.

    To do this automatically I tried to create a systemd service but it doesn't work, i.e. it doesn't seem to do the unmount in time and the shutdown process still takes quite long. My approach is inspired by the answers to this questions as well as some browsing of the systemd.service man pages...

    [unit]
    description=Unmount network drives on shutdown
    Before=shutdown.target reboot.target halt.target network.target
    
    [Service]
    type=oneshot
    RemainAfterExit=true
    ExecStart=/bin/true
    ExecStop=/bin/sh umount /media/networkdrive1 /media/networkdrive2
    
    [Install]
    WantedBy=multi-user.target
    

    Edit

    The network drives are mounted in /etc/fstab with the following lines:

    //192.168.1.5/networkdrive1 /media/nw1 cifs _netdev,uid=myuser,credentials=/home/myuser/.credfile
    //192.168.1.5/networkdrive2 /media/nw2 cifs _netdev,uid=myuser,credentials=/home/myuser/.credfile
    
    • Admin
      Admin about 7 years
      Here someone said in 2008: "it is a very old and well known problem". It is 2017 and I still suffer from this issue. Did anyone find a working solution?
  • pandita
    pandita over 8 years
    I have this in my fstab already, see edit above. The drives also show up in the dependencies list. However if I shutdown without manually unmounting the drives, it takes ~2 mintues or so. If I unmount the drives manually before shutting down, the PC turns down nearly instantly....
  • Ferenc Wágner
    Ferenc Wágner over 8 years
    Interesting. You could try following the official debug instructions.