Mounting SMB shares reliably on a Linux WiFi Laptop with AutoFS

As I mentioned here, I have been running Manjaro Linux on my home laptop for almost a year now and for the most part it has worked well. My setup is that most of the useful storage is on my home server and I was mounting these as SMB shares on the laptop. Initially, I used SMB4k and it worked but was always annoying in that it didn’t deal with suspend/resume or WiFi connectivity loss very well – sometimes hanging, sometimes failing to remount the shares etc. After a recent update, this behavior really worsened and SMB4k became unusable every time I resumed from sleep in addition to having problems like mounting the share with the wrong user id (trust me, I tried to figure that one out and gave up).

Instead of trying to figure out a way to fix SMB4k, I decided to simplify the mounting setup to a command-line and kernel event driven setup instead of the added complexities of integration with the graphical environment. Since my setup was relatively simple – I want the shares mounted every time I am on a particular network and have them mounted permanently as long as I am on that network, quick suspend/resume and no hang during these operations. After some digging around, I settled on AutoFS which allows you to mount devices on access and supports SMB/CIFS among other types of network devices. Setting up AutoFS is complicated only because the documentation is sparse and inadequate. In the end, it is fairly straightforward – just create a file under /etc/autofs/ called auto.cifs.myserver which lists the shares you want to mount along with the mount points:

Backup -fstype=cifs,uid=<uid>,gid=<gid>,credentials=/etc/creds/myserver "://myserver/Backup"

Then add the following line in autofs.master:

# Adjust the timeout to your preference - I like my shares mounted permanently when I'm on the laptop
/mnt /etc/autofs/auto.cifs.myserver --timeout=30000

This will mount the share \\myserver\Backup at /mnt/Backup with the uid and gid specified and using the credentials specified in /etc/creds/myserver. Here’s the format in which you supply credentials in /etc/creds/myserver:

username=<username on myserver>
password=<password for user on myserver>

That’s all you need for autofs to mount the share. autofs will mount the share when you access the mount point locally and unmount it after the timeout.

The next step is dealing with suspend/resume and WiFi network changes – I used the NetworkManager hook scripts to automatically stop and start autofs and trigger the mount. I created /etc/NetworkManager/dispatcher.d/30-smb.sh:

#!/bin/bash

# Find the connection UUID with "nmcli con show" in terminal.
WANTED_CON_UUID="<UUID of your WiFi Connection>"

if [[ "$CONNECTION_UUID" == "$WANTED_CON_UUID" ]]; then
# Script parameter $1: NetworkManager connection name, not used
# Script parameter $2: dispatched event
    case "$2" in
        "up")
            systemctl start autofs
            cd /mnt/Backup
        ;;
        "pre-down");&
        "down");&
        "vpn-pre-down")
            systemctl stop autofs
        ;;
    esac
fi

Then create a symlink for this in /etc/NetworkManager/dispatcher.d/pre-down.d as well. This script takes care of unmounting on suspend and remounting on resume. So far this setup has worked flawlessly for a few days and the overall experience is really seamless compared to SMB4k.

Leave a Reply

Your email address will not be published. Required fields are marked *