User Tools

Site Tools


unix:linux:iscsi_tricks_and_techniques

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
unix:linux:iscsi_tricks_and_techniques [2016/09/06 19:57] – external edit 127.0.0.1unix:linux:iscsi_tricks_and_techniques [2022/12/17 14:05] (current) – [Adding new target on target device] rodolico
Line 22: Line 22:
  
 ===== Tricks and Techniques ===== ===== Tricks and Techniques =====
 +
 +==== Script to update all targets ====
 +
 +For some reason, I'm occasionally a little out of sync between what my target offers and what my initiator knows about. The following script will not //remove// anything, but it will add every target offered by the iscsi target.
 +
 +Add one or more entries into @servers, and it will scan the targets in question, then compare against what our initiator knows, then add any new entries.
 +
 +<code perl addAlliSCSIS.pl>
 +#! /usr/bin/env perl
 +
 +use strict;
 +use warnings;
 +
 +# change following to be a list of 1 or more iSCSI targets to be queried
 +my @servers = ( '10.10.10.10','10.10.10.9' ); 
 +my %targets;
 +
 +
 +foreach my $server ( @servers ) {
 +   print "\n" . '-'x40 . "\nGetting targets on server $server\n" . '-'x40 . "\n";
 +   my @list = `iscsiadm -m discovery -t st -p $server`;
 +   chomp @list;
 +   # @list contains lines of type
 +   # 10.19.209.2:3260,1 iqn.2014-11.net.dailydata.castor:simon0
 +   # split them apart and add them to the hash
 +   foreach my $entry ( @list ) {
 +      my ( $portal, $targetName ) = split( ' ', $entry );
 +      # $portal has some extra info after a comma, so clean it up
 +      $portal =~ m/^([0-9:.]+)/;
 +      $portal = $1;
 +      # some targets return multiple IP's for a given name, so 
 +      # only add them if they are in this IP
 +      $targets{ $targetName } = $portal if $portal =~ m/^$server/;
 +      print "$targetName\t$targets{ $targetName }\n";
 +   }
 +}
 +
 +print "\n" . '-'x40 . "\nGetting active sessions\n". '-'x40 . "\n";
 +# now, get active sessions so we can filter them
 +my @activeSessions = `iscsiadm -m session`;
 +chomp @activeSessions;
 +foreach my $session ( @activeSessions ) {
 +   $session =~ m/^.*[^0-9:.]([0-9,:.]+).*(iqn\S*)/;
 +   my ( $portal,$targetName ) = ( $1,$2 );
 +   print "$portal\t$targetName";
 +   if ( exists( $targets{$targetName} ) ) {
 +      print "\tNOT updating\n";
 +      delete $targets{ $targetName };
 +   } else {
 +      print "Needs to be added\n";
 +   }
 +}
 +
 +# check if we have any new entries and bail if not
 +if ( scalar keys %targets ) {
 +   # We have new entries, so run them;
 +   foreach my $targetName ( sort keys %targets ) {
 +      my $portal = $targets{$targetName};
 +      print "Adding $targetName\n";
 +      `iscsiadm -m node --targetname '$targetName' --portal '$portal' --login`;
 +   }
 +} else {
 +   print "No new entries\n";
 +}
 +# print `ls /dev/disk/by-path/`;
 +
 +</code>
 +
 +==== Fixing bolluxed machine ====
 +
 +Ok, your initiator is in a really bad shape, or you did something on the target you shouldn't have, and you want to basically reinitialize everything. But, you don't want to shut everything down. This was the procedure I used to do so. I migrated all of my Xen devices onto a different machine, then ran the following on the machine I wanted to reinitialize:
 +
 +<code bash>
 +/etc/init.d/open-iscsi stop
 +rm -fR /etc/iscsi/send_targets/* /etc/iscsi/nodes/*
 +/etc/init.d/open-iscsi start
 +/media/xen-store/scripts/addAlliSCIS.pl
 +</code>
 +
 +**Note**: This is on an old Debian Wheezy machine, so start/stop commands are different after that.
 +
 +The first line stops the initiator. The second line removes all remembered connections from the initiators cache, then we start the initiator again.
 +
 +At this point, we don't "know" any targets, so we use the script (above) to grab all targets available on the iscsi target. YEAH, we're done.
  
 ==== Viewing targets exported from a target device, from the target device ==== ==== Viewing targets exported from a target device, from the target device ====
Line 37: Line 121:
 </code> </code>
  
-Edit /etc/iet/ietd.conf and add the following lines+Edit /etc/iet/ietd.conf (Linux) or /etc/ctl.conf (BSD) and add the following lines
  
 <code>   <code>  
Line 45: Line 129:
 </code> </code>
 Restart iet Restart iet
-<code bash>  /etc/init.d/iscsitarget restart</code>+<code bash>/etc/init.d/iscsitarget restart # Linux 
 +service ctld reload # FreeBSD 
 +</code>
  
 ==== Adding new target to initiator ==== ==== Adding new target to initiator ====
Line 92: Line 178:
 # first, log out of the session # first, log out of the session
 iscsiadm -m node --target='name of target' --portal "ip:port" --logout iscsiadm -m node --target='name of target' --portal "ip:port" --logout
-# now, do a discovery and delete the entry from your local database +# now, do a remove it from the session 
-iscsiadm -m discovery --target='name of target' --portal "ip:port" -o delete+iscsiadm -m node --target='name of target' --portal "ip:port" -o delete
 </code> </code>
  
unix/linux/iscsi_tricks_and_techniques.1473209837.txt.gz · Last modified: 2016/09/06 19:57 by 127.0.0.1