0

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 Backups
After=blockdev@dev-disk-by\x2duuid-6ac875cb\x2d2a99\x2d494a\x2db7d9\x2d60619646155d.target

[Mount] What=/dev/disk/by-uuid/6ac875cb-2a99-494a-b7d9-60619646155d Where=/media/archive/public/GameBackups Type=ext4 Options=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 drives
ACTION=="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!

Hyrikan
  • 49

1 Answers1

2

In the comments, many solutions were presented. They can be found in questions with very specific titles. That made it difficult for me to find them. So i present my solution here.

This worked for me:

  1. Have a udev rule starting a systemd service
  2. Have that oneshot systemd service stating a script
  3. Have that script calling the mount command

As far as i understood, udev only releases the devices for the system after handling all rules. This is the reason, that directly called actions on the device symlinks do not work.

Script:

#!/bin/bash

ACTION=$1 if [[ ${ACTION} == "mount" ]]; then /bin/mount -o rw,dev,exec,auto,nouser,sync,relatime,nofail /dev/disk/by-uuid/6ac875cb-2a99-494a-b7d9-60619646155d "/media/archive/public/Game Backups" else /bin/umount "/media/archive/public/Game Backups" fi /usr/bin/logger -t 'game backups mount script' "${ACTION} on /media/archive/public/Game Backups"

Oneshot systemd service:

[Unit]
Description=Mount External Game Backups

[Service] Type=oneshot RemainAfterExit=true ExecStart=/usr/local/bin/mount-external-game-backups.sh mount ExecStop=/usr/local/bin/mount-external-game-backups.sh umount

udev rule:

ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="6ac875cb-2a99-494a-b7d9-60619646155d", RUN+="/usr/bin/logger -t 'udev mount game backups' 'starting mount script for game backups'", RUN+="/bin/systemctl start mountgamebackups.service"
ACTION=="remove", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="6ac875cb-2a99-494a-b7d9-60619646155d", RUN+="/usr/bin/logger -t 'udev mount game backups' 'stopping mount script for game backups'", RUN+="/bin/systemctl stop mountgamebackups.service"
Hyrikan
  • 49