Ubuntu18上udev热插拔事件无法使用mount挂载U盘的问题

        在Ubuntu10、Ubuntu14上在udev规则配置文件中,可以使用mount来挂载捕获到的sdXX设备,但在Ubuntu18上却无法挂载成功,甚是捉急。

        查阅大量资料后方知,这是因为udev 服务设置的挂载命名空间是私有的,仅对该服务内部可用,对其他服务不可访问。

  以下引用自udev介绍信息:    

Mounting drives in rules

To mount removable drives, do not call mount from udev rules. This is ill-advised for two reasons: (1) systemd by default runs systemd-udevd.service with a separate "mount namespace" (see namespaces(7)), which means that mounts will not be visible to the rest of the system. (2) Even if you change the service parameters to fix this (commenting out the PrivateMounts and MountFlags lines), there is another problem which is that processes started from Udev are killed after a few seconds. In case of FUSE filesystems, such as NTFS, "mount" starts a user-space process to handle the filesystem internals; when this is killed you will get Transport endpoint not connected errors if you try to access the filesystem.

There are some options that work:

  • Start a custom systemd service from the Udev rule; the systemd service can invoke a script which can start any number of long-running processes (like FUSE). A concise example which automatically mounts USB disks under /media is udev-media-automount. It was found to be fast and reliable. A variant of the same idea is explained in this blog post.
  • Use systemd-mount instead of mount in your Udev rule. This is recommended by systemd developers. For example this Udev rule should mount USB disks under "/media":
  • Use a package like udisks or udiskie. These are very powerful, but difficult to set up. Also, they are meant to be used in single user sessions, since they make some filesystems available under the ownership of the unprivileged user whose session is currently active.

原文翻译如下:

要挂载可移动驱动器,不要mountudev规则调用。这是不明智的,原因有两个:(1) systemd 默认systemd-udevd.service使用单独的“挂载命名空间”运行(请参阅namespaces(7)),这意味着挂载对系统的其余部分不可见。(2) 即使您更改服务参数来解决此问题(注释掉PrivateMountsMountFlags行),还有另一个问题是从 Udev 启动的进程在几秒钟后被杀死。对于 FUSE 文件系统,例如NTFS,“mount”会启动一个用户空间进程来处理文件系统内部;当它被杀死时,Transport endpoint not connected如果您尝试访问文件系统,您将收到错误。

有一些选项有效:

  • 从 Udev 规则启动自定义 systemd 服务;systemd 服务可以调用一个脚本,该脚本可以启动任意数量的长时间运行的进程(如 FUSE)。自动挂载 USB 磁盘的一个简洁示例/mediaudev-media-automount。它被发现是快速和可靠的。这篇博文中解释了相同想法的一个变体。
  • 在您的 Udev 规则中使用systemd-mount而不是mount。这是systemd 开发人员推荐的。例如这个 Udev 规则应该在“/media”下挂载 USB 磁盘ACTION=="add", SUBSYSTEMS=="usb", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem", RUN{program}+="/usr/bin/systemd-mount --no-块 --automount=yes --collect $devnode /media"
  • 然而,在撰写本文时,它被发现速度缓慢且不可靠。
  • 使用像udisksudiskie这样的包。这些功能非常强大,但很难设置。此外,它们旨在用于单用户会话,因为它们使某些文件系统在会话当前处于活动状态的非特权用户的所有权下可用。

 

        正如文中所说,一种办法是以systemd-mount代替mount来挂载、卸载设备。另一种方法是舍弃RUN方法,而是采用关联启动一个systemd服务来解决。 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值