Created
July 21, 2012 06:14
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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