创建 udev 规则以挂载驱动器

创建 udev 规则以挂载驱动器

我有一个驱动器连接到 Ubuntu 22.04 系统,有时会随机断开连接(我猜是接触不良造成的)。因为我需要这个驱动器,所以我想找到一种方法,每次它断开连接时自动挂载驱动器(如热插拔),然后在几秒钟后重新连接。

为此,我尝试创建一个 udev 规则,该规则在检测到驱动器的分区时运行,然后挂载此分区。但是我无法让它工作,也不知道为什么。

规则如下:

ACTION=="add", KERNEL=="sd?1", ENV{ID_FS_UUID}=="4eac45cc-236a-43fd-89fe-6c8872749c3f", RUN+="/usr/bin/logger --tag Media-SSD-Mount Mounting the media SSD (%k)", RUN+="/usr/bin/mount /dev/%k /home/felix/server/Jellyfin/data/media"

规则运行,但是我得到了permission denied安装驱动器的信息。日志如下所示

  21002 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: /etc/udev/rules.d/99-media-ssd.rules:1 RUN '/usr/bin/logger --tag Media-SSD-Mount Mounting the media SSD (%k)'
  21003 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: /etc/udev/rules.d/99-media-ssd.rules:1 RUN '/usr/bin/mount /dev/%k /home/felix/server/Jellyfin/data/media'
  21004 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: Setting permissions /dev/sdb1, uid=0, gid=6, mode=0660

  21017 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: Running command "/usr/bin/logger --tag Media-SSD-Mount Mounting the media SSD (sdb1)"
  21018 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: Starting '/usr/bin/logger --tag Media-SSD-Mount Mounting the media SSD (sdb1)'
  21019 Mar 31 13:44:23 grievous systemd-udevd[488540]: Successfully forked off '(spawn)' as PID 488571.
  21020 Mar 31 13:44:23 grievous Media-SSD-Mount: Mounting the media SSD (sdb1)
  21021 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: Process '/usr/bin/logger --tag Media-SSD-Mount Mounting the media SSD (sdb1)' succeeded.

  21022 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: Running command "/usr/bin/mount /dev/sdb1 /home/felix/server/Jellyfin/data/media"
  21023 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: Starting '/usr/bin/mount /dev/sdb1 /home/felix/server/Jellyfin/data/media'
  21024 Mar 31 13:44:23 grievous systemd-udevd[488540]: Successfully forked off '(spawn)' as PID 488572.
  21025 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: '/usr/bin/mount /dev/sdb1 /home/felix/server/Jellyfin/data/media'(err) 'mount: /home/felix/server/Jellyfin/data/media: permission denied.'
  21026 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: Process '/usr/bin/mount /dev/sdb1 /home/felix/server/Jellyfin/data/media' failed with exit code 32.
  21027 Mar 31 13:44:23 grievous systemd-udevd[488540]: sdb1: Command "/usr/bin/mount /dev/sdb1 /home/felix/server/Jellyfin/data/media" returned 32 (error), ignoring.

规则运行(按预期),如root(用测试whoami)一样,因此权限应该不是问题。

我环顾四周,发现帖子说mount无法使用安装驱动器systemd-mount,应改为使用。但是这是一篇针对 arch linux 的帖子,我不知道这是否适用于 Ubuntu,因为我找到了一些使用mount这里这里)。

所以我尝试使用systemd-mount并调整了我的规则

ACTION=="add", KERNEL=="sd?1", ENV{ID_FS_UUID}=="4eac45cc-236a-43fd-89fe-6c8872749c3f", RUN+="/usr/bin/logger --tag Media-SSD-Mount Mounting the media SSD (%k)", RUN+="/usr/bin/systemd-mount /dev/%k /home/felix/server/Jellyfin/data/media"

然而这会产生以下错误

  26743 Mar 31 14:06:47 grievous systemd-udevd[495468]: sdb1: Running command "/usr/bin/systemd-mount /dev/sdb1 /home/felix/server/Jellyfin/data/media"
  26744 Mar 31 14:06:47 grievous systemd-udevd[495468]: sdb1: Starting '/usr/bin/systemd-mount /dev/sdb1 /home/felix/server/Jellyfin/data/media'
  26745 Mar 31 14:06:47 grievous systemd-udevd[495468]: Successfully forked off '(spawn)' as PID 495499.
  26746 Mar 31 14:06:47 grievous systemd[1]: Mounting /dev/sdb1...
  26747 Mar 31 14:06:47 grievous mount[495500]: mount: /dev/sdb1: /dev/sdd1 already mounted or mount point busy.
  26748 Mar 31 14:06:47 grievous systemd[1]: dev-sdb1.mount: Mount process exited, code=exited, status=32/n/a
  26749 Mar 31 14:06:47 grievous systemd[1]: dev-sdb1.mount: Failed with result 'exit-code'.
  26750 Mar 31 14:06:47 grievous systemd[1]: Failed to mount /dev/sdb1.
  26751 Mar 31 14:06:47 grievous systemd[1]: Dependency failed for /home/felix/server/Jellyfin/data/media.
  26752 Mar 31 14:06:47 grievous systemd[1]: home-felix-server-Jellyfin-data-media.mount: Job home-felix-server-Jellyfin-data-media.mount/start failed with result 'dependency'.
  26753 Mar 31 14:06:47 grievous systemd-udevd[495468]: sdb1: '/usr/bin/systemd-mount /dev/sdb1 /home/felix/server/Jellyfin/data/media'(err) 'A dependency job for home-felix-server-Jellyfin-data-media.mount failed. See 'journalctl -xe' for details.'
  26754 Mar 31 14:06:47 grievous systemd-udevd[495468]: sdb1: Process '/usr/bin/systemd-mount /dev/sdb1 /home/felix/server/Jellyfin/data/media' failed with exit code 1.
  26755 Mar 31 14:06:47 grievous systemd-udevd[495468]: sdb1: Command "/usr/bin/systemd-mount /dev/sdb1 /home/felix/server/Jellyfin/data/media" returned 1 (error), ignoring.

当我尝试在普通 shell 中运行此代码时,出现了同样的错误:

┌─[14:20:26]-[:)]-[root

相关内容