按照如下步骤:
1. 更改/media/lianro目录权限
使用如下命令:
linaro@bionic:~$ sudo chown linaro:linaro -R /media/linaro/
linaro@bionic:~$ ls /media/ -l
total 8
drwxr-xr-x 4 linaro linaro 4096 Oct 21 17:42 linaro
drwxr-x---+ 2 root root 4096 Oct 21 14:11 root
这样, 就可以把U盘挂载到/media/linaro目录。
2. 创建udev规则
在如下目录创建文件: /etc/udev/rules.d/99-usb-mount.rules, 内容如下:
ACTION=="add", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", ENV{ID_FS_TYPE}!="", RUN+="/bin/systemctl restart usb-mount.service"
ACTION=="remove", SUBSYSTEM=="block", RUN+="/bin/systemctl start usb-unmount@%k.service"
3. 创建systemd service
3.1 usb-mount.service
使用如下步骤:
(1). 创建service
sudo vim /etc/systemd/system/usb-mount.service
内容如下:
linaro@bionic:~$ cat /etc/systemd/system/usb-mount.service
[Unit]
Description=USB auto-mount service
After=dev-sda4.device
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mount_usb.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
(2). 启用service
sudo systemctl enable usb-mount.service
3.2 usb-unmount@.service
- (1). 创建service
sudo vim /etc/systemd/system/usb-unmount@.service
内容如下:
linaro@bionic:~$ cat /etc/systemd/system/usb-unmount@.service
[Unit]
Description=Unmount USB Drive %i
Requires=dev-%i.device
After=dev-%i.device
[Service]
Type=oneshot
ExecStart=/usr/local/bin/unmount_usb.sh /dev/%i
[Install]
WantedBy=multi-user.target
(2). 启动
如下命令
sudo systemctl enable /etc/systemd/system/usb-unmount@.service
4. 创建执行脚本文件
分别创建如下2个脚本文件:
4.1 mount_usb.sh
linaro@bionic:~$ cat /usr/local/bin/mount_usb.sh
#!/bin/bash
device=$(lsblk -p -o NAME,TYPE --noheadings -r | grep -E "part" | grep -v "boot\|rpmb\|loop\|mmcblk" | awk '{print $1}' | tail -n 1)
if [ -n "$device" ]; then
label=$(blkid -o value -s LABEL "$device")
if [ -z "$label" ]; then
label="Unknown"
fi
mount_point="/media/linaro/$label"
mkdir -p "$mount_point"
sudo mount "$device" "$mount_point"
else
echo "cannot find any u disk"
fi
更改为可执行权限, 如下命令:
linaro@bionic:~$ sudo chmod +x /usr/local/bin/mount_usb.sh
linaro@bionic:~$ ls -l /usr/local/bin/mount_usb.sh
-rwxr-xr-x 1 root root 799 Oct 21 15:50 /usr/local/bin/mount_usb.sh
4.2 unmount_usb.sh
linaro@bionic:~$ cat /usr/local/bin/unmount_usb.sh
#!/bin/bash
# get the device name that was removed.
device="$1"
mount_point=$(mount | grep "$device" | awk '{print $3}')
if [ -n "$mount_point" ]; then
sudo umount "$device"
label=$(blkid -o value -s LABEL "$device")
if [ -z "$label" ]; then
label="Unknown"
fi
sudo rmdir "$mount_point"
else
echo "cannot find mounted disk"
fi
更改为可执行权限, 如下:
linaro@bionic:~$ sudo chmod +x /usr/local/bin/unmount_usb.sh
linaro@bionic:~$ ls -l /usr/local/bin/unmount_usb.sh
-rwxr-xr-x 1 root root 1136 Oct 21 17:41 /usr/local/bin/unmount_usb.sh
5. 测试
首先确保udev生效, 可以使用如下命令:
linaro@bionic:~$ sudo udevadm control --reload
linaro@bionic:~$ sudo udevadm trigger
linaro@bionic:~$ systemctl status udev
● systemd-udevd.service - udev Kernel Device Manager
Loaded: loaded (/lib/systemd/system/systemd-udevd.service; static; vendor preset: enabled)
Active: active (running) since Mon 2024-10-21 17:35:15 CST; 42min ago
Docs: man:systemd-udevd.service(8)
man:udev(7)
Main PID: 20237 (systemd-udevd)
Status: "Processing with 20 children at max"
Tasks: 1
CGroup: /system.slice/systemd-udevd.service
└─20237 /lib/systemd/systemd-udevd
当然, 这里也可以reboot设备, 再测试。
分别插入不同的u盘
linaro@bionic:~$ mount | grep media
/dev/sda4 on /media/linaro/DEBIAN type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=936,iocharset=utf8,shortname=mixed,errors=remount-ro)
linaro@bionic:~$ ls /media/linaro/DEBIAN/
'Camtasia Studio2019-64位.zip' SDIO-Fimware tek00000.png tek00002.png wifi-swt6652
MATLAB_Runtime_R2022b_Update_4_win64.zip 'System Volume Information' tek00001.png tek00003.png
linaro@bionic:~$ mount | grep media
linaro@bionic:~$ mount | grep media
/dev/sda1 on /media/linaro/B1 type fuseblk (rw,relatime,user_id=0,group_id=0,allow_other,blksize=4096)
linaro@bionic:~$ ls /media/linaro/B1/
ND_Test over_night 'System Volume Information'
linaro@bionic:~$ mount | grep media
linaro@bionic:~$
从上面的log看, 插入U盘时自动mount到/media/linaro/DEBIAN目录, 其中DEBIAN是U盘的卷标; 拔出U盘后, 自动卸载。