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:
backupDownloadMultimedia, with directoriesMusic,PhotosandVideos
(There are a few more shares, but they aren’t relevant now.)
In Ubuntu, a user home directory has these default directories:
DownloadsMusicPicturesVideos
I want to store the files in these directories on my NAS.
Mounting shares, the obvious way
First I moved all existing files from ~/Downloads, ~/Music, ~/Pictures, ~/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 /etc/hosts:
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 /home/amedee/.smb:
username=amedee
password=NOT_GOING_TO_TELL_YOU_:-p
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 /etc/fstab:
//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.
uid=1000andgid=1000are the user ID and group ID of the useramedee, so that all files appear to be owned by me when I dols -l. - The
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.
You may need quotes around the password!
username=amedee
password=’NOT GOING TO TELL YOU :-p’
๐
That may be true for older versions of
smbfsorcifs, but not forcifsof at least version 4.5 or higher.For a
mountcommand withusernameandpasswordon the shell: yes you need to put them in quotes, or escape special characters like spaces.But in a
credentialsfile, you don’t need quotes or escapes. Please refer to themount.cifs(8)manual page. ๐I was under the (possibly incorrect) impression that CIFS has Unix extensions, where you *can* have UID/GIDs stored on the filesystem. This may only work when the server runs Samba, though.
is there a specific reason that you don’t use systemd for that?
like it is described here for example:
https://gist.github.com/akiross/1aa81f67514ef4753f2c8a15040364a3
Yes there is a very specific reason: I don’t know what systemd is and I hope that some friendly commenter helps me out. Like you just did, so thanks!