create-versioned-image 20.9 KB
Newer Older
1
#!/usr/bin/perl -w
2 3

#
4
# Copyright (c) 2000-2017 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
# 
# {{{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/>.
# 
# }}}
#

use English;
use Getopt::Std;

#
# Client-side to create a disk image. Caller must have sudo permission!
#
# This is an enhanced version of create-image that knows how to work with
# signature files and created delta images (for versioning). It is a
# separate script just to make backward-compat easier.
#
# In addition, it can create multiple images (e.g., to capture multiple
# partitions or disks) based on the parameters given. Right now these
# parameters are either specified on the command line (allows only
# one image to be made) or come from a file (one line per image to make).
# Eventually, these may come down via tmcc.
#
# For each image, the possible parameters are:
#
# METHOD=<method>
#    Method to use for uploading image and up/downloading any metadata
#    (just old/new signature files right now). Choices are "frisbee" or
#    "file" where file means "across NFS". We may add "http" at some point,
#    but currently that would just be a choice for downloading.
#
# SERVER=<name>
51 52 53 54 55 56 57 58 59 60
#    Server to use for uploading image and metadata. May also be used
#    for downloading metadata if DOWNSERVER is not set. If METHOD=file,
#    SERVER won't be set or will be ignored by the client. Otherwise it
#    is the name or IP to use with the frisupload -S option.
#
# DOWNSERVER=<name>
#    Server to use for downloading metadata.  If not set SERVER is used
#    instead. If METHOD=file, DOWNSERVER won't be set or will be ignored
#    by the client. Otherwise it is the name or IP to use with the
#    frisbee -S option.
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
#
# IMAGENAME=<string>
#    Context-sensitive name of the image to create. If the SERVER is set
#    and we are using the frisbee uploader, then this string is the argument
#    to present via the -F option (either an imageid or a path). If server
#    is not set, then it is a local (NFS) path at which to save the image.
#
# OLDSIGNAME=<string>
# NEWSIGNAME=<string>
#    Optional context-sensitive names of the old and new signature files.
#    OLDSIGNAME is either a path to read from the filesystem (method=file)
#    or the argument to the frisbee -F option. NEWSIGNAME is where to put
#    the newly created signature; either in the FS or uploaded via frisupload.
#
#    If OLDSIGNAME is given, we are creating a delta image. In this case
#    NEWSIGNAME may also be specified if a new signature is desired.
#
#    If OLDSIGNAME is not given, then we are creating a full disk image.
#    In this case we might or might not create a new signature file for
#    the image depending on whether NEWSIGNAME is present.
#
#    If both are absent, then we are running in the old, pre-delta image
#    world and just creating full disk images always.
#
# DISK=<disk>
#    BSD-style disk name (e.g., "da0") identifying the disk from which to
#    create the image. Used for imagezip disk argument. Note that like
#    the argument to loadinfo, this is not at all a sound technique given
#    the differences in device names used by BSD and Linux and the fact
#    that disk ordering is not deterministic in either! However SNs are
#    hard to extract, so we live with a name instead.
#
# PART=<part>
#    Partition on DISK from which to load image. Used for imagezip -s option.
#    If not set or set to zero, then it is a whole-disk image.
#
97 98 99
# IZOPTS=<string>
#    Additional options for imagezip.
#
100 101 102
# PROXY=<vnodeid>
#    The proxy argument for use on XEN, when acting on behalf of a container.
#
Mike Hibler's avatar
Mike Hibler committed
103 104 105 106 107
# BSNAME=<blockstore>
#    The image we are creating is for a local blockstore identified by
#    the given name. Uses the blockstore support library to extract what
#    specific device to image.
#
108 109 110 111 112

sub usage()
{
    print STDERR
	"Usage:\n".
113
	"create-versioned-image [-nvx] -f param-file\n".
114
	"  or\n".
115
	"create-versioned-image [-nv] [-x vnode_id] KEY=VALUE ...\n";
116 117
    exit(-1);
}
118
my  $optlist = "f:nvx:";
119 120 121 122 123 124 125 126 127 128 129 130

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

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

131 132 133 134 135
# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }
use libsetup;
use liblocsetup;

136 137 138
#
# No configure vars.
#
139
my $sudo = "";
140 141 142 143 144 145
my $zipper = "/usr/local/bin/imagezip";
my $uploader = "/usr/local/bin/frisupload";
my $frisbee = "/usr/local/bin/frisbee";
my $localdir = "/local";
my $impotent = 0;
my $verbose = 0;
146
my $isxen   = 0;
147
my $domfs   = 0;
148 149 150

sub process_image($);
sub mysystem($);
151
sub nukemfs();
152 153 154 155 156 157 158 159 160 161 162 163

my %iinfo = ();

#
# Map DB (BSD-ish) disknames into actual /dev device names on
# FreeBSD or Linux.
#
sub map_diskname($)
{
    my ($dev) = @_;
    my ($dtype, $dunit);

164 165 166 167 168 169 170
    #
    # When called on XEN, the diskname is correct, and in fact we will
    # just mess it up.
    #
    return $dev
	if ($isxen);

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    # strip off /dev/ if it is there
    $dev =~ s/^\/dev\///;

    if ($dev =~ /^([-a-zA-Z_]+)(\d+)$/) {
	($dtype,$dunit) = ($1,$2);
    } else {
	return undef;
    }

    # Hack for the Linux MFS: we still use the BSD device
    # names in the database so we try to convert them to
    # the equivalent Linux devices here.  This happens to
    # work at the moment, but if device names change again
    # it could break.
    if ($^O eq 'linux') {
186 187 188 189
	# XXX hack for NVMe
	if ($dtype eq "nvd") {
	    return "/dev/nvme${dunit}n1";
	}
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	$dtype = "sd";
	$dunit -= 4
	    if ($dtype eq 'ad' && $dunit > 3);
	$dunit =~ y/01234567/abcdefgh/;

	#
	# XXX woeful TPM dongle-boot hack.
	# If we are imaging /dev/sda and dmesg reports that
	# that device is write-protected, assume it is the boot dongle
	# and use /dev/sdb instead!
	#
	if ($dunit eq "a") {
	    if (!system("dmesg | fgrep -q '[sda] Write Protect is on'")) {
		print STDERR "WARNING: suspect dongle-booted node, using sdb instead of sda\n";
		$dunit = "b";
	    }
	}
    }

209
    return "/dev/$dtype$dunit";
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
#
# Map a blockstore name to its device and fstype.
#
my $storageconfig = undef;

sub map_bsname($)
{
    my ($bsname) = @_;
    my $bsref;
    my $device;
    require liblocstorage;

    if (!defined($storageconfig)) {
	my @allcmds = ();
	if (getstorageconfig(\@allcmds) != 0) {
	    print STDERR "Error grabbing storage config!\n";
	    goto bad;
	}
	$storageconfig = \@allcmds;
    }
    # Find the blockstore we care about.
    foreach my $ref (@$storageconfig) {
	if ($ref->{'CMD'} eq "SLICE" && $ref->{'VOLNAME'} eq $bsname) {
	    $bsref = $ref;
	    last;
	}
    }
    if (!defined($bsref)) {
	print STDERR "Could not find storage configuration for $bsname\n";
	goto bad;
    }
    #
    # The storage map tells us the device info.
    #
    open(MAP, TMSTORAGEMAP()) or
	die("Could not open the storage map!\n");
    while (<MAP>) {
	if ($_ =~ /^([-\w]+)\s+([-\w\.\/]+)\s+([-\w\.\/]+)\s*/) {
	    if ($1 eq $bsname) {
		$device = $2;
		#
		# Hmm, storagemap does not have the actual mount device,
		# which seems wrong since now I have to figure it out
		# in order to check to see if its mounted.
		#
257
		if ($bsref->{'BSID'} ne "SYSVOL") {
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 292
		    $device = "/dev/mapper/emulab-${bsname}";
		}
		$bsref->{'DEVICE'} = $device;
	    }
	}
    }
    close(MAP);
    if (!defined($device)) {
	print STDERR "Could not find $bsname in the storage map!\n";
	goto bad;
    }
    #
    # Make sure we can unmount now. Remount it, we will unmount again
    # later in process_image.
    #
    if (os_ismounted($device) &&
	os_unmount($bsref->{'MOUNTPOINT'})) {
	print STDERR "Could not unmount $device!\n";
	goto bad;
    }
    my $fstype = liblocstorage::get_fstype($bsref, $device);
    if (os_mount($bsref->{'MOUNTPOINT'})) {
	print STDERR "Could not remount $device!\n";
	goto bad;
    }
    if (!defined($fstype)) {
	print STDERR "Could not determine fstype of $device\n";
	goto bad;
    }
    $bsref->{'FSTYPE'} = $fstype;
    return $bsref;
  bad:
    return undef;
}

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
sub parse_params(@)
{
    my ($method,$iname,$disk,$part,$sigfile,$nsigfile);
    my $errors = 0;

    my $iid = 1;
    foreach my $line (@_) {
	my @kvs = split(' ', $line);
	foreach my $kv (@kvs) {
	    if ($kv =~ /^([-\w]+)=(\S*)$/) {
		my $key = lc($1);
		my $val = $2;

		if ($key eq "method") {
		    if ($val =~ /^(frisbee|file)$/) {
			$iinfo{$iid}{'method'} = $1;
		    } else {
			print STDERR "Bogus METHOD '$val'\n";
			$errors++;
		    }
		    next;
		}
		if ($key eq "server") {
		    $iinfo{$iid}{'server'} = $val;
		    next;
		}
319 320 321 322
		if ($key eq "downserver") {
		    $iinfo{$iid}{'downserver'} = $val;
		    next;
		}
323 324 325 326 327 328 329 330 331 332 333 334
		if ($key eq "imagename") {
		    $iinfo{$iid}{'iname'} = $val;
		    next;
		}
		if ($key eq "disk") {
		    $iinfo{$iid}{'disk'} = map_diskname($val);
		    if (!defined($iinfo{$iid}{'disk'})) {
			print STDERR "Bogus DISK '$val'\n";
			$errors++;
		    }
		    next;
		}
335 336 337 338 339 340 341
		if ($key eq "bsname") {
		    $iinfo{$iid}{'bsref'} = map_bsname($val);
		    if (!defined($iinfo{$iid}{'bsref'})) {
			$errors++;
		    }
		    next;
		}
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
		if ($key eq "part") {
		    if ($val =~ /^(\d+)$/) {
			$iinfo{$iid}{'part'} = $1;
		    } else {
			print STDERR "Bogus PART '$val'\n";
			$errors++;
		    }
		    next;
		}
		if ($key eq "oldsigfile") {
		    $iinfo{$iid}{'sigfile'} = $val;
		    next;
		}
		if ($key eq "newsigfile") {
		    $iinfo{$iid}{'nsigfile'} = $val;
		    next;
		}
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
		if ($key eq "izopts") {
		    #
		    # No spaces in string, so options are encoded; e.g.:
		    #   -N -z 9 -d -a SHA1
		    # would be encoded as:
		    #   N,z=9,d,a=SHA1
		    # We unencode them here.
		    #
		    my $optstr = "";
		    foreach my $opt (split(',', $val)) {
			$optstr .= " -" . join(' ', split('=', $opt));
		    }
		    $iinfo{$iid}{'izopts'} = $optstr;
		    next;
		}
374 375 376 377
		if ($key eq "proxy") {
		    $iinfo{$iid}{'proxy'} = $val;
		    next;
		}
378 379 380 381 382 383 384 385 386 387 388 389 390
	    } else {
		print STDERR "Bogus parameter: '$kv'\n";
		$errors++;
	    }
	}
	$iid++;
    }
    if ($errors) {
	print STDERR "Could not parse all arguments\n";
	exit(2);
    }
}

391 392 393 394 395
#
# If we are running as a user, then we will need sudo
#
if ($EUID != 0) {
    for my $path (qw#/usr/local/bin /usr/bin#) {
396
	if (-e "$path/sudo") {
397 398
	    $sudo = "$path/sudo";
	    last;
399
	}
400
    }
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"v"})) {
    $verbose = 1;
}
417 418 419
if (defined($options{"x"})) {
    $isxen = 1;
}
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
if (defined($options{"f"})) {
    my $pfile = $options{"f"};
    if ($pfile =~ /^(\/tmp\/[-\w\.]+)$/) {
	$pfile = $1;
	if (! -r "$pfile") {
	    print STDERR "Cannot read paramfile '$pfile'\n";
	    exit(1);
	}
    } else {
	print STDERR "Bogus '-f' file name\n";
	exit(1);
    }
    my @params = `cat $pfile`;
    chomp @params;
    parse_params(@params);
} elsif (@ARGV > 0) {
    my $pline = join(' ', @ARGV);
    parse_params($pline);
} else {
    # someday maybe use tmcc to get params

    print STDERR "No parameters given!\n";
    exit(1);
}

#
# Consistency checks
#
my $dofrisbee = 0;
449
my $gotbs = 0;
450
foreach my $iid (sort keys %iinfo) {
451 452 453 454 455
    if (defined($iinfo{$iid}{'disk'})) {
	;
    } elsif (defined($iinfo{$iid}{'bsref'})) {
	$gotbs++;
    } else {
456
	print STDERR "Must specify disk or blockstore\n";
457
	exit(1);
458 459 460 461 462 463
    }
    if (!defined($iinfo{$iid}{'part'})) {
	$iinfo{$iid}{'part'} = 0;
    }
    if (!defined($iinfo{$iid}{'iname'})) {
	print STDERR "Must specify imagename\n";
464
	exit(1);
465
    }
Mike Hibler's avatar
Mike Hibler committed
466 467
    if (!defined($iinfo{$iid}{'method'})) {
	print STDERR "Must specify method\n";
468
	exit(1);
Mike Hibler's avatar
Mike Hibler committed
469
    }
470 471 472 473 474 475 476 477 478
    if ($iinfo{$iid}{'method'} eq "frisbee") {
	$dofrisbee++;
	if (!defined($iinfo{$iid}{'server'})) {
	    print STDERR "Must specify server for frisbee\n";
	    exit(1);
	}
    }
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492
#
# See if we need to create a local tmp dir, possibly as an MFS.
# For a vnode we are running in dom0 and can use local disk.
# For a blockstore we are running in the regular OS and can use /tmp.
# Otherwise, if we are using frisbee we will have to create an MFS.
#
if ($isxen) {
    $localdir = "/capture/" . $options{"x"} . "/frisbee";
} elsif ($gotbs) {
    $localdir = "/tmp/bstore/frisbee";
} elsif ($dofrisbee) {
    $domfs = 1;
}

493 494 495 496 497
#
# For uniformity, all sigfiles are rooted here.
# For method=frisbee, the actual files will be here.
# For method=file, a symlink to the actual file will be here.
#
498
if (! -e $localdir && mysystem("$sudo mkdir -p $localdir")) {
499
    print STDERR "Could not create $localdir\n";
500
    $domfs = 0;
501 502 503 504 505 506 507 508 509 510 511 512 513 514
    exit(1);
}

#
# If any of the images are using frisbee, we need to ensure that we
# have sufficient local storage for old/new signature files.
#
# XXX with our current MBR3 FS size of 16GB, signature files for
# partition images will be around 8MB. For a 500GB disk and full
# disk image, it would be more like 250MB. We need enough space to
# hold up to two of these signature files (old and new). 512MB of
# memory filesystem is too much for our older machines--let's go
# with 64MB for now.
#
515 516 517
if ($domfs) {
    my $MEMFS_SIZE = "64m";

518
    if ($^O eq 'linux') {
519
	if (mysystem("$sudo mount -t tmpfs -o size=$MEMFS_SIZE tmpfs $localdir")) {
520 521 522
	    print STDERR "Could not create $MEMFS_SIZE byte local MFS\n";

	    # XXX try NFS instead
523
	    $domfs = 0;
524 525
	    exit(1);
	}
526
    } else {
527
	if (mysystem("$sudo mdconfig -a -t swap -s $MEMFS_SIZE -u 4") ||
Mike Hibler's avatar
Mike Hibler committed
528
	    mysystem("$sudo newfs -b 8192 -i 25000 -o space /dev/md4") ||
529 530 531 532
	    mysystem("$sudo mount /dev/md4 $localdir")) {
	    print STDERR "Could not create $MEMFS_SIZE byte local MFS\n";

	    # XXX try NFS instead
533
	    $domfs = 0;
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	    exit(1);
	}
    }
}

if (mysystem("$sudo chmod 1777 $localdir")) {
    print STDERR "Could not make $localdir writeable\n";
    exit(1);
}

#
# Process each image
#
foreach my $iid (sort keys %iinfo) {
    process_image($iid);
}

551 552
exit(0);

553 554 555
#
# Get rid of any extra FS.
#
556 557
sub nukemfs()
{
558 559 560 561 562 563 564 565 566
}

sub fetch($$)
{
    my ($iid,$file) = @_;

    my $lfile = "$localdir/$file";
    my $ifile = $iinfo{$iid}{$file};

Mike Hibler's avatar
Mike Hibler committed
567 568 569
    if ($iinfo{$iid}{'method'} eq "file") {
	if (! -r "$ifile" || mysystem("ln -sf $ifile $lfile")) {
	    return -1;
570 571 572
	}
    }

Mike Hibler's avatar
Mike Hibler committed
573
    elsif ($iinfo{$iid}{'method'} eq "frisbee") {
574 575 576 577 578
	my $srv = $iinfo{$iid}{'downserver'};
	if (!defined($srv)) {
	    $srv = $iinfo{$iid}{'server'};
	}

579 580 581 582 583 584
	#
	# Since we are fetching small files into an MFS, there is
	# not much need for lots of buffer memory. We also don't
	# randomize requests.
	#
	my $opts = "-O -W 1 -C 32";
585 586 587
	if (exists($iinfo{$iid}{'proxy'})) {
	    $opts .= " -P " . $iinfo{$iid}{'proxy'};
	}
588
	if (mysystem("$frisbee $opts -B 5 -N -S $srv -F $ifile $lfile")) {
Mike Hibler's avatar
Mike Hibler committed
589 590
	    return -1;
	}
591 592
    }

Mike Hibler's avatar
Mike Hibler committed
593
    return 0;
594 595
}

Mike Hibler's avatar
Mike Hibler committed
596
sub writetest($$)
597 598 599 600 601 602
{
    my ($iid,$file) = @_;

    my $lfile = "$localdir/$file";
    my $ifile = $iinfo{$iid}{$file};

Mike Hibler's avatar
Mike Hibler committed
603 604 605 606 607 608
    #
    # For file (NFS) we make sure we can write the actual file.
    # If it doesn't currently exist, we try creating it (if we do create
    # it, we remove it again to make sure we don't leave turds behind).
    # If all is well, we create a local symlink to use.
    #
609
    if ($iinfo{$iid}{'method'} eq "file") {
Mike Hibler's avatar
Mike Hibler committed
610 611 612 613 614 615 616 617 618 619 620 621 622
	if (! -e "$ifile") {
	    if (!$impotent && !open(FD, ">$ifile")) {
		return -1;
	    }
	    if (!$impotent) {
		close(FD);
		unlink("$ifile");
	    }
	}
	elsif (! -w "$ifile") {
	    return -1;
	}

623
	if (mysystem("ln -sf $ifile $lfile")) {
Mike Hibler's avatar
Mike Hibler committed
624
	    return -1;
625 626 627
	}
    }

Mike Hibler's avatar
Mike Hibler committed
628 629 630 631 632 633 634
    #
    # For frisbee, we just check with the server to ensure we can
    # upload to the file. Note: the local file has to exist for this to
    # work, so we create that.
    #
    elsif ($iinfo{$iid}{'method'} eq "frisbee") {
	my $srv = $iinfo{$iid}{'server'};
635 636 637 638 639
	my $cmd = "$uploader -S $srv -Q $ifile";
	if (exists($iinfo{$iid}{'proxy'})) {
	    $cmd .= " -P " . $iinfo{$iid}{'proxy'};
	}
	$cmd .= " $lfile";
Mike Hibler's avatar
Mike Hibler committed
640 641 642 643 644 645 646 647 648 649 650 651

	if ($impotent) {
	    print STDERR "Would: '$cmd' ...\n";
	} else {
	    if (open(FD, ">$lfile")) {
		close(FD);
	    }
	    print STDERR "Doing: '$cmd' ...\n"
		if ($verbose);
	    my $out = `$cmd`;
	    if ($? != 0 || $out !~ /error=0/) {
		return -1;
652 653 654 655
	    }
	}
    }

Mike Hibler's avatar
Mike Hibler committed
656
    return 0;
657 658 659 660
}

sub process_image($)
{
Mike Hibler's avatar
Mike Hibler committed
661
    my ($iid) = @_;
662
    my $retval = 0;
663 664 665 666 667 668 669 670 671

    if ($verbose) {
	print "Image #$iid:\n";
	foreach my $k (sort keys %{$iinfo{$iid}}) {
	    print "  $k=", $iinfo{$iid}{$k}, "\n";
	}
    }

    #
Mike Hibler's avatar
Mike Hibler committed
672
    # Make sure the image file is writeable.
673
    #
Mike Hibler's avatar
Mike Hibler committed
674 675 676
    if (writetest($iid, "iname")) {
	print STDERR
	    "Cannot write new image '", $iinfo{$iid}{'iname'}, "'\n";
677 678 679 680
	exit(3);
    }

    #
Mike Hibler's avatar
Mike Hibler committed
681
    # Make sure we can write the new sig file if needed.
682
    #
Mike Hibler's avatar
Mike Hibler committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696
    if (exists($iinfo{$iid}{'nsigfile'}) && writetest($iid, "nsigfile")) {
	print STDERR
	    "Cannot write new signature file '",
	    $iinfo{$iid}{'nsigfile'}, "'\n";
	exit(3);
    }

    #
    # Download the current signature file if needed.
    #
    if (exists($iinfo{$iid}{'sigfile'}) && fetch($iid, "sigfile")) {
	print STDERR
	    "Cannot fetch/read signature file '",
	    $iinfo{$iid}{'sigfile'}, "'\n";
697 698 699 700 701 702 703
	exit(3);
    }

    #
    # Fire off the command:
    #
    # file:
704
    #   imagezip $izopts [-s $part] [-H $sigfile] [-U $nsigfile] $disk $ifile
705 706
    #
    # frisbee:
707
    #   imagezip $izopts [-s $part] [-H $sigfile] [-U $localdir/$nsigfile] $disk - | \
708 709 710
    #	    frisupload -S $server -F $ifile -
    #   [ cat $localdir/$nsigfile | frisupload -S $server -F $nsigfile ]
    #
711
    my $cmd = "$sudo $zipper";
Mike Hibler's avatar
Mike Hibler committed
712 713 714
    if ($verbose) {
	$cmd .= " -o";
    }
715 716 717
    if (exists($iinfo{$iid}{'izopts'})) {
	$cmd .= $iinfo{$iid}{'izopts'};
    }
718 719
    if ($iinfo{$iid}{'part'} != 0) {
	$cmd .= " -s " . $iinfo{$iid}{'part'};
720 721
    }
    if (exists($iinfo{$iid}{'sigfile'})) {
722
	$cmd .= " -H $localdir/sigfile";
723 724
    }
    if (exists($iinfo{$iid}{'nsigfile'})) {
725
	$cmd .= " -U $localdir/nsigfile";
726
    }
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
    if (exists($iinfo{$iid}{'bsref'})) {
	my $bsref = $iinfo{$iid}{'bsref'};
	
	$cmd .= " " . $bsref->{'DEVICE'};
	# Must tell imagezip what is in the partition.
	if ($bsref->{'FSTYPE'} eq "ufs") {
	    $cmd .= " -b";
	}
	else {
	    $cmd .= " -l";
	}
    }
    else {
	$cmd .= " " . $iinfo{$iid}{'disk'};
    }
742 743 744 745 746 747

    my $image = $iinfo{$iid}{'iname'};
    if ($iinfo{$iid}{'method'} eq "file") {
	$cmd .= " $image";
    } elsif ($iinfo{$iid}{'method'} eq "frisbee") {
	my $srv = $iinfo{$iid}{'server'};
748 749 750 751

	# use basic shell sleezy trick to capture exit status from imagezip
	$cmd = "( $cmd - || echo \$? > $localdir/imagezip.stat )";

752 753 754 755 756 757
	$cmd .= " | $uploader -S $srv -F $image";
	
	if (exists($iinfo{$iid}{'proxy'})) {
	    $cmd .= " -P " . $iinfo{$iid}{'proxy'};
	}
	$cmd .= " - ";
758 759
    }

760 761 762 763 764 765 766 767
    if (exists($iinfo{$iid}{'bsref'}) &&
	os_ismounted($iinfo{$iid}{'bsref'}->{'DEVICE'}) &&
	os_unmount($iinfo{$iid}{'bsref'}->{'MOUNTPOINT'})) {
	print STDERR "Could not unmount " .
	    $iinfo{$iid}{'bsref'}->{'MOUNTPOINT'} . "!\n";
	exit(4);
    }

768 769 770
    # XXX make sure there is no stale imagezip.stat file
    unlink("$localdir/imagezip.stat");

771 772 773 774 775 776 777 778 779 780 781 782 783
    if (mysystem("$cmd")) {
	$retval = $?;
    }

    if (exists($iinfo{$iid}{'bsref'}) &&
	os_mount($iinfo{$iid}{'bsref'}->{'MOUNTPOINT'})) {
	print STDERR "Could not remount ".
	    $iinfo{$iid}{'bsref'}->{'MOUNTPOINT'} . "!\n";
	exit(6);
    }

    if ($retval || -e "$localdir/imagezip.stat") {
	my $stat = sprintf("0x%04x", $retval);
784 785 786 787 788 789 790
	my $izstat = 0;
	if (-e "$localdir/imagezip.stat") {
	    $izstat = `cat $localdir/imagezip.stat`;
	    chomp($izstat);
	}
	$izstat = sprintf("0x%04x", $izstat);

791
	print STDERR "*** Failed to create image!\n";
792 793 794 795
	print STDERR "    command:   '$cmd'\n";
	print STDERR "    status:    $stat\n";
	print STDERR "    izstatus:  $izstat\n"
	    if ($izstat);
796 797 798 799 800 801
	exit(3);
    }

    if ($iinfo{$iid}{'method'} eq "frisbee" &&
	exists($iinfo{$iid}{'nsigfile'})) {
	$cmd = "$uploader -S " . $iinfo{$iid}{'server'} .
802 803 804 805 806
	    " -F " . $iinfo{$iid}{'nsigfile'};
	if (exists($iinfo{$iid}{'proxy'})) {
	    $cmd .= " -P " . $iinfo{$iid}{'proxy'};
	}
	$cmd .= " $localdir/nsigfile";
807 808 809 810 811 812 813
	if (mysystem("$cmd")) {
	    print STDERR "*** Failed to upload signature for created image!\n";
	    print STDERR "    command: '$cmd'\n";
	    exit(3);
	}
    }

814
    mysystem("$sudo rm $localdir/*");
815 816 817 818 819 820 821 822 823 824 825 826 827 828
}

sub mysystem($)
{
    my ($cmd) = @_;

    if ($impotent) {
	print STDERR "Would: '$cmd' ...\n";
	return 0;
    }
    print STDERR "Doing: '$cmd' ...\n"
	if ($verbose);
    return system($cmd);
}
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

# Get rid of any MFS we created
END {
    if ($domfs) {
	if ($^O eq 'linux') {
	    if (mysystem("$sudo umount $localdir")) {
		print STDERR "WARNING: could not destroy local MFS\n";
	    }
	} else {
	    if (mysystem("$sudo umount $localdir") ||
		mysystem("$sudo mdconfig -d -u 4")) {
		print STDERR "WARNING: could not destroy local MFS\n";
	    }
	}
    }
}