File: lsluns

package info (click to toggle)
s390-tools 2.3.0-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,176 kB
  • sloc: ansic: 87,755; sh: 8,398; cpp: 8,384; perl: 3,783; makefile: 1,476; asm: 654
file content (336 lines) | stat: -rwxr-xr-x 10,937 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/perl
#
# lsluns - list LUNs discovered in the FC SAN, or show encryption state of attached LUNs
#
# Copyright IBM Corp. 2008, 2017
#
# s390-tools is free software; you can redistribute it and/or modify
# it under the terms of the MIT license. See LICENSE for details.
#

use strict;
use warnings;
use English;
use Getopt::Long;
use File::Basename;
use File::Glob;

my %res_hash = ();
my @adapter = ();
my @port = ();
my $active = "";

my $wlun = "0xc101000000000000";
my $lun0 = "0x0000000000000000";
my $sg_dir = "/sys/class/scsi_generic";
my $udevsettle_call;
my $udevadm = "/bin/udevadm";


if (! -e $udevadm) {
        $udevsettle_call = "/sbin/udevsettle";
} else {
        $udevsettle_call = "$udevadm settle";
}

# read the first line of a sysfs-entry and compare it to a given string
# parameters:
#   1 - $sysfs_attr_path:   path to the sysfs-entry
#   2 - $compare_value:     compare-value (used as string)
# return:
#   true/false: depending on the comparison (true for equal)
#   undef:      if I/O on the given path isn't possible
sub sysfs_read_and_compare($$)
{
    my ($sysfs_attr_path, $compare_value) = @_;

    open(my $sysfs_attr, "<", $sysfs_attr_path) or return undef;
    my $sysfs_value = <$sysfs_attr>;
    close($sysfs_attr);

    # EOF?
    if (!defined($sysfs_value)) {
        return undef;
    }

    chomp($sysfs_value);
    return $sysfs_value eq $compare_value;
}

sub list_luns
{
    my %lun_hash = get_lun_hash();
    my $drv_dir = "/sys/bus/ccw/drivers/zfcp";
    my $man_att;
    my $cnt;

    foreach my $a (sort keys %res_hash) {
        # first check whether the adapter is online and good to use
        if (!sysfs_read_and_compare($drv_dir.'/'.$a.'/online', "1") ||
            !sysfs_read_and_compare($drv_dir.'/'.$a.'/availability', "good") ||
            !sysfs_read_and_compare($drv_dir.'/'.$a.'/failed', "0") ||
            !sysfs_read_and_compare($drv_dir.'/'.$a.'/in_recovery', "0"))
        {
            print "Adapter $a is not in a good state; skipping LUN scan.\n";
            next;
        }

        print "Scanning for LUNs on adapter $a\n";
        foreach my $p (@{$res_hash{$a}}) {
            next if (! -e $drv_dir."/$a/$p");
            my $status = `cat $drv_dir/$a/$p/access_denied`;
            $status .= `cat $drv_dir/$a/$p/failed`;
            $status .= `cat $drv_dir/$a/$p/in_recovery`;
            if ($status =~ /1/) {
                print "\tat port $p:\n";
                print "\t\tPort not online. Cannot scan for LUNs.\n";
                next;
            }
            if (!defined($lun_hash{$a}{$p})) {
                `echo $lun0 >> $drv_dir/$a/$p/unit_add 2>/dev/null`;
                for ($cnt = 0; $cnt < 4; $cnt++) {
                    `$udevsettle_call`;
                    %lun_hash = get_lun_hash();
                    last if (defined($lun_hash{$a}{$p}));
                    select(undef, undef, undef, 0.1);
                }
                if (!defined($lun_hash{$a}{$p})) {
                    `echo $lun0 >> $drv_dir/$a/$p/unit_remove 2>/dev/null`;
                    `echo $wlun >> $drv_dir/$a/$p/unit_add 2>/dev/null`;
                    for ($cnt = 0; $cnt < 4; $cnt++) {
                        `$udevsettle_call`;
                        %lun_hash = get_lun_hash();
                        last if (defined($lun_hash{$a}{$p}));
                        select(undef, undef, undef, 0.1);
                    }
                    if (!defined($lun_hash{$a}{$p})) {
                    	`echo $wlun >> $drv_dir/$a/$p/unit_remove 2>/dev/null`;
                    	print"\tat port $p:\n";
                    	print "\t\tCannot attach WLUN / LUN0 for scanning.\n";
                    	next;
		    }
                }
                $man_att = 1;
            }

	    my $retries = 0;
            foreach my $lun (@{[keys %{$lun_hash{$a}{$p}}]}) {
            	my $sg_dev = $lun_hash{$a}{$p}{$lun};
            	select(undef, undef, undef, 0.1) while (! -e "/dev/$sg_dev");
            	my @output = `sg_luns /dev/$sg_dev 2>/dev/null`;
            	my $error = $?;
            	if ($man_att) {
                	`echo 1 >> $sg_dir/$sg_dev/device/delete 2>/dev/null`;
                	select(undef, undef, undef, 0.1);
                	`echo $lun >> $drv_dir/$a/$p/unit_remove 2>/dev/null`;
                	$man_att = 0;
            	}
            	print "\tat port $p:\n" if (!$retries);
            	if (!$error && @output) {
                	splice(@output, 0, 2);
                	map { s/\s*(\w{16})\s*/\t\t0x$1\n/ } @output;
                	print @output;
			last;
            	}
            	if ($error) {
                	print "\t\tUnable to send the REPORT_LUNS command to LUN.\n";
            	}
		$retries++;
		last if ($retries > 3);
	    }
        }
        if (! -d $sg_dir) {
        	print "$PROGRAM_NAME: Error: Please load/configure SCSI Generic (sg) to use $PROGRAM_NAME.\n";
        }
    }
}

# Look only for LUN0 and the REPORT LUNs WLUN. SAM specifies that the storage
# only has to response on one of those to the REPORT LUNs command.
sub get_lun_hash
{
    my %lun_hash;

    foreach my $device (</$sg_dir/sg*>) {
        my $l = `cat $device/device/fcp_lun`;
        my $p = `cat $device/device/wwpn`;
        my $a = `cat $device/device/hba_id`;

        $l =~ s/(0x\w{16})*\n/$1/;
        $p =~ s/(0x\w{16})*\n/$1/;
        chomp($a);

        if ($active or ($l eq $lun0 or $l eq $wlun)) {
            $lun_hash{$a}{$p}{$l} = ${[split('/', $device)]}[-1];
	}
    }
    return %lun_hash;
}

sub lsluns_usage {
    print <<EOD;
Usage:
This tool is designed for environments where all SCSI devices are attached
through the zfcp device driver. Expect error messages in mixed environments
such as with iSCSI.

$PROGRAM_NAME [-c <busid>] ... [-p <wwpn>] ... [-h] [-v]

    List LUNs discovered in the Fibre Channel (FC) Storage Area Network (SAN).
    This causes extra SAN traffic for each target port WWPN.
      Discovering LUNs only makes sense for NPIV-enabled FCP devices
    without zfcp automatic LUN scan. zfcp automatic LUN scan is available
    as of kernel version 2.6.37, if not disabled with zfcp.allow_lun_scan=0.
      For storage products that do not support a REPORT LUNS
    well-known logical unit (such as IBM Storwize products),
    ensure that LUN 0 represents a valid peripheral device type.
    See the man page for more information.

$PROGRAM_NAME -a [-c <busid>] ... [-p <wwpn>] ... [-h] [-v]

    Show encryption state of the attached LUNs.
    This causes extra SAN traffic for each attached LUN.

For all other uses, such as listing attached LUNs or properties other than
encryption, use other tools such as "lszfcp -D" or "lsscsi -tv"
or "lszdev zfcp-lun -ii".

Limit the listing by specifying one or more adapters (FCP device
bus-IDs) or target port WWPNs or both.

Options:
    -c <busid>, --ccw <busid>
        Filter LUNs by adapter with the specified FCP device bus-ID. Can be
        specified multiple times.
        For example: "$PROGRAM_NAME -c 0.0.3922"

    -p <wwpn>, --port <wwpn>
        Filter LUNs by target port with the specified WWPN. Can be specified
        multiple times.
        For example: "$PROGRAM_NAME -p 0x5005123456789000"

    -h, --help
        Print help message and exit.

    -v, --version
        Display version information and exit.
EOD
    exit 0;
}

sub lsluns_version {
    print "$PROGRAM_NAME: version %S390_TOOLS_VERSION%\n";
    print "Copyright IBM Corp. 2008, 2017\n";
}

sub lsluns_invalid_usage {
    print "$PROGRAM_NAME: invalid option\n";
    print "Try '$PROGRAM_NAME --help' for more information.\n";
}

sub get_env_list
{
    my $a_ref_list = shift();
    my $p_ref_list = shift();
    my @res ;
    my %res_hash;

    @res = </sys/bus/ccw/drivers/zfcp/*.*.*/0x*>;
    return () if (!@res);
    foreach my $entry (@res) {
        my $a = ${[split('/', $entry)]}[-2];
        my $p = ${[split('/', $entry)]}[-1];
        next if (@$a_ref_list && "@$a_ref_list" !~ /$a/);
        next if (@$p_ref_list && "@$p_ref_list" !~ /$p/);
        push @{ $res_hash{$a} }, $p;
    }

    if (!%res_hash) {
        print "$PROGRAM_NAME: Adapter and/or port filter(s) did not match anything\n";
    }
    return %res_hash;
}

sub show_attached_lun_info
{
        my %lun_hash = get_lun_hash();
        my @txt = ("Disk", "Tape", "Printer", "Proc", "WRO",
                   "CD/DVD", "Scanner", "OMD", "Changer","Comm","n/a",
                   "n/a","RAID", "Encl");

        if (glob("/sys/class/scsi_device/*") && (! -d $sg_dir)) {
                print "$PROGRAM_NAME: Error: Please load/configure SCSI Generic (sg) to use $PROGRAM_NAME.\n";
        }
        foreach my $a (sort keys %lun_hash) {
                next if ("@adapter" !~ /$a/);
                print "adapter = $a\n";
                foreach my $p (sort keys %{$lun_hash{$a}}) {
                        next if ("@port" !~ /$p/);
                        print "\tport = $p\n";
                        foreach my $l (sort keys %{$lun_hash{$a}{$p}}) {
                                my $sg_dev = "/dev/".$lun_hash{$a}{$p}{$l};
                                my $inq = `sg_inq -r $sg_dev 2>/dev/null`;
				if (!$inq) {
					print("\t\tlun = $l [offline]\n");
					next;
				}
                                (my $vend = substr($inq, 0x8, 0x8)) =~ s/\s*//g;
                                (my $mod = substr($inq, 0x10, 0x10)) =~ s/\s*//g;
                                my $type = ord(substr($inq, 0x0, 0x1));
                                my $enc = ($mod =~ /2107/) ?
                                        ord(substr($inq, 0xa2, 0x1)) : 0;
                                $l .= "(X)" if ($enc & 0x80);
                                $txt[$type] = $type if (!defined($txt[$type]));
                                print("\t\tlun = $l\t$sg_dev\t$txt[$type]",
                                      "\t$vend:$mod\n");
                        }
                }
        }
}



########### main ################

$PROGRAM_NAME = basename($PROGRAM_NAME);

Getopt::Long::Configure(qw(bundling));
GetOptions('c|ccw=s' => \@adapter,
	   'p|port=s'    => \@port,
	   'a|active'    => \$active,
	   'v|version'   => sub { lsluns_version(); exit 0; },
	   'h|help'      => sub { lsluns_usage(); exit 0; },
           ) or do {
               lsluns_invalid_usage();
               exit 1;
           };

@adapter = split(',', join(',', @adapter));
foreach (@adapter) {
	$_ =~ tr/A-Z/a-z/;
}

@port = split(',', join(',', @port));
foreach (@port) {
	$_ =~ tr/A-Z/a-z/;
}

%res_hash = get_env_list(\@adapter, \@port);

@adapter = keys %res_hash;
push @port, map { @{$res_hash{$_}} } keys %res_hash;

# checking for helper progs

die "$PROGRAM_NAME: Unable to execute due to missing sg3-utils package. ".
    "Processing stopped.\n" if system("sg_luns -V > /dev/null 2>&1");



if ($active) {
        show_attached_lun_info();
} else {
        list_luns();
}