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 -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.
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",
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};
81 $conf{config} = "/etc/ldev.conf";
82 $conf{nidsfile} = "/etc/nids";
83 $conf{hostname} = `uname -n`; chomp $conf{hostname};
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},
106 usage() if $help || !$rc;
108 log_fatal ("cannot read config file\n") if (! -r $conf{config});
111 $conf{execcmd} = " " . join " ", @ARGV;
114 parse_nids () if ($conf{execcmd} =~ /(%n|%N)/);
121 my %label2local = ();
123 my %label2journal = ();
124 my %label2raidtab = ();
126 my %label2zpool = ();
127 my @local_labels = ();
128 my @foreign_labels = ();
130 open (CONF, "< $conf{config}") or log_fatal ("$conf{config}: $!\n");
139 my ($local, $foreign, $label, $dev, $j, $raidtab) = split;
140 if ($dev !~ /^\// && $dev =~ /^([^:]+):(.+)$/) {
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;
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;
156 $label2type{$label} = $type;
157 if ($type eq "zfs" && $dev =~ m{^([^/]+)/[^/]+$}) {
158 $label2zpool{$label} = $1;
162 if ($local eq $conf{hostname}) {
163 push @local_labels, $label;
164 } elsif ($foreign eq $conf{hostname}) {
165 push @foreign_labels, $label;
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 $_);
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;
194 open (NIDS, "< $conf{nidsfile}") or log_fatal ("$conf{nidsfile}: $!\n");
201 my ($host, $nid, $morenids) = split (/\s+/, $_, 3);
203 log_fatal ("$conf{nidsfile} line $line: incomplete line\n");
205 $host2nid{$host} = $nid;
206 $nid2host{$nid} = $host;
207 map { $nid2host{$_} = $host; } split (/\s+/, $morenids);
211 %{$conf{host2nid}} = %host2nid;
212 %{$conf{nid2host}} = %nid2host;
217 my %l2f = %{$conf{l2f}};
218 my $hostname = $conf{hostname};
219 if (exists $l2f{$hostname} && $l2f{$hostname} ne "-") {
220 print "$l2f{$hostname}\n";
226 map { print "$_\n"; } @{$conf{local_labels}};
231 map { print "$_\n"; } @{$conf{foreign_labels}};
242 my %label2dev = %{$conf{label2dev}};
244 if (exists $label2dev{$conf{device}}) {
245 print "$label2dev{$conf{device}}\n";
251 my %label2raidtab = %{$conf{label2raidtab}};
253 if (exists $label2raidtab{$conf{raidtab}}) {
254 print "$label2raidtab{$conf{raidtab}}\n";
260 my %label2journal = %{$conf{label2journal}};
262 if (exists $label2journal{$conf{journal}} &&
263 $label2journal{$conf{journal}} ne "-") {
264 print "$label2journal{$conf{journal}}\n";
270 my %label2type = %{$conf{label2type}};
272 if (exists $label2type{$conf{type}}) {
273 print "$label2type{$conf{type}}\n";
279 my %label2zpool = %{$conf{label2zpool}};
281 if (exists $label2zpool{$conf{zpool}}) {
282 print "$label2zpool{$conf{zpool}}\n";
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`;
307 my @local_labels = @{$conf{local_labels}};
308 my @foreign_labels = @{$conf{foreign_labels}};
309 my %label2dev = %{$conf{label2dev}};
310 my %label2journal = %{$conf{label2journal}};
312 foreach (@local_labels, @foreign_labels) {
313 my $lpath = "/dev/disk/by-label/$_";
314 my $dpath = $label2dev{$_};
315 my $jpath = $label2journal{$_};
318 log_error ("$lpath does not exist\n");
322 log_error ("$dpath does not exist\n");
324 } elsif (!dd_test ($dpath)) {
325 log_error ("$dpath failed dd test\n");
328 if (`readlink -f $lpath` ne `readlink -f $dpath`) {
329 log_error ("$lpath and $dpath point to different things\n");
334 log_error ("$jpath (journal for $label) does not exist\n");
336 } elsif (!dd_test ($jpath)) {
337 log_error ("$jpath failed dd test\n");
353 my $tmpfile = `mktemp \${TMPDIR:-/tmp}/ldev.XXXXXXXXXX`; chomp $tmpfile;
354 log_fatal ("failed to create $tmpfile\n") if (! -e $tmpfile);
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;
367 log_fatal ("label: fork: $!\n"); unlink $tmpfile;
370 while (($pid = wait) != -1) {
371 #print STDERR "$pid2label{$pid}: completed\n";
374 # sentinel is intact, so there were no errors
387 my %label2dev = %{$conf{label2dev}};
388 my %label2journal = %{$conf{label2journal}};
389 my %l2f = %{$conf{l2f}};
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");
397 $nid = $host2nid{$conf{hostname}};
399 if ($conf{execcmd} =~ /%N/) {
400 if (!defined $l2f{$conf{hostname}}) {
401 log_fatal ("%N used but foreign host is undefined\n");
403 my %host2nid = %{$conf{host2nid}};
404 if (!defined $host2nid{$l2f{$conf{hostname}}}) {
405 log_fatal ("%N used but foreign nid is undefined\n");
407 $failnid = $host2nid{$l2f{$conf{hostname}}};
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}};
415 @labels = (@{$conf{local_labels}}, @{$conf{foreign_labels}});
418 /(\w+)-(OST|MDT|MGT)([0-9a-fA-F]{4})/;
421 my $type = $2; $type =~ tr/A-Z/a-z/;
423 my $decindex = hex($3);
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");
430 my $journal = $label2journal{$_};
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
442 push @cmds, "$_ $cmd";
445 par_exec (@cmds) or log_fatal ("parallel command execution failed\n");
451 print STDERR "$usage";
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"); }