Hot swapping SATA devices (ThinkPad) / fstab questions

Hmm - booting with entries in fstab which may or may not be available - is not worth the trouble.

Using systemd mount / automount units

Replace the entries with a systemd unit combo.

/etc/fstab

#UUID=dd91319d-c731-4939-a3c8-c4b472c2a958       /media/Data             ext4    defaults,nofail                 0       2
#UUID=24b34b91-f83f-4239-af3d-211dd1104bee       /media/Backup           ext4    defaults,nofail                 0       2
#UUID=373ADDA6610597A5                           /media/Windows          ntfs    defaults,nofail                 0       0
#UUID=AFE5A3B58FCBD714                           /mnt/WinSysRes          ntfs    defaults,nofail,noauto          0       0
#UUID=BC2CF6852CF639CA                           /media/ExterneHN        ntfs    defaults,nofail                 0       0

Data

 $ cat /etc/systemd/system/media-Data.mount
[Unit]
Description=Data partition on /media

[Mount]
TimeoutSec=10
What=/dev/disk/by-uuid/dd91319d-c731-4939-a3c8-c4b472c2a958
Where=/media/Data
Type=ext4
Options=defaults,rw,noatime
 $ cat /etc/systemd/system/media-Data.automount
[Unit]
Description=Automount Data partition on /media

[Automount]
Where=/media/Data
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

Backup

 $ cat /etc/systemd/system/media-Backup.mount
[Unit]
Description=Backup partition on /media

[Mount]
TimeoutSec=10
What=/dev/disk/by-uuid/24b34b91-f83f-4239-af3d-211dd1104bee
Where=/media/Backup
Type=ext4
Options=defaults,rw,noatime
 $ cat /etc/systemd/system/media-Backup.automount
[Unit]
Description=Automount Backup partition on /media

[Automount]
Where=/media/Backup
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

Windows

 $ cat /etc/systemd/system/media-Windows.mount
[Unit]
Description=Windows partition on /media

[Mount]
TimeoutSec=10
What=/dev/disk/by-uuid/373ADDA6610597A5
Where=/media/Windows
Type=ntfs
Options=defaults,rw,noatime
 $ cat /etc/systemd/system/media-Windows.automount
[Unit]
Description=Automount Windows partition on /media

[Automount]
Where=/media/Windows
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

WinSysRes

 $ cat /etc/systemd/system/mnt-WinSysRes.mount
[Unit]
Description=WinSysRes partition on /mnt

[Mount]
TimeoutSec=10
What=/dev/disk/by-uuid/AFE5A3B58FCBD714
Where=/mnt/WinSysRes
Type=ntfs
Options=defaults,rw,noatime
 $ cat /etc/systemd/system/mnt-WinSysRes.automount
[Unit]
Description=Automount WinSysRes partition on /mnt

[Automount]
Where=/mnt/WinSysRes
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

ExterneHN

 $ cat /etc/systemd/system/media-ExterneHN.mount
[Unit]
Description=ExterneHN partition on /media

[Mount]
TimeoutSec=10
What=/dev/disk/by-uuid/BC2CF6852CF639CA
Where=/media/ExterneHN
Type=ntfs
Options=defaults,rw,noatime
 $ cat /etc/systemd/system/media-ExterneHN.automount
[Unit]
Description=Automount ExterneHN partition on /media

[Automount]
Where=/media/ExterneHN
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

Enable the automount unit only (assuming folders exist)

sudo systemctl enable --now \
    media-Data.automount \
    media-Backup.automount \
    media-Windows.automount \
    mnt-WinSysRes.automount \
    media-ExterneHN.automount

When ever you access the folder the system will mount the relevant device if accessible and unmount after 10s inactivity.

The idle time before unmount is in the automount unit TimeoutIdleSec property
The wait time for device ready is in the mount unit TimeoutSec property

The solution is fairly failsafe as you will not be able to access the mount point unless the device is in fact present.

A caveat for NTFS whether you use fstab or mount unit is the kernel vs userspace driver.

The kernel space driver will refuse to mount if it thinks the filesystem is dirty.

To work around such issue you will need to blacklist the kernel driver and ensure the userspace driver is installed.

2 Likes