create-image 4.86 KB
Newer Older
1 2 3 4 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
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
# 
# {{{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;
use strict;

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

my $VNODESETUP	= "$BINDIR/vnodesetup";
my $CAPTURE	= "$BINDIR/capturevm.pl";
33
my $EXTRAFS	= "/capture";
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 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
my $TAR         = "/bin/tar";

#
# Client-side to create a disk image. Caller must have sudo permission!
# This is the OpenVZ specific version. 
#
sub usage()
{
    print STDOUT "Usage: create-image [-S image-server] [-F imageid] ".
	"<vnodeid> <filename>\n";
    exit(-1);
}
my  $optlist = "F:S:";

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

# Need this for predicates.
use libsetup;
use libvnode;
use libvnode_xen;

#
# No configure vars.
#
my $sudo;
my $zipper   = "/usr/local/bin/imagezip";
my $uploader = "/usr/local/etc/emulab/frisupload";
my $vnodeid;
my $filename;
my $error    = 0;

for my $path (qw#/usr/local/bin /usr/bin#) {
	if (-e "$path/sudo") {
		$sudo = "$path/sudo";
		last;
	}
}

# Frisbee master server params
my $iserver = "boss";	# XXX
my $imageid;

#
# 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 (@ARGV != 2) {
    usage();
}

if (defined($options{"S"})) {
    $iserver = $options{"S"};
    if ($iserver =~ /^([-\w\.]+)$/) {
	$iserver = $1;
    } else {
	die("Bad -S hostname: '$iserver'");
    }
}
if (defined($options{"F"})) {
    $imageid = $options{"F"};
    if ($imageid =~ /^(\S+)$/) {
	$imageid = $1;
    } else {
	die("Bad -F imageid: '$imageid'");
    }
}

$vnodeid = $ARGV[0];
if (defined($imageid)) {
    $filename = "-";
} else {
    $filename = $ARGV[1];
}

#
# Untaint the arguments.
#
# Note different taint check (allow /).
if ($filename =~ /^([-\w.\/\+]+)$/) {
    $filename = $1;
}
else {
    die("Tainted output filename: $filename");
}

sub domainStatus($)
{
    my ($id) = @_;

    my $status = `$sudo xm list --long $id 2>/dev/null`;
    if ($status =~ /\(state ([\w-]+)\)/) {
	return $1;
    }
    return "";
}

#
# Check contaner status. If it is running, we need to stop it,
# but first set it up to run "prepare" on the way down.
#
# XEN: Any status means it is running and needs to halted.
#
my $status = domainStatus($vnodeid);

#
# Boss has already arranged for prepare to run; we cannot exec a
# command inside the container from here, which is pretty dumb.
# The VM should halt on its own, so we wait for a bit and if it
# does not happen, we call it an error.
#
if ($status ne "") {
    for (my $i = 20; $i >= 0; $i--) {
	sleep(5);
	$status = domainStatus($vnodeid);
	if ($status eq "") {
	    $status = "stopped";
	    last;
	}
	print STDERR "Container is still running. Waiting ...\n";
    }
    if ($status ne "stopped") {
	die("Container would not stop!\n");
    }
}

#
# Use capture.pl; there are potentially multiple disks, the kernel,
# and a config file. All of this goes into a directory which we then
# tar up and upload. 
#
system("$sudo $CAPTURE $vnodeid");
if ($?) {
    print STDERR "Failed to capture the container!\n";
    $error = 1;
    goto cleanup;
}
Leigh B Stoller's avatar
Leigh B Stoller committed
177
if (! -e "$EXTRAFS/$vnodeid" || ! -e "$EXTRAFS/$vnodeid/xm.conf") {
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    print STDERR "$EXTRAFS/$vnodeid appears to be missing or incomplete\n";
    $error = 1;
    goto cleanup;
}

#
# If imageid is defined, we use the frisbee uploader.
#
my $cmd = "$TAR zcf - -C $EXTRAFS/$vnodeid . | $zipper -f - $filename";
if (defined($imageid)) {
    $cmd .= " | $uploader -S $iserver -F $imageid";
    if (SHAREDHOST()) {
	$cmd .= " -P $vnodeid";
    }
    $cmd .= " -";
}

#
# Run the command using sudo, since by definition only testbed users
# with proper trust should be able to zip up a disk. sudo will fail
# if the user is not in the proper group.
#
if (system("$sudo $cmd")) {
    print STDERR "*** Failed to create image!\n";
    print STDERR "    command: '$sudo $cmd'\n";
    $error = 1;
}

cleanup:
# Clean up the directory.
system("$sudo /bin/rm -rf $EXTRAFS/$vnodeid");

#
# Reboot the vnode.
#
system("$sudo $VNODESETUP -jbVt $vnodeid");
if ($?) {
    die("Could not restart container!\n");
}
exit($error);