rc.bootsetup 16.9 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2004-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
#
use English;
use Getopt::Std;
26
use IO::Handle;
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

#
# This script is run directly from boot. It should NOT be run after
# that since some stuff is not setup to properly restart yet. For
# general reconfig or update, use rc.config instead.
#
sub usage()
{
    print "Usage: " . scriptname() . "[-b] boot|shutdown|reconfig|reset\n";
    exit(1);
}
my $optlist    = "b";
my $background = 0;
my $action     = "boot";

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

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

# Only root.
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

#
# Load the OS independent support library. It will load the OS dependent
56 57
# library and initialize itself.
#
58 59 60
use libsetup;
use libtmcc;
use librc;
61
use libtestbed ('TB_EVENTSERVER', 'TB_BOSSNODE', 'TBBackGround');
62

63 64 65 66 67
# Script specific goo.
my $RCDIR	= "$BINDIR/rc";
my $LOGFILE	= "$LOGDIR/bootsetup.debug";
my $BOOTLOG     = "/var/log/emulab.bootlog";
my %TMCCTIMEOUT	= (REMOTE() ? ("timeout" => 5) : ());
68 69
my $PUBSUBCONF  = "/usr/local/etc/pubsubd.conf";
my $PUBSUBEXPR  = "/usr/local/etc/pubsubd.expr";
70

71 72 73 74 75
# Protos.
sub doboot();
sub doshutdown();
sub doreconfig();
sub docleanup();
Mike Hibler's avatar
Mike Hibler committed
76
sub BootReport($;$);
77

78 79 80 81 82 83 84 85 86
#
# If this is the MFS, hand off to that script.
#
if (MFS()) {
    exec("$RCDIR/rc.mfs", @ARGV);
    die("*** $0:\n".
	"    Could not exec $RCDIR/rc.mfs!\n");
}

87 88 89 90 91 92 93 94 95 96 97 98
# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'b'})) {
    $background = 1;
}
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#
# Booting up. If this is a widearea node, we do not want to hang
# on boss if its down, but instead let the machine boot and do this
# stuff later.
#
if (REMOTE() && $action eq "boot") {
    require POSIX;
    import POSIX;

    if (TBBackGround($BOOTLOG)) {
	sleep(1);
	exit(0);
    }
    # Fully disconnect from bootup. 
    POSIX::setsid();
    # Let the boot continue on for a bit.
    sleep(5);
}

118 119
STDOUT->autoflush(1);
STDERR->autoflush(1);
120

121 122 123
#
# We want to save all of the output off, but also dup it to the console.
#
124 125
if ($action eq "boot" || $action eq "reconfig") {
    open(LOG, "> $LOGFILE") or
Leigh B Stoller's avatar
Leigh B Stoller committed
126
	BootReport(-1, "Could not open $LOGFILE!");
127 128 129

    LOG->autoflush(1);
    LOG->autoflush(1);
130
    
131 132 133 134
    #
    # This open implicitly forks a child, which goes on to execute the rest
    # of the script. The parent is going to sit in this loop and capture the
    # output of the child, writing it to the logfile and to the console.
135 136
    #
    my $pid = open(FOO, "-|");
137 138 139
    if (!defined($pid)) {
	print LOG "Boot Failure; popen failed!\n";
	close(LOG);
Leigh B Stoller's avatar
Leigh B Stoller committed
140
	BootReport($? >> 8, "Boot Failure; popen failed!")
141
    }
142 143
    
    if ($pid) {
144 145
	while (<FOO>) {
	    print LOG $_;
146 147 148
	    if ($action ne "reconfig") {
		print "$_";
	    }
149
	}
150 151 152
	if ($?) {
	    print LOG "Boot Failure: $?\n";
	}
153
	close(LOG);
154
	close(FOO);
155
	if ($?) {
Leigh B Stoller's avatar
Leigh B Stoller committed
156
	    BootReport($? >> 8, "Boot Failure!");
157 158 159
	}
	exit(0);
    }
160

161 162 163 164
    if ($action eq "reconfig") {
	# Need this or ssh will hang, even though it was called with -n option.
	open(STDIN, "</dev/null");
    }
165
    open(STDERR, ">&STDOUT");
166 167
}

Leigh B Stoller's avatar
Leigh B Stoller committed
168 169 170 171 172
#
# Grab our manifest so we can enable/disable/replace scripts and 
# run (or not) its hooks.
#
my %manifest;
173
getrcmanifest(\%manifest);
Leigh B Stoller's avatar
Leigh B Stoller committed
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
# Execute the action.
SWITCH: for ($action) {
    /^boot$/i && do {
	doboot();
	last SWITCH;
    };
    /^shutdown$/i && do {
	doshutdown();
	last SWITCH;
    };
    /^reconfig$/i && do {
	doreconfig();
	last SWITCH;
    };
    /^reset$/i && do {
	docleanup();
	last SWITCH;
    };
    fatal("Invalid action: $action\n");
}
exit(0);

197 198 199 200
#
# This version of fatal sends the console log to tmcd, and then generates
# a TBFAILED state transition. 
#
Leigh B Stoller's avatar
Leigh B Stoller committed
201
sub BootReport($;$)
202 203 204
{
    my ($code, $msg) = @_;
    
Leigh B Stoller's avatar
Leigh B Stoller committed
205 206
    print "$msg\n"
	if (defined($msg));
Mike Hibler's avatar
Mike Hibler committed
207

208 209 210 211
    #
    # Send the console log to the server.
    #
    if (-e $LOGFILE && -s $LOGFILE &&
212 213
	tmcc(TMCCCMD_BOOTLOG, "", undef,
	     ("datafile" => $LOGFILE, "timeout" => 5)) < 0) {
Leigh B Stoller's avatar
Leigh B Stoller committed
214
	print "Error sending logfile to Emulab Control!\n";
215
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
216 217 218 219 220
    if ($code) {
	# XXX "--" ensures that code < 0 not interpreted as a tmcc cmdline arg
	if (tmcc(TMCCCMD_BOOTERRNO, "-- $code", undef, %TMCCTIMEOUT) < 0) {
	    print "Error sending boot errno to Emulab Control!\n";
	}
221
    
Leigh B Stoller's avatar
Leigh B Stoller committed
222 223 224
	if (tmcc(TMCCCMD_STATE, "TBFAILED", undef, %TMCCTIMEOUT) < 0) {
	    print "Error sending TBFAILED to Emulab Control!\n";
	}
225
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
226 227
    exit($code)
	if ($code);
228 229
}

230 231 232 233 234 235 236 237 238
#
# Boot Action.
#
sub doboot()
{
    #
    # Sort of a hodgepodge of stuff!
    #
    # Get the boss info for below.
239 240 241 242 243 244 245 246 247 248 249 250
    #
    # So, this appears to fail. Not sure why, but a retry in a
    # a few seconds appears to work. So before we give up, lets
    # retry a few times.
    #
    my ($bossname, $bossip);
    for (my $i = 0; $i < 15; $i++) {
	($bossname, $bossip) = tmccbossinfo();
	last
	    if (defined($bossname));
	sleep(2);
    }
251 252 253 254
    if (!defined($bossname)) {
	fatal("Could not determine the name of the boss server!");
    }

255 256 257 258 259 260 261 262 263 264 265 266 267
    #
    # On the REMOTEDED nodes, we have to look for the ssl version
    # of tmcc in case the image was built with nonssl version.
    #
    if (REMOTEDED()) {
	if (-e "$BINDIR/tmcc-ssl.bin") {
	    if (-e "$BINDIR/tmcc.bin") {
		system("/bin/mv -f $BINDIR/tmcc.bin $BINDIR/tmcc-nossl.bin");
	    }
	    system("/bin/mv -f $BINDIR/tmcc-ssl.bin $BINDIR/tmcc.bin");
	}
    }

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    #
    # We need a hostname that resolves to something. If we get here and
    # there is no hostname, set it.
    #
    if (REMOTE()) {
	my $curname = `hostname -s`;
	if (! ($curname =~ /.+/)) {
	    my ($domain) = ($bossname =~ /^[^\.]+\.(.*)$/);
	    my @tmccresults;
	    
	    if (tmcc(TMCCCMD_NODEID, undef, \@tmccresults) < 0) {
		fatal("Could not get nodeid from tmccd!");
	    }
	    my $nodeid = $tmccresults[0];
	    chomp($nodeid);
	
	    print STDERR
		"Hostname not set. Setting it to ${nodeid}.${domain}\n";
	    
	    if (system("hostname", "${nodeid}.${domain}")) {
		fatal("sethostname failed!");
	    }
	    # Delay a moment to let things settle down.
	    sleep(5);
	}
    }

295 296 297 298 299
    #
    # Check for an os dependent initialization script.
    #
    my $os_script = "rc." . lc($OSNAME);
    if (-x "$RCDIR/$os_script") {
300
        print("Running os dependent initialization script $os_script\n");    
301 302 303 304 305 306
	system("$RCDIR/$os_script");
	if ($?) {
	    fatal("Error running $RCDIR/$os_script");
	}
    }

307 308 309 310 311 312 313 314 315 316
    #
    # Check for kernel rename first. Probably not required so early.
    #
    if (-x "$RCDIR/rc.kname") {
	system("$RCDIR/rc.kname");
	if ($?) {
	    fatal("Error running $RCDIR/rc.kname");
	}
    }

317
    if (-x "$BINDIR/tbshutdown" && ! WINDOWS()) {
318 319 320 321 322 323 324 325
        print("Starting up shutdown notification daemon\n");
        system("$BINDIR/tbshutdown");
	if ($?) {
	    fatal("Error running $BINDIR/tbshutdown");
	}
    }

    print("Informing Emulab Control that we have rebooted\n");
326
    runhooks(\%manifest,'pre','rc.tbsetup','boot');
327 328
    if (tmcc(TMCCCMD_STATE, "TBSETUP") < 0) {
	fatal("Error sending TBSETUP to Emulab Control!");
329 330
    };
    runhooks(\%manifest,'post','rc.tbsetup','boot');
331 332 333 334 335 336 337 338

    # Now we get into the real work.
    print("Checking Testbed reservation status\n");
    my ($pid, $eid, $vname) = bootsetup();

    #
    # This stuff is run regardless of reservation status.
    #
339
    if (-x "$RCDIR/rc.ipod" && ! WINDOWS() && ! INDOCKERVM()) {
340
	print("Setting up Ping of Death\n");
341 342 343 344 345 346
	# This is allowed to fail by default; ipod might not be supported.
	if (!exists($manifest{'rc.ipod'})
	    || !exists($manifest{'rc.ipod'}{'FATAL'})) {
	    $manifest{'rc.ipod'}{'FATAL'} = 0;
	}
	runbootscript(\%manifest,$RCDIR,'rc.ipod','boot');
347 348
    }

349
    if (-x "$RCDIR/rc.healthd" && ! (WINDOWS() || REMOTE())) {
350 351
	print("Starting node health monitoring daemon\n");
	# If it fails, that is okay. Keep going.
352 353 354 355 356
	if (!exists($manifest{'rc.healthd'})
	    || !exists($manifest{'rc.healthd'}{'FATAL'})) {
	    $manifest{'rc.healthd'}{'FATAL'} = 0;
	}
	runbootscript(\%manifest,$RCDIR,'rc.healthd','boot','start');
357 358
    }
    
359
    if (-x "$RCDIR/rc.slothd" && (SHAREDHOST() || !REMOTE())) {
360
	print("Starting slothd usage detector\n");
361
	runbootscript(\%manifest,$RCDIR,'rc.slothd','boot','start');
362 363
    }

364
    if (-x "$BINDIR/watchdog") {
365 366 367 368 369 370 371
        print("Starting testbed update watchdog\n");
	system("$BINDIR/watchdog start");
	if ($?) {
	    fatal("Error running $BINDIR/watchdog");
	}
    }

372 373 374 375 376 377 378 379 380 381 382
    #
    # Run the stargate startup if necessary
    # 
    if (-x "$RCDIR/rc.stargate" && STARGATE()) {
        print("Firing off garcia/stargate processes\n");
        system("$RCDIR/rc.stargate start");
        if ($?) {
            fatal("Error running $RCDIR/rc.stargate");
        }
    }

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    #
    # At this point, if we are a free node just run the config scripts
    # to clean things up, although there should not be anything to clean
    # up. We do it anyway just to be safe.
    #
    if (!defined($pid)) {
	if (-x "$RCDIR/rc.config") {
	    print("Resetting node to a clean state\n");
	    system("$RCDIR/rc.config reset");
	    if ($?) {
		fatal("Error running $RCDIR/rc.config");
	    }
	}
	# And then we are done. 
	goto isfree;
    }

    #
401 402 403
    # Setup pubsub for clustering and restart. In general, all agents
    # on a node talk to the local pubsubd so that the number of subscriptions
    # on ops is limited to the number of physical nodes.
404
    #
405 406 407 408 409 410 411
    my $clusteringoptions;
    
    if (SHAREDHOST() || STORAGEHOST() || SUBBOSS()) {
	#
	# We run pubsubd in clustering mode, asking for all notifications
	# since we do not know what experiments are resident on this node.
	#
412 413 414 415
	my $eventserver = "event-server";
	if (REMOTE()) {
	    $eventserver = TB_EVENTSERVER();
	}
416 417 418 419 420 421
	$clusteringoptions = "-c $eventserver -F $PUBSUBEXPR";

	open(CONF, "> $PUBSUBEXPR") or
	    fatal("Error opening $PUBSUBEXPR for writing");
	print CONF "(SCHEDULER == 0)\n";
	close(CONF);
422 423
    }
    elsif (!(REMOTE() && !REMOTEDED()) && !GENVNODE()) {  
424
	#
425
	# We run pubsubd in clustering mode, asking for just notifications
426 427 428
	# for the current experiment. Note that we explicitly exclude those
	# intended for the event scheduler running on ops. In theory, they
	# should not cause problems, but they are confusing at best.
429
	#
430 431 432
	my $eventserver = "event-server";
	if (REMOTE()) {
	    $eventserver = TB_EVENTSERVER();
433
	}
434 435 436 437
	$clusteringoptions = "-c $eventserver -F $PUBSUBEXPR";

	open(CONF, "> $PUBSUBEXPR") or
	    fatal("Error opening $PUBSUBEXPR for writing");
438
	print CONF "(EXPT == \"$pid/$eid\") && SCHEDULER == 0\n";
439
	close(CONF);
440
    }
441 442 443 444 445 446 447 448 449
    if (defined($clusteringoptions)) {
	#
	# We do this so that an invocation of the system startup file will
	# see the right clustering options.
	#
	open(CONF, "> $PUBSUBCONF") or
	    fatal("Error opening $PUBSUBCONF for writing");
	print CONF "pubsubd_clustering_flags=\"$clusteringoptions\"\n";
	close(CONF);
450

451 452 453 454 455 456 457
	#
	# Kill off pubsubd and restart in cluster mode.
	#
	if (-x "/usr/local/libexec/pubsubd") {
	    system("killall pubsubd");
	    print("Starting pubsubd in cluster mode\n");
	    system("/usr/local/libexec/pubsubd $clusteringoptions");
458
	    if ($?) {
459
		fatal("Error running pubsubd");
460
	    }
461 462
	} else {
	    print STDERR "WARNING: pubsubd not installed, not started\n";
463 464 465 466 467 468 469 470 471 472 473 474 475
	}
    }

    #
    # This is where we run all of the config scripts. These talk to the
    # DB and setup the node the way it is supposed to be. 
    # 
    print("Running config scripts\n");
    system("$RCDIR/rc.config boot");
    if ($?) {
	fatal("Error running $RCDIR/rc.config");
    }

476
    if (-x "$RCDIR/rc.canaryd" && -x "$BINDIR/canaryd" && !REMOTE()) {
Timothy Stack's avatar
 
Timothy Stack committed
477
	print("Starting canaryd usage tracer\n");
478
	runhooks(\%manifest,'pre','rc.canaryd','boot');
Timothy Stack's avatar
 
Timothy Stack committed
479 480 481 482
	system("$RCDIR/rc.canaryd start");
	if ($?) {
	    fatal("Error running $RCDIR/rc.canaryd");
	}
483
	runhooks(\%manifest,'post','rc.canaryd','boot');
Timothy Stack's avatar
 
Timothy Stack committed
484 485
    }

486
    #
487 488
    # Start the linktest daemon. This script will exit without running
    # linktest is the node is hosting SIM or JAILS.
489
    #
490
    if (-x "$RCDIR/rc.linktest" && !REMOTE()) {
491
	runhooks(\%manifest,'pre','rc.linktest','boot');
492 493
	system("$RCDIR/rc.linktest start");
	if ($?) {
Leigh B Stoller's avatar
Leigh B Stoller committed
494 495 496 497 498
	    # Not a fatal error; linktest will just hang.
	    ;
	}
	else {
	    runhooks(\%manifest,'post','rc.linktest','boot');
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	}
    }

    #
    # The simulator might run here, and that is special. It will send its
    # own ISUP. Sounds icky. 
    #
    if (-x TMSIMRC) {
	print("Starting Network Simulation; ISUP delayed until later\n");
	system(TMSIMRC);
	if ($?) {
	    fatal("Error running " . TMSIMRC);
	}
	# We are done. 
	return;
    }

    # Now send ISUP
    print("Informing Emulab Control that we are up and running\n");
518
    runhooks(\%manifest,'pre','rc.isup','boot');
519 520 521
    if (tmcc(TMCCCMD_STATE, "ISUP") < 0) {
	fatal("Error sending ISUP to Emulab Control!");
    }
522
    runhooks(\%manifest,'post','rc.isup','boot');
523

524 525 526 527 528 529 530
    #
    # After everything is setup, run any startup command.
    #
    # Note that this mechanism is only used for the admin MFS now,
    # regular user startup commands are run via the program agent.
    #
    if (-x "$RCDIR/rc.startcmd") {
531
	runbootscript(\%manifest,$RCDIR,'rc.startcmd','boot');
532 533
    }

534 535 536
    #
    # Boot vnodes. Doing it after ISUP is better; os_setup will
    # consider the node ready, and move onto waiting for vnodes to
537
    # boot. Ditto for subnodes below which should be merged with vnodes.
538 539 540
    #
    if (-x "$BINDIR/bootvnodes") {
	print("Booting up vnodes\n");
541
	runhooks(\%manifest,'pre','rc.vnodes','boot');
542 543 544 545
	# Foreground mode.
	system("$BINDIR/bootvnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootvnodes");
546
	}
547
	runhooks(\%manifest,'post','rc.vnodes','boot');
548 549 550
    }
    if (-x "$BINDIR/bootsubnodes") {
	print("Booting up subnodes\n");
551
	runhooks(\%manifest,'pre','rc.subnodes','boot');
552 553 554 555 556
	# Foreground mode.
	system("$BINDIR/bootsubnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootsubnodes");
	}
557
	runhooks(\%manifest,'post','rc.subnodes','boot');
558
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
559 560
    # Always send in report. Very handy.
    BootReport(0);
561

562 563 564 565 566 567 568 569
    # we are done!
    return;

  isfree:
    print("Informing Emulab Control that we are up and running\n");
    if (tmcc(TMCCCMD_STATE, "ISUP") < 0) {
	fatal("Error sending ISUP to Emulab Control!");
    }
Timothy Stack's avatar
 
Timothy Stack committed
570 571
    if (-x "$BINDIR/bootsubnodes") {
	print("Booting up subnodes\n");
572
	runhooks(\%manifest,'pre','rc.subnodes','boot');
Timothy Stack's avatar
 
Timothy Stack committed
573 574 575 576 577
	# Foreground mode.
	system("$BINDIR/bootsubnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootsubnodes");
	}
578
	runhooks(\%manifest,'post','rc.subnodes','boot');
Timothy Stack's avatar
 
Timothy Stack committed
579
    }
580 581 582 583 584 585 586 587 588 589 590 591
}

#
# Shutdown Action.
#
sub doshutdown()
{
    #
    # Halt vnodes. The filesystems are left intact!
    #
    if (-x "$BINDIR/bootvnodes") {
	print("Halting vnodes\n");
592
	runhooks(\%manifest,'pre','rc.vnodes','shutdown');
593 594 595 596 597
	# Foreground mode.
	system("$BINDIR/bootvnodes -f -h");
	if ($?) {
	    fatal("Error running $BINDIR/bootvnodes");
	}
598
	runhooks(\%manifest,'post','rc.vnodes','shutdown');
599 600 601
    }

    print("Informing Emulab Control that we are are rebooting\n");
602
    runhooks(\%manifest,'pre','rc.shutdown','shutdown');
603
    if (tmcc(TMCCCMD_STATE, "SHUTDOWN", undef, %TMCCTIMEOUT) < 0) {
604 605
	fatal("Error sending SHUTDOWN to Emulab Control!");
    }
606
    runhooks(\%manifest,'post','rc.shutdown','shutdown');
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
}

#
# Node Reconfig Action (without rebooting).
#
sub doreconfig()
{
    print("Informing Emulab Control that we are doing a reconfig\n");
    if (tmcc(TMCCCMD_STATE, "TBSETUP") < 0) {
	fatal("Error sending TBSETUP to Emulab Control!");
    }

    #
    # Now tell the node to reconfig, but first have to make sure we have the
    # right hostname since that can change to. Silly, stupid, dumb. Will
    # probably break anyway since DHCP has to run again as well.
    # 
    if (-x "$BINDIR/sethostname") {
	print("Setting hostname\n");
	# Foreground mode.
	system("$BINDIR/sethostname");
	if ($?) {
	    fatal("Error running $BINDIR/sethostname");
	}

    }

    print("Running reconfig scripts\n");
    system("$RCDIR/rc.config reconfig");
    if ($?) {
	fatal("Error running $RCDIR/rc.config");
    }

    #
641
    # Send our ISUP.
642 643
    #
    print("Informing Emulab Control that we are back up and running\n");
644
    runhooks(\%manifest,'pre','rc.isup','reconfig');
645 646 647
    if (tmcc(TMCCCMD_STATE, "ISUP") < 0) {
	fatal("Error sending ISUP to Emulab Control!");
    }
648
    runhooks(\%manifest,'post','rc.isup','reconfig');
649 650 651 652 653 654 655 656 657 658

    return 0;
}

#
# Node cleanup action (node is reset to completely clean state).
#
sub docleanup()
{
}