]> oss.titaniummirror.com Git - ovzbpc.git/commitdiff
Dynamically find SCSIDEV
authorR. Steve McKown <rsmckown@gmail.com>
Thu, 7 Feb 2013 23:27:47 +0000 (16:27 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Thu, 7 Feb 2013 23:27:47 +0000 (16:27 -0700)
Working from the expected device, BASEDEV, determine the SCSI host value
by interrogating the /sys filesystem.  This prevents registration from
working, but now the device should automatically register when plugged
in (hot swap drive on a sil3132 chipset).  This allows the device to be
properly unregistered regardless of the attached scsi host.

esata

diff --git a/esata b/esata
index f95995aee2f8ed1e653c21d585754d64e32bab63..0fd5b4a6887498c18472c99e7b0766ece13e4526 100755 (executable)
--- a/esata
+++ b/esata
@@ -8,11 +8,25 @@
 BASEDEV=/dev/sdc
 PARTITION=2
 MNT=/media/esata
-SCSIDEV=3 # This can change based on OS, bus enumeration, etc.
 # ----
 DEV=${BASEDEV}${PARTITION}
 DELAY=5
 
+error_exit() {
+    echo "$0: $1" >&2
+    exit 1
+}
+
+have_root() {
+    [ $(whoami) == "root" ] && return 0 || return 1
+}
+
+scsi_dev() {
+    local d=$(basename "$BASEDEV")
+    ls /sys/block/$d/device/scsi_disk | 2>/dev/null \
+       sed -e "s/^\([0-9a-f]*\):.*$/\1/"
+}
+
 registered() {
     if [ -b "$BASEDEV" ]; then
        [ -n "$VERBOSE" ] && echo "registered"
@@ -44,9 +58,10 @@ mounted() {
 }
 
 doregister() {
+    have_root || error_exit "need root privileges to register device"
     registered && return 0
     [ -n "$VERBOSE" ] && echo "register SATA device"
-    scsiadd -a $SCSIDEV 0 0 0 >/dev/null || return 1
+    scsiadd -a $(scsi_dev) 0 0 0 >/dev/null || return 1
     sleep $DELAY
     registered || return 1
     [ -n "$VERBOSE" ] && echo "register ok"
@@ -54,6 +69,7 @@ doregister() {
 }
 
 domount() {
+    have_root || error_exit "need root privileges to mount device"
     mounted && return 0
     doregister || return 1
     partitioned || return 1
@@ -65,9 +81,10 @@ domount() {
 }
 
 dounregister() {
+    have_root || error_exit "need root privileges to unregister device"
     registered || return 0
     [ -n "$VERBOSE" ] && echo "unregister SATA device"
-    scsiadd -r $SCSIDEV 0 0 0 >/dev/null || return 1
+    scsiadd -r $(scsi_dev) 0 0 0 >/dev/null || return 1
     sleep $DELAY
     registered && return 1
     [ -n "$VERBOSE" ] && echo "unregister ok"
@@ -76,6 +93,7 @@ dounregister() {
 
 doumount() {
     if mounted; then
+       have_root || error_exit "need root privileges to unmount device"
        [ -n "$VERBOSE" ] && echo "unmount SATA $DEV"
        umount "$MNT" || return 1
        mounted && return 1
@@ -103,16 +121,14 @@ elif [ "$1" = "umount" -o "$1" = "unmount" ]; then
     if doumount; then
        echo "esata is unmounted"
     else
-       echo "$0: cannot unmount esata" >&2
-       exit 1
+       error_exit "cannot unmount esata"
     fi
 elif [ "$1" = "register" ]; then
     if doregister; then
        echo "esata drive registered as $BASEDEV"
        partitioned || echo "WARNING: drive is not partitioned"
     else
-       echo "$0: cannot register esata" >&2
-       exit 1
+       error_exit "cannot register esata"
     fi
 elif [ "$1" = "status" ]; then
     if mounted; then