1 |
A small email to sumarize some thoughts about udev, removable |
2 |
storage and incron to make the job of hal and re-invent the wheel. |
3 |
|
4 |
---- context (skipable) ----- |
5 |
there is ONE common thing where gnome is quicker than bash : mounting |
6 |
an unknown usb stick. |
7 |
|
8 |
For my own stuff, I already used labels and uuids for ages but for |
9 |
stranger devices it's not possible. |
10 |
|
11 |
So those are the steps I did thousands of times : |
12 |
- grab root (the device can be sdc3 as sde1 so there is no specific fstab entry) |
13 |
- [dmesg|tail to check the device name or bash completion] |
14 |
- mount -o users,rw /dev/new_device_X /mnt/remusb |
15 |
- ^D |
16 |
- cd /mnt/remusb |
17 |
|
18 |
So what I needed was to get my plugged devices automounted to have |
19 |
quickly a terminal in the directory. (I use fluxbox without idesk or |
20 |
whatever) |
21 |
|
22 |
Here comes the solution I found : |
23 |
|
24 |
---- configuration ----------- |
25 |
|
26 |
FSTAB : some lines of /etc/fstab |
27 |
|
28 |
LABEL=docus /home/ftp/pub/Docus ext3 users,auto 0 0 |
29 |
UUID=0467-FFC1 /mnt/mp3 vfat users,rw,noatime 0 0 |
30 |
|
31 |
UDEV : some lines of /etc/udev/rules.d/80-custom.rules |
32 |
|
33 |
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_LABEL}=="docus", "RUN+="/bin/mount -L docus" |
34 |
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="0467-FFC1", RUN+="/bin/mkdir -p /mnt/mp3", RUN+="/bin/mount -U 0467-FFC1" |
35 |
|
36 |
Ok, so this UDEV rules only make KNOWN devices automounted (but |
37 |
nothing for unknown devices) |
38 |
|
39 |
I then needed to have unknown devices mounted into /mnt/remusb, so the |
40 |
other devices are given a $ENV{} variable. |
41 |
|
42 |
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_LABEL}=="docus", RUN+="/bin/mount -L docus", ENV{custom_rules}="add" |
43 |
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="0467-FFC1", RUN+="/bin/mkdir -p /mnt/mp3", RUN+="/bin/mount -U 0467-FFC1", ENV{custom_rules}="add" |
44 |
# so I can add : |
45 |
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[b-z][1-9]", ENV{custom_rules}=="", RUN="mkdir -p /mnt/remusb", RUN+="mount /dev/%k /mnt/remusb" |
46 |
|
47 |
But as I have many more removable medias, I factorised the code and used |
48 |
exclusively ENV{} like the following : |
49 |
|
50 |
## definitions examples : |
51 |
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_LABEL}=="integral", ENV{mount_point}="integral", ENV{mount_cmd}="-L integral", ENV{custom_rules}="add"ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_LABEL}=="docus", ENV{mount_cmd}="-L docus", ENV{custom_rules}="add" |
52 |
|
53 |
## actions : |
54 |
ENV{custom_rules}=="add", ENV{mount_point}!="", RUN+="/bin/mkdir -p /mnt/$env{mount_point}" |
55 |
ENV{custom_rules}=="add", ENV{mount_cmd}!="", RUN+="/bin/mount $env{mount_cmd}" |
56 |
|
57 |
-------------------- |
58 |
Now I want to be notified ! |
59 |
2 solutions : |
60 |
- the bad one, udev runs as root : who && su -l my_user && DISPLAY=:0 osd_cat "new |
61 |
device" |
62 |
- another one, use a per-session user daemon (as I ignored the hal |
63 |
documentation I didn't even looked at dbus-launch or others hal |
64 |
"plugins"). |
65 |
|
66 |
I decided to be notified by a file creation : INCRON |
67 |
UDEV has to create a file in a directory monitored by incron. |
68 |
[The creation a file whose name gives some useful information monitored by a daemon |
69 |
is a kind a replacement of a real bus messaging system] |
70 |
|
71 |
So, in the udev rule for my mp3 player I add the following : |
72 |
RUN+="/bin/mkdir /tmp/.incron.device/%k" |
73 |
(in fact I added it as the last one of the "actions" part of the udev rules file) |
74 |
|
75 |
For my user I create a incrontab like : |
76 |
/tmp/.incron.device IN_CREATE,IN_DELETE /home/myuser/.automount-script.sh $@ $# $% |
77 |
So it's now possible to control what to do as udev notify us by incron, |
78 |
better, monitoring /proc/partitions or whatever is useless as udev passes the |
79 |
device name to incron which forward it to the script. |
80 |
|
81 |
Example of automount-script.sh : |
82 |
if [[ $theevent =~ IN_CREATE ]]; then |
83 |
destdir=$(mount|sed -n "s;^/dev/$thedevice on \(.*\) type.*$;\1;p") |
84 |
urxvtc -cd $destdir |
85 |
fi |
86 |
|
87 |
In that way I have a terminal spawned in the mount point each time |
88 |
a removable device is plugged. |
89 |
|
90 |
I attached my fstab, udev rule, incrontab -l and custom script. |
91 |
|
92 |
I would be curious to read some advices about this architecture. |
93 |
|
94 |
I also have 2 questions : |
95 |
- what's the way to have a incremental number for unknown devices like |
96 |
/mnt/remusb1 then /mnt/remusb2 instead of /mnt/remusb (within udev) ? |
97 |
- where to put the creation of /tmp/.incron.device in the init scripts |
98 |
to be sure it will be created BEFORE incron starts (without having to |
99 |
create a new startup script) ? |
100 |
|
101 |
Raph |