nesswaw
Posts: 12
Joined: Tue Oct 03, 2017 3:24 pm

[Resolved ]Mout usb drive with udev

Tue Oct 03, 2017 3:48 pm

Hello,

The goal is to execute a script when you plug in a USB flash drive.

I have Raspbian Stretch Lite (2017-09-07) in my Raspberry.

The scrit must do the following operations:

1) Create a folder
2) Mount usb drive in a folder
3) Copy file from usb key to a folder

Here is my udev rule:

Code: Select all

# /etc/udev/rules.d/a-myusb.rules
ACTION=="add", KERNEL=="sda1", RUN+="/home/pi/usbScript.sh"
Reload and restart:

Code: Select all

sudo udevadm control --reload-rules
sudo service udev restart
My script usbScript.sh

Code: Select all

#!/bin/sh
# /home/pi/usbScript.sh
nohup /home/pi/usbCopy.sh
exit
Script usbCopy.sh

Code: Select all

#!/bin/sh
# /home/pi/usbCopy.sh
export PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin
mkdir /home/pi/testUsbCopy
mount /dev/sda1 /home/pi/usb_drive
This folder /home/pi/usb_drive already exist (for test)

When I insert my USB flash drive, the folder testUsbCopy is well created, but it doesn't mount the flash drive...

If I run the script by hand, it works:

Code: Select all

pi@raspberry: $ ./usbScript.sh
Why does the mounting not work with udev? Any idea?

Thank you in advance
Last edited by nesswaw on Thu Oct 05, 2017 3:54 pm, edited 1 time in total.

epoch1970
Posts: 5203
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Mout usb drive with udev

Wed Oct 04, 2017 12:48 pm

In Jessie days, this would work:

Code: Select all

RUN+="/usr/local/bin/udev_caller.sh /usr/local/bin/some_script.sh $env{my_stuff}"
along with:

Code: Select all

$ cat /usr/local/bin/udev_caller.sh 
#!/bin/sh
# Caller needed to detach the runner from udev
echo "$@" | at now
It could be out-of-date, I think the blessed way of launching a long-running script is now to spawn a systemd unit, with ENV{SYSTEMD_WANTS}="some.service" in place of RUN+=. What you need to put in the unit file to finally call your script is another subject...
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

nesswaw
Posts: 12
Joined: Tue Oct 03, 2017 3:24 pm

Re: Mout usb drive with udev

Thu Oct 05, 2017 3:53 pm

Hello,

I have this solution here: https://stackoverflow.com/questions/451 ... r-45279908

Code: Select all

sudo cp /lib/systemd/system/systemd-udevd.service /etc/systemd/system/systemd-udevd.service
sudo nano /etc/systemd/system/systemd-udevd.service
- Found & modify line: MountFlags=slave to MountFlags=shared
- Save file and restart Raspberry

smilingpoplar
Posts: 1
Joined: Wed Dec 13, 2017 8:21 am

Re: [Resolved ]Mout usb drive with udev

Wed Dec 13, 2017 8:31 am

Changing MountFlags from 'slave' to 'shared' doesn't work for my pi with stretch.
Just comment the MountFlags line in /lib/systemd/system/systemd-udevd.service. It works.

Code: Select all

sudo nano /lib/systemd/system/systemd-udevd.service
- comment out MountFlags line: #MountFlags=slave

Return to “Beginners”