Posts Tagged ‘udev’
poor auto-mounting with udev
Although I should learn for my ongoing exams I found a few moments to try getting auto-mounting on the console. (Note: This is rather a post for me to remember than anything useful 😉 )
As I didn’t find a properly working solution for my minimal installation (some troubles with thunar and I wanted also for the console), I decided to do a poor alternative on my own. I tried to make it almost as convenient as in GNOME (or KDE…) as I was sick of always typing “sudo mount /dev/sd?1 /mnt” which also led to some troubles when using more than one stick.
It’s no more than two small scripts combined with two rules of udev (save as /etc/udev/rules/01-YOURNAME.rules and modify the script path):
BUS=="usb", KERNEL=="sd??", ACTION=="add", RUN+="/home/flo/.scripts/udev/mount.sh %k"
BUS=="usb", KERNEL=="sd??", ACTION=="remove", RUN+="/home/flo/.scripts/udev/umount.sh %k"
These lines execute mount.sh when attaching an USB device and execute umount.sh when removing it. “%k” gives the device name (e.g. sdb1) as parameter for the script.
mount.sh:
#!/bin/bash
label=$(ls -l /dev/disk/by-label/ | grep $1)
if [ $? -eq 0 ]; then #found the device
label=$(echo $label | sed -e 's/.*:...//g' | sed -e 's/.->.*//g')
else
label=$(ls -l /dev/disk/by-uuid | grep $1 | sed -e 's/.*:...//g' | sed 's/.->.*//g')
fi
mkdir /media/$label
mount /dev/$1 /media/$label -o sync,gid=46,umask=007
This script tries to find the label of the partition. If not possible it uses the UUID as a name for the mount directory, which is /media/{UUID|LABEL}. It finally mounts the device into this newly created folder, setting the “sync” option, which makes it save to remove the device without umounting it. “gid=46,umask=007” means, that only root and members of the group “plugdev” are allowed to acces the device – this is needed for FAT formated sticks.
umount.sh:
#!/bin/bash
label=$(mount | grep $1 | sed -e 's/.*\/media\///g' | sed -e 's/.type.*//g' )
umount -l /dev/$1
rmdir /media/$label
This script umounts the device and removes the folder, that was created.
Important note: These scripts should *not* be editable by users as they are executed with root rights – and therefor might be used to “hack” the system when modified!
I have no idea, how well these lines work with hard drives containing several partitions (I don’t have one here at the moment) or with encrypted partitions. I’ll have a look at that after the exams (in roughly 2 weeks).
As always: Use with care, I’m not responsible for any harm 😉