Skip to content

Instantly share code, notes, and snippets.

@eqhmcow
Created July 21, 2012 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eqhmcow/3154821 to your computer and use it in GitHub Desktop.
Save eqhmcow/3154821 to your computer and use it in GitHub Desktop.
generate an id based on a block device's iscsi target name and lun number
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Digest::SHA1 qw(sha1_base64);
my $hash;
GetOptions("hash" => \$hash);
my $dev = shift or die "Need a block device";
my $devs = `echo /sys/class/iscsi_session/*/device/target*/*/scsi_device:*/device/block:*`;
chomp $devs;
my @devs = split m/ /, $devs;
my %block_dev;
foreach (@devs) {
my ($session, $lun, $bd) = m!^(/sys/class/iscsi_session/[^/]+)/.*scsi_device:\d+:\d+:\d+:(\d+)/device/block:(\w+)$!;
$bd or die "Couldn't parse $_";
$block_dev{$bd}{lun} = $lun;
my $tgt = `cat $session/targetname`;
chomp $tgt;
$block_dev{$bd}{target} = $tgt;
}
my $bd = $block_dev{$dev} or die "$dev: invalid block device";
print "dev: $dev\n" unless $hash;
my $s;
foreach (qw/target lun/) {
$s .= "$_: " . $bd->{$_} . "\n";
}
chomp $s;
$s = sha1_base64($s) if $hash;
print "$s\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment