clone_image.in 13.9 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29
#
use English;
use strict;
use Getopt::Std;
use Data::Dumper;
use File::Temp qw(tempfile);
use CGI;
30
use File::Basename;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#
# Clone an image (descriptor) from a node and then snapshot
# that node into the descriptor. Creates the descriptor if
# if it does not exist. The idea is to use all of the info
# from the current image descriptor that is loaded on the node
# to quickly create a new descriptor by inheriting all of the
# attributes of the original.
#
# We also want to support taking a snapshot of a previously
# created clone. To make everything work properly, require
# that the imagename exist in the experiment project, which
# ensures that we are operating on a clone, not an image in
# some other project or a system image.
#
sub usage()
{
48
    print("Usage: clone_image [-dwe] [-n | -s] <imagename> <node_id>\n".
49 50 51
	  "Options:\n".
	  "       -d     Turn on debug mode\n".
	  "       -e     Create a whole disk image\n".
52
	  "       -g 0,1 Override base image global setting\n".
53
	  "       -s     Create descriptor but do not snapshot\n".
54 55
	  "       -n     Impotent mode\n".
	  "       -w     Wait for image to be created\n");
56 57
    exit(-1);
}
58
my $optlist     = "densg:w";
59 60 61 62
my $debug       = 0;
my $wholedisk   = 0;
my $impotent    = 0;
my $nosnapshot  = 0;
63
my $isvirtnode  = 0;
64
my $waitmode    = 0;
65
my $global;
66 67 68 69 70 71

#
# Configure variables
#
my $TB           = "@prefix@";
my $PROJROOT     = "@PROJROOT_DIR@";
72
my $GROUPROOT    = "@GROUPSROOT_DIR@";
73 74
my $CREATEIMAGE  = "$TB/bin/create_image";
my $NEWIMAGEEZ   = "$TB/bin/newimageid_ez";
75
my $DOPROVENANCE = @IMAGEPROVENANCE@;
76
my $doprovenance = 0;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

#
# Untaint the path
#
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use EmulabConstants;
use emutil;
use User;
use Project;
use Image;
use OSinfo;
use Node;
100
use EmulabFeatures;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

# Protos
sub fatal($);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"e"})) {
    $wholedisk = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"s"})) {
    $nosnapshot = 1;
}
125 126 127
if (defined($options{"w"})) {
    $waitmode = 1;
}
128 129 130
if (defined($options{"g"})) {
    $global = $options{"g"};
}
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
usage()
    if (@ARGV != 2);

my $imagename = shift(@ARGV);
my $node_id   = shift(@ARGV);

#
# Map invoking user to object. 
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}

#
# The node must of course be allocated and the user must have
# permission to clone it. 
#
my $node = Node->Lookup($node_id);
if (!defined($node)) {
    fatal("No such node");
}
153 154
$isvirtnode = $node->isvirtnode();

155 156 157 158 159 160 161 162 163
if (!$node->AccessCheck($this_user, TB_NODEACCESS_LOADIMAGE())) {
    fatal("Not enough permission");
}
my $experiment = $node->Reservation();
if (!defined($experiment)) {
    fatal("Node is not reserved");
}
my $pid     = $experiment->pid();
my $group   = $experiment->GetGroup();
164
my $gid     = $group->gid();
165 166 167 168 169 170
my $project = $experiment->GetProject();
if (! (defined($project) && defined($group))) {
    fatal("Could not get project/group for $experiment");
}
my $image = Image->Lookup($project->pid(), $imagename);

171 172 173 174 175 176 177 178 179 180 181 182
#
# Need to look up the base image; the image that is currently running
# on the node and being cloned.
#
my ($base_osinfo, $base_image) = $node->RunningOsImage();
# No support for cloning MFSs, so there will always be a base image.
if (! (defined($base_osinfo) && defined($base_image))) {
    fatal("Could not determine osid/imageid for $node_id");
}
print "$node_id is running $base_osinfo,$base_image\n"
    if ($debug);

183 184 185 186
# Feature override. 
$doprovenance = 
    EmulabFeatures->FeatureEnabled("ImageProvenance", $this_user, $project);

187 188 189 190 191
#
# The simple case is that the descriptor already exists. So it is just
# a simple snapshot to the image file. 
#
if (defined($image)) {
192 193
    my $needdelete = 0;
    
194 195 196 197 198 199
    #
    # Only EZ images via this interface.
    #
    if (!$image->ezid()) {
	fatal("Cannot clone a non-ez image");
    }
200

201 202 203 204 205 206 207 208 209 210
    #
    # The access check above determines if the caller has permission
    # to overwrite the image file. 
    # Not that this matters, cause create_image is going to make the
    # same checks.
    #
    if ($impotent) {
	print "Not doing anything in impotent mode\n";
	exit(0);
    }
211 212 213 214 215 216 217

    #
    # Before we do anything destructive, we lock the image.
    #
    if ($image->Lock()) {
	fatal("Image is locked, please try again later!\n");
    }
218
    if ($DOPROVENANCE && $doprovenance) {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        #
	# This will include unreleased images (in image_versions, but
	# not the one pointed to by the images table). 
	#
	$image = $image->LookupMostRecent();
	if (!defined($image)) {
	    $image->Unlock();
	    fatal("Cannot lookup osinfo for $image");
	}
	
	#
	# We create a new version of the image descriptor for the new
	# snapshot. We mark it as not ready so that others know it is
	# in transition. When we later call createimage, it will make
	# sure the ready bit is clear before trying to use it.
	#
	my $needclone = 1;
	
237
	#
238 239 240 241
	# Does the most recent version in the table not have its ready bit set?
	# If so it means something went wrong with a previous image creation.
	# We can reuse it, but reset the provenance just in case the node got
	# reloaded.
242 243
	#
	if (!$image->ready()) {
244
	    my $osinfo = OSinfo->Lookup($image->imageid(), $image->version());
245 246 247 248 249 250
	    if (!defined($osinfo)) {
		$image->Unlock();
		fatal("Cannot lookup osinfo for $image");
	    }
	    $image->SetProvenance($base_image);
	    $osinfo->SetProvenance($base_osinfo);
251 252 253 254
	    $needclone = 0;
	    print "Reusing image version " . $image->version() . ", ".
		"since it was never marked ready.\n";
	    
255
	}
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	#
	# If the new image is going to based on the exact same base,
	# lets not create another new version, but overwrite the current
	# one. Save lots of space this way. We miss saving intermediate
	# versions, but this is a typical approach to getting an image
	# ready for use; change, snapshot, test, change, snapshot ... 
	#
	if ($needclone && !$image->released() && 
	    ($image->parent_imageid() == $base_image->imageid() &&
	     $image->parent_version() == $base_image->version())) {
	    # For create_image to be happy. 
	    $image->ClearReady(0);
	    $needclone = 0;
	    print "Reusing image version " . $image->version() . ", ".
		"since the base is the same and it was not released.\n";
	}

	if ($needclone) {
274 275
	    my $clone_error;
	    my $clone = $image->NewVersion($this_user,
276
					   $base_image, \$clone_error);
277 278 279 280 281 282 283 284 285 286 287 288
	    if (!defined($clone)) {
		$image->Unlock();
		fatal("Could not clone image descriptor" .
		      (defined($clone_error) ? ": $clone_error" : "") . "\n");
	    }
	    $image = $clone;
	    $needdelete = 1;

	    #
	    # Watch for a system image that is saved elsewhere; see equiv code
	    # in create_image. We change the path to point over to the /proj
	    # directory so that we do not burn up space on boss until it is
289
	    # officially "released". We *can* use this version of the image
290 291
	    # by explicitly using its version number, before it is released. 
	    #
292
	    if ($image->path() =~ /^$TB/) {
293
		my $path = $PROJROOT . "/" . $image->pid() . "/images/" .
Leigh B Stoller's avatar
Leigh B Stoller committed
294
		    basename($image->path());
295

296
		if ($image->Update({"path" => $path})) {
297
		    $image->DeleteVersion();
298
		    fatal("Could not update path!");
299 300
		}
	    }
301 302 303 304
	}
    }
    $image->Unlock();
    
305
    if ($nosnapshot) {
306 307
	print "Not taking a snapshot, as directed\n"
	    if ($debug);
308 309
	exit(0);
    }
310 311 312
    my $opts = "-p $pid ";
    $opts   .= "-w " if ($waitmode);
    
313
    my $output =
314
	emutil::ExecQuiet("$CREATEIMAGE $opts ".
315
			  "  $imagename:" . $image->version() . " $node_id");
316
    if ($?) {
317 318
	$image->DeleteVersion()
	    if ($needdelete);
319 320 321 322 323 324
	print STDERR $output;
	fatal("Failed to create image");
    }
    print "Image is being created. This can take 15-30 minutes.\n";
    exit(0);
}
325
DoNew:
326 327

#
328
# Only EZ images via this interface.
329
#
330 331 332 333
if (!$base_image->ezid()) {
    fatal("Cannot clone a non-ez image");
}

334 335 336 337 338
#
# Not allowed to derive an image from one that has not been released.
# Maybe relax this in the future, but this is a good simplification for
# now.
#
339
if ($DOPROVENANCE && $doprovenance && !$base_image->released()) {
340 341 342 343
    fatal("Not allowed to derive a new image from unreleased ".
	  "base $base_image");
}

344 345 346 347 348 349
#
# To avoid confusion, we do not allow users to shadow system images
# in their own project. 
#
if (Image->LookupByName($imagename) && !$this_user->IsAdmin()) {
    fatal("Not allowed to shadow snapshot a system image");
350 351
}

352 353 354 355 356
# Subgroups change the path
my $path = ($experiment->pid() eq $experiment->gid() ?
	    "$PROJROOT/$pid/images/${imagename}.ndz" :
	    "$GROUPROOT/$pid/$gid/images/${imagename}.ndz");

357 358 359 360 361 362 363 364 365 366 367 368
#
# Create the image descriptor. We use the backend script to do the
# heavy lifting, but we have to cons up an XML file based on the image
# descriptor that is being cloned.
#
# These are the fields we have to come up with, plus a number
# of mtype_* entries.
#
my %xmlfields =
    ("imagename"	=> $imagename,
     "pid"		=> $project->pid(),
     "gid"		=> $experiment->gid(),
369
     "description"	=> $base_osinfo->description(),
370 371
     "OS"		=> $base_osinfo->OS(),
     "version"		=> $base_osinfo->version(),
372
     "path"		=> $path,
373
     "op_mode",		=> $base_osinfo->op_mode(),
374
     "wholedisk",	=> $wholedisk,
375
);
Leigh B Stoller's avatar
Leigh B Stoller committed
376 377
$xmlfields{"reboot_waittime"} = $base_osinfo->reboot_waittime()
    if (defined($base_osinfo->reboot_waittime()));
378 379 380
$xmlfields{"osfeatures"} = $base_osinfo->osfeatures()
    if (defined($base_osinfo->osfeatures()) &&
	$base_osinfo->osfeatures() ne "");
381 382
$xmlfields{"global"} = 1
    if (defined($global) && $global);
Leigh B Stoller's avatar
Leigh B Stoller committed
383
    
384 385
if (defined($base_image)) {
    $xmlfields{"mbr_version"}     = $base_image->mbr_version();
386
    $xmlfields{"loadpart"}        = $base_image->loadpart();
387
    $xmlfields{"noexport"}        = $base_image->noexport();
388 389 390 391 392 393 394 395 396

    # Short form uses wholedisk instead. Should fix this. 
    if ($base_image->loadpart() == 0 && $base_image->loadlength() == 4) {
	$xmlfields{"loadpart"}    = 1;
	$xmlfields{"wholedisk"}   = 1;
    }
    elsif ($wholedisk) {
	$xmlfields{"loadpart"}    = 1;
    }
397 398 399 400 401 402 403 404 405 406 407
}
elsif ($isvirtnode) {
    $xmlfields{"reboot_waittime"} = 240;
    $xmlfields{"loadpart"}        = 1;
    $xmlfields{"mtype_pcvm"}      = 1;
    $xmlfields{"wholedisk"}       = 1;
}
else {
    fatal("No base image for $node_id");
}
# This needs more thought.
408
if (($isvirtnode || $base_osinfo) && $base_osinfo->def_parentosid()) {
409 410 411 412
    my $parentosinfo = OSinfo->Lookup($base_osinfo->def_parentosid());
    if (!defined($parentosinfo)) {
	fatal("Could not lookup object for parent osid of $base_osinfo");
    }
413
    $xmlfields{"def_parentosid"} =
414
	$parentosinfo->pid() . "," . $parentosinfo->osname();
415 416

    # And this is just plain bogus. 
417
    #$xmlfields{"mbr_version"} = 99;
418
}
419 420 421 422

#
# Grab the existing type list and generate new mtype_* variables.
#
423 424 425 426 427 428 429
if (defined($base_image)) {
    my @typelist = $base_image->TypeList($base_osinfo);
    if (! @typelist) {
	fatal("$base_image does not run on any types");
    }
    foreach my $type (@typelist) {
	my $type_id = $type->type();
430
    
431 432
	$xmlfields{"mtype_${type_id}"} = 1;
    }
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
}

#
# Create the XML file to pass to newimageid_ez.
#
my ($fh, $filename) = tempfile(UNLINK => 1);
fatal("Could not create temporary file")
    if (!defined($fh));

print $fh "<image>\n";
foreach my $key (keys(%xmlfields)) {
    my $value = $xmlfields{$key};

    print $fh "<attribute name=\"$key\">";
    print $fh "<value>" . CGI::escapeHTML($value) . "</value>";
    print $fh "</attribute>\n";
}
print $fh "</image>\n";
close($fh);

453 454 455 456 457
if ($debug) {
    system("/bin/cat $filename");
}

my $output = emutil::ExecQuiet("$NEWIMAGEEZ -s -v $filename");
458 459
if ($?) {
    print STDERR $output;
460 461
    my $foo = `cat $filename`;
    print STDERR $foo;
462 463 464 465 466 467 468
    fatal("Failed to verify image descriptor from $filename");
}
if ($impotent) {
    print "Not doing anything in impotent mode\n";
    system("cat $filename");
    exit(0);
}
469
$output = emutil::ExecQuiet("$NEWIMAGEEZ -s $filename");
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
if ($?) {
    print STDERR $output;
    my $foo = `cat $filename`;
    print STDERR $foo;
    fatal("Failed to create image descriptor");
}

$image = Image->Lookup($project->pid(), $imagename);
if (!defined($image)) {
    fatal("Cannot lookup newly created image for $imagename");
}
my $osinfo = OSinfo->Lookup($image->imageid());
if (!defined($osinfo)) {
    fatal("Cannot lookup newly created osinfo for $image");
}
485
if ($DOPROVENANCE && $doprovenance) {
486 487 488
    $image->SetProvenance($base_image);
    $osinfo->SetProvenance($base_osinfo);
}
489 490 491 492 493
if ($debug) {
    print "Created $osinfo\n";
    print "Created $image\n";
}
if ($nosnapshot) {
494 495
    print "Not taking a snapshot, as directed\n"
	if ($debug);
496 497
    exit(0);
}
498 499 500
my $opts = "-p $pid ";
$opts   .= "-w " if ($waitmode);
$output  = emutil::ExecQuiet("$CREATEIMAGE $opts $imagename $node_id");
501 502 503 504 505 506 507 508 509 510 511 512 513 514
if ($?) {
    print STDERR $output;
    fatal("Failed to create image");
}
print "Image is being created. This can take 15-30 minutes.\n";
exit(0);

sub fatal($)
{
    my ($mesg) = @_;

    die("*** $0:\n".
	"    $mesg\n");
}