为什么Systemd_wants不会从udev'evose''传递参数到服务文件。规则?
我正在尝试使用UDEV规则和SystemD来安装和卸载USB密钥。
我将解决方案基于此博客 env {systemd_wants}在“ add” udev规则中使用。我无法让env {systemd_wants}为“删除”规则工作,所以我最终使用了此博客条目从UDEV运行键运行SystemD Service文件。
尽管此解决方案可起作用 udev man page 说:“运行长时间的事件过程可能会阻止此或依赖设备的所有其他事件。”
这些是我使用的规则...
添加规则:
ACTION=="add", SUBSYSTEM=="block", ENV{ID_TYPE}="disk", \
ENV{DEVTYPE}=="partition", ENV{ENV_MOUNT_USB}="MOUNT", \
PROGRAM="/usr/bin/systemd-escape -p [email protected] \
$env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
删除规则:
ACTION=="remove", SUBSYSTEM=="block", ENV{ID_TYPE}="disk", \
ENV{DEVTYPE}=="partition", RUN+="/bin/systemctl start usb-umount@%k.service"
这些是UDEV规则开始的SystemD服务文件。他们只是执行bash脚本以安装和安装USB密钥。
=“/cdn-cgi/l/email-potection” class =“ __ cf_email__” data-cfemail =“ 631610014E0C160D17234D1006111150AMAIL =”
[Unit]
Description=Mount USB Drive on %i
BindTo=%i.device
After=%i.device
[Service]
Type=oneshot
TimeoutStartSec=300
ExecStart=/usr/local/sbin/usb-mount.sh /%I
[Unit]
Description=UnMount USB Drive on %i
[Service]
Type=oneshot
TimeoutStartSec=300
ExecStart=/usr/local/sbin/usb-umount.sh /%I
> br> 但是,如果我在“删除”规则中使用env {system_wants},则独立USB密钥的设备块(例如: /dev /sdb1)不会传递给服务文件。实际上,我认为服务文件根本不运行。
我的问题
我的删除规则是否可以更改为与Env {System_wants}一起使用,而不是运行+= {..},以使 /dev /sdb1传递给服务文件,然后转到可以执行Un-Mount的脚本?
欢呼,
弯曲
I am trying to use udev rules and systemd to mount and unmount a USB key.
I am basing my solution below on an example from this blog where ENV{SYSTEMD_WANTS} is used in an "Add" udev rule. I could not get ENV{SYSTEMD_WANTS} to work for a "Remove" rule so I ended up using the example in this blog entry which runs a systemd service file from a udev RUN key.
Although this solution works the udev man page says: "Running an event process for a long period of time may block all further events for this or a dependent device."
These are the rules I use...
The Add Rule:
ACTION=="add", SUBSYSTEM=="block", ENV{ID_TYPE}="disk", \
ENV{DEVTYPE}=="partition", ENV{ENV_MOUNT_USB}="MOUNT", \
PROGRAM="/usr/bin/systemd-escape -p [email protected] \
$env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
The Remove Rule:
ACTION=="remove", SUBSYSTEM=="block", ENV{ID_TYPE}="disk", \
ENV{DEVTYPE}=="partition", RUN+="/bin/systemctl start usb-umount@%k.service"
These are the systemd service files that the udev rules start. They simply execute bash scripts to mount and un-mount the USB key.
[Unit]
Description=Mount USB Drive on %i
BindTo=%i.device
After=%i.device
[Service]
Type=oneshot
TimeoutStartSec=300
ExecStart=/usr/local/sbin/usb-mount.sh /%I
[Unit]
Description=UnMount USB Drive on %i
[Service]
Type=oneshot
TimeoutStartSec=300
ExecStart=/usr/local/sbin/usb-umount.sh /%I
My Problem
But if I use ENV{SYSTEM_WANTS} in the "remove" rule the device block (e.g: /dev/sdb1) of the detaching USB key does NOT get passed to the service file. Actually I don't think the service file runs at all.
My Question
Can my remove rule be changed to work with ENV{SYSTEM_WANTS} and NOT RUN+={..} such that the /dev/sdb1 is passed to the service file and on to a script that can do the un-mount?
Cheers,
Flex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量调查,我对上面的问题有一个答案。
问题的说明
关键点在:
我通过插入和删除USB密钥进行了测试。通过测试此UDEV规则不起作用:
我会像这样进行调试:
当我删除已经插入的USB密钥时,我没有在
Journal> Journal> Journal ctl
调试输出中看到任何提及USB-Mount服务。因此,该服务文件甚至没有启动,这是因为人页面所述的内容(请参见上文)。相比之下,当我将规则更改为此时:
现在在
Journal ctl
输出中,我看到:关于为什么Systemd_wants在UDEV中不起作用的UDEV删除规则
从该reddit线程引用:
这也解释了为什么SystemD_Wants不会使UDEV“删除”规则运行的USB安装服务。
Reddit线程的新解决方案
还显示了使用UDEV“删除”操作的SystemD_Wants的回旋方式:
现在使用Udev响应从我的计算机上插入和插入的USB键,我只需要一个UDEV规则,灵感来自RedDit的启发post and 此博客/a>:
UDEV规则
•/etc/udev/rules.d/99-local.rules
服务文件
•/etc/systemd/system/
077736274732a6a66872697269734729747297462746275716e6464646462我使用的脚本...
•/USR/local/sbin/test-mount.sh
•/USR/Local/sbin/sbin/test-umount.sh
我用来测试和调试的命令
•插入USB并删除USB密钥:
这样的调试...
•这确实是您要打扰测试UDEV规则的唯一方法!
•运行命令,然后插入并删除USB密钥。
还有这个...
这样的测试...
•此测试不如上述测试,因为它仅模拟UDEV规则将做!
•例如:对于 /dev /sdb1,您以下测试UDEV规则:
After much investigation I have an answer to my question above.
Explanation of Problem
The key points are in the Systemd device man page:
I tested by inserting and removing a USB key. From testing this udev rule does not work:
I debugged like this:
When I removed the already inserted USB key I didn't see any mention of the usb-mount service in the
journalctl
debug output. So that service file doesn't even start which makes sense because of what the man page states (see above).In contrast when I change the rule to this:
Now in the
journalctl
output I see:There is a good discussion about why Systemd_Wants will not work in a udev remove rule in this Reddit thread.
To quote from that Reddit Thread:
This also explains why Systemd_Wants does not make the usb-mount service run from a udev "remove" rule.
A New solution
That Reddit Thread also shows a roundabout way to use systemd_wants with a udev “remove” action:
Now to use udev to respond to a USB key being plugged and unplugged from my machine I need just ONE udev rule, inspired by the Reddit post and this blog:
The udev rule
• /etc/udev/rules.d/99-local.rules
The Service file
• /etc/systemd/system/[email protected]
The scripts I used...
• /usr/local/sbin/test-mount.sh
• /usr/local/sbin/test-umount.sh
The commands I used to Test and Debug
• Insert USB and Remove USB key:
Debug like this...
• This is really the only way you should bother testing a udev rule!
• Run the commands then insert and remove the USB key.
And also this...
Test like this...
• This tests is not as useful as the above, as it only simulates what the udev rule would do!
• E.g: For /dev/sdb1 you test the udev rule like this: