| 1 | #!/bin/bashscip-shell filesystem . . `/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/diskonkeyscip-shell filesystem . . `/dev/diskonkey`. |
| 40 | MOUNTPOINT=/mnt/diskonkeyscip-shell filesystem . . `/mnt/diskonkey`. |
| 41 | DEVLABEL=/sbin/devlabelscip-shell filesystem . . `/sbin/devlabel`. |
| 42 | DEVLABELCONFIG=/etc/sysconfig/devlabelscip-shell filesystem . . `/etc/sysconfig/devlabel`. |
| 43 | IAM=$0 |
| 44 | |
| 45 | ## |
| 46 | # Functions lifted near-verbatim from usb-mount code. |
| 47 | # |
| 48 | function allAttachedScsiUsbscip-shell shell abs-guide 10-4 allAttachedScsiUsb(). { |
| 49 | findscip-shell system . . find. /proc/scsi/scip-shell filesystem . . `/proc/scsi/`. -path '/proc/scsi/usb-storage*scip-shell filesystem . . `/proc/scsi/usb-storage*`.' -type f | |
| 50 | xargsscip-shell system . . xargs. grep -l 'Attached: Yes' |
| 51 | } |
| 52 | function scsiDevFromScsiUsbscip-shell shell abs-guide 10-4 scsiDevFromScsiUsb(). { |
| 53 | echoscip-shell system . . echo. $1 | awkscip-shell system . . awk. -F"[-/]" '{ n=$(NF-1); |
| 54 | print "/dev/sd" substr("abcdefghijklmnopqrstuvwxyz", n+1, 1) }' |
| 55 | } |
| 56 | |
| 57 | if [scip-shell system . . `[`. "${ACTION}" = "add" ] && [scip-shell system . . `[`. -f "${DEVICE}" ]; then |
| 58 | ## |
| 59 | # lifted from usbcam code. |
| 60 | # |
| 61 | if [scip-shell system . . `[`. -f /var/run/console.lockscip-shell filesystem . . `/var/run/console.lock`. ]; then |
| 62 | CONSOLEOWNER=`catscip-shell system . . cat. /var/run/console.lockscip-shell filesystem . . `/var/run/console.lock`.` |
| 63 | elif [scip-shell system . . `[`. -f /var/lock/console.lockscip-shell filesystem . . `/var/lock/console.lock`. ]; then |
| 64 | CONSOLEOWNER=`catscip-shell system . . cat. /var/lock/console.lockscip-shell filesystem . . `/var/lock/console.lock`.` |
| 65 | else |
| 66 | CONSOLEOWNER= |
| 67 | fi |
| 68 | for procEntry in $(allAttachedScsiUsbscip-shell shell abs-guide 10-4 allAttachedScsiUsb().); do |
| 69 | scsiDev=$(scsiDevFromScsiUsbscip-shell shell abs-guide 10-4 scsiDevFromScsiUsb(). $procEntry) |
| 70 | # Some bug with usb-storage? |
| 71 | # Partitions are not in /proc/partitions until they are accessed |
| 72 | #+ somehow. |
| 73 | /sbin/fdiskscip-shell filesystem . . `/sbin/fdisk`. -l $scsiDev >/dev/nullscip-shell filesystem . . `/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 grepscip-shell system . . grep. -q `basenamescip-shell system . . basename. $scsiDev`1 /proc/partitionsscip-shell filesystem . . `/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 [scip-shell system . . `[`. ! -z "$CONSOLEOWNER" ]; then |
| 91 | chownscip-shell system . . 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 ! grepscip-shell system . . grep. -q $prodid $DEVLABELCONFIG; then |
| 99 | # cross our fingers and hope it works |
| 100 | $DEVLABEL add -d $part -s $SYMLINKDEV 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 101 | fi |
| 102 | ## |
| 103 | # Check if the mount point exists and create if it doesn't. |
| 104 | # |
| 105 | if [scip-shell system . . `[`. ! -e $MOUNTPOINT ]; then |
| 106 | mkdirscip-shell system . . mkdir. -p $MOUNTPOINT |
| 107 | fi |
| 108 | ## |
| 109 | # Take care of /etc/fstab so mounting is easy. |
| 110 | # |
| 111 | if ! grepscip-shell system . . grep. -q "^$SYMLINKDEV" /etc/fstabscip-shell filesystem . . `/etc/fstab`.; then |
| 112 | # Add an fstab entry |
| 113 | echoscip-shell system . . echo. -e \ |
| 114 | "$SYMLINKDEV\t\t$MOUNTPOINT\t\tauto\tnoauto,owner,kudzu 0 0" \ |
| 115 | >> /etc/fstabscip-shell filesystem . . `/etc/fstab`. |
| 116 | fi |
| 117 | done |
| 118 | if [scip-shell system . . `[`. ! -z "$REMOVER" ]; then |
| 119 | ## |
| 120 | # Make sure this script is triggered on device removal. |
| 121 | # |
| 122 | mkdirscip-shell system . . mkdir. -p `dirnamescip-shell system . . dirname. $REMOVER` |
| 123 | lnscip-shell system . . ln. -s $IAM $REMOVER |
| 124 | fi |
| 125 | elif [scip-shell system . . `[`. "${ACTION}" = "remove" ]; then |
| 126 | ## |
| 127 | # If the device is mounted, unmount it cleanly. |
| 128 | # |
| 129 | if grepscip-shell system . . grep. -q "$MOUNTPOINT" /etc/mtabscip-shell filesystem . . `/etc/mtab`.; then |
| 130 | # unmount cleanly |
| 131 | umountscip-shell system . . umount. -l $MOUNTPOINT |
| 132 | fi |
| 133 | ## |
| 134 | # Remove it from /etc/fstab if it's there. |
| 135 | # |
| 136 | if grepscip-shell system . . grep. -q "^$SYMLINKDEV" /etc/fstabscip-shell filesystem . . `/etc/fstab`.; then |
| 137 | grepscip-shell system . . grep. -v "^$SYMLINKDEV" /etc/fstabscip-shell filesystem . . `/etc/fstab`. > /etc/.fstab.newscip-shell filesystem . . `/etc/.fstab.new`. |
| 138 | mvscip-shell system . . mv. -f /etc/.fstab.newscip-shell filesystem . . `/etc/.fstab.new`. /etc/fstabscip-shell filesystem . . `/etc/fstab`. |
| 139 | fi |
| 140 | fi |
| 141 | |
| 142 | exit 0 |