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

#
4
# Copyright (c) 2000-2012 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
    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 65 66
for my $path (qw#/usr/local/bin /usr/bin#) {
	if (-e "$path/sudo") {
		$sudo = "$path/sudo";
		last;
	}
}

67 68 69 70
# Frisbee master server params
my $iserver = "boss";	# XXX
my $imageid;

71 72 73 74 75 76 77 78 79 80 81 82
#
# 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();
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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'");
    }
}

100 101 102
if (defined($options{"s"})) {
    my $num = $options{"s"};

103 104 105 106 107
    if ($num =~ /(\d)/) {
	$num = $1;
    }
    else {
	die("Tainted slice number: $num");
108 109 110
    }
    
    $slice = "-s $num";
111 112 113 114

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

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

140 141 142 143 144 145
# 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.

146
if ($^O eq 'linux') {
147 148 149 150 151
	$device =~ m#/dev/(\D+)(\d+)#;
	($dtype, $dunit) = ($1, $2);
	$dunit -= 4 if ($dtype eq 'ad' && $dunit > 3);
	$dunit =~ y/01234567/abcdefgh/;

152 153 154 155 156 157 158 159 160 161 162 163 164
	#
	# 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";
	    }
	}

165 166 167
	$device = "/dev/sd$dunit";
}

168 169 170 171 172 173 174 175
#
# If imageid is defined, we use the frisbee uploader.
#
my $cmd = "$zipper $slice $device $filename";
if (defined($imageid)) {
    $cmd .= " | $uploader -S $iserver -F $imageid -";
}

176 177 178 179 180
#
# 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.
#
181
if (system("$sudo $cmd")) {
182
    print STDERR "*** Failed to create image!\n";
183
    print STDERR "    command: '$sudo $cmd'\n";
184 185 186 187
    exit 1;
}

exit 0;