You are not logged in.

#1 2010-08-30 00:27:24

BaconPie
Member
Registered: 2010-08-11
Posts: 209

[SOLVED] Udev rules, auto mount and display a message

When I switch on an external hard drive or plug in a usb pen I have udev set up to auto mount the device (using pmount for user unmount). I want it to notify me as it sometimes takes a while mounting when I plug in a device using ntfs as the file system.

I modified the udev rule /etc/udev/rules.d/11-media-by-label-with-pmount.rules (mostly copied from the wiki entry) to look like this:

KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"

# Get label
PROGRAM=="/sbin/blkid -o value -s LABEL %N", ENV{dir_name}="%c"
# use basename to correctly handle labels such as ../mnt/foo
PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"

ACTION=="add", ENV{dir_name}!="", RUN+="/bin/su tom -c '/home/tom/test.sh'", RUN+="/bin/su tom -c '/usr/bin/pmount %N %E{dir_name}'"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/su tom -c '/usr/bin/pumount /media/%E{dir_name}'"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# Exit
LABEL="media_by_label_auto_mount_end"

I've added this bit:

RUN+="/bin/su tom -c '/home/tom/test.sh'"

/home/tom/test.sh contains:

notify-send test

and the command

# /bin/su tom -c '/home/tom/test.sh'

makes a pop up window appear when run as root.

When I switch on my device, after a few seconds it auto mounts and I can unmount it with:

$ pumount /media/ExternalHD

But there is no notification displayed. Does anyone have any experience with creating udev rules and using notify-osd?

#################
Solution @ post #13
#################

Last edited by BaconPie (2010-08-31 13:37:27)

Offline

#2 2010-08-30 00:33:59

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Udev rules, auto mount and display a message

Maybe you can execute just one command? Try

ACTION=="add", ENV{dir_name}!="", RUN+="/bin/su tom -c '/home/tom/test.sh'"

The drive won't get mounted but you should see the notification.

Offline

#3 2010-08-30 01:03:03

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [SOLVED] Udev rules, auto mount and display a message

Nope, no notification then. So it has to be a problem with udev calling my script.

I've seen others call scripts from udev. Like here.

Edit: I just tried running the script without using 'su'. It still never worked.

Last edited by BaconPie (2010-08-30 01:06:04)

Offline

#4 2010-08-30 21:03:35

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [SOLVED] Udev rules, auto mount and display a message

Bump, someone must know why my messages aren't being displayed.

Offline

#5 2010-08-30 21:08:40

Coacher
Guest

Re: [SOLVED] Udev rules, auto mount and display a message

You should probably add this to your script:

export DISPLAY=":0"

Last edited by Coacher (2010-08-30 21:09:50)

#6 2010-08-30 21:33:14

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [SOLVED] Udev rules, auto mount and display a message

Ok, I now have:

export DISPLAY=":0"
KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"

# Get label
PROGRAM=="/sbin/blkid -o value -s LABEL %N", ENV{dir_name}="%c"
# use basename to correctly handle labels such as ../mnt/foo
PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"

ACTION=="add", ENV{dir_name}!="", RUN+="su tom -c '/home/tom/test.sh'", RUN+="/bin/su tom -c '/usr/bin/pmount %N %E{dir_name}'"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/su tom -c '/usr/bin/pumount /media/%E{dir_name}'"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# Exit
LABEL="media_by_label_auto_mount_end"

and it still doesn't work. sad

I also tried it with

export DISPLAY=":0.0"

That doesn't work either.

Last edited by BaconPie (2010-08-30 21:35:41)

Offline

#7 2010-08-30 22:13:29

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [SOLVED] Udev rules, auto mount and display a message

Ok, I tried running:

su tom -c '/home/tom/test.sh'

as root from just a terminal in tty2. It never worked because of the display. I then ran

su tom -c 'export DISPLAY=:0.0; /home/tom/test.sh'

and it displayed a notification on tty7 (my main display). So now my rule looks like this:

KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"

# Get label
PROGRAM=="/sbin/blkid -o value -s LABEL %N", ENV{dir_name}="%c"
# use basename to correctly handle labels such as ../mnt/foo
PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"

ACTION=="add", ENV{dir_name}!="", RUN+="su tom -c 'export DISPLAY=:0.0; /home/tom/test.sh'", RUN+="/bin/su tom -c '/usr/bin/pmount %N %E{dir_name}'"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/su tom -c '/usr/bin/pumount /media/%E{dir_name}'"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# Exit
LABEL="media_by_label_auto_mount_end"

After a reboot it still only mounts the drive and does not display a message.

I did all this after checking this thread.

I don't understand how to put udev into verbose though? How can I tell udev to show me errors?

Last edited by BaconPie (2010-08-30 22:14:15)

Offline

#8 2010-08-30 22:29:30

Coacher
Guest

Re: [SOLVED] Udev rules, auto mount and display a message

try running this directly from root:

export DISPLAY=":0.0"; notify-send test

and provide the output.

#9 2010-08-30 22:37:46

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [SOLVED] Udev rules, auto mount and display a message

Um, I already did. Did you read my previous post? From both root in xterm and root in tty2 it displays a notification in tty7.

Offline

#10 2010-08-30 22:49:08

Coacher
Guest

Re: [SOLVED] Udev rules, auto mount and display a message

then just put this line to test.sh and you are done

#11 2010-08-30 23:04:31

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [SOLVED] Udev rules, auto mount and display a message

It never worked.

Offline

#12 2010-08-31 12:57:46

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [SOLVED] Udev rules, auto mount and display a message

I've found this information on running external files from within udev. http://reactivated.net/writing_udev_rul … ternal-run

I've started my test.sh executable file with a #!/bin/bash too. But it still is not working.

Offline

#13 2010-08-31 13:35:58

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: [SOLVED] Udev rules, auto mount and display a message

Woohoo! I just cracked it. Turns out it wasn't running my shell script because I was using su - .

My rule now looks like this:

KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"

# Get label
PROGRAM=="/sbin/blkid -o value -s LABEL %N", ENV{dir_name}="%c"
# use basename to correctly handle labels such as ../mnt/foo
PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"

ACTION=="add", ENV{dir_name}!="", RUN+="/home/tom/bin/notifyDeviceFound %E{dir_name}", RUN+="/bin/su tom -c '/usr/bin/pmount %N %E{dir_name}'"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/su tom -c '/usr/bin/pumount /media/%E{dir_name}'"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# Exit
LABEL="media_by_label_auto_mount_end"

This runs the shell script and passed the label onto it for displaying where the device is about to be mounted. deviceFoundNotification looks like this:

#!/bin/bash
export DISPLAY=":0.0"
notify-send "Device Found" "Mounting at $1..."

I'll improve it with some images but for now, it works great! Thanks to everyone who helped.

Also, about my rule naming. I have two rules starting with the same numbers. From reading the hackaday article (see below) it appears that these are detailing the order of the rules. Does the same number simply mean that they have the same precedence or should I renumber them?

 $ ls -l /etc/udev/rules.d
total 16
-rw-r--r-- 1 root root  640 Aug 29 00:20 11-discs-auto-mount.rules
-rw-r--r-- 1 root root  779 Aug 31 14:25 11-media-by-label-with-pmount.rules
-rw-r--r-- 1 root root  462 Aug 14 22:42 75-cd-aliases-generator.rules.optional
-rw-r--r-- 1 root root 3770 Aug 14 22:42 75-persistent-net-generator.rules.optional

Thanks.

Offline

Board footer

Powered by FluxBB