You are not logged in.

#1 2010-08-31 15:43:14

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

[SOLVED] Udev, pmount and spaces

Ok, after my successful auto udev mount was sorted I decided to give it a whirl with my camera and PSP. The PSP worked fine, due to no label it simply mounted at /media/sdf1 and there was a nice notification of this from notify-osd. The problem comes when I tried to mount my camera. The camera's name 'NIKON D40' has a space in it. So when the udev rule calls pmount, it gives it two arguments for a mount label; NIKON and D40. If I add double quotes around this when running pmount from the command line it mounts fine in the directory /media/NIKON D40. So, the likely solution would be to put double quotes in the udev rule around the label. The only problem is, these double quote conflict with the RUN+="" ones.

I think it's best to show you:

# If the device is not sd[a-z]* then exit
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/deviceFoundNotification '/media/%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"

As you can see I have escaped the double quotes surrounding the directory name. But this just causes pmount to mount the device under its name in /dev (sdf1).

Double quotes on their own do the same thing. As so escaped single quotes

Single quotes on their own end the single quotes for the su command.

How can I pass the device label, with spaces, to the pmount command.

Alternatively how can I get udev to translate all spaces into underscores. Will tr work?

Last edited by BaconPie (2010-08-31 17:49:43)

Offline

#2 2010-08-31 16:14:18

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: [SOLVED] Udev, pmount and spaces

I just use the first udev rule and it automatically creates underscores for spaces for me. Try that rule instead of pmount one and see if it helps.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#3 2010-08-31 16:41:35

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

Re: [SOLVED] Udev, pmount and spaces

Inxsible wrote:

I just use the first udev rule and it automatically creates underscores for spaces for me. Try that rule instead of pmount one and see if it helps.

Which rule would that be?

Offline

#4 2010-08-31 17:31:35

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

Re: [SOLVED] Udev, pmount and spaces

I've tried using the tr program. This will work but I can't use pipes. So the echo just echo's the whole thing:

# If the device is not sd[a-z]* then exit
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"

# Strip all spaces from the device label
PROGRAM=="/bin/echo '%E{dir_name}' | /bin/tr ' ' '_'", ENV{dir_name}="%c"

ACTION=="add", ENV{dir_name}!="", RUN+="/home/tom/bin/deviceFoundNotification '/media/%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 just sets the %E{dir_name} to: <deviceName>_/bin/tr ' ' '_'

Offline

#5 2010-08-31 17:49:29

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

Re: [SOLVED] Udev, pmount and spaces

I created a script to do the translation for me. I think its a bit messy but it works well! Any device can now be auto mounted and unmounted with pmount/pumount even if the device has spaces in its name. Great stuff.

Here is my rule at /etc/udev/rules.d/11-media-by-label-with-pmount.rules:

# If the device is not sd[a-z]* then exit
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"

# Strip all spaces from the device label
#PROGRAM=="/bin/echo '%E{dir_name}' | /bin/tr ' ' '_'", ENV{dir_name}="%c"
PROGRAM=="/home/tom/bin/udevAutomountScripts/translateSpaceToUnderscore '%E{dir_name}'", ENV{dir_name}="%c"

ACTION=="add", ENV{dir_name}!="", RUN+="/home/tom/bin/udevAutomountScripts/deviceFoundNotification '/media/%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"

Here is my ~/bin/udevAutomountScripts/deviceFoundNotification:

#!/bin/bash
export DISPLAY=":0.0"
notify-send -i "/home/tom/.icons/elementary/devices/48/drive-harddisk-usb.svg" "Device Found" "Mounting at $1..."

and ~/bin/udevAutomountScripts/translateSpaceToUnderscore:

#!/bin/bash
# This script translates spaces to underscores. It is for use with the udev
# automount rule as it cannot do this in one line.

/bin/echo $1 | /bin/tr ' ' '_'

Offline

#6 2010-08-31 21:43:43

Cape
Member
From: Mogliano Veneto, Italy
Registered: 2008-11-15
Posts: 105

Re: [SOLVED] Udev, pmount and spaces

Sorry guys, but i'm a bit confused...
I'm still using HAL on my system (because of gnome), but i thought it was possible to use UUID with udev?
Also, can you use a device id (the one you see with lsusb) instead of disk id?

Offline

#7 2010-08-31 22:41:28

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

Re: [SOLVED] Udev, pmount and spaces

Cape wrote:

I'm still using HAL on my system (because of gnome), but i thought it was possible to use UUID with udev?
Also, can you use a device id (the one you see with lsusb) instead of disk id?

What do you mean by using UUID? Mounting the disk based on the UUID? But then i'd have to do that for every device I wanted to plug in. I want to be able to plug in my friends usb stick and it auto-mount automatically. On top of that I want it to tell me its auto mounting and for me to not have to type my password in when un mounting.

I can now do this with udev. The last I heard udev was in favor over HAL...

Offline

#8 2010-09-01 12:10:23

Cape
Member
From: Mogliano Veneto, Italy
Registered: 2008-11-15
Posts: 105

Re: [SOLVED] Udev, pmount and spaces

mmm... this is going a bit off-topic but, you said you're using labels to automount partitions. Do you mean partition labels? Because in that case, if your supposed friend named his stick, let's say "tom's stick" and you told udev to automount "my stick".... well it won't be automounted.
That's why i've added:

Also, can you use a device id (the one you see with lsusb) instead of disk id?

This way, every "usb mass storage device" (a là windòwz wink) would be automounted in the same way HAL was doing the thing.

Now, everybody are saying udev is better than HAL just because HAL is now deprecated (witch is good since HAL was pretty blobby and undocumented), so nobody is developing it anymore and programmers are advised to base their programs on something else, but still a bunch of programs are using it (eg: gnome and kde for automounting devices) mainly because the udev method is not yet mature and/or not yet implemented.

PS: ...am i right?

Offline

#9 2010-09-01 14:07:04

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

Re: [SOLVED] Udev, pmount and spaces

As far as I'm aware, udev detects a new sd[a-z]* and then mounts that using pmount at the location called <device label>. It used pmount to mount the device so if the label was incorrect then it would be an error with pmount, which, if I'm right, would just mount the device under its device name (i.e. sd[a-z]*).

Unless you mean something else... I'm fairly new to this.

Offline

Board footer

Powered by FluxBB