Debian Codegraph @

usb.sh

raw symbols SCIP
1 #!/bin/bash
2 # ==> usb.sh
3 # ==> Script for mounting and installing pen/keychain USB storage devices.
4 # ==> Runs as root at system startup (see below).
5 # ==>
6 # ==> Newer Linux distros (2004 or later) autodetect
7 # ==> and install USB pen drives, and therefore don't need this script.
8 # ==> But, it's still instructive.
9
10 # This code is free software covered by GNU GPL license version 2 or above.
11 # Please refer to http://www.gnu.org/ for the full license text.
12 #
13 # Some code lifted from usb-mount by Michael Hamilton's usb-mount (LGPL)
14 #+ see http://users.actrix.co.nz/michael/usbmount.html
15 #
16 # INSTALL
17 # -------
18 # Put this in /etc/hotplug/usb/diskonkey.
19 # Then look in /etc/hotplug/usb.distmap, and copy all usb-storage entries
20 #+ into /etc/hotplug/usb.usermap, substituting "usb-storage" for "diskonkey".
21 # Otherwise this code is only run during the kernel module invocation/removal
22 #+ (at least in my tests), which defeats the purpose.
23 #
24 # TODO
25 # ----
26 # Handle more than one diskonkey device at one time (e.g. /dev/diskonkey1
27 #+ and /mnt/diskonkey1), etc. The biggest problem here is the handling in
28 #+ devlabel, which I haven't yet tried.
29 #
30 # AUTHOR and SUPPORT
31 # ------------------
32 # Konstantin Riabitsev, <icon linux duke edu>.
33 # Send any problem reports to my email address at the moment.
34 #
35 # ==> Comments added by ABS Guide author.
36
37
38
39 SYMLINKDEV=/dev/diskonkey
40 MOUNTPOINT=/mnt/diskonkey
41 DEVLABEL=/sbin/devlabel
42 DEVLABELCONFIG=/etc/sysconfig/devlabel
43 IAM=$0
44
45 ##
46 # Functions lifted near-verbatim from usb-mount code.
47 #
48 function allAttachedScsiUsb {
49 find /proc/scsi/ -path '/proc/scsi/usb-storage*' -type f |
50 xargs grep -l 'Attached: Yes'
51 }
52 function scsiDevFromScsiUsb {
53 echo $1 | awk -F"[-/]" '{ n=$(NF-1);
54 print "/dev/sd" substr("abcdefghijklmnopqrstuvwxyz", n+1, 1) }'
55 }
56
57 if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ]; then
58 ##
59 # lifted from usbcam code.
60 #
61 if [ -f /var/run/console.lock ]; then
62 CONSOLEOWNER=`cat /var/run/console.lock`
63 elif [ -f /var/lock/console.lock ]; then
64 CONSOLEOWNER=`cat /var/lock/console.lock`
65 else
66 CONSOLEOWNER=
67 fi
68 for procEntry in $(allAttachedScsiUsb); do
69 scsiDev=$(scsiDevFromScsiUsb $procEntry)
70 # Some bug with usb-storage?
71 # Partitions are not in /proc/partitions until they are accessed
72 #+ somehow.
73 /sbin/fdisk -l $scsiDev >/dev/null
74 ##
75 # Most devices have partitioning info, so the data would be on
76 #+ /dev/sd?1. However, some stupider ones don't have any partitioning
77 #+ and use the entire device for data storage. This tries to
78 #+ guess semi-intelligently if we have a /dev/sd?1 and if not, then
79 #+ it uses the entire device and hopes for the better.
80 #
81 if grep -q `basename $scsiDev`1 /proc/partitions; then
82 part="$scsiDev""1"
83 else
84 part=$scsiDev
85 fi
86 ##
87 # Change ownership of the partition to the console user so they can
88 #+ mount it.
89 #
90 if [ ! -z "$CONSOLEOWNER" ]; then
91 chown $CONSOLEOWNER:disk $part
92 fi
93 ##
94 # This checks if we already have this UUID defined with devlabel.
95 # If not, it then adds the device to the list.
96 #
97 prodid=`$DEVLABEL printid -d $part`
98 if ! grep -q $prodid $DEVLABELCONFIG; then
99 # cross our fingers and hope it works
100 $DEVLABEL add -d $part -s $SYMLINKDEV 2>/dev/null
101 fi
102 ##
103 # Check if the mount point exists and create if it doesn't.
104 #
105 if [ ! -e $MOUNTPOINT ]; then
106 mkdir -p $MOUNTPOINT
107 fi
108 ##
109 # Take care of /etc/fstab so mounting is easy.
110 #
111 if ! grep -q "^$SYMLINKDEV" /etc/fstab; then
112 # Add an fstab entry
113 echo -e \
114 "$SYMLINKDEV\t\t$MOUNTPOINT\t\tauto\tnoauto,owner,kudzu 0 0" \
115 >> /etc/fstab
116 fi
117 done
118 if [ ! -z "$REMOVER" ]; then
119 ##
120 # Make sure this script is triggered on device removal.
121 #
122 mkdir -p `dirname $REMOVER`
123 ln -s $IAM $REMOVER
124 fi
125 elif [ "${ACTION}" = "remove" ]; then
126 ##
127 # If the device is mounted, unmount it cleanly.
128 #
129 if grep -q "$MOUNTPOINT" /etc/mtab; then
130 # unmount cleanly
131 umount -l $MOUNTPOINT
132 fi
133 ##
134 # Remove it from /etc/fstab if it's there.
135 #
136 if grep -q "^$SYMLINKDEV" /etc/fstab; then
137 grep -v "^$SYMLINKDEV" /etc/fstab > /etc/.fstab.new
138 mv -f /etc/.fstab.new /etc/fstab
139 fi
140 fi
141
142 exit 0