os_load.in 12.7 KB
Newer Older
1 2
#!/usr/bin/perl -wT
use English;
3
use Getopt::Std;
4

5
#
6
# XXX boss.emulab.net and users.emulab.net wired in. 
7
#     wd0 wired in. Should come from node_types table in DB
8 9 10
# 

#
11 12 13
# Load an image onto a disk. The image must be in the DB images table,
# which defines how/where to load, and what partitions are affected.
# The nodes and partitions tables are updated appropriately. 
14
# 
15 16
sub usage()
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
17
    print STDOUT
18 19 20 21
	"Usage: os_load [-s] [[-p <pid>] -i <imagename>] [-m <imageid>] ".
        "<node> [node ...]\n".
	"       os_load [-s] [[-p <pid>] -i <imagename>] [-m <imageid>] ".
        "-e pid,eid\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
22
	"       os_load -l\n".
23 24
	"Use -i to specify an image name. Use node default otherwise.\n".
	"Use -m to specify an image ID (internal name, TB admins only!).\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
25
	"Use -s to start reload, but do not wait for it to complete.\n".
26 27
	"Use -e to reload all the nodes in an experiment.\n" .
        "Use -l to get a list of images you are permitted to load.\n";
28 29
    exit(-1);
}
30
my  $optlist = "sldrni:e:p:m:b";
31 32 33 34 35

#
# Configure variables
#
my $TB		= "@prefix@";
36
my $TESTMODE    = @TESTMODE@;
37
my $TBOPS       = "@TBOPSEMAIL@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
38 39 40
my $BOSSADDR	= "@BOSSNODE@";
my $USERADDR	= "@USERNODE@";

41 42 43 44
#
# Max number of simultaneous loads. Will be better with Frisbee.
# 
my $MAXLOADS	= 2;
45

Leigh B. Stoller's avatar
Leigh B. Stoller committed
46 47 48 49 50 51
#
# Max number of retries (per node) before its deemed fatal. This allows
# for the occasional pxeboot failure.
#
my $MAXRETRIES  = 1;

52 53 54
#
# Load the Testbed support stuff. 
#
55 56 57
use lib "@prefix@/lib";
use libdb;
use libtestbed;
58

59
my $NETDISKOSID = "NETDISK-STD";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60
my $FRISBEEPATH = "$BOSSADDR:/tftpboot/pxeboot.frisbee";
61
my $FRISBEELAUNCHER = "$TB/sbin/frisbeelauncher";
62
my $nodereboot	= "$TB/bin/node_reboot";
63
my $schedreload	= "$TB/bin/sched_reload";
64
my $ping	= "/sbin/ping";
65
my $dbg		= 0;
66
my @row;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
67
my $usedefault  = 1;
68
my $imagename;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69
my $imageid;
70
my $imagepid    = TB_OPSPID;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71
my %imageid_row;
72
my @nodes       = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
73
my %retries	= ();
74
my $mereuser    = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
75
my $waitmode    = 1;
76
my $failures    = 0;
77
my $startwait   = 0;
78
my $type	= TB_DEFAULT_RELOADTYPE;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
79
my $cmdline     = "";
80 81 82 83 84 85 86 87

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
88 89 90 91 92 93 94
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
95 96 97
if (defined($options{"d"})) {
    $dbg++;
}
98 99 100 101
if (defined($options{"l"})) {
    dolisting();
    exit(0);
}
102
if (defined($options{"s"})) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
103
    $waitmode = 0;
104
}
105
if (defined($options{"r"})) {
106 107 108 109 110 111 112
    if (defined($options{"n"})) {
	die "Only one of -r or -n should be given\n";
    }
    $type = TB_RELOADTYPE_FRISBEE;
}
if (defined($options{"n"})) {
    $type = TB_RELOADTYPE_NETDISK;
113
}
114 115 116 117 118

if (defined($options{"i"}) && defined($options{"m"})) {
    usage();
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
119
if (defined($options{"i"})) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    $imagename  = $options{"i"};
    $usedefault = 0;

    if ($imagename =~ /^([-\w\.\+]+)$/) {
	$imagename = $1;
    }
    else {
	die("*** Bad data in $imagename.\n");
    }
    if (defined($options{"p"})) {
	$imagepid = $options{"p"};
	
	if ($imagepid =~ /^([-\w\.\+]+)$/) {
	    $imagepid = $1;
	}
	else {
	    die("*** Bad data in $imagepid.\n");
	}
    }
}
if (defined($options{"m"})) {
    $imageid = $options{"m"};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
142
    $usedefault = 0;
143

144
    if ($imageid =~ /^([-\w\.\+]+)$/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
145 146 147
	$imageid = $1;
    }
    else {
148
	die("*** Bad data in $imageid\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
149
    }
150
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
151 152 153 154 155 156 157 158 159 160 161
if (defined($options{"e"})) {
    if (@ARGV) {
	usage();
    }
    my $pideid = $options{"e"};
    
    if ($pideid =~ /([-\w]*),([-\w]*)/) {
	if (! (@nodes = ExpNodes($1, $2))) {
	    die("*** $0:\n".
		"    There are no nodes in $1/$2!\n");
	}
162 163
    }
    else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
164 165
	die("Invalid argument to -e option: $pideid\n");
	usage();
166
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}
else {
    if (! @ARGV) {
	usage();
    }
    #
    # Untaint nodes.
    #
    foreach my $node ( @ARGV ) {
	if ($node =~ /^([-\@\w]+)$/) {
	    $node = $1;
	}
	else {
	    die("*** Bad node name: $node.\n");
	}
182
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
183 184
	push(@nodes, $node);
    }
185
}
186 187 188 189 190 191

#
# Figure out who called us. Root and admin types can do whatever they
# want. Normal users can only change nodes in experiments in their
# own projects.
#
192 193
if ($UID && !TBAdmin($UID)) {
    $mereuser = 1;
194

Leigh B. Stoller's avatar
Leigh B. Stoller committed
195
    if (! TBNodeAccessCheck($UID, TB_NODEACCESS_LOADIMAGE, @nodes)) {
196 197 198 199 200 201
	die("*** $0:\n".
            "    You do not have permission to load images on one (or more) ".
	    "nodes!\n");
    }
    if (defined($imageid)) {
	usage();
202 203 204 205
    }
}

#
206
# Convert external name to internal (imageid), and check permission.
207
#
208 209 210 211 212 213 214 215 216 217 218
if (defined($imagename)) {
    if (! ($imageid = TBImageID($imagepid, $imagename))) {
	die("*** $0:\n".
	    "    No such image $imagename in project $imagepid!\n");
    }

    if ($mereuser &&
	! TBImageIDAccessCheck($UID, $imageid, TB_IMAGEID_READINFO)) {
	die("*** $0:\n".
	    "    You do not have permission to load this image!\n");
    }
219 220 221
}

#
222
# Loop for each node.
223 224
# 
foreach my $node (@nodes) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225 226 227 228 229 230
    #
    # Oh, I suppose it would be nice to do this just once per imageid,
    # but that would be a pain. Not in the mood.
    #
    
    #
231
    # Get default imageid for this node.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
232
    #
233 234 235 236 237
    my $default_imageid;
    if (! ($default_imageid = DefaultImageID($node))) {
	die("*** $0:\n".
	    "    No default imageid is defined for $node!\n");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
238
    if ($usedefault) {
239
	$imageid = $default_imageid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
240
    }
241 242

    print STDERR "Using $imageid for for $node\n" if $dbg;
243
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
244 245 246 247
    my $db_result =
	DBQueryFatal("select * from images where imageid='$imageid'");
    
    if ($db_result->numrows < 1) {
248 249
	die("*** $0:\n".
	    "    No such imageid $imageid is defined in the DB!\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
250 251 252 253 254 255 256
    }
    %imageid_row = $db_result->fetchhash();
    
    my $loadpart  = $imageid_row{'loadpart'};
    my $loadlen   = $imageid_row{'loadlength'};
    my $imagepath = $imageid_row{'path'};
    my $defosid   = $imageid_row{'default_osid'};
257
    my $shared    = $imageid_row{'shared'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

    #
    # 0 means load the entire disk.
    #
    my $diskpart = "";
    if ($loadpart) {
	$diskpart = "wd0:s${loadpart}";
    }
    else {
	$diskpart = "wd0";
    }

    #
    # For now, all testbed default images come from paper and all pid specific
    # images come from plastic:/proj.
    # 
274
    if (! $shared) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
275
	if (! ($imagepath =~ /^\/proj\//)) {
276 277
	    die("*** $0:\n".
		"    Your image must reside in /proj\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
278 279 280 281 282 283 284 285
	}
	$cmdline = "${USERADDR}:$imagepath $diskpart";    
    }
    else {
	$cmdline = "${BOSSADDR}:$imagepath $diskpart";
    }
    
    print STDOUT "Changing default OS for $node to $defosid\n";
286 287 288
    if (!$TESTMODE) {
      DBQueryFatal("update nodes set ".
		   "def_boot_osid='$defosid',def_boot_path='' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
289
		   "where node_id='$node'");
290
    }
291

292
    # Put it in the current_reloads table so that nodes can find out which
Leigh B. Stoller's avatar
Leigh B. Stoller committed
293
    # OS to load
294
    DBQueryFatal("replace into current_reloads ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
295
		 "(node_id, image_id) values ('$node', '$imageid')");
296

297
    #
298
    # If loading an image (which is not the default) then
299 300 301 302
    # schedule a reload for it so that when the experiment is terminated
    # it will get a fresh default image before getting reallocated to
    # another experiment.
    #
303
    if ($imageid ne $default_imageid) {
304 305 306 307 308 309
	if (! TBSetSchedReload($node, $default_imageid)) {
	    print "*** $0:\n".
		  "    WARNING: Could not schedule default reload for $node!";
	}
    }

310
    #
311 312 313
    # Assign partition table entries for each partition in the image.
    # This is complicated by the fact that an image that covers only
    # part of the slices, should only change the partition table entries
314
    # for the subset of slices that are written to disk.
315
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
316 317 318 319
    my $startpart = $loadpart == 0 ? 1 : $loadpart;
    my $endpart   = $loadpart + $loadlen;
	
    for ($i = $startpart; $i < $endpart; $i++) {
320 321 322 323 324
	my $partname = "part${i}_osid";
	
	if (defined($imageid_row{$partname})) {
	    my $osid = $imageid_row{$partname};

325 326
	    DBQueryFatal("replace into partitions ".
			 "(partition, osid, node_id) ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
327
			 "values('$i', '$osid', '$node')");
328 329
	}
	else {
330
	    DBQueryFatal("delete from partitions ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
331
			 "where node_id='$node' and partition='$i'");
332 333
	}
    }
334
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
335
    print STDOUT "Setting up reload for $node\n";
336
    if (!$TESTMODE) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
337
        SetupReload($node);
338
    }
339
}
340

341
#
342
# Exit if not doing about actual reload.
343
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
344
if ($TESTMODE) {
345 346 347 348 349 350 351 352
    print STDOUT "OS Reload (Setup/Testmode) Done!\n";
    exit 0;
}

#
# Fire off a mass reboot if not in waitmode.
#
if (! $waitmode) {
353 354
    system("$nodereboot @nodes");
    $failures = $? >> 8;
355 356 357

    print STDOUT "OS Reload (no waiting) Done!\n";
    exit $failures;
358 359
}

360 361 362 363
#
# Okay, in waitmode we do a couple at a time and wait for them to come
# back alive before proceeding to the next one.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
364 365 366 367 368 369 370
# The retry vector is initialized to the number of retries we allow per
# node, afterwhich its a fatal error.
#
foreach my $node (@nodes) {
    $retries{$node} = $MAXRETRIES;
}

371
while (@nodes) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
372 373
    my @batch  = ();
    my @failed = ();
374 375
    my $i;

376 377 378 379 380 381 382 383 384 385 386
    #
    # If doing a netdisk load, only do a few at a time. For frisbee, we do
    # 'em all
    #
    if ($type eq TB_RELOADTYPE_NETDISK) {
	for ($i = 0; $i < $MAXLOADS && @nodes > 0; $i++) {
	    push(@batch, shift(@nodes));
	}
    } elsif ($type eq TB_RELOADTYPE_FRISBEE) {
	@batch = @nodes;
	@nodes = ();
387 388
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
389
    print "Issuing reboot for @batch and then waiting ...\n";
390 391

    system("$nodereboot @batch");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
392
    if ($?) {
393
	print "Reboot failed for (some of) @batch. Quitting!\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
394
	exit ($? >> 8);
395 396 397 398 399 400
    }

    #
    # Now wait for them.
    #
    $startwait = time;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
401 402 403 404 405 406 407 408 409 410 411 412 413
    @failed = WaitTillReloadDone(@batch);
    while (@failed) {
	my $node = shift(@failed);

	if ($retries{$node}) {
	    print "*** Trying $node again (resetting/rebooting) ...\n";
	    push(@nodes, $node);

	    # Possible race with reboot?
	    SetupReload($node);

	    # Retry until count hits zero.
	    $retries{$node} -= 1;
414
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	else {
	    print "*** $node failed too many times. Skipping!\n";
	    $failures++;
	}
    }
}

print "OS Reload Done! There were $failures failures!\n";
exit($failures);

#
# Wait for a reload to finish. We do this in a rather kludgey manner,
# by waiting for bootinfo to clear the DB state (next_boot_osid).
# 
sub WaitTillReloadDone {
    my (@nodes) = @_;
    my %done	= ();
    my $count   = @nodes;
    my @failed  = ();
434

Leigh B. Stoller's avatar
Leigh B. Stoller committed
435
    #
436
    # Seems like a long time to wait, but it ain't!
Leigh B. Stoller's avatar
Leigh B. Stoller committed
437
    # 
438
    my $maxwait = (60 * 10);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

    foreach my $node ( @nodes ) {
	$done{$node}  = 0;
    }

    print STDERR "Waiting for @nodes to finish reloading\n" if $dbg;

    #
    # Start a counter going, relative to the time we rebooted the first
    # node. 
    # 
    my $waittime  = 0;
    my $minutes   = 0;

    while ($count) {
	foreach my $node ( @nodes ) {
	    if (! $done{$node}) {
		my ($query_result, @row);

		if ($type eq TB_RELOADTYPE_FRISBEE) {
		    $query_result =
			DBQueryFatal("SELECT next_pxe_boot_path FROM nodes ".
				     "where node_id='$node'");
		}
		else {
		    $query_result =
			DBQueryFatal("SELECT next_boot_osid FROM nodes ".
				     "where node_id='$node'");
		}

		@row = $query_result->fetchrow_array();
		if (! $row[0]) {
		    print STDERR "$node alive and well\n" if $dbg;
		    $count--;
		    $done{$node} = 1;
		    next;
		}
	
		$waittime = time - $startwait;
		if ($waittime > $maxwait) {
		    my $t = (int ($waittime / 60));
		    
		    print "*** $node appears wedged; ".
			"its been $t minutes since it was rebooted.\n";

		    $count--;
		    $done{$node} = 1;
		    push(@failed, $node);
		    next;
		}
		if (int($waittime / 60) > $minutes) {
		    $minutes = int($waittime / 60);
		    print "Still waiting for $node - ".
			"its been $minutes minute(s)\n";
		}
	    }
	}
	sleep(5);
    }
    return @failed;
}

#
# Setup a reload. Note that type, imageid, and cmdline are global.
#
sub SetupReload($)
{
    my ($node) = @_;

    if ($type eq TB_RELOADTYPE_FRISBEE) {
	DBQueryFatal("update nodes set ".
		     "next_pxe_boot_path='$FRISBEEPATH'" .
		     "where node_id='$node'");
	
	system "$FRISBEELAUNCHER $imageid" and
	    die "*** Unable to launch frisbee daemon\n";
    }
    elsif ($type eq TB_RELOADTYPE_NETDISK) {
	DBQueryFatal("update nodes set ".
		     "next_boot_osid='$NETDISKOSID',".
		     "next_boot_cmd_line='$cmdline' ".
		     "where node_id='$node'");
    }
    else {
	die "*** Unknown reload type ($type)\n";
524 525
    }
}
526

527 528 529 530 531 532 533 534 535 536 537 538
#
# Print a listing of imageids.
#
sub dolisting()
{
    my($query_result);
    
    if ($UID && !TBAdmin($UID)) {
	my ($me) = getpwuid($UID);
	
	$query_result =
	    DBQueryFatal("select distinct i.* from images as i ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
539 540
			 "left join group_membership as p on ".
			 " i.pid IS NULL or (p.pid=i.pid and p.gid=p.pid) ".
541
			 "where p.uid='$me' order by i.pid,i.imageid");
542 543 544 545 546 547 548 549 550 551 552 553 554 555
    }
    else {
	$query_result =
	    DBQueryFatal("SELECT * FROM images order by imageid");
    }
    
    for ($i = 0; $i < $query_result->numrows; $i++) {
	my %row  = $query_result->fetchhash();
	my $id   = $row{'imageid'};
	my $desc = $row{'description'};

	printf "%-20s %s\n", $id, $desc;
    }
}