rc.bootsetup 15.8 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2004-2014 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 68
# Script specific goo.
my $RCDIR	= "$BINDIR/rc";
my $LOGFILE	= "$LOGDIR/bootsetup.debug";
my $BOOTLOG     = "/var/log/emulab.bootlog";
my %TMCCTIMEOUT	= (REMOTE() ? ("timeout" => 5) : ());

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

76 77 78 79 80 81 82 83 84
#
# 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");
}

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

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#
# 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);
}

116 117
STDOUT->autoflush(1);
STDERR->autoflush(1);
118

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

    LOG->autoflush(1);
    LOG->autoflush(1);
128
    
129 130 131 132
    #
    # 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.
133 134
    #
    my $pid = open(FOO, "-|");
135 136 137
    if (!defined($pid)) {
	print LOG "Boot Failure; popen failed!\n";
	close(LOG);
Leigh Stoller's avatar
Leigh Stoller committed
138
	BootReport($? >> 8, "Boot Failure; popen failed!")
139
    }
140 141
    
    if ($pid) {
142 143
	while (<FOO>) {
	    print LOG $_;
144 145 146
	    if ($action ne "reconfig") {
		print "$_";
	    }
147
	}
148 149 150
	if ($?) {
	    print LOG "Boot Failure: $?\n";
	}
151
	close(LOG);
152
	close(FOO);
153
	if ($?) {
Leigh Stoller's avatar
Leigh Stoller committed
154
	    BootReport($? >> 8, "Boot Failure!");
155 156 157
	}
	exit(0);
    }
158

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

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

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
# 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);

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

206 207 208 209
    #
    # Send the console log to the server.
    #
    if (-e $LOGFILE && -s $LOGFILE &&
210 211
	tmcc(TMCCCMD_BOOTLOG, "", undef,
	     ("datafile" => $LOGFILE, "timeout" => 5)) < 0) {
Leigh Stoller's avatar
Leigh Stoller committed
212
	print "Error sending logfile to Emulab Control!\n";
213
    }
Leigh Stoller's avatar
Leigh Stoller committed
214 215 216 217 218
    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";
	}
219
    
Leigh Stoller's avatar
Leigh Stoller committed
220 221 222
	if (tmcc(TMCCCMD_STATE, "TBFAILED", undef, %TMCCTIMEOUT) < 0) {
	    print "Error sending TBFAILED to Emulab Control!\n";
	}
223
    }
Leigh Stoller's avatar
Leigh Stoller committed
224 225
    exit($code)
	if ($code);
226 227
}

228 229 230 231 232 233 234 235 236
#
# Boot Action.
#
sub doboot()
{
    #
    # Sort of a hodgepodge of stuff!
    #
    # Get the boss info for below.
237 238 239 240 241 242 243 244 245 246 247 248
    #
    # 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);
    }
249 250 251 252
    if (!defined($bossname)) {
	fatal("Could not determine the name of the boss server!");
    }

253 254 255 256 257 258 259 260 261 262 263 264 265
    #
    # 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");
	}
    }

266 267 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
    #
    # 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);
	}
    }

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

305 306 307 308 309 310 311 312 313 314
    #
    # 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");
	}
    }

315
    if (-x "$BINDIR/tbshutdown" && ! WINDOWS()) {
316 317 318 319 320 321 322 323
        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");
324
    runhooks(\%manifest,'pre','rc.tbsetup','boot');
325 326
    if (tmcc(TMCCCMD_STATE, "TBSETUP") < 0) {
	fatal("Error sending TBSETUP to Emulab Control!");
327 328
    };
    runhooks(\%manifest,'post','rc.tbsetup','boot');
329 330 331 332 333 334 335 336

    # 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.
    #
337 338
    if (-x "$RCDIR/rc.ipod" && ! WINDOWS()) {
	print("Setting up Ping of Death\n");
339 340 341 342 343 344
	# 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');
345 346
    }

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

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

370 371 372 373 374 375 376 377 378 379 380
    #
    # 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");
        }
    }

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    #
    # 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;
    }

    #
399
    # Start the event proxy.
400
    #
401 402
    # For SHAREDHOSTs we now use clusterd (no pubsubd or evproxy).
    #
403
    if (SHAREDHOST() || STORAGEHOST()) {
404 405 406 407
	my $eventserver = "event-server";
	if (REMOTE()) {
	    $eventserver = TB_EVENTSERVER();
	}
408 409 410 411
	#
	# Kill off pubsubd and start clusterd. 
	#
	system("killall pubsubd");
412 413 414 415 416 417 418 419
	if (-x "/usr/local/libexec/clusterd") {
	    print("Starting pubsub cluster daemon\n");
	    system("/usr/local/libexec/clusterd -s $eventserver");
	    if ($?) {
		fatal("Error running clusterd");
	    }
	} else {
	    print STDERR "WARNING: clusterd not installed, not started\n";
420 421 422 423
	}
    }
    elsif (!(REMOTE() && !REMOTEDED()) && !GENVNODE()) {  
	my $evproxy = "$BINDIR/evproxy";
424 425 426 427 428 429

	if (-x $evproxy) {
	    my $eventserver = "event-server";
	    if (REMOTE()) {
		$eventserver = TB_EVENTSERVER();
	    }
430
	    print("Starting event proxy daemon\n");
431
	    system("$evproxy -s $eventserver -e $pid/$eid");
432 433 434 435 436 437
	    if ($?) {
		fatal("Error running $evproxy");
	    }
	}
	else {
	    print("$evproxy does not exist!\n");
438 439 440 441 442 443 444 445 446 447 448 449 450
	}
    }

    #
    # 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");
    }

451
    if (-x "$RCDIR/rc.canaryd" && -x "$BINDIR/canaryd" && !REMOTE()) {
452
	print("Starting canaryd usage tracer\n");
453
	runhooks(\%manifest,'pre','rc.canaryd','boot');
454 455 456 457
	system("$RCDIR/rc.canaryd start");
	if ($?) {
	    fatal("Error running $RCDIR/rc.canaryd");
	}
458
	runhooks(\%manifest,'post','rc.canaryd','boot');
459 460
    }

461
    #
462 463
    # Start the linktest daemon. This script will exit without running
    # linktest is the node is hosting SIM or JAILS.
464
    #
465
    if (-x "$RCDIR/rc.linktest" && !REMOTE()) {
466
	runhooks(\%manifest,'pre','rc.linktest','boot');
467 468
	system("$RCDIR/rc.linktest start");
	if ($?) {
Leigh Stoller's avatar
Leigh Stoller committed
469 470 471 472 473
	    # Not a fatal error; linktest will just hang.
	    ;
	}
	else {
	    runhooks(\%manifest,'post','rc.linktest','boot');
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	}
    }

    #
    # 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");
493
    runhooks(\%manifest,'pre','rc.isup','boot');
494 495 496
    if (tmcc(TMCCCMD_STATE, "ISUP") < 0) {
	fatal("Error sending ISUP to Emulab Control!");
    }
497
    runhooks(\%manifest,'post','rc.isup','boot');
498

499 500 501 502 503 504 505
    #
    # 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") {
506
	runbootscript(\%manifest,$RCDIR,'rc.startcmd','boot');
507 508
    }

509 510 511
    #
    # Boot vnodes. Doing it after ISUP is better; os_setup will
    # consider the node ready, and move onto waiting for vnodes to
512
    # boot. Ditto for subnodes below which should be merged with vnodes.
513 514 515
    #
    if (-x "$BINDIR/bootvnodes") {
	print("Booting up vnodes\n");
516
	runhooks(\%manifest,'pre','rc.vnodes','boot');
517 518 519 520
	# Foreground mode.
	system("$BINDIR/bootvnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootvnodes");
521
	}
522
	runhooks(\%manifest,'post','rc.vnodes','boot');
523 524 525
    }
    if (-x "$BINDIR/bootsubnodes") {
	print("Booting up subnodes\n");
526
	runhooks(\%manifest,'pre','rc.subnodes','boot');
527 528 529 530 531
	# Foreground mode.
	system("$BINDIR/bootsubnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootsubnodes");
	}
532
	runhooks(\%manifest,'post','rc.subnodes','boot');
533
    }
Leigh Stoller's avatar
Leigh Stoller committed
534 535
    # Always send in report. Very handy.
    BootReport(0);
536

537 538 539 540 541 542 543 544
    # 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!");
    }
545 546
    if (-x "$BINDIR/bootsubnodes") {
	print("Booting up subnodes\n");
547
	runhooks(\%manifest,'pre','rc.subnodes','boot');
548 549 550 551 552
	# Foreground mode.
	system("$BINDIR/bootsubnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootsubnodes");
	}
553
	runhooks(\%manifest,'post','rc.subnodes','boot');
554
    }
555 556 557 558 559 560 561 562 563 564 565 566
}

#
# Shutdown Action.
#
sub doshutdown()
{
    #
    # Halt vnodes. The filesystems are left intact!
    #
    if (-x "$BINDIR/bootvnodes") {
	print("Halting vnodes\n");
567
	runhooks(\%manifest,'pre','rc.vnodes','shutdown');
568 569 570 571 572
	# Foreground mode.
	system("$BINDIR/bootvnodes -f -h");
	if ($?) {
	    fatal("Error running $BINDIR/bootvnodes");
	}
573
	runhooks(\%manifest,'post','rc.vnodes','shutdown');
574 575 576
    }

    print("Informing Emulab Control that we are are rebooting\n");
577
    runhooks(\%manifest,'pre','rc.shutdown','shutdown');
578
    if (tmcc(TMCCCMD_STATE, "SHUTDOWN", undef, %TMCCTIMEOUT) < 0) {
579 580
	fatal("Error sending SHUTDOWN to Emulab Control!");
    }
581
    runhooks(\%manifest,'post','rc.shutdown','shutdown');
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
}

#
# 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");
    }

    #
616
    # Send our ISUP.
617 618
    #
    print("Informing Emulab Control that we are back up and running\n");
619
    runhooks(\%manifest,'pre','rc.isup','reconfig');
620 621 622
    if (tmcc(TMCCCMD_STATE, "ISUP") < 0) {
	fatal("Error sending ISUP to Emulab Control!");
    }
623
    runhooks(\%manifest,'post','rc.isup','reconfig');
624 625 626 627 628 629 630 631 632 633

    return 0;
}

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