LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Mounting USB-storage devices using udev-rules (http://www.linuxquestions.org/questions/slackware-14/mounting-usb-storage-devices-using-udev-rules-4175431668/)

markush 10-11-2012 08:27 AM

Mounting USB-storage devices using udev-rules
 
Hello,

I've created an udev-rule
Code:

SUBSYSTEMS=="usb", KERNEL=="sd?1", SYMLINK+="usbstick", RUN+="/usr/local/bin/mount-usb.sh
which executes the following script:
Code:

#!/bin/bash

# USB-Stick mounten
device=`dmesg | tail -n 15 | grep -e sd.: | sed -n 's/.*sd.:\s\+\?\(.*\)/\1/i p'`
name=`dmesg | tail -n 15 | grep -i product: | sed -n 's/.*product:\s\+\(.*\)/\1/i p' | sed -n 's/\ /_/g p'`


if [ ! -d /media/$name ]; then
      mkdir /media/$name
fi
mount /dev/$device /media/$name

notify-send "USB-Speicherstick eingehängt auf /media/$name"

but the "notify-send" command only works when I execute the script manually as root. Why?

Also it seems that the script is too complicated. Is there another way to get the name of the device-node and the product-name of the device?

I want to unmount the device as a normal user. How can I do this?

Also I want to delete the directory in /media after unmounting the device, how can I do this? I consider to write a script which cleans /media when the system starts.
How did you solve this issues?

BTW: I don't use KDE or XFCE.

Markus

Didier Spaier 10-11-2012 12:08 PM

Quote:

Originally Posted by markush (Post 4802980)
Also it seems that the script is too complicated. Is there another way to get the name of the device-node and the product-name of the device?

lsusb or usb-devices

markush 10-11-2012 12:20 PM

Quote:

Originally Posted by Didier Spaier (Post 4803188)
lsusb or usb-devices

Yes, I know this commands, but they don't show the devicenode.

Markus

guanx 10-11-2012 12:45 PM

Did you try "/dev/bus/usb/${BUSNUM}/${DEVNUM}"? Or simply use "set" to list all environment variables in your script to see which you'd like to use.

markush 10-11-2012 12:59 PM

Well, thanks for the answers, maybe I'm missing something. But the only place where I can see the path for the lastest connected USB-device is the output of dmesg.
I could compare the output of df and "ls /dev/sd*", this would show me which sd?? isn't yet mounted.

I found out that the "notify-send" command only works together with dbus. Now I'll try to find out how this works.

Markus

markush 10-11-2012 01:35 PM

I've changed the script,
Code:

#!/bin/bash

# USB-Stick mounten
sleep 5                                                                         
device=`dmesg | grep -e sd.: | tail -n 1 | sed -n 's/.*sd.:\s\+\?\(.*\)/\1/i p'` 
name=`dmesg | grep -i product: | tail -n 1 | sed -n 's/.*product:\s\+\(.*\)/\1/i p' | sed -n 's/\ /_/g p'`                                                         
                                                                                 
                                                                                 
if [ ! -d /media/$name ]; then                                                   
      mkdir /media/$name                               
fi                                                                               
mount /dev/$device /media/$name                                                 
                                                                 
notify-send "USB-Speicherstick eingehängt auf /media/$name"

I've swaped the tail and grep commands. It works properly now.

As of "notify-send", when I do as root on the commandline "export $(dbus-launch)" and afterwards execute the script manually, the notification works. Unfortunately there seems to be no manpage for dbus, so I couldn't find out how to get this working.

Markus

T3slider 10-11-2012 06:25 PM

I think the proper way to get the device name/information is to pass them as arguments in the udev rule. See `man udev` for some possible variables (eg. %k). I'm no udev expert and I don't have time right now to check what the proper arguments should be, but doing a
Code:

grep RUN /lib/udev/rules.d/*
should give you some examples of how it is done for other devices.


All times are GMT -5. The time now is 09:44 PM.