Hotplug USB Drive, automount and udev

Hello,

the USB drive mounts and unmounts into /media folder, but only read-only.
How do I have to change the udev-rule, to mount the device with read-write access.
Or do I have to create a seperate rule/script and how would it look like?
Changing /etc/fstab is not an option for me, because when the USB drive is unplugged, the folder (named by usb-drive label) is not deleted. I use the creation/deletion of the folder to recognize if an USB drive is plugged in or not.

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Hello,

To ensure your USB drive mounts with read-write access using udev rules, you can create or modify a udev rule to specify the desired mount options. Here’s a step-by-step guide to help you achieve this:

  1. Identify the USB Drive:

    • First, plug in your USB drive and use the lsblk or dmesg command to identify the device name (e.g., /dev/sda1).
  2. Create a Udev Rule:

    • Create a new udev rule file in /etc/udev/rules.d/, for example, 99-usb-mount.rules.
    • Add a rule to match your USB drive and specify the mount options. Here’s an example rule:
      ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z][0-9]", ENV{ID_FS_TYPE}!="", RUN+="/usr/bin/mount -o rw,uid=1000,gid=1000 /dev/%k /media/%E{ID_FS_LABEL}"
      
    • This rule mounts the USB drive with read-write permissions and sets the owner to a specific user (replace uid=1000,gid=1000 with the appropriate user and group IDs).
  3. Reload Udev Rules:

    • After creating or modifying the rule, reload the udev rules with the following commands:
      sudo udevadm control --reload-rules
      sudo udevadm trigger
      
  4. Test the Configuration:

    • Unplug and replug your USB drive to test if it mounts with the specified options.

This approach allows you to manage USB drive mounting behavior without modifying /etc/fstab, maintaining the dynamic creation and deletion of mount points based on the drive’s presence.

I hope this helps you achieve the desired functionality for your USB drive.

Best regards,
Toradex AI Assistant

Please, also check if these links can help you solve your problem:

Define auto mountpoint for usb storage and hot plug.

The new udev-rule is not working for me.

Torizon 6.8

udev-rules:

10-imx.rules
10-toradex-net-rename.rules
90-toradex-gpio.rules
91-toradex-i2cdev.rules
92-toradex-spidev.rules
93-toradex-backlight.rules
94-toradex-pwm.rules
99-toradex.rules
99-usb-mount.rules
bootpart-automount.rules
touchscreen.rules

I can not write to the usb-drive with user ‘torizon’ (uid=1000,gid=1000).

udev monitor output: (USB Drive plugged in)
UDEV [492.211791] add /devices/virtual/workqueue/scsi_tmf_0 (workqueue)
UDEV [492.214269] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1 (usb)
UDEV [492.219631] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0 (usb)
UDEV [492.224808] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0 (scsi)
UDEV [492.229486] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/scsi_host/host0 (scsi_host)
UDEV [492.233554] bind /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0 (usb)
UDEV [492.237709] bind /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1 (usb)
UDEV [493.278307] add /devices/virtual/bdi/8:0 (bdi)
UDEV [493.278931] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0 (scsi)
UDEV [493.284924] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0/0:0:0:0 (scsi)
UDEV [493.291791] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0/0:0:0:0/scsi_device/0:0:0:0 (scsi_device)
UDEV [493.293851] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0 (scsi_disk)
UDEV [493.295799] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0/0:0:0:0/bsg/0:0:0:0 (bsg)
UDEV [493.374524] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0/0:0:0:0/block/sda (block)
UDEV [493.582446] add /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0/0:0:0:0/block/sda/sda1 (block)
UDEV [493.591507] bind /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0/0:0:0:0 (scsi)
UDEV [493.697732] change /devices/platform/soc@0/32f10108.usb/38200000.usb/xhci-hcd.1.auto/usb1/1-1/1-1.1/1-1.1:1.0/host0/target0:0:0/0:0:0:0/block/sda/sda1 (block)

torizon@verdin-imx8mp$ cat /proc/mounts
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
tmpfs /run tmpfs rw,nosuid,nodev 0 0
devtmpfs /dev devtmpfs rw,relatime,size=1731960k,nr_inodes=432990,mode=755 0 0
devpts /dev/pts devpts rw,relatime,gid=5,mode=620,ptmxmode=666 0 0
/dev/disk/by-label/otaroot /sysroot ext4 rw,relatime 0 0
/dev/disk/by-label/otaroot / ext4 rw,relatime 0 0
/dev/disk/by-label/otaroot /boot ext4 rw,relatime 0 0
/dev/disk/by-label/otaroot /usr ext4 ro,relatime 0 0
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,size=4096k,nr_inodes=1024,mode=755 0 0
cgroup2 /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,name=systemd 0 0
pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
bpf /sys/fs/bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,relatime,pagesize=2M 0 0
mqueue /dev/mqueue mqueue rw,nosuid,nodev,noexec,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,nosuid,nodev,noexec,relatime 0 0
tracefs /sys/kernel/tracing tracefs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /tmp tmpfs rw,nosuid,nodev,size=1999372k,nr_inodes=1048576 0 0
fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0 0
configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0
/dev/disk/by-label/otaroot /var ext4 rw,relatime 0 0
tmpfs /var/volatile tmpfs rw,relatime 0 0
tmpfs /run/user/1000 tmpfs rw,nosuid,nodev,relatime,size=399872k,nr_inodes=99968,mode=700,uid=1000,gid=1000 0 0
/dev/sda1 /var/rootdirs/media/CETA vfat rw,nosuid,nodev,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro 0 0

Greetings @Lars.ceta,

Just a suggestion. So the most straight-forward way I know to indicate mount options for USB drives automatically is via /etc/fstab. Now you say can’t use this method because:

Changing /etc/fstab is not an option for me, because when the USB drive is unplugged, the folder (named by usb-drive label) is not deleted. I use the creation/deletion of the folder to recognize if an USB drive is plugged in or not.

Would it not be possible to just change your detection method and then use /etc/fstab. For example instead of looking at the folder why not look if there is a listed mount or if there is a /dev/sda* device connected as an indication that a USB drive is plugged. Or is there a reason you can’t do this in your system design?

Best Regards,
Jeremias

I debugged udev-rules and found this lines:

Jan 30 07:14:32 verdin-imx8mp-14707144 systemd-udevd[1165]: sda1: ‘/usr/bin/mount -o rw,uid=1000,gid=1000 /dev/sda1 /media/CETA’(err) ‘mount: /media/CETA: mount point is not a directory.’
Jan 30 07:14:32 verdin-imx8mp-14707144 systemd-udevd[1165]: sda1: Process ‘/usr/bin/mount -o rw,uid=1000,gid=1000 /dev/sda1 /media/CETA’ failed with exit code 32.
Jan 30 07:14:32 verdin-imx8mp-14707144 systemd-udevd[1165]: sda1: Command “/usr/bin/mount -o rw,uid=1000,gid=1000 /dev/sda1 /media/CETA” returned 32 (error), ignoring

Maybe I need a script which creates the directory before mounting the device. Can anyone provide help?

Greeting @jeremias.tx,
it would be possible to use fstab. I did that already but was not happy with this solution. If I cannot find a udev-solution, I will switch back to fstab.

Best regards,
Lars

Maybe I need a script which creates the directory before mounting the device. Can anyone provide help?

With udev rules you can designate a script/external program to be executed upon an event. In theory you could have a script that creates the mount directory and then mount it. More details here: You would just need to have the RUN argument point to your program/script.

Best Regards,
Jeremias