rc.bootsetup 10.8 KB
Newer Older
1
2
3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2004, 2005 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
# All rights reserved.
#
use English;
use Getopt::Std;

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

# Script specific goo.
my $RCDIR   = "$BINDIR/rc";
my $LOGFILE = "$LOGDIR/bootsetup.debug";

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;
use libtmcc;
use librc;

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

# Protos.
sub doboot();
sub doshutdown();
sub doreconfig();
sub docleanup();
62
sub BootFatal($$);
63
64
65
66
67
68
69
70
71
72
73
74
75

# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'b'})) {
    $background = 1;
}
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

76
77
78
79
80
81
if ($action eq "boot" || $action eq "reconfig") {
    #
    # We want to save all of the output off, but also dup it to the console.
    #
    open(LOG, "> $LOGFILE") or
	BootFatal(-1, "Could not open $LOGFILE!");
82
    autoflush(LOG, 1);
83
    
84
85
86
87
88
89
90
91
92
93
94
    #
    # 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.
    # 
    if (open(FOO, "-|")) {
	while (<FOO>) {
	    print LOG $_;
	    print "$_";
	}
	close(LOG);
95
	close(FOO);
96
97
98
99
100
	if ($?) {
	    BootFatal($? >> 8, "Boot Failure!");
	}
	exit(0);
    }
101
    open(STDERR, ">&STDOUT");
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
}

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

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
#
# This version of fatal sends the console log to tmcd, and then generates
# a TBFAILED state transition. 
#
sub BootFatal($$)
{
    my ($code, $msg) = @_;
    
    #
    # Send the console log to the server.
    #
    if (-e $LOGFILE && -s $LOGFILE &&
	tmcc(TMCCCMD_BOOTLOG, "", undef, ("datafile" => $LOGFILE)) < 0) {
	print "Error sending TBFAILED to Emulab Control!\n";
    }
    
    if (tmcc(TMCCCMD_BOOTERRNO, $code) < 0) {
	print "Error sending boot errno to Emulab Control!\n";
    }
    
    if (tmcc(TMCCCMD_STATE, "TBFAILED") < 0) {
	print "Error sending TBFAILED to Emulab Control!\n";
    }
    exit($code);
}

152
153
154
155
156
157
158
159
160
161
162
163
164
165
#
# Boot Action.
#
sub doboot()
{
    #
    # Sort of a hodgepodge of stuff!
    #
    # Get the boss info for below.
    my ($bossname, $bossip) = tmccbossinfo();
    if (!defined($bossname)) {
	fatal("Could not determine the name of the boss server!");
    }

166
167
168
169
170
    #
    # Check for an os dependent initialization script.
    #
    my $os_script = "rc." . lc($OSNAME);
    if (-x "$RCDIR/$os_script") {
171
        print("Running os dependent initialization script $os_script\n");    
172
173
174
175
176
177
	system("$RCDIR/$os_script");
	if ($?) {
	    fatal("Error running $RCDIR/$os_script");
	}
    }

178
179
180
181
182
183
184
185
186
187
    #
    # 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");
	}
    }

188
189
190
191
192
193
194
195
196
197
    #
    # Get the firewall up
    #
    if (-x "$RCDIR/rc.firewall") {
	system("$RCDIR/rc.firewall");
	if ($?) {
	    fatal("Error running $RCDIR/rc.firewall");
	}
    }

198
    if (-x "$BINDIR/tbshutdown" && ! WINDOWS()) {
199
200
201
202
203
204
205
        print("Starting up shutdown notification daemon\n");
        system("$BINDIR/tbshutdown");
	if ($?) {
	    fatal("Error running $BINDIR/tbshutdown");
	}
    }

206
    if (-x "$RCDIR/rc.ipod" && ! WINDOWS()) {
207
208
	print("Setting up Ping of Death\n");
	system("$RCDIR/rc.ipod");
209
	# This is allowed to fail; ipod might not be supported.
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    }

    print("Informing Emulab Control that we have rebooted\n");
    if (tmcc(TMCCCMD_STATE, "TBSETUP") < 0) {
	fatal("Error sending TBSETUP to Emulab Control!");
    }

    # 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.
    #
224
    if (-x "$RCDIR/rc.healthd" && ! WINDOWS()) {
225
226
227
228
229
	print("Starting node health monitoring daemon\n");
	system("$RCDIR/rc.healthd start");
	# If it fails, that is okay. Keep going.
    }
    
230
    if (-x "$RCDIR/rc.slothd" && ! WINDOWS()) {
231
232
233
234
235
236
237
	print("Starting slothd usage detector\n");
	system("$RCDIR/rc.slothd start");
	if ($?) {
	    fatal("Error running $RCDIR/rc.slothd");
	}
    }

238
    if (-x "$BINDIR/watchdog") {
239
240
241
242
243
244
245
        print("Starting testbed update watchdog\n");
	system("$BINDIR/watchdog start");
	if ($?) {
	    fatal("Error running $BINDIR/watchdog");
	}
    }

246
247
248
249
250
251
252
253
254
255
256
    #
    # 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");
        }
    }

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    #
    # 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;
    }

    #
    # Start the elvin proxy.
    #
277
    if (-x "$BINDIR/evproxy" && ! WINDOWS()) {
278
	print("Starting elvin proxy daemon\n");
279
	system("$BINDIR/evproxy -s event-server -e $pid/$eid");
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
	if ($?) {
	    fatal("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. 
    # 
    print("Running config scripts\n");
    system("$RCDIR/rc.config boot");
    if ($?) {
	fatal("Error running $RCDIR/rc.config");
    }

Timothy Stack's avatar
   
Timothy Stack committed
295
296
297
298
299
300
301
302
    if (-x "$RCDIR/rc.canaryd" && -x "$BINDIR/canaryd") {
	print("Starting canaryd usage tracer\n");
	system("$RCDIR/rc.canaryd start");
	if ($?) {
	    fatal("Error running $RCDIR/rc.canaryd");
	}
    }

303
    #
304
305
    # Start the linktest daemon. This script will exit without running
    # linktest is the node is hosting SIM or JAILS.
306
    #
307
    if (-x "$RCDIR/rc.linktest" && ! WINDOWS()) {
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
	system("$RCDIR/rc.linktest start");
	if ($?) {
	    fatal("Error running $RCDIR/rc.linktest");
	}
    }

    #
    # 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");
    if (tmcc(TMCCCMD_STATE, "ISUP") < 0) {
	fatal("Error sending ISUP to Emulab Control!");
    }

    #
    # Boot vnodes. Doing it after ISUP is better; os_setup will
    # consider the node ready, and move onto waiting for vnodes to
    # boot. Ditto for subnodes belowl which should be merged with vnodes.
    #
    if (-x "$BINDIR/bootvnodes") {
	print("Booting up vnodes\n");
	# Foreground mode.
	system("$BINDIR/bootvnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootvnodes");
345
	}
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    }
    if (-x "$BINDIR/bootsubnodes") {
	print("Booting up subnodes\n");
	# Foreground mode.
	system("$BINDIR/bootsubnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootsubnodes");
	}
    }
    # 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
363
364
365
366
367
368
369
370
    if (-x "$BINDIR/bootsubnodes") {
	print("Booting up subnodes\n");
	# Foreground mode.
	system("$BINDIR/bootsubnodes -f");
	if ($?) {
	    fatal("Error running $BINDIR/bootsubnodes");
	}
    }
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
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
438
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
}

#
# Shutdown Action.
#
sub doshutdown()
{
    #
    # Halt vnodes. The filesystems are left intact!
    #
    if (-x "$BINDIR/bootvnodes") {
	print("Halting vnodes\n");
	# Foreground mode.
	system("$BINDIR/bootvnodes -f -h");
	if ($?) {
	    fatal("Error running $BINDIR/bootvnodes");
	}
    }

    print("Informing Emulab Control that we are are rebooting\n");
    if (tmcc(TMCCCMD_STATE, "SHUTDOWN") < 0) {
	fatal("Error sending SHUTDOWN to Emulab Control!");
    }
}

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

    #
    # Very hacky. These options just tell bootvnodes to halt/kill vnodes.
    # 
    if (-x "$BINDIR/bootvnodes") {
	print("Halting vnodes\n");
	# Foreground mode. -c is "reconfig" mode.
	system("$BINDIR/bootvnodes -f -c -h");
	if ($?) {
	    fatal("Error running $BINDIR/bootvnodes");
	}
    }

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

    #
    # And (re)boot vnodes. As with boot, doing it after ISUP is better;
    # os_setup will consider the node ready, and move onto waiting for
    # vnodes to boot.
    #
    print("Informing Emulab Control that we are back up and running\n");
    if (tmcc(TMCCCMD_STATE, "ISUP") < 0) {
	fatal("Error sending ISUP to Emulab Control!");
    }

    #
    # And (re)boot vnodes.
    #
    if (-x "$BINDIR/bootvnodes") {
	print("Booting vnodes\n");
	# Foreground mode. -c is "reconfig" mode.
	system("$BINDIR/bootvnodes -f -c -b");
	if ($?) {
	    fatal("Error running $BINDIR/bootvnodes");
	}
    }
    
    return 0;
}

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