os_load.in 8.37 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
# usage: os_load [-s] <imageid> <node> [node ...]
16
# usage: os_load -l
17
#
18 19
sub usage()
{
20
    print STDOUT "Usage: os_load [-sw] <imageid> <node> [node ...]\n".
21
	"Use -s to setup reload only, but do not issue a reboot\n".
22 23
	"Use -w to block waiting for nodes to finish reloading\n".
	"    (-s and -w are mutually exclusive)\n".
24
	"Use -l to get a list of images you are permitted to load\n";
25 26
    exit(-1);
}
27
my  $optlist = "sldw";
28 29 30 31 32

#
# Configure variables
#
my $TB		= "@prefix@";
33
my $TESTMODE    = @TESTMODE@;
34 35 36 37 38 39
my $TBOPS       = "@TBOPSEMAIL@";

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

#
# Load the Testbed support stuff. 
#
44 45 46
use lib "@prefix@/lib";
use libdb;
use libtestbed;
47

48 49
my $BOSSADDR	= "boss.emulab.net";
my $USERADDR	= "users.emulab.net";
50
my $NETDISKOSID = "NETDISK-STD";
51
my $nodereboot	= "$TB/bin/node_reboot";
52
my $ping	= "/sbin/ping";
53
my $dbg		= 0;
54
my @row;
55
my %imageid_row = ();
56
my @nodes       = ();
57
my $mereuser    = 0;
58
my $setuponly   = 0;
59
my $waitmode    = 0;
60
my $failures    = 0;
61
my $startwait   = 0;
62 63 64 65 66 67 68 69

# 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

#
70 71 72 73 74 75 76
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
77 78 79 80
if (defined($options{"l"})) {
    dolisting();
    exit(0);
}
81
if (@ARGV < 2) {
82 83 84 85 86
    usage();
}
if (defined($options{"s"})) {
    $setuponly = 1;
}
87 88 89 90 91 92
if (defined($options{"w"})) {
    $waitmode = 1;
}
if ($waitmode && $setuponly) {
    usage();
}
93
my $imageid = shift;
94

95 96 97 98 99 100 101 102
#
# Untaint args.
#
if ($imageid =~ /^([-\@\w.\+]+)$/) {
    $imageid = $1;
}
else {
    die("Bad data in $imageid.");
103
}
104 105 106 107 108 109 110 111 112 113 114

foreach my $node ( @ARGV ) {
    if ($node =~ /^([-\@\w]+)$/) {
	$node = $1;
    }
    else {
	die("Bad node name: $node.");
    }
    
    push(@nodes, $node);
}
115 116 117 118 119 120

#
# 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.
#
121 122
if ($UID && !TBAdmin($UID)) {
    $mereuser = 1;
123 124
}

125 126 127 128 129 130
#
# Grab the imageid description from the DB. The permission check for this
# is that mere user can load an image from any project he/she is a member of,
# or any image that has a null pid field, since those are defined to be
# open to anyone. 
#
131
$db_result = DBQueryFatal("select * from images where imageid='$imageid'");
132 133 134 135 136 137 138 139 140 141 142
    
if ($db_result->numrows < 1) {
    die("No such imageid $imageid!");
}
%imageid_row = $db_result->fetchhash();

my $imagepid = 0;
if (defined($imageid_row{'pid'})) {
    $imagepid = $imageid_row{'pid'};
}
    
143 144
if ($mereuser && $imagepid && !ProjMember($imagepid)) {
    die("You do not have permission to load imageid $imageid!");
145 146
}

147 148 149 150 151
#
# Check to make sure that mere user is allowed to muck with nodes
#
if ($mereuser) {
    foreach my $node (@nodes) {
152
	if (! NodeAccessCheck(\$node)) {
153
	    die("You do not have permission to load images on $node\n");
154 155 156 157
	}
    }
}

158 159 160 161
my $loadpart  = $imageid_row{'loadpart'};
my $loadlen   = $imageid_row{'loadlength'};
my $imagepath = $imageid_row{'path'};
my $defosid   = $imageid_row{'default_osid'};
162 163

#
164
# 0 means load the entire disk.
165 166
#
my $diskpart = "";
167 168
if ($loadpart) {
    $diskpart = "wd0:s${loadpart}";
169 170 171 172 173 174
}
else {
    $diskpart = "wd0";
}

#
175 176
# For now, all testbed default images from from paper and all pid specific
# images come from plastic:/proj.
177 178
# 
my $cmdline = "";
179
if ($imagepid) {
180 181 182
    if (! ($imagepath =~ /^\/proj\//)) {
	die("Your image must reside in /proj\n");
    }
183
    $cmdline = "${USERADDR}:$imagepath $diskpart";    
184 185
}
else {
186
    $cmdline = "${BOSSADDR}:$imagepath $diskpart";
187 188 189
}

#
190
# Loop for each node.
191 192 193 194
# 
foreach my $node (@nodes) {
    my $pc = $node;
	
195
    print STDOUT "Changing default OS for $pc to $defosid\n";
196 197 198 199 200
    if (!$TESTMODE) {
      DBQueryFatal("update nodes set ".
		   "def_boot_osid='$defosid',def_boot_path='' ".
		   "where node_id='$pc'");
    }
201 202

    #
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    # 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
    # for the subset of slices that are written to disk. In reality, this
    # is silly since there is no telling what the disk is going to end
    # up looking like after a partial image is written, especially if its
    # a user generated image. Not sure how to handle this yet. For now
    # lets just say that a user defined images essentially wipe the disk
    # except for the stuff they write. 
    #
    for ($i = 1; $i <= 4; $i++) {
	my $partname = "part${i}_osid";
	
	if (defined($imageid_row{$partname})) {
	    my $osid = $imageid_row{$partname};

219 220 221
	    DBQueryFatal("replace into partitions ".
			 "(partition, osid, node_id) ".
			 "values('$i', '$osid', '$pc')");
222 223
	}
	else {
224 225
	    DBQueryFatal("delete from partitions ".
			 "where node_id='$pc' and partition='$i'");
226 227
	}
    }
228
    
229
    print STDOUT "Setting up reload for $pc\n";
230 231 232 233 234 235
    if (!$TESTMODE) {
      DBQueryFatal("update nodes set ".
		   "next_boot_osid='$NETDISKOSID',".
		   "next_boot_cmd_line='$cmdline' ".
		   "where node_id='$pc'");
    }
236
}
237

238
#
239
# Exit if not doing about actual reload.
240
#
241 242 243 244 245 246 247 248 249
if ($setuponly || $TESTMODE) {
    print STDOUT "OS Reload (Setup/Testmode) Done!\n";
    exit 0;
}

#
# Fire off a mass reboot if not in waitmode.
#
if (! $waitmode) {
250 251
    system("$nodereboot @nodes");
    $failures = $? >> 8;
252 253 254

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

257 258 259 260 261 262 263 264 265 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
#
# Okay, in waitmode we do a couple at a time and wait for them to come
# back alive before proceeding to the next one.
#
while (@nodes) {
    my @batch = ();
    my $i;

    for ($i = 0; $i < $MAXLOADS && @nodes > 0; $i++) {
	push(@batch, shift(@nodes));
    }
    
    print "Issuing reload/reboot for @batch and then waiting ...\n";

    system("$nodereboot @batch");
    $failures = $? >> 8;
    if ($failures) {
	print "Reboot failed for (some of) @batch. Quitting!\n";
	exit $failures;
    }

    #
    # Now wait for them.
    #
    $startwait = time;
    foreach my $node (@batch) {
	if (WaitTillReloadDone($node) == 0) {
	    print "$node appears have reloaded okay.\n";
	    next;
	}

	print "$node may be down.\n".
	      "Please contact $TBOPS for assistance.\n";
    }
}
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
#
# 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 ".
			 "left join proj_memb as p on ".
			 " i.pid IS NULL or p.pid=i.pid ".
			 "where p.uid='$me' order by i.pid,o.imageid");
    }
    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;
    }
}
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

#
# 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 ($pc) = @_;

    print STDERR "Waiting for $pc to finish reloading\n" if $dbg;
    
    #
    # Seven minutes seems like a long time to wait, but it ain't!
    # 
    my $maxwait = (60 * 7);

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

    while (1) {
	my ($query_result, @row);

	$query_result =
	    DBQueryFatal("SELECT next_boot_osid FROM nodes ".
			 "where node_id='$pc'");

        @row = $query_result->fetchrow_array();
	if (! $row[0]) {
	    print STDERR "$pc alive and well\n" if $dbg;
	    return 0;
	}
	
	$waittime = time - $startwait;
	if ($waittime > $maxwait) {
	    print "$pc appears unresponsive; its been ",
	    (int ($waittime / 60))," minutes since reload started.\n";
	    return 1;
	}
	if (int($waittime / 60) > $minutes) {
	    $minutes = int($waittime / 60);
	    print "Still waiting for $pc - its been $minutes minute(s)\n";
	}
	sleep(5);
    }
}