Add a device by its serial number and not only /dev/sdX.
This is important for persistent binding to physical devices.
The device mapping may change between reboots due different
bringup time of the storage devices.

Signed-off-by: Doron Shoham <[EMAIL PROTECTED]>
---
 scripts/tgt-admin |   60 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/scripts/tgt-admin b/scripts/tgt-admin
index 9083417..8442474 100755
--- a/scripts/tgt-admin
+++ b/scripts/tgt-admin
@@ -70,6 +70,22 @@ if ($show == 1) {
        exit;
 }
 
+# Create a hash of all the scsi id and their mapped devices
+my %sn_hash=();
+sub map_devices {
+       my @sd_list=();
+       my $sg_list=`sg_map -i -x`;
+       while ($sg_list=~/(\/dev\/sd\w)/sgi) {
+               push(@sd_list,$1);
+       }
+       foreach my $sd (@sd_list) {
+               my $inq=`sg_inq $sd`;
+               if ($inq=~/Unit serial number:\s*(\w+)/) {
+                       $sn_hash{$1}=$sd;
+               }
+       }
+}
+
 # Check if alternative configuration file was given
 if ($alternate_conf ne 0) {
        # Check if alternative configuration file exist
@@ -134,6 +150,7 @@ sub add_targets {
 
 # Process options from the config file
 sub process_options {
+       &map_devices;
        if ( $option eq "backing-store" ) {
         # if we have one command, force it to be an array anyway
                unless (ref($value) eq 'ARRAY') {
@@ -142,6 +159,10 @@ sub process_options {
                my @value_arr = @$value;
                my $i = 1;
                foreach my $backing_store (@value_arr) {
+                       # Check if a serial number was given
+                       if ($backing_store!~/\/dev\//) {
+                               $backing_store=$sn_hash{$backing_store};
+                       }
                        # Check if device exists
                        if ( -e $backing_store) {
                                execute("tgtadm --lld iscsi --op new --mode 
logicalunit --tid $next_tid --lun $i -b $backing_store");
@@ -167,22 +188,33 @@ sub process_options {
                my @value_arr = @$value;
                my $i = 1;
                foreach my $direct_store (@value_arr) {
-                       $inq=`sg_inq $direct_store`;
-                       if ($inq=~/Vendor identification:\s*(\w+)\s*\n*Product 
identification:\s*([\w\s\/\-]+)\n\s*\n*Product revision 
level:\s*(\w*)\s*\n*Unit serial number:\s*(\w+)/)
-                       {
-                               $vendor_id="$1";
-                               $prod_id="$2";
-                               $prod_rev="$3";
-                               $scsi_serial="$4";
+                       # Check if a serial number was given
+                       if ($direct_store!~/\/dev\//) {
+                               $direct_store=$sn_hash{$direct_store};
                        }
-                       $vendor_id =~ s/\s+$//;
-                       $prod_id =~ s/\s+$//;
-                       $prod_rev =~ s/\s+$//;
-                       $scsi_serial =~ s/\s+$//;
+                       # Check if device exists
+                       if ( -e $direct_store) {
+                               $inq=`sg_inq $direct_store`;
+                               if ($inq=~/Vendor 
identification:\s*(\w+)\s*\n*Product 
identification:\s*([\w\s\/\-]+)\n\s*\n*Product revision 
level:\s*(\w*)\s*\n*Unit serial number:\s*(\w+)/)
+                               {
+                                       $vendor_id="$1";
+                                       $prod_id="$2";
+                                       $prod_rev="$3";
+                                       $scsi_serial="$4";
+                               }
+                               $vendor_id =~ s/\s+$//;
+                               $prod_id =~ s/\s+$//;
+                               $prod_rev =~ s/\s+$//;
+                               $scsi_serial =~ s/\s+$//;
 
-                       execute("tgtadm --lld iscsi --op new --mode logicalunit 
--tid $next_tid --lun 1 -b $direct_store");
-                       execute("tgtadm --lld iscsi --op update --mode 
logicalunit --tid  $next_tid --lun 1 --params 
vendor_id=\"$vendor_id\",product_id=\"$prod_id\",product_rev=\"$prod_rev\",scsi_sn=\"$scsi_serial\"");
-                       $i += 1;
+                               execute("tgtadm --lld iscsi --op new --mode 
logicalunit --tid $next_tid --lun 1 -b $direct_store");
+                               execute("tgtadm --lld iscsi --op update --mode 
logicalunit --tid  $next_tid --lun 1 --params 
vendor_id=\"$vendor_id\",product_id=\"$prod_id\",product_rev=\"$prod_rev\",scsi_sn=\"$scsi_serial\"");
+                               $i += 1;
+                       }
+                       else {
+                               print("skipping device $direct_store\n");
+                               print("$direct_store does not exist - please 
check the configuration file\n");
+                       }
                }
        }
 
-- 
1.5.2




_______________________________________________
Stgt-devel mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/stgt-devel

Reply via email to