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