create-image 7.72 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
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
# 
# {{{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/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25
#

26 27 28 29
use English;
use Getopt::Std;

#
30
# Client-side to create a disk image. Caller must have sudo permission!
31 32 33
#
sub usage()
{
34 35
    print STDOUT "Usage: create-image [-S image-server] [-F imageid] ".
	"[-s slice] [-b bsname | <device file>] [<filename>]\n";
36 37
    exit(-1);
}
38 39 40 41 42 43
my  $optlist = "F:S:s:b:";

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }
use libsetup;
use liblocsetup;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

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

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

#
# No configure vars.
#
59
my $sudo = "";
60
my $zipper = "/usr/local/bin/imagezip";
61
my $uploader = "/usr/local/bin/frisupload";
62
my $xenscript = "/usr/local/bin/create-xen-image";
63
my $slice  = "";
64
my $slicenum = 0;
65 66
my $device;
my $filename;
67 68 69
my $bsname;
my $bsref;
my $exitval = 0;
70

71 72 73 74 75
#
# If we are running as a user, then we will need sudo
#
if ($EUID != 0) {
    for my $path (qw#/usr/local/bin /usr/bin#) {
76
	if (-e "$path/sudo") {
77 78
	    $sudo = "$path/sudo";
	    last;
79
	}
80
    }
81 82
}

83 84 85 86 87 88 89 90 91 92 93 94 95
#
# A newer server side is going to invoke this script for XEN nodes, to be
# backwards compatible with older XEN client sides that had its own version
# of create-image. It is now called create-xen-image, so call that script,
# which conveniently is argument compatible with this script. This test for
# the file is kinda bogus, but this script does not include libsetup, which
# hides that. Not sure why we do not include libsetup (ask Mike).
#
if ($^O eq 'linux' && -e "/etc/emulab/genvmtype") {
    exec $xenscript, @ARGV;
    die("Could not exec $xenscript");
}

96 97 98 99
# Frisbee master server params
my $iserver = "boss";	# XXX
my $imageid;

100 101 102 103 104 105 106 107
#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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'");
    }
}
124
if (defined($options{"s"})) {
125
    $slicenum = $options{"s"};
126

127 128
    if ($slicenum =~ /(\d)/) {
	$slicenum = $1;
129 130
    }
    else {
131
	die("Tainted slice number: $slicenum");
132 133
    }
    
134
    $slice = "-s $slicenum";
135 136 137 138

    # XXX for now we do not generate relocation info on slices
    # XXX there are still some issues with LILO/GRUB
    $slice = "-N $slice";
139
}
140 141 142 143
if (defined($options{"b"})) {
    $bsname = $options{"b"};
    require liblocstorage;
    require liblocsetup;
144 145
}
else {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    #
    # Normal imaging operation.
    #
    usage()
	if (!@ARGV);

    $device = shift(@ARGV);

    # Untaint the arguments.
    if ($device =~ /^([-\w.\/]+)$/) {
	$device = $1;
    }
    else {
	die("Tainted device name: $device");
    }
161
}
162 163
if (defined($imageid)) {
    $filename = "-";
164 165
}
else {
166 167 168 169
    usage()
	if (!@ARGV);

    $filename = shift(@ARGV);
170

171 172 173 174 175 176 177 178
    # Untaint.
    if ($filename =~ /^([-\w.\/\+]+)$/) {
	$filename = $1;
    }
    else {
	die("Tainted output filename: $filename");
    }
}
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
if (defined($bsname)) {
    #
    # Taking a snapshot of a blockstore. We need to find the info for
    # the blockstore so we know the device and mount point, etc. 
    #
    my @allcmds = ();
    if (getstorageconfig(\@allcmds) != 0) {
	die("Error grabbing storage config!\n");
    }
    # Find the blockstore we care about.
    foreach my $ref (@allcmds) {
	if ($ref->{'CMD'} eq "SLICE" && $ref->{'VOLNAME'} eq $bsname) {
	    $bsref = $ref;
	    last;
	}
    }
    if (!defined($bsref)) {
	die("Could not find storage configuration for $bsname\n");
    }
    #
    # 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.
		#
213
		if ($bsref->{'BSID'} ne "SYSVOL") {
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
		    $device = "/dev/mapper/emulab-${bsname}";
		}
		$bsref->{'DEVICE'} = $device;
	    }
	}
    }
    close(MAP);
    if (!defined($device)) {
	die("Could not find $bsname in the storage map!\n");
    }
    #
    # Need to unmount the FS so we can take the snapshot.
    #
    if (os_ismounted($device)) {
	os_unmount($bsref->{'MOUNTPOINT'}) == 0 or
	    die("Could not unmount $device!\n");
    }
    my $fstype = liblocstorage::get_fstype($bsref, $device);
    if (!defined($fstype)) {
	os_mount($bsref->{'MOUNTPOINT'});
	die("Could not determine fstype of $device\n");
    }
    # Hacky, cause the blockstore filesystems do not have stub MBRs.
    if ($fstype eq "ufs") {
	$slice = "-b";
    }
    else {
	$slice = "-l";
    }
}
else {
    # 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') {
251 252
	$device =~ m#/dev/(\D+)(\d+)#;
	($dtype, $dunit) = ($1, $2);
253 254 255 256 257 258

	# XXX hack for NVMe
	if ($dtype eq "nvd") {
	    return "/dev/nvme${dunit}n1";
	}

259 260 261
	$dunit -= 4 if ($dtype eq 'ad' && $dunit > 3);
	$dunit =~ y/01234567/abcdefgh/;

262 263 264 265 266 267 268 269 270 271 272 273 274
	#
	# 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";
	    }
	}

275
	$device = "/dev/sd$dunit";
276
    }
277 278
}

279 280 281
#
# If imageid is defined, we use the frisbee uploader.
#
282
my $cmd = "$sudo $zipper $slice $device $filename";
283
if (defined($imageid)) {
284 285 286
    # use basic shell sleezy trick to capture exit status from imagezip
    $cmd = "( $cmd || echo \$? > /tmp/imagezip.stat )";

287 288 289
    $cmd .= " | $uploader -S $iserver -F $imageid -";
}

290 291
print STDERR "Command: '$cmd'\n";

292 293 294 295 296
#
# 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.
#
297 298 299 300 301 302 303 304 305
if (system("$cmd") || -e "/tmp/imagezip.stat") {
    my $stat = sprintf("0x%04x", $?);
    my $izstat = 0;
    if (-e "/tmp/imagezip.stat") {
	$izstat = `cat /tmp/imagezip.stat`;
	chomp($izstat);
    }
    $izstat = sprintf("0x%04x", $izstat);

306
    print STDERR "*** Failed to create image!\n";
307 308 309 310
    print STDERR "    command:   '$cmd'\n";
    print STDERR "    status:    $stat\n";
    print STDERR "    izstatus:  $izstat\n"
	if ($izstat);
311
    $exitval = 1;
312
}
313 314 315 316 317 318
if (defined($bsref) &&
    os_mount($bsref->{'MOUNTPOINT'})) {
    print STDERR "Could not remount " . $bsref->{'MOUNTPOINT'} . "\n";
    exit(6);
}
exit $exitval;