3 # ldev - parser for /etc/ldev.conf
7 use Getopt::Long qw/ :config posix_default no_ignore_case/;
9 $ENV{PATH} = "/sbin:/usr/sbin:/bin:/usr/bin";
11 my $prog = basename($0);
14 Usage: $prog [OPTIONS]...
16 Parse ldev.conf and answer the following queries:
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 CMD [ARGS] ... Run CMD in parallel for each device substituting:
33 %f=fsname %d=device %i=dec-index %n=main-nid %l=label
34 %t=srvtype %j=journal %I=hex-index %N=fail-nid %m=mgs-nid
35 %H=hostname %b=backing-fs
36 May be used in combination with -l, -f, -a, -F options.
40 elabel_uniq => "label used more than once",
41 epairwise => "local and foreign host not mapped to each other",
42 efieldcount => "line has less than the minimum number of fields (4)",
43 ekeyval => "malformed id=name",
56 sanity () if $conf{sanity};
57 exec_cmd () if $conf{execcmd};
58 query_partner () if $conf{partner};
59 query_local () if $conf{local};
60 query_foreign () if $conf{foreign};
61 query_all () if $conf{all};
62 query_device () if $conf{device};
63 query_journal () if $conf{journal};
64 query_raidtab () if $conf{raidtab};
65 query_type () if $conf{type};
66 query_zpool () if $conf{zpool};
67 query_filesys () if $conf{fsname};
84 $conf{config} = "/etc/ldev.conf";
85 $conf{nidsfile} = "/etc/nids";
86 $conf{hostname} = `uname -n`; chomp $conf{hostname};
95 "partner|p!" => \$conf{partner},
96 "all|a!" => \$conf{all},
97 "local|l!" => \$conf{local},
98 "foreign|f!" => \$conf{foreign},
99 "config|c=s" => \$conf{config},
100 "nidsfile|n=s" => \$conf{nidsfile},
101 "hostname|H=s" => \$conf{hostname},
102 "sanity|s!" => \$conf{sanity},
103 "device|d=s" => \$conf{device},
104 "journal|j=s" => \$conf{journal},
105 "raidtab|r=s" => \$conf{raidtab},
106 "type|t=s" => \$conf{type},
107 "zpool|z=s" => \$conf{zpool},
108 "filesys|F=s" => \$conf{fsname},
111 usage() if $help || !$rc;
113 log_fatal ("cannot read config file\n") if (! -r $conf{config});
116 $filters++ if ($conf{local});
117 $filters++ if ($conf{foreign});
118 $filters++ if ($conf{all});
119 $filters++ if ($conf{fsname});
120 log_fatal ("Only one of -l, -f, -a, or -F may be used.\n") if ($filters > 1);
123 $conf{execcmd} = " " . join " ", @ARGV;
126 parse_nids () if ($conf{execcmd} =~ /(%n|%N|%m)/);
133 my %label2local = ();
135 my %label2journal = ();
136 my %label2raidtab = ();
138 my %label2zpool = ();
139 my %filesys2mgs = ();
140 my %label2hostname = ();
141 my @local_labels = ();
142 my @foreign_labels = ();
145 open (CONF, "< $conf{config}") or log_fatal ("$conf{config}: $!\n");
154 my ($local, $foreign, $label, $dev, $j, $raidtab) = split;
155 if ($dev !~ /^\// && $dev =~ /^([^:]+):(.+)$/) {
159 eparse_line ($line, "efieldcount") if (!defined $dev);
160 eparse_line ($line, "epairwise") if (exists $l2f{$local}
161 && $l2f{$local} ne $foreign);
162 $l2f{$local} = $foreign;
164 eparse_line ($line, "elabel_uniq") if (exists $label2dev{$label}
165 || exists $label2local{$label});
166 /(\w+)-(OST|MDT|MGT|MGS)([0-9a-fA-F]{4})/, $label;
169 $label2dev{$label} = $dev;
170 $label2local{$label} = $local;
171 $label2journal{$label} = $j if defined $j;
172 $label2raidtab{$label} = $raidtab if defined $raidtab;
174 $label2type{$label} = $type;
175 if ($type eq "zfs" && $dev =~ m{^([^/]+)/[^/]+$}) {
176 $label2zpool{$label} = $1;
179 $label2hostname{$label}=$local;
180 if ($nodetype eq "MGS" or $nodetype eq "MGT") {
181 $filesys2mgs{$filesys} = $label;
184 if ($local eq $conf{hostname}) {
185 push @local_labels, $label;
186 } elsif ($foreign eq $conf{hostname}) {
187 push @foreign_labels, $label;
190 if ($conf{fsname} and $filesys eq $conf{fsname}) {
191 push @fs_labels, $label;
196 foreach (keys %l2f) {
197 my $foreign = $l2f{$_};
198 next if ($foreign eq "-");
199 eparse_str ($_, "epairwise")
200 unless (!exists $l2f{$foreign} or $l2f{$foreign} eq $_);
203 @{$conf{local_labels}} = @local_labels;
204 @{$conf{foreign_labels}} = @foreign_labels;
205 @{$conf{fs_labels}} = @fs_labels;
206 %{$conf{l2f}} = %l2f;
207 %{$conf{label2dev}} = %label2dev;
208 %{$conf{label2local}} = %label2local;
209 %{$conf{label2journal}} = %label2journal;
210 %{$conf{label2raidtab}} = %label2raidtab;
211 %{$conf{label2type}} = %label2type;
212 %{$conf{label2zpool}} = %label2zpool;
213 %{$conf{filesys2mgs}} = %filesys2mgs;
214 %{$conf{label2hostname}} = %label2hostname;
223 open (NIDS, "< $conf{nidsfile}") or log_fatal ("$conf{nidsfile}: $!\n");
230 my ($host, $nid, $morenids) = split (/\s+/, $_, 3);
232 log_fatal ("$conf{nidsfile} line $line: incomplete line\n");
234 $host2nid{$host} = $nid;
235 $nid2host{$nid} = $host;
236 map { $nid2host{$_} = $host; } split (/\s+/, $morenids);
240 %{$conf{host2nid}} = %host2nid;
241 %{$conf{nid2host}} = %nid2host;
246 my %l2f = %{$conf{l2f}};
247 my $hostname = $conf{hostname};
248 if (exists $l2f{$hostname} && $l2f{$hostname} ne "-") {
249 print "$l2f{$hostname}\n";
255 map { print "$_\n"; } @{$conf{local_labels}};
260 map { print "$_\n"; } @{$conf{foreign_labels}};
265 map { print "$_\n"; } @{$conf{fs_labels}};
276 my %label2dev = %{$conf{label2dev}};
278 if (exists $label2dev{$conf{device}}) {
279 print "$label2dev{$conf{device}}\n";
285 my %label2raidtab = %{$conf{label2raidtab}};
287 if (exists $label2raidtab{$conf{raidtab}}) {
288 print "$label2raidtab{$conf{raidtab}}\n";
294 my %label2journal = %{$conf{label2journal}};
296 if (exists $label2journal{$conf{journal}} &&
297 $label2journal{$conf{journal}} ne "-") {
298 print "$label2journal{$conf{journal}}\n";
304 my %label2type = %{$conf{label2type}};
306 if (exists $label2type{$conf{type}}) {
307 print "$label2type{$conf{type}}\n";
313 my %label2zpool = %{$conf{label2zpool}};
315 if (exists $label2zpool{$conf{zpool}}) {
316 print "$label2zpool{$conf{zpool}}\n";
324 my $bs = `blockdev --getss $dpath 2>/dev/null`; chomp $bs;
325 my $max512 = `blockdev --getsize $dpath 2>/dev/null`; chomp $max512;
326 if ($? == 0 && $bs > 0 && $max512 > 0) {
327 my $maxb = ($max512 / $bs) * 512;
328 my $count = 10 * 1024 * 1024 / $bs; # read first 10mb
329 my $dev = `readlink -f $dpath`; chomp $dev;
330 $count = $maxb if ($count > $maxb);
331 `dd if=$dev of=/dev/null bs=$bs count=$count >/dev/null 2>&1`;
341 my @local_labels = @{$conf{local_labels}};
342 my @foreign_labels = @{$conf{foreign_labels}};
343 my %label2dev = %{$conf{label2dev}};
344 my %label2journal = %{$conf{label2journal}};
346 foreach (@local_labels, @foreign_labels) {
347 my $lpath = "/dev/disk/by-label/$_";
348 my $dpath = $label2dev{$_};
349 my $jpath = $label2journal{$_};
352 log_error ("$lpath does not exist\n");
356 log_error ("$dpath does not exist\n");
358 } elsif (!dd_test ($dpath)) {
359 log_error ("$dpath failed dd test\n");
362 if (`readlink -f $lpath` ne `readlink -f $dpath`) {
363 log_error ("$lpath and $dpath point to different things\n");
368 log_error ("$jpath (journal for $label) does not exist\n");
370 } elsif (!dd_test ($jpath)) {
371 log_error ("$jpath failed dd test\n");
387 my $tmpfile = `mktemp \${TMPDIR:-/tmp}/ldev.XXXXXXXXXX`; chomp $tmpfile;
388 log_fatal ("failed to create $tmpfile\n") if (! -e $tmpfile);
391 my ($label, $cmd) = split (/\s+/, $_, 2);
392 my ($basecmd) = split (/\s+/, $cmd);
393 if (($pid = fork)) { # parent
394 $pid2label{$pid} = $label;
395 $pid2cmd{$pid} = $basecmd;
396 } elsif (defined $pid) { # child
397 #print STDERR "$label: running $cmd\n";
398 exec "($cmd 2>&1 || rm -f $tmpfile) | sed -e 's/^/$label: /'";
399 print STDERR "$label: exec $basecmd: $!\n"; unlink $tmpfile;
401 log_fatal ("label: fork: $!\n"); unlink $tmpfile;
404 while (($pid = wait) != -1) {
405 #print STDERR "$pid2label{$pid}: completed\n";
408 # sentinel is intact, so there were no errors
421 my %label2dev = %{$conf{label2dev}};
422 my %label2type = %{$conf{label2type}};
423 my %label2journal = %{$conf{label2journal}};
424 my %filesys2mgs = %{$conf{filesys2mgs}};
425 my %label2hostname = %{$conf{label2hostname}};
426 my %l2f = %{$conf{l2f}};
429 if ($conf{execcmd} =~ /%n/) {
430 my %host2nid = %{$conf{host2nid}};
431 if (!defined $host2nid{$conf{hostname}}) {
432 log_fatal ("%n used but no nid defined for this host\n");
434 $nid = $host2nid{$conf{hostname}};
436 if ($conf{execcmd} =~ /%N/) {
437 if (!defined $l2f{$conf{hostname}}) {
438 log_fatal ("%N used but foreign host is undefined\n");
440 my %host2nid = %{$conf{host2nid}};
441 if (!defined $host2nid{$l2f{$conf{hostname}}}) {
442 log_fatal ("%N used but foreign nid is undefined\n");
444 $failnid = $host2nid{$l2f{$conf{hostname}}};
447 if ($conf{foreign}) {
448 @labels = @{$conf{foreign_labels}};
449 } elsif ($conf{all}) {
450 @labels = (@{$conf{local_labels}}, @{$conf{foreign_labels}});
451 } elsif ($conf{fsname}) {
452 @labels = (@labels, @{$conf{fs_labels}});
454 @labels = @{$conf{local_labels}};
458 /(\w+)-(OST|MDT|MGT|MGS)([0-9a-fA-F]{4})/;
461 my $type = $2; $type =~ tr/A-Z/a-z/;
463 my $decindex = hex($3);
465 my $cmd = $conf{execcmd};
466 my $device = $label2dev{$_};
467 if ($conf{execcmd} =~ /%j/ && !defined $label2journal{$_}) {
468 log_fatal ("%j used but no journal defined for $_\n");
470 my $journal = $label2journal{$_};
471 my $fstype = $label2type{$_};
472 if (!defined $fstype or $fstype ne "zfs") {
475 my $hostname = $label2hostname{$_};
478 if (!exists $filesys2mgs{$fsname}) {
479 log_fatal ("$fsname has no MGS defined\n");
482 my $mgs = $filesys2mgs{$fsname};
483 if (!exists $label2hostname{$mgs}) {
484 log_fatal ("$mgs has no hostname defined\n");
487 my %host2nid = %{$conf{host2nid}};
488 $mgs = $label2hostname{$mgs};
489 if (!exists $host2nid{$mgs}) {
490 log_fatal ("$mgs has no NID defined\n");
492 $mgsnid = $host2nid{$mgs};
495 $cmd =~ s/%f/$fsname/g; # %f = fsname
496 $cmd =~ s/%t/$type/g; # %t = server type
497 $cmd =~ s/%I/$hexindex/g;# %I = index (hex)
498 $cmd =~ s/%i/$decindex/g;# %i = index (dec)
499 $cmd =~ s/%l/$label/g; # %l = label
500 $cmd =~ s/%d/$device/g; # %d = device
501 $cmd =~ s/%j/$journal/g; # %j = journal device
502 $cmd =~ s/%n/$nid/g; # %n = nid
503 $cmd =~ s/%N/$failnid/g; # %N = failnid
504 $cmd =~ s/%m/$mgsnid/g; # %m = MGS nid
505 $cmd =~ s/%b/$fstype/g; # %b = backing file system type
506 $cmd =~ s/%H/$hostname/g;# %H = hostname
508 push @cmds, "$_ $cmd";
511 par_exec (@cmds) or log_fatal ("parallel command execution failed\n");
517 print STDERR "$usage";
521 sub log_msg { print STDERR "$prog: ", @_; }
522 sub log_error { log_msg ("Error: ", @_) }
523 sub log_fatal { log_msg ("Fatal: ", @_); exit 1; }
524 sub eparse_line { log_fatal ("$conf{config} line $_[0]: $eparse{$_[1]}\n"); }
525 sub eparse_str { log_fatal ("$conf{config}: $_[0]: $eparse{$_[1]}\n"); }