Whamcloud - gitweb
LU-8055 ldev: Add %H and %b to command sub
[fs/lustre-release.git] / lustre / scripts / ldev
old mode 100644 (file)
new mode 100755 (executable)
index 4b7b008..8005bcf
@@ -22,6 +22,7 @@ Parse ldev.conf and answer the following queries:
   -l, --local         Print labels for local devices.
   -f, --foreign       Print labels for foreign devices.
   -a, --all           Print labels for local and foreign devices.
+  -F, --filesys NAME  Print labels for file system NAME.
   -s, --sanity        Sanity check config on this node.
   -d, --device LABEL  Print storage device of LABEL.
   -j, --journal LABEL Print journal device of LABEL if it exists.
@@ -30,8 +31,9 @@ Parse ldev.conf and answer the following queries:
   -z, --zpool LABEL   Print zpool containing LABEL.
   CMD [ARGS] ...      Run CMD in parallel for each device substituting:
                       %f=fsname  %d=device  %i=dec-index %n=main-nid %l=label
-                      %t=srvtype %j=journal %I=hex-index %N=fail-nid
-                      May be used in combination with -l, -f, -a options.
+                      %t=srvtype %j=journal %I=hex-index %N=fail-nid %m=mgs-nid
+                      %H=hostname %b=backing-fs
+                      May be used in combination with -l, -f, -a, -F options.
 EOF
 
 my %eparse = (
@@ -62,6 +64,7 @@ query_journal ()  if $conf{journal};
 query_raidtab ()  if $conf{raidtab};
 query_type ()     if $conf{type};
 query_zpool ()    if $conf{zpool};
+query_filesys ()  if $conf{fsname};
 
 exit(0);
 
@@ -85,6 +88,7 @@ sub parse_cmdline
     $conf{sanity} = 0;
     $conf{execcmd} = "";
     $conf{journal} = "";
+    $conf{fsname} = "";
 
     my $rc = GetOptions (
         "help|h!"         => \$help,
@@ -101,17 +105,25 @@ sub parse_cmdline
         "raidtab|r=s"     => \$conf{raidtab},
         "type|t=s"        => \$conf{type},
         "zpool|z=s"       => \$conf{zpool},
+        "filesys|F=s"     => \$conf{fsname},
     );
 
     usage() if $help || !$rc;
 
     log_fatal ("cannot read config file\n") if (! -r $conf{config});
 
+    my $filters = 0;
+    $filters++ if ($conf{local});
+    $filters++ if ($conf{foreign});
+    $filters++ if ($conf{all});
+    $filters++ if ($conf{fsname});
+    log_fatal ("Only one of -l, -f, -a, or -F may be used.\n") if ($filters > 1);
+
     if (@ARGV) {
         $conf{execcmd} = " " . join " ", @ARGV;
     }
 
-    parse_nids () if ($conf{execcmd} =~ /(%n|%N)/);
+    parse_nids () if ($conf{execcmd} =~ /(%n|%N|%m)/);
 }
 
 sub parse_config
@@ -124,8 +136,11 @@ sub parse_config
     my %label2raidtab = ();
     my %label2type = ();
     my %label2zpool = ();
+    my %filesys2mgs = ();
+    my %label2hostname = ();
     my @local_labels = ();
     my @foreign_labels = ();
+    my @fs_labels = ();
 
     open (CONF, "< $conf{config}") or log_fatal ("$conf{config}: $!\n");
 
@@ -148,6 +163,9 @@ sub parse_config
 
         eparse_line ($line, "elabel_uniq") if (exists $label2dev{$label}
                                          || exists $label2local{$label});
+        /(\w+)-(OST|MDT|MGT|MGS)([0-9a-fA-F]{4})/, $label;
+        my $filesys = $1;
+        my $nodetype = $2;
         $label2dev{$label} = $dev;
         $label2local{$label} = $local;
         $label2journal{$label} = $j if defined $j;
@@ -158,12 +176,20 @@ sub parse_config
                 $label2zpool{$label} = $1;
             }
         }
+        $label2hostname{$label}=$local;
+        if ($nodetype eq "MGS" or $nodetype eq "MGT") {
+            $filesys2mgs{$filesys} = $label;
+        }
 
         if ($local eq $conf{hostname}) {
             push @local_labels, $label;
         } elsif ($foreign eq $conf{hostname}) {
             push @foreign_labels, $label;
         }
+
+        if ($conf{fsname} and $filesys eq $conf{fsname}) {
+            push @fs_labels, $label;
+        }
     }
     close CONF;
 
@@ -176,6 +202,7 @@ sub parse_config
 
     @{$conf{local_labels}} = @local_labels;
     @{$conf{foreign_labels}} = @foreign_labels;
+    @{$conf{fs_labels}} = @fs_labels;
     %{$conf{l2f}} = %l2f;
     %{$conf{label2dev}} = %label2dev;
     %{$conf{label2local}} = %label2local;
@@ -183,6 +210,8 @@ sub parse_config
     %{$conf{label2raidtab}} = %label2raidtab;
     %{$conf{label2type}} = %label2type;
     %{$conf{label2zpool}} = %label2zpool;
+    %{$conf{filesys2mgs}} = %filesys2mgs;
+    %{$conf{label2hostname}} = %label2hostname;
 }
 
 sub parse_nids ()
@@ -231,6 +260,11 @@ sub query_foreign
     map { print "$_\n"; } @{$conf{foreign_labels}};
 }
 
+sub query_filesys
+{
+    map { print "$_\n"; } @{$conf{fs_labels}};
+}
+
 sub query_all
 {
     query_local ();
@@ -385,7 +419,10 @@ sub exec_cmd
     my @labels = ();
     my @cmds = ();
     my %label2dev = %{$conf{label2dev}};
+    my %label2type = %{$conf{label2type}};
     my %label2journal = %{$conf{label2journal}};
+    my %filesys2mgs = %{$conf{filesys2mgs}};
+    my %label2hostname = %{$conf{label2hostname}};
     my %l2f = %{$conf{l2f}};
     my ($nid, $failnid);
 
@@ -407,15 +444,18 @@ sub exec_cmd
         $failnid = $host2nid{$l2f{$conf{hostname}}};
     }
 
-    if ($conf{foreign} and !$conf{local} and !$conf{all}) {
+    if ($conf{foreign}) {
         @labels = @{$conf{foreign_labels}};
-    } elsif (!$conf{foreign} and !$conf{all}) {
-        @labels = @{$conf{local_labels}};
-    } else {
+    } elsif ($conf{all}) {
         @labels = (@{$conf{local_labels}}, @{$conf{foreign_labels}});
+    } elsif ($conf{fsname}) {
+        @labels = (@labels, @{$conf{fs_labels}});
+    } else {
+        @labels = @{$conf{local_labels}};
     }
+
     foreach (@labels) {
-        /(\w+)-(OST|MDT|MGT)([0-9a-fA-F]{4})/;
+        /(\w+)-(OST|MDT|MGT|MGS)([0-9a-fA-F]{4})/;
 
         my $fsname = $1;
         my $type = $2; $type =~ tr/A-Z/a-z/;
@@ -428,6 +468,29 @@ sub exec_cmd
             log_fatal ("%j used but no journal defined for $_\n");
         }
         my $journal = $label2journal{$_};
+        my $fstype = $label2type{$_};
+        if (!defined $fstype or $fstype ne "zfs") {
+            $fstype = "ldiskfs";
+        }
+        my $hostname = $label2hostname{$_};
+        my $mgsnid;
+        if ($cmd =~ /%m/) {
+            if (!exists $filesys2mgs{$fsname}) {
+                 log_fatal ("$fsname has no MGS defined\n");
+            }
+
+            my $mgs = $filesys2mgs{$fsname};
+            if (!exists $label2hostname{$mgs}) {
+                 log_fatal ("$mgs has no hostname defined\n");
+            }
+
+            my %host2nid = %{$conf{host2nid}};
+            $mgs = $label2hostname{$mgs};
+            if (!exists $host2nid{$mgs}) {
+                 log_fatal ("$mgs has no NID defined\n");
+            }
+            $mgsnid = $host2nid{$mgs};
+        }
 
         $cmd =~ s/%f/$fsname/g;  # %f = fsname
         $cmd =~ s/%t/$type/g;    # %t = server type
@@ -438,6 +501,9 @@ sub exec_cmd
         $cmd =~ s/%j/$journal/g; # %j = journal device
         $cmd =~ s/%n/$nid/g;     # %n = nid
         $cmd =~ s/%N/$failnid/g; # %N = failnid
+        $cmd =~ s/%m/$mgsnid/g;  # %m = MGS nid
+        $cmd =~ s/%b/$fstype/g;  # %b = backing file system type
+        $cmd =~ s/%H/$hostname/g;# %H = hostname
 
         push @cmds, "$_ $cmd";
     }