-
Notifications
You must be signed in to change notification settings - Fork 1
/
shelf_disks
executable file
·66 lines (51 loc) · 1.75 KB
/
shelf_disks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl
# trying to establish mapping between physical drive number in given shelf and linux logical device name
die "Usage: shelf_disks /dev/sgX [--sg]" if !defined $ARGV[0];
$dev = $ARGV[0];
my $sg = 0;
$sg = 1 if defined $ARGV[1] && $ARGV[1] eq '--sg';
# building list of SAS addresses on shelf
open SES, "sg_ses -p 0xA $dev 2>/dev/null |" || die "sg_ses start failed";
$disk = -1;
while (<SES>) {
chomp;
$disk++ if /Transport protocol/;
$sas{$disk} .= " $1" if /^\s*SAS address: (0x[0-9a-f]+)\s*$/;
}
close SES;
# find SAS ddresses of visible disks
# /sys/class/sas_device/end_device-1:2:6/device/target1:0:7/1:0:7:0
$base = "/sys/class/sas_device";
foreach $dev (glob ("$base/*")) {
next unless -e "$dev/sas_address";
if ($sg) {
foreach $tgt (glob ("$dev/device/target*/*")) {
next unless $tgt =~ /\/(\d+:\d+:\d+:\d+)$/;
foreach $block (glob ("$tgt/scsi_generic:*")) {
next unless $block =~ /\/scsi_generic:(.+)$/;
$name = $1;
$sas = `cat $dev/sas_address`;
chomp $sas;
$addr{$sas} = $name;
}
}
} else {
foreach $tgt (glob ("$dev/device/target*/*")) {
next unless $tgt =~ /\/(\d+:\d+:\d+:\d+)$/;
foreach $block (glob ("$tgt/block:*")) {
next unless $block =~ /\/block:(.+)$/;
$name = $1;
$sas = `cat $dev/sas_address`;
chomp $sas;
$addr{$sas} = $name;
}
}
}
}
foreach $disk (sort {$a <=> $b} keys(%sas)) {
$dev = "";
foreach $sas (split / +/, $sas{$disk}) {
$dev .= " /dev/".$addr{$sas} if defined $addr{$sas};
}
print "$disk:\t$dev\n";
}