Firewall.pm.in 22.5 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2009-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
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#

#
# Some firewall stuff.
#
package Firewall;
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA = "Exporter";
@EXPORT = qw ( doFWlans undoFWNodes
	       FWSETUP FWADDNODES FWDELNODES FWTEARDOWN
	       );

# Configure variables
my $TB	      = "@prefix@";
my $TBOPS     = "@TBOPSEMAIL@";
my $POWER     = "$TB/bin/power";
my $SNMPIT    = "$TB/bin/snmpit";
42
my $VNODESETUP= "$TB/sbin/vnode_setup";
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

# Flags.
sub FWSETUP()		{ return 1; }
sub FWADDNODES()	{ return 2; }
sub FWDELNODES()	{ return 3; }
sub FWTEARDOWN()	{ return 4; }

# XXX fixme: should not be hardwired!
my $cnetstack    = "-S Control";
my $cnetvlanname = "Control";

use emdbi;
use libdb;
use libtestbed;
use libadminmfs;
use libtblog_simple;
use Node;
use Interface;
use Experiment;
use Lan;

#
# Setup and teardown experiment firewall.
#
# XXX note that right now, we just setup the switch infrastructure
# first, and then just let everything else go.  Firewalled nodes will
# not boot until the firewall is up since the VLAN is isolated til then.
# The firewall will boot ok since it still talks to the real control net.
#
# XXX for tearing down firewalls, we assume that nodes have been "cleansed"
# and it is safe to put ports back into the default control net VLAN.
#
sub doFWlans($$$) {
    my ($experiment, $action, $nodelist) = @_;
    my ($fwnode, $fwvlanname, $fwvlan, $fwport, $fwvid);
    my %nodenames;

    my $pid = $experiment->pid();
    my $eid = $experiment->eid();

    #
    # See if there is a firewall, fetching node/VLAN info if so.
    # If not, we are all done.
    #
    if (!$experiment->IsFirewalled(\$fwnode, \$fwvid, \$fwvlan)) {
	return 0;
    }

    if ($action == FWSETUP) {
92 93 94
	# Allow redo.
	$fwvid = TBGetUniqueIndex("cnet_vlanid")
	    if (!defined($fwvid));
95 96 97 98 99 100 101 102 103 104 105 106 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

	print "Setting up control net firewall.\n";
    }
    else {
	if ($action == FWADDNODES) {
	    print "Adding nodes to control net firewall.\n";
	}
	elsif ($action == FWDELNODES) {
	    print "Removing nodes from control net firewall.\n";
	}
	else {
	    print "Tearing down control net firewall.\n";
	}

	# Prior setup didn't succeed, nothing to do
	if (!defined($fwvid)) {
	    return 0;
	}
    }

    # See below.
    if (defined($nodelist)) {
	foreach my $nodeid (@$nodelist) {
	    my $node = Node->Lookup($nodeid);
	    if (!defined($node)) {
		tberror("Could not map $nodeid to its object");
		return 1;
	    }
	    $nodenames{$nodeid} = $node;
	}
    }

    # Get current list of reserved nodes. 
    my @allnodes;
    if (Node->BulkLookup($experiment, \@allnodes) < 0) {
	tberror("Failed to load reserved nodes");
	return 1;
    }

    # XXX vlanid in the DB is currently an int, we need a more unique name
    $fwvlanname = "fw$fwvid";

    #
    # Find all the experiment nodes and their control interface switch ports
    #
    my $portlist  = "";
    foreach my $node (@allnodes) {
	next
	    if ($node->isremotenode() || $node->isvirtnode());

	my $control_iface = Interface->LookupControl($node);
	if (!defined($control_iface)) {
	    tberror("Could not find control iface object for $node");
	    return 1;
	}
	my $node_id = $node->node_id();
	my $cif = $control_iface->iface();
	    
	if ($node_id eq $fwnode) {
	    $fwport = "$node_id:$cif";
	}
	elsif (defined($nodelist)) {
	    # Only nodes we are moving in/out of the experiment.
	    $portlist .= " $node_id:$cif"
		if (exists($nodenames{$node_id}));
	}
	else {
	    $portlist .= " $node_id:$cif";
	}
    }
    if (!defined($fwport)) {
	tberror "Firewall node '$fwnode' not found in $pid/${eid}!";
	return 0;
    }
    if ($portlist eq "") {
	#
	# We catch this up in swapexp; admin users can specify just a firewall,
	# but mere users must have at least one firewalled node. Just print
	# the warning though. 
	# 
	tbwarn "No firewalled nodes in $pid/${eid}!";
    }

    #
    # XXX hack commands til we nail down the API
    #
    my $fwsetupstr1 = "$SNMPIT $cnetstack -m $fwvlanname $pid $eid $portlist";
    my $fwsetupstr3 = "$SNMPIT $cnetstack -T $fwport $cnetvlanname ";
    my $fwtakedownstr0 = "$SNMPIT $cnetstack -e $fwport";
    my $fwtakedownstr1 = ($portlist eq "" ? "true" :
		  "$SNMPIT $cnetstack -m $cnetvlanname $pid $eid $portlist");
186 187
    my $fwtakedownstr1b = ($portlist eq "" ? "true" :
		  "$SNMPIT -f $cnetstack -m $cnetvlanname $portlist");
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    my $fwtakedownstr2 = "$SNMPIT $cnetstack -o $fwvlanname $pid $eid";
    my $fwtakedownstr3 = "$SNMPIT $cnetstack -U $fwport";
    my $fwtakedownstr4 = "$SNMPIT $cnetstack -f -m $cnetvlanname $fwport";

    if ($action == FWSETUP) {
	TBDebugTimeStamp("snmpit firewall setup: VLAN");
	print "doFW: '$fwsetupstr1'\n";
	if (system($fwsetupstr1)) {
	    tberror({type => 'secondary', severity => SEV_SECONDARY,
		     error => ['fwcnvlan_setup_failed']},
		    "Failed to setup Firewall control net VLAN.");
	    return 1;
	}
	my $vlan = VLan->Lookup($experiment, $fwvlanname);
	if (!defined($vlan)) {
	    tberror({type => 'secondary', severity => SEV_SECONDARY,
		     error => ['fwcnvlan_setup_failed']},
		    "Failed to locate vlan object for $fwvlanname");
	    return 1;
	}
	if ($vlan->GetTag(\$fwvlan) != 0) {
	    tberror("No vlan tag associated with $vlan");
	    goto badsetup;
	}

	#
	# No point to trunking if thre are no ports; fails.
	#
	if ($portlist ne "") {
	    $fwsetupstr3 = "$fwsetupstr3 " . $vlan->id();
	    TBDebugTimeStamp("snmpit firewall setup: trunk");
	    print "doFW: '$fwsetupstr3'\n";
	    if (system($fwsetupstr3)) {
		tberror "Failed to setup Firewall trunk on port $fwport.";
	      badsetup:
		print "doFW: '$fwtakedownstr1'\n";
		if (system($fwtakedownstr1)) {
		    tberror "Could not return $portlist to Control VLAN!";
		    return 1;
		}
		print "doFW: '$fwtakedownstr2'\n";
		if (system($fwtakedownstr2)) {
		    tberror "Could not destroy VLAN $fwvlanname ($fwvlan)!";
		    return 1;
		}
		print "doFW: '$fwtakedownstr3'\n";
		if (system($fwtakedownstr3)) {
		    tberror "Could not untrunk $fwport!";
		    return 1;
		}
		print "doFW: '$fwtakedownstr4'\n";
		if (system($fwtakedownstr4)) {
		    tberror "Could not move $fwport back to Control lan!";
		}
		return 1;
	    }
	}
	TBDebugTimeStamp("snmpit firewall setup done");

	# Record VLAN info now that everything is done
	$experiment->SetFirewallVlan($fwvid, $fwvlan);
	return 0;
    }
    elsif ($action == FWADDNODES) {
	my $vlan = VLan->Lookup($experiment, $fwvlanname);
	if (!defined($vlan)) {
	    tberror "Cannot find vlan object for $fwvlanname";
	    return 1;
	}
	TBDebugTimeStamp("snmpit firewall port addition");
	print "doFW: '$fwsetupstr1'\n";
	if (system($fwsetupstr1)) {
	    tberror "Failed to add nodes to Firewall control net VLAN.";
	    return 1;
	}
	#
	# Redo the trunk operation since there might not have been
	# any ports last time, and the vlan would not have existed,
	# so the trunk would not be setup.
	#
	$fwsetupstr3 = "$fwsetupstr3 " . $vlan->id();
	print "doFW: '$fwsetupstr3'\n";
	if (system($fwsetupstr3)) {
	    tberror "Failed to setup Firewall trunk on port $fwport.";
	    return 1;
	}
	TBDebugTimeStamp("snmpit firewall setup done");
    }
    elsif ($action == FWDELNODES) {
	TBDebugTimeStamp("snmpit firewall port deletion");
	print "doFW: '$fwtakedownstr1'\n";
	if (system($fwtakedownstr1)) {
280 281 282 283 284 285 286 287 288 289 290 291 292
	    #
	    # If the exit value is "2" then retry with hidden force option
	    # since the port was probably already moved, but snmpit has no
	    # way to verify that. We do know it is out of the old vlan though.
	    #
	    if ($? >> 8 == 2) {
		system($fwtakedownstr1b);
	    }
	    if ($?) {
		tberror "Failed to remove nodes from Firewall ".
			"control net VLAN.\n";
		return 1;
	    }
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	}
	TBDebugTimeStamp("snmpit firewall setup done");
    }
    else {
	TBDebugTimeStamp("snmpit re-enable fw control port: $fwport");
	print "doFW: '$fwtakedownstr0'\n";
	my $failed = 0;
	if (system($fwtakedownstr0)) {
	    tberror "Could not re-enable firewall control port $fwport!";
	    $failed = 1;
	}
	#
	# Do not try to do this if the vlan is already gone.
	#
	my $vlan = VLan->Lookup($experiment, $fwvlanname);
	if (defined($vlan)) {
	    TBDebugTimeStamp("snmpit firewall teardown: VLAN");
	    print "doFW: '$fwtakedownstr1'\n";
	    if (system($fwtakedownstr1)) {
		tberror "Could not return $portlist to Control VLAN!";
		return 1;
	    }
	    print "doFW: '$fwtakedownstr2'\n";
	    if (system($fwtakedownstr2)) {
		tberror "Could not destroy VLAN $fwvlanname ($fwvlan)!";
		return 1;
	    }
	}
	TBDebugTimeStamp("snmpit firewall teardown: trunk");
	print "doFW: '$fwtakedownstr3'\n";
	if (system($fwtakedownstr3)) {
	    tberror "Could not tear down trunk on $fwport!";
	    $failed = 1;
	}
	print "doFW: '$fwtakedownstr4'\n";
	if (system($fwtakedownstr4)) {
	    tberror "Could not return $fwport to Control VLAN!";
	    $failed = 1;
	}
	if ($failed) {
	    return 1;
	}
	TBDebugTimeStamp("snmpit firewall teardown done");

	# Clean VLAN info from DB
	$experiment->ClearFirewallVlan();
    }
    return 0;
}

#
# Undo the firewall state for a set of nodes in the indicated experiment.
# If no nodes are specified, we remove all nodes and tear down the firewall.
#
# This function takes care of ensuring that all such nodes have been
# neutered prior to being released:
#
# Change the OSID of all nodes (firewall included) to reboot into
# the admin MFS and then power cycle them.  We must power cycle to
# ensure nodes don't spoof a simple reboot request and pretend to
# come up in the MFS.  The power "cycle" is actually an "off"
# followed by an "on", since true power cycling of large numbers
# of nodes may be done in batches to avoid network (or power)
# overload on restart.  Skewed reboots like this open a window of
# vulnerability where nodes rebooted later might be able, before
# they are rebooted, to spoof the reload server for nodes that have
# just rebooted.  So we first turn everyone off, then batched power
# ons are safe.
#
# Note that we take down the firewall while the nodes are turned
# off.  This is a convenient time while we know no nodes are in
# transition.  Plus, since we take down the firewall too, other
# nodes would not be able to reboot if we left the firewall up.
#
# BIG SECURITY ASSUMPTION: we are assuming that upon power on,
# no node can somehow reboot from the hard disk instead of
# from the network.  If it does, it is out from behind the
# firewall and can wreak havoc.
#
sub undoFWNodes($;$@) {
    my ($experiment, $leavefw, @nodes) = @_;
    my $doall   = 0;
    my $fwerr   = 0;
    my @fwstate = ();
    my $zap = 1;
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
    $leavefw = 0
	if (!defined($leavefw));

    my $fwnode;
Leigh Stoller's avatar
Leigh Stoller committed
384
    return 0
385 386 387 388 389
	if ($experiment->IsFirewalled(\$fwnode) == 0);

    if (!@nodes || scalar(@nodes) == 0) {
	$experiment->LocalNodeListNames(\@nodes, 1);
	if ($leavefw) {
Leigh Stoller's avatar
Leigh Stoller committed
390
	    @nodes = grep {$_ ne $fwnode} @nodes;
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	}
	else {
	    $doall = 1;
	}
    }

    #
    # There has to at least be a firewall node to be interesting
    #
    if (@nodes == 0) {
	return 0;
    }

    if ($doall) {
	print "Taking down experiment firewall.\n";
    } else {
	if (grep {$_ eq $fwnode} @nodes) {
	    tberror "Cannot remove firewall node from an experiment!";
	    return 1;
	}
	print "Removing firewalled nodes from experiment.\n";
    }

    #
    # At the lowest level of security, we don't do the diskzap dance
    # unless we have paniced.
    #
    # This level ("blue") is used for experiments where we are trying to
    # protect the inside from outside; i.e., there is no bad stuff inside
    # to clean up.
    #
    my $paniced = $experiment->paniced();
    my $security_level = $experiment->security_level();
    if ($paniced == 0 && $security_level < TBDB_SECLEVEL_ZAPDISK()) {
	$zap = 0;
    }

    #
    # First turn off all the machines.
    # If we fail, the firewall in left in place, and some nodes may
    # be powered off.
    #
    if ($zap) {
	print STDERR "Powering down firewalled nodes.\n";
	TBDebugTimeStamp("Powering down nodes");
	system("$POWER off @nodes");
	if ($?) {
438
	    tberror(SEV_SECONDARY, 'power_off_failed');
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	    $fwerr = "Failed to power off all nodes!";
	    @fwstate = ("Firewall is still in place",
			"Some nodes may NOT be powered off",
			"Nodes NOT switched to admin MFS");
	    goto done;
	}

	#
	# Force all nodes into admin mode.
	# If we fail, the firewall is left in place.
	#
	my %myargs;
	$myargs{'name'} = "tbswap";
	$myargs{'on'} = 1;
	$myargs{'clearall'} = 1;
	if (TBAdminMfsSelect(\%myargs, undef, @nodes)) {
	    $fwerr = "Failed to force all nodes into admin mode!";
	    @fwstate = ("Firewall is still in place",
			"All nodes are powered off",
			"Not all nodes have been switched to admin MFS");
	    goto done;
	}
    }

    #
    # Once all nodes have been turned off and their DB state changed
    # to force MFS booting, we can modify the switch firewall state,
    # either tearing down the firewall ($doall) entirely or just moving
    # the indicated nodes out from behind it (!$doall).
    #
    # If this fails, we warn and punt.  The switch or DB state could
    # be screwed up at this point.
    #
    if ($doall) {
	if (doFWlans($experiment, FWTEARDOWN, undef)) {
	    $fwerr = "Failed to tear down firewall!";
	    @fwstate = ("Firewall may NOT be in place",
			$zap ? ("All nodes are powered off",
				"All nodes set to admin mode") : (),
			"Switch/DB firewall state could be inconsistent!");
	    goto done;
	}
    } else {
	my @deleted = @nodes;
	if (doFWlans($experiment, FWDELNODES, \@deleted)) {
	    $fwerr = "Failed to remove nodes from firewall VLAN!";
	    @fwstate = ("Nodes may still be in firewall VLAN",
			$zap ? ("All nodes are powered off",
				"All nodes set to admin mode") : ());
	    goto done;
	}
    }

    #
    # Now we power on the nodes and let them boot into the MFS,
    # where they will run the disk bootblock zapper.
    #
    # If this fails, we power off all the nodes again and get a
    # little edgy in our error messages to emphasize the gravity
    # of the situation.  Someday we could just move the failed
    # nodes into a special firewalled holding experiment, and
    # let the experiment swapout finish, freeing up the nodes that
    # did succeed.
    #
    if ($zap) {
	print STDERR "Booting nodes into admin MFS and zapping bootblocks.\n";
	TBDebugTimeStamp("Booting admin MFS/zapping bootblocks");
	my @failed = ();
	my %myargs = ();
	$myargs{'name'} = "tbswap";
	$myargs{'command'} = "sudo /usr/local/bin/diskzap";
	$myargs{'poweron'} = 1;
	$myargs{'retry'} = 1;
	if (TBAdminMfsRunCmd(\%myargs, \@failed, @nodes)) {
	    foreach my $failed (@failed) {
514
		tberror(SEV_ERROR, 'invalidate_bootblock_failed', $failed);
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
	    }
	    $fwerr = "Failed to invalidate bootblocks on @failed!";
	    @fwstate = ("Firewall is NOT in place",
			"All nodes set to admin mode");
	    system("$POWER off @nodes");
	    if ($?) {
		push(@fwstate, "Some nodes may NOT be powered off");
	    } else {
		push(@fwstate, "All nodes are powered off");
	    }
	    push(@fwstate, "MAKE SURE THESE NODES DO NOT BOOT FROM DISK!");
	}
    }

done:
    #
    # If we had a failure when tearing down the firewall completely,
    # we act as though the panic button had been pressed (set panic
    # bit in DB, disable cnet port, and inform tbops).  This hopefully
    # ensures that everything will get cleaned up correctly.  This may
    # eventually prove to be overkill.
    #
    # If we failed while removing some nodes from behind the firewall
    # we don't get quite so cranky.
    #
    if ($fwerr) {
	my $op;

	if ($doall) {
544 545 546
	    my $fwiface;
	    if ($experiment->FirewallAndIface(\$fwnode, \$fwiface) != 0 ||
		system("$SNMPIT -d ${fwnode}:${fwiface}") != 0) {
547
		push(@fwstate,
548
		     "Firewall cnet interface ${fwnode}:${fwiface} NOT disabled");
549 550
	    } else {
		push(@fwstate,
551
		     "Firewall cnet interface ${fwnode}:${fwiface} disabled");
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	    }
	    $experiment->SetPanicBit(2);
	    $op = "Swapout";
	} else {
	    $op = "Modify";
	}

	my $this_user = User->ThisUser();
	my $user_uid;
	my $user_name;
	my $user_email;
	if (defined($this_user)) {
	    $user_uid   = $this_user->uid();
	    $user_name  = $this_user->name();
	    $user_email = $this_user->email();
	}
	tberror "$fwerr" . "\nINFORMING $TBOPS!";
	if (defined($user_email)) {
	    SENDMAIL("$user_name <$user_email>",
		     "Firewalled experiment $op failed".
		     " for $pid/$eid",
		     "$op of firewalled experiment $pid/$eid".
		     " by $user_uid failed!\n".
		     "Admin intervention required:\n\n$fwerr\n\n".
		     "Current state of @nodes:\n\n".
		     join("\n", @fwstate) . "\n",
		     "$user_name <$user_email>",
		     "Cc: $TBOPS");
	}
	return 1;
    }

    return 0;
}

Leigh Stoller's avatar
Leigh Stoller committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
#
# The guts of "panic", which is also used for non-firewalled experiments.
#
# Flags.
sub PANIC_PANIC()		{ return 1; }
sub PANIC_CLEAR()		{ return 2; }
sub PANIC_ZAP()			{ return 3; }

sub Panic($$$)
{
    my ($experiment, $level, $which) = @_;

    my $pid = $experiment->pid();
    my $eid = $experiment->eid();

    my $firewalled = $experiment->IsFirewalled();
603
    my ($firewall, $iface);
Leigh Stoller's avatar
Leigh Stoller committed
604
    if ($firewalled) {
605
	if ($experiment->FirewallAndIface(\$firewall, \$iface) != 0) {
Leigh Stoller's avatar
Leigh Stoller committed
606 607 608 609 610 611
	    print STDERR "Could not determine firewall port for $experiment\n";
	    return -1;
	}
    }
    my @nodes = ();
    $experiment->LocalNodeListNames(\@nodes, 1);
612
    my @vnodes = $experiment->VirtNodeList(1);
Leigh Stoller's avatar
Leigh Stoller committed
613 614 615
	
    if ($which == PANIC_ZAP()) {
	$level = $experiment->paniced();
616 617 618
	goto nonodes1
	    if (!@nodes);
	
Leigh Stoller's avatar
Leigh Stoller committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	print STDERR "Powering down paniced nodes.\n";
	system("$POWER off @nodes");
	if ($?) {
	    print STDERR "Failed to power off all nodes!\n";
	    print STDERR "Nodes NOT switched to admin MFS\n";
	    goto badzap;
	}

	#
	# Force all nodes into admin mode.
	#
	my %myargs;
	$myargs{'name'}     = "$0";
	$myargs{'on'}       = 1;
	$myargs{'clearall'} = 1;
	if (TBAdminMfsSelect(\%myargs, undef, @nodes)) {
	    print STDERR "Failed to force all nodes into admin mode!\n";
	    goto badzap;
	}
638
      nonodes1:
Leigh Stoller's avatar
Leigh Stoller committed
639 640 641 642 643 644 645 646 647 648 649

	#
	# This code is not used for firewalled experiments, so we only
	# have to worry about the control network.
	#
	if ($level == 2) {
	    system("$SNMPIT -R $pid $eid");
	    if ($?) {
		goto badzap;
	    }
	}
650 651 652
	goto nonodes2
	    if (!@nodes);
	
Leigh Stoller's avatar
Leigh Stoller committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	#
	# Now we power on the nodes and let them boot into the MFS,
	# where they will run the disk bootblock zapper.
	#
	# If this fails, we power off all the nodes again and get a
	# little edgy in our error messages to emphasize the gravity
	# of the situation.  Someday we could just move the failed
	# nodes into a special firewalled holding experiment, and
	# let the experiment swapout finish, freeing up the nodes that
	# did succeed.
	#
	print STDERR "Booting nodes into admin MFS and zapping bootblocks.\n";
	my @failed = ();
	%myargs = ();
	$myargs{'name'}    = "$0";
	$myargs{'command'} = "sudo /usr/local/bin/diskzap";
	$myargs{'poweron'} = 1;
	$myargs{'retry'}   = 1;
	if (TBAdminMfsRunCmd(\%myargs, \@failed, @nodes)) {
	    print STDERR "Failed to invalidate bootblocks on @failed!\n";
	  badzap:
	    print STDERR "Powering nodes off again ...\n";
	    system("$POWER off @nodes");
	    if ($?) {
		print STDERR "Some nodes may NOT be powered off.\n";
	    } else {
		print STDERR "All nodes are powered off.\n";
	    }
	    print STDERR "MAKE SURE THESE NODES DO NOT BOOT FROM DISK!\n";

	    SENDMAIL($TBOPS,
		     "Failed to invalidate boot blocks for $pid/$eid\n",
		     "Failed to invalidate boot blocks while swapping out\n".
		     "paniced experiment $pid/$eid.\n");
	    goto bad;
	}
689
      nonodes2:
Leigh Stoller's avatar
Leigh Stoller committed
690 691 692 693 694
    }
    elsif ($which == PANIC_CLEAR()) {
	$level = $experiment->paniced();
	
	if ($level == 1) {
695 696
	    goto nonodes
		if (!@nodes);
697 698 699 700 701 702 703 704 705

	    #
	    # Do the VMs. We continue if we get errors, want to do
	    # as much as possible.
	    #
	    if (@vnodes) {
		print "Restarting all containers ...\n";
		system("$VNODESETUP -d -f -j $pid $eid");
	    }
706
	    
Leigh Stoller's avatar
Leigh Stoller committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	    #
	    # Turn admin mode back off and reboot back to the old OS
	    #
	    print "Allowing all nodes to reboot out of admin mode ...\n";

	    my %myargs;
	    $myargs{'name'}     = "$0";
	    $myargs{'on'}       = 0;
	    $myargs{'clearall'} = 0;
	    if (TBAdminMfsSelect(\%myargs, undef, @nodes)) {
		print STDERR "Could not turn admin mode off for nodes\n";
		goto bad;
	    }
	    $myargs{'reboot'} = 1;
	    $myargs{'wait'}   = 0;
	    if (TBAdminMfsBoot(\%myargs, undef, @nodes)) {
		print STDERR "Failed to reboot nodes out of admin mode\n";
		goto bad;
	    }
726
	  nonodes:
Leigh Stoller's avatar
Leigh Stoller committed
727 728 729 730
	}
	else {
	    print "Enabling the control network ...\n";
	    if ($firewalled) {
731
		system("$SNMPIT -e ${firewall}:${iface}");
Leigh Stoller's avatar
Leigh Stoller committed
732 733 734 735 736 737 738 739 740 741 742 743 744
	    }
	    else {
		system("$SNMPIT -R $pid $eid");
	    }
	    if ($?) {
		print STDERR "snmpit exited with $?\n";
		goto bad;
	    }
	}
	$experiment->SetPanicBit(0);
    }
    elsif ($which == PANIC_PANIC()) {
	if ($level == 1) {
745 746 747 748 749 750 751 752 753
	    #
	    # Do the VMs. We continue if we get errors, want to do
	    # as much as possible.
	    #
	    if (@vnodes) {
		print "Halting all containers ...\n";
		system("$VNODESETUP -d -f -h $pid $eid");
	    }
	    
Leigh Stoller's avatar
Leigh Stoller committed
754 755 756
	    #
	    # Boot into the admin MFS
	    #
757
	    print "Booting all phys nodes into admin mode and waiting ...\n";
Leigh Stoller's avatar
Leigh Stoller committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	    
	    my %myargs;
	    $myargs{'name'} = "$0";
	    $myargs{'on'}   = 1;
	    if (TBAdminMfsSelect(\%myargs, undef, @nodes)) {
		print STDERR "Failed to force all nodes into admin mode!\n";
		print STDERR "Falling back to control network disable\n";
		$level = 2;
		goto level2;
	    }
	    $myargs{'reboot'} = 1;
	    $myargs{'wait'}   = 1;
	    $myargs{'retry'}  = 1;
	    if (TBAdminMfsBoot(\%myargs, undef, @nodes)) {
		print STDERR "Failed to boot all nodes into admin mode!\n";
		print STDERR "Falling back to control network disable\n";
		$level = 2;
		goto level2;
	    }
	}
	else {
	  level2:
	    print "Disabling the control network ...\n";
	    if ($firewalled) {
782
		system("$SNMPIT -d ${firewall}:${iface}");
Leigh Stoller's avatar
Leigh Stoller committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	    }
	    else {
		system("$SNMPIT -D $pid $eid");
	    }
	    if ($?) {
		print STDERR "snmpit exited with $?\n";
		goto bad;
	    }
	}
	$experiment->SetPanicBit($level);
    }
    return 0;

  bad:
    return -1;
}