{"id":306,"date":"2020-09-04T19:42:25","date_gmt":"2020-09-05T03:42:25","guid":{"rendered":"http:\/\/nramkumar.org\/tech\/?p=306"},"modified":"2020-09-04T19:42:25","modified_gmt":"2020-09-05T03:42:25","slug":"mounting-smb-shares-reliably-on-a-linux-wifi-laptop-with-autofs","status":"publish","type":"post","link":"https:\/\/nramkumar.org\/tech\/blog\/2020\/09\/04\/mounting-smb-shares-reliably-on-a-linux-wifi-laptop-with-autofs\/","title":{"rendered":"Mounting SMB shares reliably on a Linux WiFi Laptop with AutoFS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">As I mentioned <a href=\"https:\/\/nramkumar.org\/tech\/blog\/2020\/09\/01\/fixing-slow-shutdown-in-linux-from-cifs-smb-mounts-over-wifi\/\">here<\/a>, 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&#8217;t deal with suspend\/resume or WiFi connectivity loss very well &#8211; 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). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8211; 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 <a href=\"https:\/\/help.ubuntu.com\/community\/Autofs\">AutoFS<\/a> 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 &#8211; just create a file under <code>\/etc\/autofs\/<\/code> called <code>auto.cifs.myserver<\/code> which lists the shares you want to mount along with the mount points:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Backup -fstype=cifs,uid=&lt;uid>,gid=&lt;gid>,credentials=\/etc\/creds\/myserver \":\/\/myserver\/Backup\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then add the following line in <code>autofs.master<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Adjust the timeout to your preference - I like my shares mounted permanently when I'm on the laptop\n\/mnt \/etc\/autofs\/auto.cifs.myserver --timeout=30000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will mount the share <code>\\\\myserver\\Backup<\/code> at <code>\/mnt\/Backup<\/code> with the <code>uid<\/code> and <code>gid<\/code> specified and using the credentials specified in <code>\/etc\/creds\/myserver<\/code>. Here&#8217;s the format in which you supply credentials in <code>\/etc\/creds\/myserver<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>username=&lt;username on myserver>\npassword=&lt;password for user on myserver><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next step is dealing with suspend\/resume and WiFi network changes &#8211; I used the NetworkManager hook scripts to automatically stop and start autofs and trigger the mount. I created <code>\/etc\/NetworkManager\/dispatcher.d\/30-smb.sh<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# Find the connection UUID with \"nmcli con show\" in terminal.\nWANTED_CON_UUID=\"&lt;UUID of your WiFi Connection>\"\n\nif &#91;&#91; \"$CONNECTION_UUID\" == \"$WANTED_CON_UUID\" ]]; then\n# Script parameter $1: NetworkManager connection name, not used\n# Script parameter $2: dispatched event\n    case \"$2\" in\n        \"up\")\n            systemctl start autofs\n            cd \/mnt\/Backup\n        ;;\n        \"pre-down\");&amp;\n        \"down\");&amp;\n        \"vpn-pre-down\")\n            systemctl stop autofs\n        ;;\n    esac\nfi\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then create a symlink for this in <code>\/etc\/NetworkManager\/dispatcher.d\/pre-down.d<\/code> 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,44],"tags":[63],"class_list":["post-306","post","type-post","status-publish","format-standard","hentry","category-linux","category-smb","tag-linux"],"_links":{"self":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/comments?post=306"}],"version-history":[{"count":1,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/posts\/306\/revisions\/307"}],"wp:attachment":[{"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/categories?post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nramkumar.org\/tech\/wp-json\/wp\/v2\/tags?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}