I found many solutions online, but each had specific errors or downsides, that made them unconvenient.
I want: On an Ubuntu 22.04 server, connect hard drive at runtime, not only boot, via sata and automatically mount them at a predefined directory per hard drive. It is ok, if the directory is just empty if the drive is not connected. But the directory must be reachable even when empty.
I tried: udev calling a simple mount script, udev calling systemctl and starting a mount unit with it, udev and using systemd-mount, systemds automount unit files.
The udev attempts failed all. The mount script just returns exit code 32. Would have wondered, if that worked. Using systemds automount unit files works in automounting the drives. But as soon as the drive is disconnected, trying to reach the now empty directory leads to massive lags as automounting attempts are made, which time out due to the requested device not starting.
Calling systemd units from udev are also timing out due to the required device not available for unknown reasons. Additionally it is a pain to have directories with white spaces in the name when using udev and systemd, as the unit file needs to have the path in its name.
My systemd unit files:
[Unit]Description=Hotswap External Game BackupsAfter=blockdev@dev-disk-by\x2duuid-6ac875cb\x2d2a99\x2d494a\x2db7d9\x2d60619646155d.target[Mount]What=/dev/disk/by-uuid/6ac875cb-2a99-494a-b7d9-60619646155dWhere=/media/archive/public/GameBackupsType=ext4Options=rw,dev,exec,noauto,nouser,sync,relatime,nofail
[Unit]Description=Automount for external Game Backups[Automount]Where=/media/archive/public/GameBackups[Install]WantedBy=multi-user.target
My udev rule:
# rule for automounting hard drivesACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_UUID}="6ac875cb-2a99-494a-b7d9-60619646155d", RUN+="/usr/bin/logger --tag 'udev automount''Trying to MOUNT external game backups.'", RUN+="/usr/bin/systemctl start media-archive-public-GameBackups.mount"ACTION=="remove", SUBSYSTEM=="block", ENV{ID_FS_UUID}="6ac875cb-2a99-494a-b7d9-60619646155d", RUN+="/usr/bin/logger --tag 'udev automount''Trying to UNMOUNT external game backups.'", RUN+="/usr/bin/systemctl stop media-archive-public-GameBackups.mount"
As i said, udev times out because the After target of the unit file is not reached. I can manually start the systemd mount unit and it works flawlessly.
I use hotswap bays, which are just connected to sata and put hard drives in it.
Any help appreciated!