Whamcloud - gitweb
77c0040bdfa551a6a6c104fefaf93406a0b3bc58
[fs/lustre-release.git] / lustre / scripts / ldev
1 #!/usr/bin/perl
2 #
3 # ldev - parser for /etc/ldev.conf
4 #
5 use strict;
6 use File::Basename;
7 use Getopt::Long qw/ :config posix_default no_ignore_case/;
8
9 $ENV{PATH} = "/sbin:/usr/sbin:/bin:/usr/bin";
10
11 my $prog = basename($0);
12
13 my $usage = <<EOF;
14 Usage: $prog [OPTIONS]...
15
16 Parse ldev.conf and answer the following queries:
17
18   -h, --help          Display this help.
19   -c, --config FILE   Set path to config file.
20   -H, --hostname NAME Use NAME instead of local hostname for queries.
21   -p, --partner       Print hostname of failover partner.
22   -l, --local         Print labels for local devices.
23   -f, --foreign       Print labels for foreign devices.
24   -a, --all           Print labels for local and foreign devices.
25   -F, --filesys NAME  Print labels for file system NAME.
26   -s, --sanity        Sanity check config on this node.
27   -d, --device LABEL  Print storage device of LABEL.
28   -j, --journal LABEL Print journal device of LABEL if it exists.
29   -r, --raidtab LABEL Print raidtab of LABEL if it exists.
30   -t, --type LABEL    Print device type of LABEL, i.e. "zfs" or "md".
31   -z, --zpool LABEL   Print zpool containing LABEL.
32   -R, --role ROLE     Filter output based on role, i.e. mdt, ost, mgs.
33   CMD [ARGS] ...      Run CMD in parallel for each device substituting:
34                       %f=fsname  %d=device  %i=dec-index %n=main-nid %l=label
35                       %t=srvtype %j=journal %I=hex-index %N=fail-nid %m=mgs-nid
36                       %H=hostname %b=backing-fs
37                       May be used in combination with -l, -f, -a, -F options.
38 EOF
39
40 my %eparse = (
41    elabel_uniq  =>    "label used more than once",
42    epairwise    =>    "local and foreign host not mapped to each other",
43    efieldcount  =>    "line has less than the minimum number of fields (4)",
44    ekeyval      =>    "malformed id=name",
45 );
46
47 my %conf = ();
48
49 #
50 # Main
51 #
52
53 parse_cmdline ();
54
55 parse_config ();
56
57 sanity ()         if $conf{sanity};
58 exec_cmd ()       if $conf{execcmd};
59 query_partner ()  if $conf{partner};
60 query_local ()    if $conf{local};
61 query_foreign ()  if $conf{foreign};
62 query_all ()      if $conf{all};
63 query_device ()   if $conf{device};
64 query_journal ()  if $conf{journal};
65 query_raidtab ()  if $conf{raidtab};
66 query_type ()     if $conf{type};
67 query_zpool ()    if $conf{zpool};
68 query_filesys ()  if $conf{fsname};
69
70 exit(0);
71
72 #
73 # Subroutines
74 #
75
76 sub parse_cmdline
77 {
78     my $help = 0;
79     my $host = "";
80
81     $conf{partner} = 0;
82     $conf{all} = 0;
83     $conf{local} = 0;
84     $conf{foreign} = 0;
85     $conf{config} = "/etc/ldev.conf";
86     $conf{nidsfile} = "/etc/nids";
87     $conf{hostname} = `uname -n`; chomp $conf{hostname};
88     $conf{device} = "";
89     $conf{sanity} = 0;
90     $conf{execcmd} = "";
91     $conf{journal} = "";
92     $conf{role} = "";
93     $conf{fsname} = "";
94
95     my $rc = GetOptions (
96         "help|h!"         => \$help,
97         "partner|p!"      => \$conf{partner},
98         "all|a!"          => \$conf{all},
99         "local|l!"        => \$conf{local},
100         "foreign|f!"      => \$conf{foreign},
101         "config|c=s"      => \$conf{config},
102         "nidsfile|n=s"    => \$conf{nidsfile},
103         "hostname|H=s"    => \$conf{hostname},
104         "sanity|s!"       => \$conf{sanity},
105         "device|d=s"      => \$conf{device},
106         "journal|j=s"     => \$conf{journal},
107         "raidtab|r=s"     => \$conf{raidtab},
108         "type|t=s"        => \$conf{type},
109         "zpool|z=s"       => \$conf{zpool},
110         "role|R=s"        => \$conf{role},
111         "filesys|F=s"     => \$conf{fsname},
112     );
113
114     usage() if $help || !$rc;
115
116     log_fatal ("cannot read config file\n") if (! -r $conf{config});
117
118     my $filters = 0;
119     $filters++ if ($conf{local});
120     $filters++ if ($conf{foreign});
121     $filters++ if ($conf{all});
122     $filters++ if ($conf{fsname});
123     log_fatal ("Only one of -l, -f, -a, or -F may be used.\n") if ($filters > 1);
124
125     if (@ARGV) {
126         $conf{execcmd} = " " . join " ", @ARGV;
127     }
128
129     parse_nids () if ($conf{execcmd} =~ /(%n|%N|%m)/);
130 }
131
132 sub parse_config
133 {
134     my $line = 0;
135     my %l2f = ();
136     my %label2local = ();
137     my %label2dev = ();
138     my %label2journal = ();
139     my %label2raidtab = ();
140     my %label2type = ();
141     my %label2zpool = ();
142     my %filesys2mgs = ();
143     my %label2hostname = ();
144     my @local_labels = ();
145     my @foreign_labels = ();
146     my @fs_labels = ();
147
148     open (CONF, "< $conf{config}") or log_fatal ("$conf{config}: $!\n");
149
150     while (<CONF>) {
151         my $type;
152         $line++;
153         s/#.*//;
154         s/(\s)*$//;
155         next if (/^(\s)*$/);
156         chomp;
157         my ($local, $foreign, $label, $dev, $j, $raidtab) = split;
158         if ($dev !~ /^\// && $dev =~ /^([^:]+):(.+)$/) {
159             $type = $1;
160             $dev = $2;
161         }
162         eparse_line ($line, "efieldcount") if (!defined $dev);
163         eparse_line ($line, "epairwise") if (exists $l2f{$local}
164                                          && $l2f{$local} ne $foreign);
165         $l2f{$local} = $foreign;
166
167         eparse_line ($line, "elabel_uniq") if (exists $label2dev{$label}
168                                          || exists $label2local{$label});
169         /(\w+)-(OST|MDT|MGS)([0-9a-fA-F]{4})/, $label;
170         my $filesys = $1;
171         my $nodetype = $2;
172         $label2dev{$label} = $dev;
173         $label2local{$label} = $local;
174         $label2journal{$label} = $j if defined $j;
175         $label2raidtab{$label} = $raidtab if defined $raidtab;
176         if (defined $type) {
177             $label2type{$label} = $type;
178             if ($type eq "zfs" && $dev =~ m{^([^/]+)/[^/]+$}) {
179                 $label2zpool{$label} = $1;
180             }
181         }
182         $label2hostname{$label}=$local;
183
184         if ($nodetype eq "MGS") {
185             $filesys2mgs{$filesys} = $label;
186         }
187
188         next if $conf{role} and lc $conf{role} ne lc $nodetype;
189
190         if ($local eq $conf{hostname}) {
191             push @local_labels, $label;
192         } elsif ($foreign eq $conf{hostname}) {
193             push @foreign_labels, $label;
194         }
195
196         if ($conf{fsname} and $filesys eq $conf{fsname}) {
197             push @fs_labels, $label;
198         }
199     }
200     close CONF;
201
202     foreach (keys %l2f) {
203         my $foreign = $l2f{$_};
204         next if ($foreign eq "-");
205         eparse_str ($_, "epairwise")
206                     unless (!exists $l2f{$foreign} or $l2f{$foreign} eq $_);
207     }
208
209     @{$conf{local_labels}} = @local_labels;
210     @{$conf{foreign_labels}} = @foreign_labels;
211     @{$conf{fs_labels}} = @fs_labels;
212     %{$conf{l2f}} = %l2f;
213     %{$conf{label2dev}} = %label2dev;
214     %{$conf{label2local}} = %label2local;
215     %{$conf{label2journal}} = %label2journal;
216     %{$conf{label2raidtab}} = %label2raidtab;
217     %{$conf{label2type}} = %label2type;
218     %{$conf{label2zpool}} = %label2zpool;
219     %{$conf{filesys2mgs}} = %filesys2mgs;
220     %{$conf{label2hostname}} = %label2hostname;
221 }
222
223 sub parse_nids ()
224 {
225     my $line = 0;
226     my %host2nid = ();
227     my %nid2host = ();
228
229     open (NIDS, "< $conf{nidsfile}") or log_fatal ("$conf{nidsfile}: $!\n");
230
231     while (<NIDS>) {
232         $line++;
233         s/#.*//;
234         next if (/^(\s)*$/);
235         chomp;
236         my ($host, $nid, $morenids) = split (/\s+/, $_, 3);
237         if (!defined $nid) {
238             log_fatal ("$conf{nidsfile} line $line: incomplete line\n");
239         }
240         $host2nid{$host} = $nid;
241         $nid2host{$nid} = $host;
242         map { $nid2host{$_} = $host; } split (/\s+/, $morenids);
243     }
244     close NIDS;
245
246     %{$conf{host2nid}} = %host2nid;
247     %{$conf{nid2host}} = %nid2host;
248 }
249
250 sub query_partner
251 {
252     my %l2f = %{$conf{l2f}};
253     my $hostname = $conf{hostname};
254     if (exists $l2f{$hostname} && $l2f{$hostname} ne "-") {
255         print "$l2f{$hostname}\n";
256     }
257 }
258
259 sub query_local
260 {
261     map { print "$_\n"; } @{$conf{local_labels}};
262 }
263
264 sub query_foreign
265 {
266     map { print "$_\n"; } @{$conf{foreign_labels}};
267 }
268
269 sub query_filesys
270 {
271     map { print "$_\n"; } @{$conf{fs_labels}};
272 }
273
274 sub query_all
275 {
276     query_local ();
277     query_foreign ();
278 }
279
280 sub query_device
281 {
282     my %label2dev = %{$conf{label2dev}};
283
284     if (exists $label2dev{$conf{device}}) {
285         print "$label2dev{$conf{device}}\n";
286     }
287 }
288
289 sub query_raidtab
290 {
291     my %label2raidtab = %{$conf{label2raidtab}};
292
293     if (exists $label2raidtab{$conf{raidtab}}) {
294         print "$label2raidtab{$conf{raidtab}}\n";
295     }
296 }
297
298 sub query_journal
299 {
300     my %label2journal = %{$conf{label2journal}};
301
302     if (exists $label2journal{$conf{journal}} &&
303        $label2journal{$conf{journal}} ne "-") {
304         print "$label2journal{$conf{journal}}\n";
305     }
306 }
307
308 sub query_type
309 {
310     my %label2type = %{$conf{label2type}};
311
312     if (exists $label2type{$conf{type}}) {
313         print "$label2type{$conf{type}}\n";
314     }
315 }
316
317 sub query_zpool
318 {
319     my %label2zpool = %{$conf{label2zpool}};
320
321     if (exists $label2zpool{$conf{zpool}}) {
322         print "$label2zpool{$conf{zpool}}\n";
323     }
324 }
325
326 sub dd_test
327 {
328     my ($dpath) = @_;
329     my $retval = 0;
330     my $bs =      `blockdev --getss   $dpath 2>/dev/null`; chomp $bs;
331     my $max512  = `blockdev --getsize $dpath 2>/dev/null`; chomp $max512;
332     if ($? == 0 && $bs > 0 && $max512 > 0) {
333         my $maxb = ($max512 / $bs) * 512;
334         my $count = 10 * 1024 * 1024 / $bs;  # read first 10mb
335         my $dev = `readlink -f $dpath`; chomp $dev;
336         $count = $maxb if ($count > $maxb);
337         `dd if=$dev of=/dev/null bs=$bs count=$count >/dev/null 2>&1`;
338         $retval = ($? == 0);
339     }
340     return $retval;
341 }
342
343 sub sanity
344 {
345     my $exit_val = 0;
346
347     my @local_labels = @{$conf{local_labels}};
348     my @foreign_labels = @{$conf{foreign_labels}};
349     my %label2dev = %{$conf{label2dev}};
350     my %label2journal = %{$conf{label2journal}};
351
352     foreach (@local_labels, @foreign_labels) {
353         my $lpath = "/dev/disk/by-label/$_";
354         my $dpath = $label2dev{$_};
355         my $jpath = $label2journal{$_};
356         my $label = $_;
357         if (! -e $lpath) {
358             log_error ("$lpath does not exist\n");
359             $exit_val = 1;
360         }
361         if (! -e $dpath) {
362             log_error ("$dpath does not exist\n");
363             $exit_val = 1;
364         } elsif (!dd_test ($dpath)) {
365             log_error ("$dpath failed dd test\n");
366             $exit_val = 1;
367         }
368         if (`readlink -f $lpath` ne `readlink -f $dpath`) {
369             log_error ("$lpath and $dpath point to different things\n");
370             $exit_val = 1;
371         }
372         if ($jpath) {
373             if (! -e $jpath) {
374                 log_error ("$jpath (journal for $label) does not exist\n");
375                 $exit_val = 1;
376             } elsif (!dd_test ($jpath)) {
377                 log_error ("$jpath failed dd test\n");
378                 $exit_val = 1;
379             }
380         }
381     }
382     exit($exit_val);
383 }
384
385 sub par_exec
386 {
387     my @pids = ();
388     my %pid2label = ();
389     my %pid2cmd = ();
390     my $pid;
391     my $result = 0;
392
393     my $tmpfile = `mktemp \${TMPDIR:-/tmp}/ldev.XXXXXXXXXX`; chomp $tmpfile;
394     log_fatal ("failed to create $tmpfile\n") if (! -e $tmpfile);
395
396     foreach (@_) {
397         my ($label, $cmd) = split (/\s+/, $_, 2);
398         my ($basecmd) = split (/\s+/, $cmd);
399         if (($pid = fork)) {       # parent
400             $pid2label{$pid} = $label;
401             $pid2cmd{$pid} = $basecmd;
402         } elsif (defined $pid) {   # child
403             #print STDERR "$label: running $cmd\n";
404             exec "($cmd 2>&1 || rm -f $tmpfile) | sed -e 's/^/$label: /'";
405             print STDERR "$label: exec $basecmd: $!\n"; unlink $tmpfile;
406         } else {                   # error
407             log_fatal ("label: fork: $!\n"); unlink $tmpfile;
408         }
409     }
410     while (($pid = wait) != -1) {
411         #print STDERR "$pid2label{$pid}: completed\n";
412     }
413
414     # sentinel is intact, so there were no errors
415     if (-e $tmpfile) {
416         unlink $tmpfile;
417         $result = 1;
418     }
419
420     return $result;
421 }
422
423 sub exec_cmd
424 {
425     my @labels = ();
426     my @cmds = ();
427     my %label2dev = %{$conf{label2dev}};
428     my %label2type = %{$conf{label2type}};
429     my %label2journal = %{$conf{label2journal}};
430     my %filesys2mgs = %{$conf{filesys2mgs}};
431     my %label2hostname = %{$conf{label2hostname}};
432     my %l2f = %{$conf{l2f}};
433     my ($nid, $failnid);
434
435     if ($conf{execcmd} =~ /%n/) {
436         my %host2nid = %{$conf{host2nid}};
437         if (!defined $host2nid{$conf{hostname}}) {
438             log_fatal ("%n used but no nid defined for this host\n");
439         }
440         $nid = $host2nid{$conf{hostname}};
441     }
442     if ($conf{execcmd} =~ /%N/) {
443         if (!defined $l2f{$conf{hostname}}) {
444             log_fatal ("%N used but foreign host is undefined\n");
445         }
446         my %host2nid = %{$conf{host2nid}};
447         if (!defined $host2nid{$l2f{$conf{hostname}}}) {
448             log_fatal ("%N used but foreign nid is undefined\n");
449         }
450         $failnid = $host2nid{$l2f{$conf{hostname}}};
451     }
452
453     if ($conf{foreign}) {
454         @labels = @{$conf{foreign_labels}};
455     } elsif ($conf{all}) {
456         @labels = (@{$conf{local_labels}}, @{$conf{foreign_labels}});
457     } elsif ($conf{fsname}) {
458         @labels = (@labels, @{$conf{fs_labels}});
459     } else {
460         @labels = @{$conf{local_labels}};
461     }
462
463     foreach (@labels) {
464         /(\w+)-(OST|MDT|MGS)([0-9a-fA-F]{4})/;
465
466         my $fsname = $1;
467         my $type = $2; $type =~ tr/A-Z/a-z/;
468         my $hexindex = $3;
469         my $decindex = hex($3);
470         my $label = $_;
471         my $cmd = $conf{execcmd};
472         my $device = $label2dev{$_};
473         if ($conf{execcmd} =~ /%j/ && !defined $label2journal{$_}) {
474             log_fatal ("%j used but no journal defined for $_\n");
475         }
476         my $journal = $label2journal{$_};
477         my $fstype = $label2type{$_};
478         if (!defined $fstype or $fstype ne "zfs") {
479             $fstype = "ldiskfs";
480         }
481         my $hostname = $label2hostname{$_};
482         my $mgsnid;
483         if ($cmd =~ /%m/) {
484             if (!exists $filesys2mgs{$fsname}) {
485                  log_fatal ("$fsname has no MGS defined\n");
486             }
487
488             my $mgs = $filesys2mgs{$fsname};
489             if (!exists $label2hostname{$mgs}) {
490                  log_fatal ("$mgs has no hostname defined\n");
491             }
492
493             my %host2nid = %{$conf{host2nid}};
494             $mgs = $label2hostname{$mgs};
495             if (!exists $host2nid{$mgs}) {
496                  log_fatal ("$mgs has no NID defined\n");
497             }
498             $mgsnid = $host2nid{$mgs};
499         }
500
501         $cmd =~ s/%f/$fsname/g;  # %f = fsname
502         $cmd =~ s/%t/$type/g;    # %t = server type
503         $cmd =~ s/%I/$hexindex/g;# %I = index (hex)
504         $cmd =~ s/%i/$decindex/g;# %i = index (dec)
505         $cmd =~ s/%l/$label/g;   # %l = label
506         $cmd =~ s/%d/$device/g;  # %d = device
507         $cmd =~ s/%j/$journal/g; # %j = journal device
508         $cmd =~ s/%n/$nid/g;     # %n = nid
509         $cmd =~ s/%N/$failnid/g; # %N = failnid
510         $cmd =~ s/%m/$mgsnid/g;  # %m = MGS nid
511         $cmd =~ s/%b/$fstype/g;  # %b = backing file system type
512         $cmd =~ s/%H/$hostname/g;# %H = hostname
513
514         push @cmds, "$_ $cmd";
515     }
516
517     par_exec (@cmds) or log_fatal ("parallel command execution failed\n");
518     exit 0;
519 }
520
521 sub usage
522 {
523     print STDERR "$usage";
524     exit 0;
525 }
526
527 sub log_msg     { print STDERR "$prog: ", @_; }
528 sub log_error   { log_msg ("Error: ", @_) }
529 sub log_fatal   { log_msg ("Fatal: ", @_); exit 1; }
530 sub eparse_line { log_fatal ("$conf{config} line $_[0]: $eparse{$_[1]}\n"); }
531 sub eparse_str  { log_fatal ("$conf{config}: $_[0]: $eparse{$_[1]}\n"); }