UDEV - Run program on USB flash drive insert
First you need your rule to detect usb storage devices
/etc/udev/rules.d/10-usbmount.rules:
KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/usr/bin/usbdevinserted"
This runs our custom executable shell script /usr/bin/usbdevinserted
:
#!/bin/bash
set 2>&1 >> /tmp/usbdevinfo
This sample script dumps the environment variables which you will need to know which device was found, eg:
DEVLINKS='/dev/disk/by-id/usb-Generic_USB_Flash_Disk-0:0 /dev/disk/by-path/pci-0000:00:13.2-usb-0:2:1.0-scsi-0:0:0:0'
DEVNAME=/dev/sdk
DEVPATH=/devices/pci0000:00/0000:00:13.2/usb2/2-2/2-2:1.0/host29/target29:0:0/29:0:0:0/block/sdk
DEVTYPE=disk
ID_BUS=usb
ID_FS_TYPE=
ID_INSTANCE=0:0
ID_MODEL=USB_Flash_Disk
ID_MODEL_ENC='USB\x20Flash\x20Disk\x20\x20'
ID_MODEL_ID=9380
ID_PART_TABLE_TYPE=dos
ID_PART_TABLE_UUID=61d1df0b
ID_PATH=pci-0000:00:13.2-usb-0:2:1.0-scsi-0:0:0:0
ID_PATH_TAG=pci-0000_00_13_2-usb-0_2_1_0-scsi-0_0_0_0
ID_REVISION=7.76
ID_SERIAL=Generic_USB_Flash_Disk-0:0
ID_TYPE=disk
ID_USB_DRIVER=usb-storage
ID_USB_INTERFACES=:080650:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Generic
ID_VENDOR_ENC='Generic\x20'
ID_VENDOR_ID=058f
MAJOR=8
MINOR=160
SUBSYSTEM=block
Ubuntu run script as user when usb inserted
Indeed, as MiiinimalLogic suggested, the problem was owner of the script.
If you want to run a script as another user via su
, the script should belong to root.
The file may still reside in user's home.
Run script with udev after USB plugged in on RPi
Well you probably described you problem. The mount process is too slow. You can mount your usb device from your script.sh
Also you probably need to disable automatic USB device mount for your system or the specific device only.
If you add a symlink to your udev rule e.g. SYMLINK+="backup"
, then you can mount this device by:
mkdir -p /path/to/foo
mount -t ext4 /dev/backup /path/to/foo
Auto mount usb drive from udev rules and shell script
I think I identified the problem.
Apparently, udev uses a specific namespace, indeed I can see the mount point by printing the content of /proc/<daemon_pid>/mountinfo
where is the pid of the systemd-udevd service.
$ cat /proc/240/mountinfo
[...]
228 43 8:17 / /media/usb/test rw,relatime - vfat /dev/sdb1 rw,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro
$ df
Sys. de fichiers blocs de 1K Utilisé Disponible Uti% Monté sur
udev 1975740 0 1975740 0% /dev
tmpfs 397376 5916 391460 2% /run
/dev/sda2 75733088 69473400 2389532 97% /
tmpfs 1986868 111860 1875008 6% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
tmpfs 1986868 0 1986868 0% /sys/fs/cgroup
tmpfs 397372 28 397344 1% /run/user/112
tmpfs 397372 24 397348 1% /run/user/1001
So the solution should be to force udev to execute the script in root userspace.
I tried the solution I found here https://unix.stackexchange.com/questions/330094/udev-rule-to-mount-disk-does-not-work
However, my system doesn't have a /usr/lib/systemd/system/systemd-udevd.service
file. I created a file /etc/systemd/system/systemd-udevd.service
with content
MountFlags=shared
But with this solution, my system is not able to boot anymore.
Does someone know how I could either execute the script in root userspace or share the mount point with users?
PS : I precise I'm running a 64 bits Debian 9
Solved edit : Finally the file was located at /lib/systemd/system/systemd-udevd.service
. I duplicated it in /etc/systemd/system/systemd-udevd.service
and changed MountFlags=slave
to MountFlags=shared
and now it works perfectly :)
Related Topics
Injecting Code into Executable at Runtime
Can't Run Uwsgi as Root, "Bind(): Permission Denied"
Bash Print Stderr Only, Not Stdout
Git Bash Is Displaying Strange Characters on Windows 7
Installing Mod_Ssl Amazon Linux
How to Sort with Multiple Lines in Bash
How to Manipulate Page Cache in Linux
Running R Scripts or Commands with Interpretor in Unix for Unix-Layman
Ubuntu Equivalent of Yum's Whatprovides, to Find Which Package Provides a File
How to Replace Single Quotes with Another Character in Sed
How to Force a Firefox Page Refresh from Linux Console
How to Check a File Exists and Execute a Command If Not
Can't Install Freetds via Yum Package Manager
How to Send Sigint (Ctrl-C) to Current Remote Process Over Ssh (Without -T Option)