我有一台基于 ARM 的 ODROID XU4,运行 Ubuntu 16.04。我想在插入 USB 记忆棒时自动将其挂载到 /media/usbdisk 上。现在我面临的问题是设备似乎没有正确挂载。
我可以使用以下方式手动安装和访问
sudo mount -t ntfs /dev/sda1 usbdisk/
然后我添加以下规则作为 /etc/udev/rules.d/11-media-by-label-auto-mount.rules
KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"
# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/usbdisk", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/usbdisk"
# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/usbdisk", RUN+="/bin/rmdir /media/usbdisk"
# Exit
LABEL="media_by_label_auto_mount_end"
当我添加 USB 记忆棒时,会创建文件夹 /media/usbdisk,但文件系统未正确安装。/media 上的 ls 显示:
/media$ ls -la
ls: cannot access 'usbdisk': Transport endpoint is not connected
total 24
drwxr-xr-x 4 root root 4096 Feb 11 16:51 .
drwxr-xr-x 22 root root 4096 Feb 11 17:03 ..
drwxr-xr-x 3 root root 16384 Jan 1 1970 boot
d????????? ? ? ? ? ? usbdisk
我无法进入它
cd usbdisk
-bash: cd: usbdisk: Transport endpoint is not connected
dmesg 输出为:
[ 221.871579] [c2] usb 4-1.2: new SuperSpeed USB device number 3 using xhci-hcd
[ 221.885847] [c2] usb 4-1.2: Parent hub missing LPM exit latency info. Power management will be impacted.
[ 221.887582] [c2] usb-storage 4-1.2:1.0: USB Mass Storage device detected
[ 221.888007] [c2] scsi2 : usb-storage 4-1.2:1.0
[ 222.886208] [c2] scsi 2:0:0:0: Direct-Access SanDisk Extreme 0001 PQ: 0 ANSI: 6
[ 222.887373] [c2] sd 2:0:0:0: Attached scsi generic sg0 type 0
[ 222.887626] [c2] sd 2:0:0:0: [sda] 250085376 512-byte logical blocks: (128 GB/119 GiB)
[ 222.888235] [c2] sd 2:0:0:0: [sda] Write Protect is off
[ 222.888257] [c2] sd 2:0:0:0: [sda] Mode Sense: 53 00 00 08
[ 222.888830] [c2] sd 2:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[ 222.893348] [c2] sda: sda1
[ 222.897331] [c2] sd 2:0:0:0: [sda] Attached SCSI removable disk
编辑-发现更多细节:
我将 udev 规则最小化为基本上只有 mount 和 unmount 命令。问题仍然存在。使用 journalctl -f 我可以看到来自 udev 的消息,mount 命令因错误退出,返回代码为 21。
更令人惊讶的是 - 我有一个脚本在设备启动时通过 systemd 运行,该脚本应该记录到 USB 磁盘。此脚本成功运行并将一些内容写入到 USB 磁盘。因此显然设备已安装,但我无权访问它。您知道 udev 以何种模式/为哪个用户运行吗?
感谢您的帮助!
答案1
udev
以 身份运行脚本。因此,在挂载块设备之前,root
请更改所创建文件夹的权限。/media/usbdisk