# working udev automount rule

## jodel

Hi

This is my udev rule for automounting any usb stick and sd cards. It works on my arch linux machine but not in gentoo.

the directories are created but not mounted[/code]

```

KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"

ACTION=="add", PROGRAM!="/sbin/blkid -p %N", GOTO="media_by_label_auto_mount_end"

# Open luks partition if necessary

PROGRAM=="/sbin/blkid -o value -p -s TYPE %N", RESULT=="crypto_LUKS", ENV{crypto}="mapper/", ENV{device}="/dev/mapper/%k"

ENV{crypto}!="?*", ENV{device}="%N"

ACTION=="add", ENV{crypto}=="?*", PROGRAM=="/usr/bin/xterm -display :0.0 -e '/usr/sbin/cryptsetup luksOpen %N %k'"

ACTION=="add", ENV{crypto}=="?*", TEST!="/dev/mapper/%k", GOTO="media_by_label_auto_mount_end"

# Global mount options

ACTION=="add", ENV{mount_options}="noatime,users"

# Filesystem specific options

ACTION=="add", PROGRAM=="/sbin/blkid -o value -p -s TYPE %E{device}", RESULT=="vfat|ntfs", ENV{mount_options}="%E{mount_options},utf8,gid=100,umask=002"

# Get label

ACTION=="add", PROGRAM=="/sbin/blkid -o value -p -s LABEL %E{device}", ENV{dir_name}="%c"

ACTION=="add", ENV{dir_name}!="?*", ENV{dir_name}="usbhd-%k"

ACTION=="add", ENV{dir_name}=="?*", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o %E{mount_options} /dev/%E{crypto}%k /media/%E{dir_name}"

ACTION=="remove", ENV{dir_name}=="?*", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"

ACTION=="remove", ENV{crypto}=="?*", RUN+="/usr/sbin/cryptsetup luksClose %k"

LABEL="media_by_label_auto_mount_end"

```

----------

## Heinz

It is working fine on my box, directories are created and mounted with your code.

mfg

----------

## Hu

If you attach a dev-util/strace to udev prior to plugging in the card, do you see it run the mount command?  Does mount print any error messages?

----------

## M

Maybe you can try http://proj.mgorny.alt.pl/uam/?html ? I use it and am very satisfied. Those are also just udev rules and some shell scripts.

Edit: ah, I see now that you use cryptsetup etc. nevermind...

----------

## khippy

Here the same. I have cryptsetup settled at /sbin/ though adjusting your udev-rule hasn't changed a thing.

When plugging in an encrypted drive, the directory at /media is created, but there is no xterm popping up. Even adding sudo before /sbin/cryptsetup has no effect.

Heinz, are you running your desktop as root?

----------

## khippy

I have got it working:

/etc/udev/rules.d/99-crypt.mount.rules

```

KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"

ACTION=="add", PROGRAM!="/sbin/blkid -p %N", GOTO="media_by_label_auto_mount_end"

# Open luks partition if necessary 

PROGRAM=="/sbin/blkid -o value -p -s TYPE %N", RESULT=="crypto_LUKS", ENV{crypto}="mapper/", ENV{device}="/dev/mapper/%k"

ENV{crypto}!="?*", ENV{device}="%N"

ACTION=="add", ENV{crypto}=="?*", PROGRAM=="/root/bin/crypt.mount.sh %N %k"

ACTION=="add", ENV{crypto}=="?*", TEST!="/dev/mapper/%k", GOTO="media_by_label_auto_mount_end"

# Global mount options 

ACTION=="add", ENV{mount_options}="noatime,users"

# Filesystem specific options 

ACTION=="add", PROGRAM=="/sbin/blkid -o value -p -s TYPE %E{device}", RESULT=="vfat|ntfs", ENV{mount_options}="%E{mount_options},utf8,gid=100,umask=002"

# Get label 

ACTION=="add", PROGRAM=="/sbin/blkid -o value -p -s LABEL %E{device}", ENV{dir_name}="%c"

ACTION=="add", ENV{dir_name}!="?*"

ACTION=="add", ENV{dir_name}=="?*", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o %E{mount_options} /dev/%E{crypto}%k /media/%E{dir_name}"

ACTION=="remove", ENV{dir_name}=="?*", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"

ACTION=="remove", ENV{crypto}=="?*", RUN+="/sbin/cryptsetup luksClose %k"

LABEL="media_by_label_auto_mount_end"

```

/root/bin/crypt.mount.sh

```

#!/bin/sh

export DISPLAY=":0.0"

WHAT=$1

WHERE=$2

WHO=<place your desktop username here>

/usr/bin/sudo -u $WHO  /usr/bin/zenity --entry --hide-text=LUKS --text="LUKS open $WHAT at $WHERE"  --title "Luks Passphrase" --width 500 | /usr/bin/sudo /sbin/cryptsetup -t 15 luksOpen $WHAT $WHERE

```

Works perfectly at jwm with rox desktop.

Placing the contents of the crypt.mount.sh external script at /etc/udev/rules.d/99-crypt.mount.rules as the author of this thread did hasn't worked. Putting it out of it solved this issue. I don't know why.

I use gnome-extra/zenity to get a nice desktop window instead of an xterm.

Finally I am done with it. It was one a the longest run ever since using cryptsetup and a windowmanager which isn't shipped with a helper like this.

Thanks for the good start which helped finishing it enourmously.

----------

