create-image 4.67 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3

#
4
# Copyright (c) 2000-2014 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 B. Stoller's avatar
Leigh B. 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
    print STDOUT "Usage: create-image [-S image-server] [-F imageid] [-s slice] <device file> <filename>\n";
35 36
    exit(-1);
}
37
my  $optlist = "F:S:s:";
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

#
# 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.
#
53
my $sudo = "";
54
my $zipper = "/usr/local/bin/imagezip";
55
my $uploader = "/usr/local/bin/frisupload";
56 57 58 59
my $slice  = "";
my $device;
my $filename;

60 61 62 63 64
#
# If we are running as a user, then we will need sudo
#
if ($EUID != 0) {
    for my $path (qw#/usr/local/bin /usr/bin#) {
65
	if (-e "$path/sudo") {
66 67
	    $sudo = "$path/sudo";
	    last;
68
	}
69
    }
70 71
}

72 73 74 75
# Frisbee master server params
my $iserver = "boss";	# XXX
my $imageid;

76 77 78 79 80 81 82 83 84 85 86 87
#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 2) {
    usage();
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
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'");
    }
}

105 106 107
if (defined($options{"s"})) {
    my $num = $options{"s"};

108 109 110 111 112
    if ($num =~ /(\d)/) {
	$num = $1;
    }
    else {
	die("Tainted slice number: $num");
113 114 115
    }
    
    $slice = "-s $num";
116 117 118 119

    # XXX for now we do not generate relocation info on slices
    # XXX there are still some issues with LILO/GRUB
    $slice = "-N $slice";
120 121
}
$device   = $ARGV[0];
122 123 124 125 126
if (defined($imageid)) {
    $filename = "-";
} else {
    $filename = $ARGV[1];
}
127 128 129 130 131 132 133 134 135

#
# Untaint the arguments.
#
# Note different taint check (allow /).
if ($device =~ /^([-\w.\/]+)$/) {
    $device = $1;
}
else {
136
    die("Tainted device name: $device");
137
}
138
if ($filename =~ /^([-\w.\/\+]+)$/) {
139 140 141
    $filename = $1;
}
else {
142
    die("Tainted output filename: $filename");
143 144
}

145 146 147 148 149 150
# 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.

151
if ($^O eq 'linux') {
152 153 154 155 156
	$device =~ m#/dev/(\D+)(\d+)#;
	($dtype, $dunit) = ($1, $2);
	$dunit -= 4 if ($dtype eq 'ad' && $dunit > 3);
	$dunit =~ y/01234567/abcdefgh/;

157 158 159 160 161 162 163 164 165 166 167 168 169
	#
	# 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";
	    }
	}

170 171 172
	$device = "/dev/sd$dunit";
}

173 174 175
#
# If imageid is defined, we use the frisbee uploader.
#
176
my $cmd = "$sudo $zipper $slice $device $filename";
177
if (defined($imageid)) {
178 179 180
    # use basic shell sleezy trick to capture exit status from imagezip
    $cmd = "( $cmd || echo \$? > /tmp/imagezip.stat )";

181 182 183
    $cmd .= " | $uploader -S $iserver -F $imageid -";
}

184 185 186 187 188
#
# 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.
#
189 190 191 192 193 194 195 196 197
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);

198
    print STDERR "*** Failed to create image!\n";
199 200 201 202
    print STDERR "    command:   '$cmd'\n";
    print STDERR "    status:    $stat\n";
    print STDERR "    izstatus:  $izstat\n"
	if ($izstat);
203 204 205 206
    exit 1;
}

exit 0;