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 -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.
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",
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};
85 $conf{config} = "/etc/ldev.conf";
86 $conf{nidsfile} = "/etc/nids";
87 $conf{hostname} = `uname -n`; chomp $conf{hostname};
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},
114 usage() if $help || !$rc;
116 log_fatal ("cannot read config file\n") if (! -r $conf{config});
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);
126 $conf{execcmd} = " " . join " ", @ARGV;
129 parse_nids () if ($conf{execcmd} =~ /(%n|%N|%m)/);
136 my %label2local = ();
138 my %label2journal = ();
139 my %label2raidtab = ();
141 my %label2zpool = ();
142 my %filesys2mgs = ();
143 my %label2hostname = ();
144 my @local_labels = ();
145 my @foreign_labels = ();
148 open (CONF, "< $conf{config}") or log_fatal ("$conf{config}: $!\n");
157 my ($local, $foreign, $label, $dev, $j, $raidtab) = split;
158 if ($dev !~ /^\// && $dev =~ /^([^:]+):(.+)$/) {
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;
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;
172 $label2dev{$label} = $dev;
173 $label2local{$label} = $local;
174 $label2journal{$label} = $j if defined $j;
175 $label2raidtab{$label} = $raidtab if defined $raidtab;
177 $label2type{$label} = $type;
178 if ($type eq "zfs" && $dev =~ m{^([^/]+)/[^/]+$}) {
179 $label2zpool{$label} = $1;
182 $label2hostname{$label}=$local;
184 if ($nodetype eq "MGS") {
185 $filesys2mgs{$filesys} = $label;
188 next if $conf{role} and lc $conf{role} ne lc $nodetype;
190 if ($local eq $conf{hostname}) {
191 push @local_labels, $label;
192 } elsif ($foreign eq $conf{hostname}) {
193 push @foreign_labels, $label;
196 if ($conf{fsname} and $filesys eq $conf{fsname}) {
197 push @fs_labels, $label;
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 $_);
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;
229 open (NIDS, "< $conf{nidsfile}") or log_fatal ("$conf{nidsfile}: $!\n");
236 my ($host, $nid, $morenids) = split (/\s+/, $_, 3);
238 log_fatal ("$conf{nidsfile} line $line: incomplete line\n");
240 $host2nid{$host} = $nid;
241 $nid2host{$nid} = $host;
242 map { $nid2host{$_} = $host; } split (/\s+/, $morenids);
246 %{$conf{host2nid}} = %host2nid;
247 %{$conf{nid2host}} = %nid2host;
252 my %l2f = %{$conf{l2f}};
253 my $hostname = $conf{hostname};
254 if (exists $l2f{$hostname} && $l2f{$hostname} ne "-") {
255 print "$l2f{$hostname}\n";
261 map { print "$_\n"; } @{$conf{local_labels}};
266 map { print "$_\n"; } @{$conf{foreign_labels}};
271 map { print "$_\n"; } @{$conf{fs_labels}};
282 my %label2dev = %{$conf{label2dev}};
284 if (exists $label2dev{$conf{device}}) {
285 print "$label2dev{$conf{device}}\n";
291 my %label2raidtab = %{$conf{label2raidtab}};
293 if (exists $label2raidtab{$conf{raidtab}}) {
294 print "$label2raidtab{$conf{raidtab}}\n";
300 my %label2journal = %{$conf{label2journal}};
302 if (exists $label2journal{$conf{journal}} &&
303 $label2journal{$conf{journal}} ne "-") {
304 print "$label2journal{$conf{journal}}\n";
310 my %label2type = %{$conf{label2type}};
312 if (exists $label2type{$conf{type}}) {
313 print "$label2type{$conf{type}}\n";
319 my %label2zpool = %{$conf{label2zpool}};
321 if (exists $label2zpool{$conf{zpool}}) {
322 print "$label2zpool{$conf{zpool}}\n";
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`;
347 my @local_labels = @{$conf{local_labels}};
348 my @foreign_labels = @{$conf{foreign_labels}};
349 my %label2dev = %{$conf{label2dev}};
350 my %label2journal = %{$conf{label2journal}};
352 foreach (@local_labels, @foreign_labels) {
353 my $lpath = "/dev/disk/by-label/$_";
354 my $dpath = $label2dev{$_};
355 my $jpath = $label2journal{$_};
358 log_error ("$lpath does not exist\n");
362 log_error ("$dpath does not exist\n");
364 } elsif (!dd_test ($dpath)) {
365 log_error ("$dpath failed dd test\n");
368 if (`readlink -f $lpath` ne `readlink -f $dpath`) {
369 log_error ("$lpath and $dpath point to different things\n");
374 log_error ("$jpath (journal for $label) does not exist\n");
376 } elsif (!dd_test ($jpath)) {
377 log_error ("$jpath failed dd test\n");
393 my $tmpfile = `mktemp \${TMPDIR:-/tmp}/ldev.XXXXXXXXXX`; chomp $tmpfile;
394 log_fatal ("failed to create $tmpfile\n") if (! -e $tmpfile);
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;
407 log_fatal ("label: fork: $!\n"); unlink $tmpfile;
410 while (($pid = wait) != -1) {
411 #print STDERR "$pid2label{$pid}: completed\n";
414 # sentinel is intact, so there were no errors
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}};
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");
440 $nid = $host2nid{$conf{hostname}};
442 if ($conf{execcmd} =~ /%N/) {
443 if (!defined $l2f{$conf{hostname}}) {
444 log_fatal ("%N used but foreign host is undefined\n");
446 my %host2nid = %{$conf{host2nid}};
447 if (!defined $host2nid{$l2f{$conf{hostname}}}) {
448 log_fatal ("%N used but foreign nid is undefined\n");
450 $failnid = $host2nid{$l2f{$conf{hostname}}};
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}});
460 @labels = @{$conf{local_labels}};
464 /(\w+)-(OST|MDT|MGS)([0-9a-fA-F]{4})/;
467 my $type = $2; $type =~ tr/A-Z/a-z/;
469 my $decindex = hex($3);
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");
476 my $journal = $label2journal{$_};
477 my $fstype = $label2type{$_};
478 if (!defined $fstype or $fstype ne "zfs") {
481 my $hostname = $label2hostname{$_};
484 if (!exists $filesys2mgs{$fsname}) {
485 log_fatal ("$fsname has no MGS defined\n");
488 my $mgs = $filesys2mgs{$fsname};
489 if (!exists $label2hostname{$mgs}) {
490 log_fatal ("$mgs has no hostname defined\n");
493 my %host2nid = %{$conf{host2nid}};
494 $mgs = $label2hostname{$mgs};
495 if (!exists $host2nid{$mgs}) {
496 log_fatal ("$mgs has no NID defined\n");
498 $mgsnid = $host2nid{$mgs};
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
514 push @cmds, "$_ $cmd";
517 par_exec (@cmds) or log_fatal ("parallel command execution failed\n");
523 print STDERR "$usage";
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"); }