There is an inefficient way to mount external storage (local or remote). An hard to die habit: fstab. Let’s try autofs.

The issue with fstab

Ok, maybe fstab is a simple way, and the most immediate way to mount resources at boot. And we are used to it. But if this external storage (i.e. a CIFS/SAMBA share, an sshfs filesystem) is not available during boot, or if the connection get lost while mounted, it could lead to undesired behaviours.

Using autofs

Install autofs

If it isn’t already installed

sudo dnf install autofs

Mount an USB disk

Using the disk label instead of /dev/sdX

Let’s suppose that you have a BTRFS filesystem on the USB disk. Let’s label it (in this way we will not use the classical /dev/sdX since it is unpredictable, and we don’t have to deal with esoteric UUIDs).

sudo btrfs filesystem label /dev/sda1 btrfs300

(For ext4 there are the e2label and tune2fs commands).

Configure autofs

Create a file like /etc/auto.master.d/trecento.autofs

1
/media/   /etc/auto.ext-usb --timeout=300,defaults,user,exec

Timeoud should umount the disk after 5 minutes, so in theory putting it to sleep.

Create the /etc/auto.ext-usb file containing

1
trecento -fstype=auto           :/dev/disk/by-label/btrfs300

As you can see, we are using the disk label to point the BTRFS partition.

Enable and start the autofs service

sudo systemctl enable autofs --now

Done

Issuing the mount command

1
2
3
...
/etc/auto.ext-usb on /media type autofs (rw,relatime,fd=24,pgrp=320148,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=2153302)
...

You see that the USB disk is not mounted. Listing or accessing the defined directory will automatically mount it

ll /media/trecento

mount

1
2
3
4
...
/etc/auto.ext-usb on /media type autofs (rw,relatime,fd=24,pgrp=320148,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=2153302)
/dev/sdd1 on /media/trecento type btrfs (rw,nosuid,nodev,relatime,seclabel,space_cache,subvolid=5,subvol=/,user)
...

sshfs

Install the sshfs package.

Create the /etc/auto.master.d/conta.autofs file.

1
/sshfs /etc/auto.sshfs uid=1000,gid=1000,--timeout=30,--ghost

Create the /etc/auto.sshfs file.

1
remote -fstype=fuse,rw,nodev,noatime,allow_other,max_read=65536 :sshfs\#user@remote.host\:

Of course you have to configure ssh keys in order to log in without a password.

CIFS/SAMBA

Same steps. Create a /etc/auto.master.d/cifs.autofs file

1
/samba       /etc/auto.samba

Then /etc/auto.samba

1
remote -fstype=cifs,rw,credentials=/etc/.credentials/windows,uid=1000,sec=ntlm,noserverino ://windows.lan/share

You can create a /etc/.credentials/ directory and then a file containing the credentials, /etc/.credentials/windows

1
2
username=shareuser
password=sharepassword

Reload the changes

sudo systemctl restart autofs