vnode_setup.in 11.6 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7
# All rights reserved.
#
8 9 10 11 12 13 14 15 16 17 18 19 20
use English;
use Getopt::Std;

#
# Set up the vnode state on a virtual (multiplexed) node.
#
# XXX - This script should only be run from os_setup!
#
# The output is all jumbled together since the updates are issued in parallel.
# Might be a pain when debugging. 
# 
sub usage()
{
21
    print STDOUT "Usage: vnode_setup [-f] [-k] <pid> <eid> [node ...]\n";
22 23 24
    exit(-1);
}
my  $optlist = "fdk";
25 26 27 28 29 30 31 32 33 34

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

35 36 37 38 39 40 41
#
# Configure variables
#
my $TB		= "@prefix@";
my $TESTMODE    = @TESTMODE@;
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";
42
my $CLIENT_BIN  = "@CLIENT_BINDIR@";
43 44 45 46 47 48 49 50

my $ssh		= "$TB/bin/sshtb -n";
my $debug       = 0;
my $force       = 0;
my $failed      = 0;
my $killmode    = 0;
my $dbuid;

51
my $MAX_CHILDREN = 8;
52 53
# Try to let auxiliary setup programs timeout naturally first.
my $CHILD_TIMEOUT = 600;  # ten minutes
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
77
if (@ARGV < 2) {
78 79 80 81 82 83 84 85 86 87 88
    usage();
}
if (defined($options{"f"})) {
    $force = 1;
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"k"})) {
    $killmode = 1;
}
89 90
my $pid   = shift(@ARGV);
my $eid   = shift(@ARGV);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

#
# Untaint the arguments.
#
if ($pid =~ /^([-\@\w]+)$/) {
    $pid = $1;
}
else {
    die("*** Bad data in pid: $pid\n");
}	
if ($eid =~ /^([-\@\w]+)$/) {
    $eid = $1;
}
else {
    die("*** Bad data in eid: $eid\n");
}

#
109 110
# Verify permission to muck with this experiment. Note that this script
# is run as root from the plab monitor daemon.
111
#
112
if ($UID && !TBAdmin($UID) &&
Leigh B. Stoller's avatar
Leigh B. Stoller committed
113 114 115
    !TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_DESTROY)) {
    die("*** $0:\n".
	"    You do not have permission to mess with $pid/$eid!\n");
116 117 118
}

#
119
# Get the list of nodes in this experiment.
120
#
121 122
my @nodes = ExpNodes($pid, $eid);
if (! @nodes) {
123 124 125
    warn("*** $0:\n".
	 "    No allocated nodes in experiment $pid/$eid!\n");
    exit(0);
126
}
127 128

# Nodes on the command line. Operate only on this set.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
if (@ARGV) {
    my %fulllist = ();

    # Temporary hash list for searching.
    foreach my $node ( @nodes ) {
	$fulllist{$node} = 1;
    }
    @nodes = ();

    foreach my $node ( @ARGV ) {
	if ($node =~ /^([-\@\w]+)$/) {
	    $node = $1;

	    if (!defined($fulllist{$node})) {
		die("*** $0:\n".
		     "   Node $node is not allocated to $pid/$eid!\n");
	    }
	}
	else {
	    die("Bad node name: $node.");
	}
	push(@nodes, $node);
    }
}
my $exptstate = ExpState($pid, $eid);
154 155 156 157

# Just the vnodes mam.
foreach my $node (@nodes) {
    my $pnode;
158
    my $jailed;
159
    my $plab;
160
    my $allocstate;
161
    my $mode = ($killmode ? "teardown" : "setup");
162

163
    if (! TBIsNodeVirtual($node, \$jailed, \$plab)) {
164 165
	next;
    }
166 167 168 169
    if (! TBPhysNodeID($node, \$pnode)) {
	die("*** $0:\n".
	    "    No physical node for $node!\n");
    }
170

171 172
    TBGetNodeAllocState($node, \$allocstate);

173 174 175
    #
    # On remote nodes, or when forcemode is on, always do the deed.
    # Otherwise, look at experiment state.
176 177
    #
    if (!$force) {
178 179 180 181
	if ($exptstate eq EXPTSTATE_SWAPPING) {
	    #
	    # When swapping, local vnodes go down with the physnode.
	    #
182 183 184 185 186
	    if (! TBIsNodeRemote($node)) {
		print "$node will $mode with local node $pnode.\n";
		next;
	    }
	    elsif ($allocstate eq TBDB_ALLOCSTATE_DOWN) {
187 188 189 190 191 192 193
		if (!$plab) {
		    print "$node failed to boot; skipping $mode.\n";
		    next;
		}
		# Plab nodes need to be cleaned up.
		print "$node failed to boot; changing to cleanup.\n";
		$mode = "cleanup";
194
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
195
	}
196 197
	elsif ($exptstate eq EXPTSTATE_ACTIVATING ||
	       $exptstate eq EXPTSTATE_MODIFY_RESWAP) {
198
	    #
199 200 201 202 203 204 205 206 207 208 209
	    # The allocstate determines if the vnode actually needs to
	    # be setup or torndown. Note that a failed experiment will
	    # cause a bunch of vnodes to be torndown, while in the
	    # ACTIVATING state. See os_setup and assign_wrapper; the
	    # idea is to avoid doing setup/teardown up vnodes on
	    # machines that are rebooting anyway, or that failed.
	    # Complicated by modify which could add/subtract a vnode on
	    # an existing machine, but not reboot the machine. Note that
	    # free (now unused) vnodes will land in RES_TEARDOWN. It is
	    # assumed that these booted okay, and need to be torndown,
	    # even though they are not RES_READY.
210
	    #
211 212 213
	    if (! TBIsNodeRemote($node)) {
		if ($killmode) {
		    if ($allocstate eq TBDB_ALLOCSTATE_DOWN) {
214 215 216 217
			print "$node failed to boot; skipping $mode.\n";
			next;
		    }
		    elsif ($allocstate eq TBDB_ALLOCSTATE_RES_INIT_CLEAN()) {
218 219 220
			print "$node never booted; skipping $mode.\n";
			next;
		    }
221 222 223 224
		    elsif ($allocstate eq TBDB_ALLOCSTATE_RES_READY()) {
			print "$node will $mode with local node $pnode.\n";
			next;
		    }
225 226
		}
		elsif ($allocstate eq TBDB_ALLOCSTATE_RES_READY()) {
227
		    print "$node is already setting up on local node $pnode\n";
228 229 230
		    next;
		}
	    }
231 232
	    else {
		if ($killmode) {
233 234 235
		    if ($allocstate eq TBDB_ALLOCSTATE_DEAD) {
			# plab only. See below.
			print "$node failed to initialize; skipping $mode.\n";
236 237
			next;
		    }
238 239 240 241 242 243 244 245 246
		    elsif ($allocstate eq TBDB_ALLOCSTATE_DOWN) {
			if (!$plab) {
			    print "$node failed to boot; skipping $mode.\n";
			    next;
			}
			# Plab nodes need to be cleaned up.
			print "$node failed to boot; changing to cleanup.\n";
			$mode = "cleanup";
		    }
247 248 249 250
		    elsif ($allocstate eq TBDB_ALLOCSTATE_RES_INIT_CLEAN()) {
			print "$node never booted; skipping $mode.\n";
			next;
		    }
251
		}
252 253
		elsif ($allocstate eq TBDB_ALLOCSTATE_RES_READY()) {
		    print "$node is already set up on remote node $pnode\n";
254 255
		    next;
		}
256
		elsif ($allocstate eq TBDB_ALLOCSTATE_RES_INIT_DIRTY()) {
257
		    print "$node needs a reboot on remote node $pnode\n";
258 259
		    $mode = "reboot";
		}
260 261
	    }
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
262 263 264 265 266 267
    }

    #
    # When setting up a vnode, force its event state into SHUTDOWN since
    # no telling what its initial state is. 
    # 
268
    if ($mode eq "teardown" || $mode eq "reboot") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
269 270
	TBSetNodeEventState($node, TBDB_NODESTATE_SHUTDOWN);
    }
271 272

    #
273 274 275 276 277 278
    # Put this into the list of calls we have to make in the next loop
    #
    push @vnodes, [$node, $pnode, $mode, $jailed, $plab];

}

Kirk Webb's avatar
 
Kirk Webb committed
279

280 281 282 283 284 285 286 287 288 289 290
my $children = 0;
my %child_vnodes = ();
while (1) {

    #
    # We're done when we've hit the last vnode, and we've outlived all of our
    # children
    #
    if ((!@vnodes) && ($children == 0)) {
	last;
    }
291

292 293 294
    #
    # There are more free slots
    #
295
    if (($children <= $MAX_CHILDREN) && @vnodes) {
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	#
	# Look for a vnode that is not on a pnode we're already working on
	# 
	# XXX - do this!
	
	my ($vnode, $pnode, $mode, $jailed, $plab) = @{pop @vnodes};
    
	print STDOUT "Doing $mode of vnode $vnode on $pnode ...\n";

	#
	# Run an ssh command in a child process, protected by an alarm to
	# ensure that the ssh is not hung up forever if the machine is in some
	# funky state.
	# 
	my $syspid = fork();

	if ($syspid) {
	    #
	    # Just keep track of it, we'll wait for it finish down below
	    #
	    $child_vnodes{$syspid} =
		[$vnode, $pnode, $mode, $jailed, $plab, time()];
	    $children++;
	} else {
Kirk Webb's avatar
 
Kirk Webb committed
320
            my $exval = 0;
321 322 323
	    # Must change our real UID to root so that ssh will work.
	    $UID = 0;

324
	    if ($plab && $mode eq "setup") {
Kirk Webb's avatar
 
Kirk Webb committed
325 326 327
		if (TBForkCmd("$TB/sbin/plabnode alloc $pid $eid $vnode", 1)) {
		    print STDERR "*** $0:\n" .
                        "    Plab node allocation failed\n";
328 329 330 331
		    # Should check DB state instead.
		    exit(99);
		}
	    }
332

333 334 335 336 337 338 339
	    # Cleanup is used only on plab nodes.
	    if ($mode ne "cleanup") {
		my $args = (($mode eq "teardown") ? "-k " :
			    ($mode eq "reboot" ? "-r " : " "));
		$args   .= ($jailed   ? "-j " : " ");
		$args   .= ($plab     ? "-p " : " ");
		$args   .= "$vnode ";
Kirk Webb's avatar
 
Kirk Webb committed
340 341 342 343 344 345 346 347 348 349 350 351
                
		$exval = TBForkCmd("$ssh -host $vnode ".
                                 "$CLIENT_BIN/vnodesetup $args",1);
            }

            # Free the plab node lease if necessary.
            if ($plab && ($mode eq "teardown" || $mode eq "cleanup")) {
                exec("$TB/sbin/plabnode free $pid $eid $vnode");
                die("*** $0:\n".
                    "    exec failed!\n");
            }
	    exit($exval);
352 353 354 355 356 357 358 359 360 361 362
	}
    } else {
	#
	# We have too many of the little rugrats, wait for one to die
	#

	#
	# Set up a timer - we want to kill processes after they hit 120 seconds
	# old (not much of a life, is it?), so we find the first one marked for
	# death.
	#
363
	my $oldest;
364 365 366
	my $oldestpid = 0;
	while (my ($pid, $aref) = each %child_vnodes) {
	    my ($vnode, $pnode, $mode, $jailed, $plab, $birthtime) = @$aref;
367
	    if ((!$oldestpid) || ($birthtime < $oldest)) {
368 369 370 371 372 373 374 375 376 377 378 379 380
		$oldest = $birthtime;
		$oldestpid = $pid;
	    }
	}

	#
	# Sanity check
	#
	if (!$oldest) {
	    die "*** $0\n".
	    	"Uh oh, I have no children left, something is wrong!\n";
	}

381 382 383 384 385
	#
	# If the oldest has already expried, just kill it off right now, and go
	# back around the loop
	#
	my $now = time();
386
	my $waittime = ($oldest + $CHILD_TIMEOUT) - time();
387

388 389 390
	#
	# Kill of the oldest if he gets too old while we're waiting
	#
391
	local $SIG{ALRM} = sub { kill("TERM", $oldestpid); };
392 393 394 395 396
	if ($waittime <= 0) {
	    kill("TERM",$oldestpid);
	} else {
	    alarm($waittime);
	}
397 398

	my $childpid = wait();
399
	my $exitstatus = $?;
400

401 402
	alarm 0;

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	#
	# Another sanity check
	#
	if ($childpid < 0) {
	    die "*** $0\n".
	    	"wait() returned <0, something is wrong!\n";
	}

	#
	# Look up to see what vnode, etc. this was associated with - if we
	# don't know about this child, ignore it
	#
	my $aref = $child_vnodes{$childpid};
	next unless @$aref;
	my ($vnode, $pnode, $mode, $jailed, $plab, $birthtime) = @$aref;
	$children--;
	delete $child_vnodes{$childpid};

421
	#
422
	# Look for setup failure, reported back through ssh.
423
	# 
424
	if ($exitstatus) {
425 426
	    print STDERR "vnode $vnode $mode on $pnode returned $?.\n"
		if $debug;
427

428
	    if ($exitstatus == 256) {
429
		print STDERR "$vnode is not running sshd.\n" if $debug;
430 431
	    }
	    elsif ($exitstatus == 15) {
432
		print STDERR "$vnode is wedged.\n" if $debug;
433
	    }
434 435 436
	    elsif ($exitstatus >> 8 == 99) {
		print STDERR "$vnode did not allocate properly.\n" if $debug;
	    }
437

438 439
	    push @failed_nodes, [$vnode, $pnode, $mode, $exitstatus];
	    
440 441
	    warn("*** $0:\n".
		 "    Virtual node $vnode $mode failure!\n");
442
	}
443

444 445 446 447 448 449 450 451 452 453
	if ($plab) {
	    #
	    # If the node was in the setup process, then mark its allocstate
	    # as down so os_setup knows not to bother waiting for it. DEAD
	    # is a temp state, different then DOWN. It indicates the node
	    # was never instantiated (important distinction for plab). 
	    #
	    if ($exitstatus &&
		(($mode eq "setup") || ($mode eq "reboot"))) {
		TBSetNodeAllocState($vnode, TBDB_ALLOCSTATE_DEAD());
454 455
	    }
	}
456 457 458
    }
}

459 460 461 462 463 464
#
# In force node, do not bother with this other stuff. 
#
exit(scalar(@failed_nodes))
    if ($force);

465 466 467 468 469 470
#
# Send mail to testbed-ops about failed nodes
#
if (@failed_nodes) { 
    my $failed_lines = join("\n",map { join("\t",@{$_}) } @failed_nodes);
    SENDMAIL($TBOPS, "Virtual Node failure for $pid/$eid",
471 472 473
	     "The following virtual nodes failed: \n" .
	     "vnode\t\tpnode\tmode\texit status\n" .
	     $failed_lines);
474 475
}

476 477 478 479 480 481
if ($killmode) {
    print STDOUT "Vnode teardown finished.\n";
}
else {
    print STDOUT "Vnode setup initiated on all nodes ...\n";
}
482
exit(0);