LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 10-11-2012, 08:27 AM   #1
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
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
 
Old 10-11-2012, 12:08 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,055

Rep: Reputation: Disabled
Quote:
Originally Posted by markush View Post
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
 
Old 10-11-2012, 12:20 PM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Didier Spaier View Post
lsusb or usb-devices
Yes, I know this commands, but they don't show the devicenode.

Markus
 
Old 10-11-2012, 12:45 PM   #4
guanx
Senior Member
 
Registered: Dec 2008
Posts: 1,176

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 10-11-2012, 12:59 PM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-11-2012, 01:35 PM   #6
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-11-2012, 06:25 PM   #7
T3slider
Senior Member
 
Registered: Jul 2007
Distribution: Slackware64-14.1
Posts: 2,367

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
VirtualBox USB devices history effect: fstab and udev rules catkin Slackware 3 11-07-2010 09:48 AM
slackware 12.0, udev rules for usb-dev works, but mounting does not work correct paalmarker Linux - General 5 03-30-2010 01:51 AM
Mounting USB storage devices COKE CAN Debian 18 08-02-2005 02:14 AM
mounting usb storage devices blues patitoconejita Linux - Hardware 7 03-29-2005 07:24 AM
Automatically Mounting USB Storage Devices??? nalmo Linux - General 6 08-13-2004 03:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration