全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 1281|回复: 7

[疑问] 请教debian插入U盘自动挂载

[复制链接]
发表于 2023-4-20 12:15:36 | 显示全部楼层 |阅读模式
1、请教debian插入U盘自动挂载,代码网上找的,试了不成功,插入U盘可以执行脚本(可以看到生成的日子文件),但是没有挂载,好像没有执行 mount $DEVNAME $MDIR 这个命令

2、把mount $DEVNAME $MDIR 换成 mount /dev/sda /www 也不行,但是mount /dev/sda /www在终端是可以挂载的


  1. #!/bin/sh
  2. LOG=/mnt/usb-000.log
  3. logtime=$(date)
  4. # 挂载位置
  5. MDIR=/www
  6. echo "$logtime:$DEVPATH requesting $ACTION" >> $LOG
  7. if [ "$ACTION" = "add" ]; then
  8.         if [ ! -d $MDIR ]; then
  9.                 echo "the $MDIR is not exist,mkdir" >>$LOG
  10.                 mkdir -p $MDIR
  11.         fi
  12.         mount $DEVNAME $MDIR
  13.         echo "$logtime:mount $DEVNAME $MDIR" >> $LOG
  14.         echo "ok-- $sda---$ACTION---$DEVNAME" >> "/mnt/333.txt"

  15. elif [ "$ACTION" = "remove" ]; then
  16.         umount $MDIR
  17.         rmdir $MDIR
  18. fi
复制代码

  1. KERNEL=="sd[a-z]*", SUBSYSTEM=="block", RUN+="/mnt/usbmount.sh"
复制代码


日志文件
  1. Thu Apr 20 11:33:11 CST 2023:/devices/platform/soc/78d9000.usb/ci_hdrc.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda requesting remove
  2. Thu Apr 20 11:33:15 CST 2023:/devices/platform/soc/78d9000.usb/ci_hdrc.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda requesting add
  3. Thu Apr 20 11:33:15 CST 2023:mount /dev/sda /www
  4. Thu Apr 20 11:41:48 CST 2023:/devices/platform/soc/78d9000.usb/ci_hdrc.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda requesting remove
  5. Thu Apr 20 11:41:52 CST 2023:/devices/platform/soc/78d9000.usb/ci_hdrc.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda requesting add
  6. Thu Apr 20 11:41:52 CST 2023:mount /dev/sda /www
  7. Thu Apr 20 11:52:53 CST 2023:/devices/platform/soc/78d9000.usb/ci_hdrc.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda requesting remove
  8. Thu Apr 20 11:52:56 CST 2023:/devices/platform/soc/78d9000.usb/ci_hdrc.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda requesting add
  9. Thu Apr 20 11:52:56 CST 2023:mount /dev/sda /www
  10. Thu Apr 20 11:55:41 CST 2023:/devices/platform/soc/78d9000.usb/ci_hdrc.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0/block/sda requesting add
  11. Thu Apr 20 11:55:41 CST 2023:mount /dev/sda /www
复制代码


我知道论坛有很多脚本大师,请帮忙解答一下,谢谢!
发表于 2023-4-20 12:35:08 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2023-4-20 14:46:42 | 显示全部楼层
挂载前sleep 3s试试

mount路径写全
比如/sbin/mount
发表于 2023-4-20 14:58:23 | 显示全部楼层
本帖最后由 larry 于 2023-4-20 15:00 编辑

You can configure your Debian system to automatically mount USB drives when they are inserted using the udev and systemd rules. Here's a step-by-step guide:

1、Install udev and systemd (if not already installed):

  1. sudo apt-get update
  2. sudo apt-get install udev systemd
复制代码


2、Create a udev rule:

Create a new udev rule file:

  1. sudo nano /etc/udev/rules.d/99-automount-usb.rules
复制代码


Add the following content to the file:

  1. ACTION=="add", KERNEL=="sd*[!0-9]|sr*", ENV{DEVTYPE}=="partition", ENV{ID_FS_TYPE}!="", ENV{ID_FS_TYPE}!="swap", RUN+="/bin/systemd-mount --no-block --automount=yes --collect $env{DEVNAME} /media/$env{DEVNAME}"
  2. ACTION=="remove", KERNEL=="sd*[!0-9]|sr*", ENV{DEVTYPE}=="partition", RUN+="/bin/systemd-mount --umount $env{DEVNAME}"
复制代码


Save and exit the file (Ctrl+X, Y, Enter).

3、Reload udev rules:

  1. sudo udevadm control --reload-rules
复制代码


4、Create a mount point directory:

  1. sudo mkdir -p /media
复制代码


5、Test the setup:

Insert a USB drive and check if it's mounted automatically under /media. You can use the df -h command to list all mounted filesystems.

Notes:

  • The above configuration mounts USB drives to /media/<device-name>. You can change the mount point by editing the udev rule file.
  • This setup assumes that your USB drive uses a supported filesystem, such as FAT32, NTFS, or ext4. If your drive uses a different filesystem, you might need to install additional packages to enable support for that filesystem.
  • To unmount a USB drive, you can use the umount /media/<device-name> command before you remove it from the system.
 楼主| 发表于 2023-4-20 15:07:42 | 显示全部楼层
CMBCHINA 发表于 2023-4-20 14:46
挂载前sleep 3s试试

mount路径写全

一样的,挂载目录日期有在改变,说明挂载命令有执行,可就是没有挂上去。

 楼主| 发表于 2023-4-20 15:31:03 | 显示全部楼层
larry 发表于 2023-4-20 14:58
You can configure your Debian system to automatically mount USB drives when they are inserted using  ...

谢谢!
发表于 2023-4-20 15:35:23 | 显示全部楼层
还写个啥脚本,有个东西叫udisks2
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-28 14:57 , Processed in 0.063186 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表