rc.invserver 3.9 KB
Newer Older
1 2 3 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 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 92 93 94 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
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008 University of Utah and the Flux Group.
# All rights reserved.
#
use strict;
use English;
use Getopt::Std;

sub usage()
{
    print "Usage: [-d] start|stop\n";
    exit(1);
}
my $optlist    = "d";
my $debug      = 0;
my $action     = "start";

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

#
# Load the emulab libraries. It will now
#
use libsetup;
use libtmcc;
use librc;

# Protos.
sub BootFatal($);
sub DoBoot();

# Script specific goo.
my $RCDIR	 = "$BINDIR/rc";

# Parse command line.
my %options;
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'d'})) {
    $debug = 1;
}
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}
usage()
    if ($action ne "stop" && $action ne "start");

#
# On shutdown kill everything.
#
if ($action eq "stop") {
    print "Killing all processes and exiting ...\n";
    $SIG{TERM} = 'IGNORE';
    system("kill -TERM -1");
    sleep(1);
    system("kill -KILL -1");
    exit(0);
}
# So that rc will know to invoke us to stop.
system("touch /var/lock/subsys/invserver") == 0
    or die("Could not create lock file!\n");

if (! -e "$BOOTDIR/realname") {
    print "$BOOTDIR/realname does not exit!\n";
    exit(1);
}
my $vnodeid = `cat $BOOTDIR/realname`;
chomp($vnodeid);

#
# Put this into the environment, which is the protocol for setting up jails.
#
$ENV{'TMCCVNODEID'} = $vnodeid;

DoBoot();
exit(0);

#
# Boot Action.
#
sub DoBoot()
{
    TBDebugTimeStamp("rc.invserver starting up");
    
    print("Checking Testbed reservation status\n");
    my ($pid, $eid, $vname) = jailsetup();

    #
    # At this point, if we are a free node just return. Something went wacky.
    #
    if (!defined($pid)) {
	return;
    }

    #
    # Do this *before* config scripts. At the moment we use a hacky test
    # in vnodesetup to see if the watchdog is running, which tells vnodesetup
    # that the vnode has finished setting up. By doing it now, we allow vnode
    # setup to proceed in parallel (okay, at most two are going to partially
    # overlap, but that is better then nothing at all!
    # 
    TBDebugTimeStamp("rc.injail starting watchdog");
    if (-x "$BINDIR/watchdog") {
        print("Starting testbed watchdog daemon\n");
	system("$BINDIR/watchdog start");
	if ($?) {
	    BootFatal("Error running $BINDIR/watchdog");
	}
    }

    #
    # 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. 
    # 
    TBDebugTimeStamp("rc.injail running config scripts");
    print("Running config scripts\n");
    system("$RCDIR/rc.config boot");
    if ($?) {
	BootFatal("Error running $RCDIR/rc.config");
    }
    TBDebugTimeStamp("rc.injail done running config scripts");

    # Linktest daemon now runs inside jails.
    if (-x "$RCDIR/rc.linktest") {
	print("Starting linktest daemon\n");
	system("$RCDIR/rc.linktest start");
	if ($?) {
	    BootFatal("Error running $RCDIR/rc.linktest");
	}
    }

    print("Informing the testbed that we are up and running\n");
    if (tmcc(TMCCCMD_STATE(), "ISUP") < 0) {
	BootFatal("Error sending ISUP to Emulab Control!");
    }

    #
    # Very hacky ... with vservers it is difficult to return an exit
    # code to the outer environment. So, touch this file and the outer
    # environment knows we got this far okay. 
    #
    system("touch $BOOTDIR/vrunning");
}

#
# This version of fatal sends boot status to tmcd, and then generates
# a TBFAILED state transition. 
#
sub BootFatal($)
{
    my ($msg) = @_;
    
160 161
    # XXX "--" ensures that -1 is not interpreted as a tmcc cmdline arg
    if (tmcc(TMCCCMD_BOOTERRNO(), "-- -1") < 0) {
162 163 164 165 166 167 168 169 170
	print "Error sending boot errno to Emulab Control!\n";
    }
    
    if (tmcc(TMCCCMD_STATE(), "TBFAILED") < 0) {
	print "Error sending TBFAILED to Emulab Control!\n";
    }
    exit(-1);
}