rc.fakejail 5.08 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
Mike Hibler's avatar
Mike Hibler committed
4
# Copyright (c) 2007, 2008 University of Utah and the Flux Group.
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
# All rights reserved.
#
use English;
use Getopt::Std;

#
# This script is the jail equiv of ../common/rc.bootsetup. It runs
# inside the jail and does a limited set of bootstrapping tasks.
#
sub usage()
{
    print "Usage: " . scriptname() . " -j vnodeid\n";
    exit(1);
}
my $optlist = "j:";
my $vnodeid;

# 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
# library and initialize itself.
#
use libsetup;
use libtmcc;
use librc;

# Protos.
sub doboot();
sub doshutdown();
sub BootFatal($);

# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'j'})) {
    $vnodeid = $options{'j'};
    libsetup_setvnodeid($vnodeid);
}
else {
    usage();
}

# Script specific goo. Needs to go after we set the vnode above.
my $RCDIR	= "$BINDIR/rc";
my $EVPIDFILE	= CONFDIR() . "/evproxy.pid";
my $done	= 0;
# The caller (vnodesetup) is writing this file. We want to send it back.
my $BOOTLOG	= "$LOGDIR/tbvnode-${vnodeid}.log";

sub handler ($) {
    my ($signame) = @_;
    
    $SIG{INT}  = 'IGNORE';
    $SIG{TERM} = 'IGNORE';
    $SIG{USR1} = 'IGNORE';
    $SIG{HUP}  = 'IGNORE';
    $done = 1;
}
$SIG{INT}  = \&handler;
$SIG{HUP}  = \&handler;
$SIG{USR1} = \&handler;
# Do not want to be shutdown except by our parent, when machine reboots.
$SIG{TERM} = 'IGNORE';

# Do the "boot"
doboot();

# And just spin waiting to be killed off
print("Waiting to be killed off\n");
while (!$done) {
    sleep(1);
}

# Kill it all off.
doshutdown();

exit(0);

#
# This version of fatal sends boot status to tmcd, and then generates
# a TBFAILED state transition. 
#
sub BootFatal($)
{
    my ($msg) = @_;

Mike Hibler's avatar
Mike Hibler committed
100 101
    print "$msg, shutting down\n";

102 103 104 105 106 107 108 109 110 111
    doshutdown();

    #
    # Send the console log to the server.
    #
    if (-e $BOOTLOG && -s $BOOTLOG) {
	tmcc(TMCCCMD_BOOTLOG, "", undef,
	     ("datafile" => $BOOTLOG, "timeout" => 5));
    }

112
    # XXX "--" ensures that -1 is not interpreted as a tmcc cmdline arg
Mike Hibler's avatar
Mike Hibler committed
113
    if (tmcc(TMCCCMD_BOOTERRNO, "-- -1") < 0) {
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	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);
}

#
# Boot Action.
#
sub doboot()
{
    TBDebugTimeStamp("rc.nojail starting up");
    
    my ($pid, $eid, $vname) = fakejailsetup();

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

139 140 141
    # Get the boss info for below.
    my ($bossname, $bossip) = tmccbossinfo();
    if (!defined($bossname)) {
Mike Hibler's avatar
Mike Hibler committed
142
	BootFatal("Could not determine the name of the boss server");
143 144
    }
    my ($domain) = ($bossname =~ /^[^\.]+\.(.*)$/);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    
    print("Checking Testbed jail configuration\n");
    my @tmccresults = ();
    if (tmcc(TMCCCMD_JAILCONFIG, undef, \@tmccresults) < 0) {
	fatal("Could not get jailconfig from server!");
    }
    return
	if (! @tmccresults);
    
    my $eventserver = "event-server.${domain}";

    # parse the config
    CONFPARSE: for (@tmccresults) {
        /^EVENTSERVER="(.*)"$/ && do {
            $eventserver = $1;
        };
    }
162 163

    #
Mike Hibler's avatar
Mike Hibler committed
164
    # Start the event proxy.
165 166
    #
    if (-x "$BINDIR/evproxy") {
Mike Hibler's avatar
Mike Hibler committed
167
	print("Starting event proxy daemon\n");
168
	system("$BINDIR/evproxy -i $EVPIDFILE ".
169
	       "   -s $eventserver -e $pid/$eid");
170 171 172 173 174 175 176 177 178 179 180 181
	if ($?) {
	    BootFatal("Error running $BINDIR/evproxy");
	}
    }

    #
    # 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");
    
182
    system("$RCDIR/rc.config -j $vnodeid");
183
    if ($?) {
184
	BootFatal("Could not run rc.config for $vnodeid");
185 186 187 188 189 190
    }

    TBDebugTimeStamp("rc.nojail done running config scripts");

    print("Informing the testbed that we are up and running\n");
    if (tmcc(TMCCCMD_STATE, "ISUP") < 0) {
Mike Hibler's avatar
Mike Hibler committed
191
	BootFatal("Error sending ISUP to Emulab Control");
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    }

    #
    # Send the console log to the server.
    #
    if (-e $BOOTLOG && -s $BOOTLOG) {
	tmcc(TMCCCMD_BOOTLOG, "", undef,
	     ("datafile" => $BOOTLOG, "timeout" => 5));
    }
}

#
# Shutdown Action.
#
sub doshutdown()
{
    print("Running termination scripts\n");
Mike Hibler's avatar
Mike Hibler committed
209
    system("$RCDIR/rc.config -j $vnodeid shutdown");
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

    if (-e $EVPIDFILE) {
	my $mpid = `cat $EVPIDFILE`;
	chomp($mpid);
	# untaint
	if ($mpid =~ /^([\d]+)$/) {
	    $mpid = $1;

	    print("Killing the event proxy\n");
	    
	    if (kill('TERM', $mpid) == 0) {
		print "*** Could not kill(TERM) process $mpid: $!\n";
		return -1;
	    }
	    #
	    # Wait for the pidfile to be removed. Do not wait too long though. 
	    # 
	    my $i = 15;
	    while (-e $EVPIDFILE && $i > 0) {
		sleep(1);
		$i -= 1;
	    }
	    if (-e $EVPIDFILE) {
		print "*** Not able to kill evproxy process $mpid!\n";
	    }
	}
    }
}