美文网首页工作笔记
udev自动挂载U盘

udev自动挂载U盘

作者: txfly | 来源:发表于2020-04-17 14:51 被阅读0次

    Linux可以通过udev自动挂载U盘,不过需要注意的是在udev中调用mount指令时会工作不正常,可以使用systemd-mount代替。
    参考文档:udevsystemd-mount

    具体步骤:

    1. /etc/udev/rules.d/目录下新建规则文件
    nano /etc/udev/rules.d/50-txfly.rules
    
    1. 输入以下内容,并在/media目录下挂载U盘
    KERNEL!="sd*", GOTO="media_by_label_auto_mount_end"
    SUBSYSTEM!="block",GOTO="media_by_label_auto_mount_end"
    IMPORT{program}="/sbin/blkid -o udev -p %N"
    ENV{ID_FS_TYPE}=="", GOTO="media_by_label_auto_mount_end"
    ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
    ENV{ID_FS_LABEL}=="", ENV{dir_name}="%E{ID_FS_UUID}"
    ACTION=="add", ENV{mount_options}="relatime,sync"
    ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/usr/bin/systemd-mount -o %E{mount_options} --no-block --automount=yes --collect /dev/%k /media/%E{dir_name}"
    ACTION=="remove", ENV{dir_name}!="", RUN+="/usr/bin/systemd-mount --umount /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}" 
    LABEL="media_by_label_auto_mount_end"
    
    1. 加载新规则
    sudo udevadm control --reload
    

    测试规则:

    # lskbl
    sda            8:0    1 14.3G  0 disk
    mtdblock0     31:0    0   16M  0 disk
    mmcblk2      179:0    0  7.3G  0 disk
    ├─mmcblk2p1  179:1    0   64M  0 part
    └─mmcblk2p2  179:2    0    3G  0 part /
    mmcblk2boot0 179:32   0    4M  1 disk
    mmcblk2boot1 179:64   0    4M  1 disk
    mmcblk2rpmb  179:96   0    4M  0 disk
    
    # udevadm test $(udevadm info -q path -n /dev/sda) 2>&1
    
    This program is for debugging only, it does not run any program
    specified by a RUN key. It may show incorrect results, because
    some values may be different, or not available at a simulation run.
    
    Load module index
    Parsed configuration file /usr/lib/systemd/network/99-default.link
    Parsed configuration file /usr/lib/systemd/network/73-usb-net-by-mac.link
    Created link configuration context.
    Reading rules file: /usr/lib/udev/rules.d/50-firmware.rules
    Reading rules file: /etc/udev/rules.d/50-txfly.rules
    Reading rules file: /usr/lib/udev/rules.d/50-udev-default.rules
    Reading rules file: /usr/lib/udev/rules.d/55-dm.rules
    Reading rules file: /usr/lib/udev/rules.d/60-autosuspend-chromiumos.rules
    Reading rules file: /usr/lib/udev/rules.d/60-block.rules
    Reading rules file: /usr/lib/udev/rules.d/60-cdrom_id.rules
    Reading rules file: /usr/lib/udev/rules.d/60-drm.rules
    Reading rules file: /usr/lib/udev/rules.d/60-evdev.rules
    Reading rules file: /usr/lib/udev/rules.d/60-fido-id.rules
    Reading rules file: /usr/lib/udev/rules.d/60-input-id.rules
    Reading rules file: /usr/lib/udev/rules.d/60-persistent-alsa.rules
    Reading rules file: /usr/lib/udev/rules.d/60-persistent-input.rules
    Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage-dm.rules
    Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage-tape.rules
    Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage.rules
    Reading rules file: /usr/lib/udev/rules.d/60-persistent-v4l.rules
    Reading rules file: /usr/lib/udev/rules.d/60-sensor.rules
    Reading rules file: /usr/lib/udev/rules.d/60-serial.rules
    Reading rules file: /usr/lib/udev/rules.d/61-autosuspend-manual.rules
    Reading rules file: /usr/lib/udev/rules.d/64-btrfs.rules
    Reading rules file: /usr/lib/udev/rules.d/64-xorg-xkb.rules
    Reading rules file: /usr/lib/udev/rules.d/65-libwacom.rules
    Reading rules file: /usr/lib/udev/rules.d/70-joystick.rules
    Reading rules file: /usr/lib/udev/rules.d/70-mouse.rules
    Reading rules file: /usr/lib/udev/rules.d/70-power-switch.rules
    Reading rules file: /usr/lib/udev/rules.d/70-touchpad.rules
    Reading rules file: /usr/lib/udev/rules.d/70-uaccess.rules
    Reading rules file: /usr/lib/udev/rules.d/71-seat.rules
    Reading rules file: /usr/lib/udev/rules.d/73-seat-late.rules
    Reading rules file: /usr/lib/udev/rules.d/73-special-net-names.rules
    Reading rules file: /usr/lib/udev/rules.d/75-net-description.rules
    Reading rules file: /usr/lib/udev/rules.d/75-probe_mtd.rules
    Reading rules file: /usr/lib/udev/rules.d/78-sound-card.rules
    Reading rules file: /usr/lib/udev/rules.d/80-debian-compat.rules
    Reading rules file: /usr/lib/udev/rules.d/80-drivers.rules
    Reading rules file: /usr/lib/udev/rules.d/80-ifupdown.rules
    Reading rules file: /usr/lib/udev/rules.d/80-libinput-device-groups.rules
    Reading rules file: /usr/lib/udev/rules.d/80-net-setup-link.rules
    Reading rules file: /usr/lib/udev/rules.d/85-hwclock.rules
    Reading rules file: /usr/lib/udev/rules.d/90-libinput-fuzz-override.rules
    Reading rules file: /usr/lib/udev/rules.d/95-dm-notify.rules
    Reading rules file: /usr/lib/udev/rules.d/96-e2scrub.rules
    Reading rules file: /usr/lib/udev/rules.d/99-systemd.rules
    Invalid inotify descriptor.
    DEVPATH=/devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/block/sda
    DEVNAME=/dev/sda
    DEVTYPE=disk
    MAJOR=8
    MINOR=0
    ACTION=add
    SUBSYSTEM=block
    ID_FS_UUID=C0F00719F0071576
    ID_FS_UUID_ENC=C0F00719F0071576
    ID_FS_TYPE=ntfs
    ID_FS_USAGE=filesystem
    dir_name=C0F00719F0071576
    ID_VENDOR=General
    ID_VENDOR_ENC=General\x20
    ID_VENDOR_ID=8644
    ID_MODEL=USB_Flash_Disk
    ID_MODEL_ENC=USB\x20Flash\x20Disk\x20\x20
    ID_MODEL_ID=8005
    ID_REVISION=1.00
    ID_SERIAL=General_USB_Flash_Disk_152200000000F9BA-0:0
    ID_SERIAL_SHORT=152200000000F9BA
    ID_TYPE=disk
    ID_INSTANCE=0:0
    ID_BUS=usb
    ID_USB_INTERFACES=:080650:
    ID_USB_INTERFACE_NUM=00
    ID_USB_DRIVER=usb-storage
    DEVLINKS=/dev/disk/by-path/platform-ci_hdrc.1-usb-0:1.2:1.0-scsi-0:0:0:0 /dev/disk/by-uuid/C0F00719F0071576 /dev/disk/by-id/usb-General_USB_Flash_Disk_152200000000F9BA-0:0
    ID_PATH=platform-ci_hdrc.1-usb-0:1.2:1.0-scsi-0:0:0:0
    ID_PATH_TAG=platform-ci_hdrc_1-usb-0_1_2_1_0-scsi-0_0_0_0
    TAGS=:systemd:
    USEC_INITIALIZED=6378678
    run: '/bin/mkdir -p /media/C0F00719F0071576'
    run: '/usr/bin/systemd-mount -o relatime,sync --no-block --automount=yes --collect /dev/sda /media/C0F00719F0071576'
    Unload module index
    Unloaded link configuration context.
    

    重新插拔U盘后可以看到:

    ls /media
    C0F00719F0071576
    

    相关文章

      网友评论

        本文标题:udev自动挂载U盘

        本文链接:https://www.haomeiwen.com/subject/vhxyvhtx.html