ubuntu20.04使用udev管理外设
程序员文章站
2022-07-14 20:25:56
...
安装udev模块
apt install udev
vim /lib/systemd/system/systemd-udevd.service
#set "PrivateMounts=no"
systemctl restart udev
systemctl daemon-reload
在 /etc/udev/rules.d/ 下增加新的rule文件
# There are a number of modifiers that are allowed to be used in some
# of the different fields. They provide the following subsitutions:
#
# %n the "kernel number" of the device.
# For example, 'sda3' has a "kernel number" of '3'
# %e the smallest number for that name which does not matches an existing node
# %k the kernel name for the device
# %M the kernel major number for the device
# %m the kernel minor number for the device
# %b the bus id for the device
# %c the string returned by the PROGRAM
# %s{filename} the content of a sysfs attribute
# %% the '%' char itself
#
#
#KERNEL=="sd[b-z][0-9]", NAME="%k", SYMLINK+="usb%b", GROUP="root", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/mkdir -p /mnt/usb/%k"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/mount -o iocharset=utf8 -t auto /dev/%k /mnt/usb/%k", OPTIONS="last_rule"
ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/umount -l /mnt/usb/%k"
ACTION=="remove", KERNEL=="sd[b-z][0-9]", RUN+="/bin/rmdir /mnt/usb/%k", OPTIONS="last_rule"
修改rule文件是热加载,不需要重启进程。