nfsmfs_setup.in 4.63 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
use English;
use Getopt::Std;

#
# Create an NFS-based root filesystem for a node to act as the admin MFS.
# We only do this for nodes with the magic admin MFS OSID set unless force
# is specified.
#
sub usage()
{
    print(STDERR
	  "Usage: nfsmfs_setup [-D] [-Ndfnq] node ...\n".
	  "switches and arguments:\n".
	  "-D       - destroy rather than setup the MFS\n".
	  "-N       - don't check DB to see if node uses an NFS MFS, just do it\n".
	  "-d       - debug; enabled additional debugging\n".
	  "-f       - force (re)creation of MFS even if it exists\n".
	  "-q       - be quiet\n".
	  "-n       - don't do anything, just say what would happen\n".
	  "node ... - list of nodes for which to create/destroy MFSes\n");
    exit(-1);
}
my $optlist    = "Ddfnq";
my $impotent   = 0;
my $debug      = 0;
my $destroy    = 0;
my $nocheck    = 0;
my $force      = 0;
my $quiet      = 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TESTMODE    = @TESTMODE@;
my $FSNODE      = "@FSNODE@";
my $WITHZFS     = @WITHZFS@;
62
my $ZFSNOEXPORT	= "@ZFS_NOEXPORT@";
63 64
my $SSH		= "$TB/bin/sshtb -n -l root -host $FSNODE";
my $PROG	= "$TB/sbin/nfsmfs_setup.proxy";
65 66
my $EXPORTSSETUP= "$TB/sbin/exports_setup";
my $NFSROOT	= "@NFSMFS_ROOT@";
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

#
# We don't want to run this script unless its the real version.
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

#
# Only implemented with ZFS cloning.
#
if ($WITHZFS == 0) {
    die("*** $0:\n".
	"    Only implemented with ZFS FS node right now.\n");
}

84 85 86 87 88
if ($NFSROOT eq "") {
    die("*** $0:\n".
	"    Must set NFSMFS_ROOT in defs file.");
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# Testbed Support libraries
# 
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use Data::Dumper;
use Node;
106
use OSImage;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

#
# Check args.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"f"})) {
    $force = 1;
}
if (defined($options{"N"})) {
    $nocheck = 1;
}
if (defined($options{"q"})) {
    $quiet = 1;
}
if (defined($options{"D"})) {
    $destroy = 1;
}
usage()
    if (@ARGV == 0);

#
# We need to serialize this script.
#
if (!$TESTMODE) {
140
    if ((my $locked = TBScriptLock("nfsmfs")) != TBSCRIPTLOCK_OKAY()) {
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	fatal("Could not get the lock after a long time!\n");
    }
}

my @nodes;
foreach my $nodeid (@ARGV) {
    if ($nodeid =~ /^([-\w]+)$/) {
	$nodeid = $1;
    } else {
	print STDERR "Skipping bogus node id '$nodeid'\n"
	    if (!$quiet);
	next;
    }

    my $nodeobj = Node->Lookup($nodeid);
    if (!$nodeobj) {
	print STDERR "Skipping bogus node '$nodeid'\n"
	    if (!$quiet);
	next;
    }

    if (!$nocheck) {
163 164
	if (OSImage->Lookup($nodeobj->adminmfs_osid())->IsNfsMfs() &&
	    $nodeobj->NeedsAdminMFS()) {
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	    push @nodes, $nodeid;
	}
    } else {
	push @nodes, $nodeid;
    }
}

if (@nodes == 0) {
    print STDERR "Found no nodes that require NFS MFS.\n"
	if (!$quiet);
    exit(0);
}

#
# Let's do it!
#
if (!$TESTMODE) {
  $UID = 0;

  my $opts = $destroy ? "-D" : "";

  $opts .= " -d" if ($debug);
  $opts .= " -f" if ($force);
  $opts .= " -n" if ($impotent);

  if ($impotent) {
      print STDERR "Would do: $SSH $PROG $opts @nodes\n";
  }
  system("$SSH $PROG $opts @nodes") == 0 or
      fatal("Failed: $SSH $PROG $opts @nodes: $?");

196 197 198 199 200 201
  # XXX do exports_setup
  if ($ZFSNOEXPORT) {
      system($EXPORTSSETUP) == 0 or
	  fatal("$EXPORTSSETUP failed");
  }

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  #
  # Release the lock!
  #
  TBScriptUnlock();
}

exit(0);

sub fatal {
    local($msg) = $_[0];

    TBScriptUnlock()
	if (!$TESTMODE);
    
    SENDMAIL($TBOPS, "NFS MFS setup: ", $msg)
	if (!$debug);
    die($msg);
}