1 |
On Fri, Nov 30, 2012 at 12:28:01AM +0000, Jorge Almeida wrote |
2 |
|
3 |
> When a pen is inserted, this is what is set: |
4 |
> |
5 |
> ACTION=add |
6 |
> DEVPATH=/devices/pci0000:00/0000:00:04.1/usb1/1-4/1-4:1.0/host12/target12:0:0/12:0:0:0/block/sdd |
7 |
> SUBSYSTEM=block |
8 |
> MAJOR=8 |
9 |
> MINOR=48 |
10 |
> DEVNAME=sdd |
11 |
> DEVTYPE=disk |
12 |
> SEQNUM=1750 |
13 |
|
14 |
[...deletia...] |
15 |
|
16 |
> I'm supposing that the environment on netlink is the same as with the hotplug |
17 |
> mechanism. Would this be true? |
18 |
|
19 |
This is almost exactly what I remember from when I was |
20 |
writing/testing/debugging my automount scripts. The one difference I |
21 |
remember is that when mdev handled it, there was no "DEVNAME" variable, |
22 |
but rather it was "MDEV". But otherwise identical. Two important notes... |
23 |
|
24 |
1) For a USB mass storage device (pen or external hard drive) with N |
25 |
partitions, the hotplug handler will get N+1 events when inserting and |
26 |
also when removing. E.g. if your pen drive has 3 partitions, you'll get |
27 |
4 events... |
28 |
* one for /dev/sdd |
29 |
* one for /dev/sdd1 |
30 |
* one for /dev/sdd2 |
31 |
* one for /dev/sdd3 |
32 |
|
33 |
2) There is one exception to the above rule. Sometimes, Windows will |
34 |
format an entire pen as one large partition, without a partition table. |
35 |
This requires an ugly hack in my script. If DEVTYPE is "disk", it |
36 |
checks for the string "FAT" in the first 512 bytes of the device |
37 |
(bleagh). Here's an excerpt from my script at |
38 |
https://wiki.gentoo.org/wiki/Mdev/Automount_USB/automount |
39 |
|
40 |
###################################################################### |
41 |
if [ "X${ACTION}" == "Xadd" ] ; then |
42 |
# |
43 |
# Flag for mounting if it's a regular partition |
44 |
if [ "X${DEVTYPE}" == "Xpartition" ] ; then |
45 |
partition=1 ; |
46 |
# |
47 |
# Further checks if DEVTYPE is disk; looking for weird setup where the |
48 |
# entire USB key is formatted as one partition, without the standard |
49 |
# partition table. |
50 |
elif [ "X${DEVTYPE}" == "Xdisk" ] ; then |
51 |
# |
52 |
# If it's "disk", check for string "FAT" in first 512 bytes of device. |
53 |
# Flag as a partition if the string is found. |
54 |
if dd if=${MDEV} bs=512 count=1 2>/dev/null | grep "FAT" 1>/dev/null ; then |
55 |
partition=1 |
56 |
fi |
57 |
fi |
58 |
fi |
59 |
###################################################################### |
60 |
|
61 |
The important line is... |
62 |
if dd if=${MDEV} bs=512 count=1 2>/dev/null | grep "FAT" 1>/dev/null ; |
63 |
|
64 |
|
65 |
Would you be OK if the devices were always mounted in /media ? The |
66 |
reason I ask is that my scripts use pmount, which can take an optional |
67 |
label argument. E.g. if MDEV is "sdd1" |
68 |
|
69 |
pmount --umask 007 --noatime /dev/${MDEV} |
70 |
would create /media/sdd1 |
71 |
|
72 |
pmount --umask 007 --noatime /dev/${MDEV} my_pendrive_1 |
73 |
would create /media/my_pendrive_1 |
74 |
|
75 |
I always wanted to add that functionality to the scripts, but never |
76 |
got around to it. |
77 |
|
78 |
> BTW, the idea behind this is: |
79 |
> -- have s6-devd listen to the netlink interface |
80 |
> (http://www.skarnet.org/software/s6-linux-utils/s6-devd.html) |
81 |
> |
82 |
> -- when a device is inserted, s6-devd launches a program that tries to |
83 |
> obtain the serial number, uses it as key to seek a string some_name |
84 |
> in a constant database, and creates the symlink /dev/some_name --> |
85 |
> $DEVNAME. On failure, exec mdev with the environment passed by |
86 |
> the kernel. |
87 |
|
88 |
The way I'm thinking of doing it is to... |
89 |
* launch my script (with minor changes) |
90 |
* on an "add" action invoke your program to look for a match |
91 |
* if a match is found, use the optional label, otherwise use the default |
92 |
name in variable MDEV |
93 |
|
94 |
Actually, if I was writing it, I would add a few lines to my script |
95 |
for the "add" ACTION |
96 |
* label=${MDEV} |
97 |
* look for a "serial" file in the PCI path of the newly-inserted device |
98 |
* if found; then |
99 |
grep through a textfile to match the contents of the "serial" file |
100 |
if matched; then |
101 |
label=custom_name |
102 |
if [ ${#MDEV} -gt 3 ]; then |
103 |
label="${label}_${MDEV:3}" |
104 |
fi |
105 |
fi |
106 |
fi |
107 |
pmount --umask 007 --noatime /dev/${MDEV} ${label} |
108 |
|
109 |
-- |
110 |
Walter Dnes <waltdnes@××××××××.org> |
111 |
We are apparently better off trying to avoid udev like the plague. |
112 |
Linus Torvalds; 2012/10/03 https://lkml.org/lkml/2012/10/3/349 |