Last year I described a simple method of automounting a directory from my windows server to my Raspberry Pi. Since then I’ve gone down a couple of paths to simplify rebuilding my Raspberry Pi machines.
The method I used last year required modifying the /etc/hosts file, the /etc/fstab file, pre-creating the mount points, and creating a credentials file to store the windows login credentials.
My new method doesn’t require modification of the /etc/hosts or /etc/fstab files, or pre-creating the mount points. Instead I’m relying on two features, Multicast DNS and systemd.automount unit files.
In the old method, to find the windows server, I added it to the local hosts file on the raspberry pi.
Using Multicast DNS, if I simply recognize that I can reach the server with the name Acid.WimsWorld.local the raspberry pi will find the server on the local network. My first step was to modify my /etc/fstab enty to use the local address and clean up my hosts file.
//acid.wimsworld.local/web /media/acid/web/ cifs credentials=/etc/wimsworld.smb.credentials,noauto,x-systemd.automount,x-systemd.idle-timeout=2min,_netdev 0 0
I’d never been happy with modifying the /etc/fstab file as part of my system configuration because in newer installations it is unique to each machine, specifying the boot partitions by their formatted serial number:
proc /proc proc defaults 0 0 PARTUUID=142ff4e3-01 /boot vfat defaults 0 2 PARTUUID=142ff4e3-02 / ext4 defaults,noatime 0 1 # a swapfile is not a swap partition, no line here # use dphys-swapfile swap[on|off] for that
In my recent programming projects I’ve been working with systemd unit files to control my service processes and have come to understand how they work for automounting directories as well. I like that each directory has its own unit files meaning that a modification is less likely to cause problems for the system as a whole.
The single line from the /etc/fstab file above is removed and replaced by two unit files, /etc/systemd/system/media-acid-web.mount and /etc/systemd/system/media-acid-web.automount.
[Unit] Description=Acid Web [Mount] What=//acid.wimsworld.local/web Where=/media/acid/web Type=cifs Options=credentials=/etc/wimsworld.smb.credentials,vers=2.1 [Install] WantedBy=multi-user.target
[Unit] Description=Automount Acid Web [Automount] Where=/media/acid/web TimeoutIdleSec=120 [Install] WantedBy=multi-user.target
I still had to create the credentials file for this to work, since I wanted the credentials file to be only root readable in a different location. /etc/wimsworld.smb.credentials
username=WindowsUsername password=WindowsPassword domain=OptionalDomainName
After the three files are created, systemd needs to reload its database with the systemctl daemon-reload command, the automount needs to be enabled with the systemctl enable media-acid-web.automount command, and then started with the systemctl start media-acid-web.automount command.
The naming of the mount files is important, and described explicitly in the man pages for each of mount and automount. In my case, /media/acid/web gets named media-acid-web.mount and media-acid-web.automount. I didn’t need to create mount points in the /media directory, as systemd automatically takes care of that.
I was able to create all of the above with a simple paste into my terminal with the following string:
sudo bash cat > /etc/systemd/system/media-acid-web.mount <<EOF [Unit] Description=Acid Web [Mount] What=//acid.wimsworld.local/web Where=/media/acid/web Type=cifs Options=credentials=/etc/wimsworld.smb.credentials,vers=2.1 [Install] WantedBy=multi-user.target EOF cat > /etc/systemd/system/media-acid-web.automount <<EOF [Unit] Description=Automount Acid Web [Automount] Where=/media/acid/web TimeoutIdleSec=120 [Install] WantedBy=multi-user.target EOF cat > /etc/wimsworld.smb.credentials <<EOF username=WindowsUsername password=WindowsPassword domain=OptionalDomainName EOF chmod 0600 /etc/wimsworld.smb.credentials systemctl daemon-reload systemctl enable media-acid-web.automount systemctl start media-acid-web.automount exit
With the standard Raspberry Pi setup, the cat command is not available as a sudo command while the bash shell is. I’m taking advantage of that by running the bash shell as root and then all of the other commands with root privileges.