I have a NAS, a QNAP TS-419P II. It’s about a decade old and it has always served me well. Due to various reasons I have never used it in an efficient way, it was always like a huge external drive, not really integrated in the rest of my filesystems.
The NAS has a couple of CIFS shares with very obvious names:
Multimedia, with directories
(There are a few more shares, but they aren’t relevant now.)
In Ubuntu, a user home directory has these default directories:
I want to store the files in these directories on my NAS.
Mounting shares, the obvious way
First I moved all existing files from
~/Videos to the corresponding directories on the NAS, to get empty directories. Then I made a few changes to the directories:
$ mkdir backup $ mkdir Multimedia $ rmdir Music $ ln -s Multimedia/Music Music $ rmdir Pictures $ ln -s Multimedia/Photos Pictures $ rmdir Videos $ ln -s Multimedia/Videos Videos
The symbolic links now point to directories that don’t (yet) exist, so they appear broken – for now.
The next step is to mount the network shares to their corresponding directories.
The hostname of my NAS is
minerva, after the Roman goddess of wisdom. To avoid using IP addresses, I added it’s IP address to
127.0.0.1 localhost 192.168.1.1 modem 192.168.1.63 minerva
The shares are password protected, and I don’t want to type the password each time I use the shares. So the login goes into a file
Even though I am the only user of this computer, it’s best practice to protect that file so I do
$ chmod 400 /home/amedee/.smb
Then I added these entries to
//minerva/download /home/amedee/Downloads cifs uid=1000,gid=1000,credentials=/home/amedee/.smb,iocharset=utf8 0 0 //minerva/backup /home/amedee/backup cifs uid=0,gid=1000,credentials=/home/amedee/.smb,iocharset=utf8 0 0 //minerva/multimedia /home/amedee/Multimedia cifs uid=0,gid=1000,credentials=/home/amedee/.smb,iocharset=utf8 0 0
- CIFS shares don’t have a concept of user per file, so the entire share is shown as owned by the same user.
gid=1000are the user ID and group ID of the user
amedee, so that all files appear to be owned by me when I do
credentialsoption points to the file with the username and password.
- The default character encoding for mounts is iso8859-1, for legacy reasons. I may have files with funky characters, so
iocharset=utf8takes care of that.
Then I did
sudo mount -a and yay, the files on the NAS appear as if they were on the local hard disk!
Fixing a slow startup
This all worked very well, until I did a reboot. It took a really, really long time to get to the login screen. I did lots of troubleshooting, which was really boring, so I’ll skip to the conclusion: the network mounts were slowing things down, and if I manually mount them after login, then there’s no problem.
It turns out that
systemd provides a way to automount filesystems on demand. So they are only mounted when the operating system tries to access them. That sounds exactly like what I need.
To achieve this, I only needed to add
noauto,x-systemd.automount to the mount options. I also added
x-systemd.device-timeout=10, which means that
systemd waits for 10 seconds, and then gives up if it’s unable to mount the share.
From now on I’ll never not use
noauto,x-systemd.automount for network shares!
While researching this, I found some documentation that claims you don’t need
noauto if you have
x-systemd.automount in your mount options. Yours truly has tried it with and without
noauto, and I can confirm, from first hand experience, that you definitely need
noauto. Without it, there is still the long waiting time at login.