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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
6 7 8
# All rights reserved.
#

9 10 11 12
use English;
use Getopt::Std;

#
13
# Client-side to create a disk image. Caller must have sudo permission!
14 15 16
#
sub usage()
{
17
    print STDOUT "Usage: create-image [-S image-server] [-F imageid] [-s slice] <device file> <filename>\n";
18 19
    exit(-1);
}
20
my  $optlist = "F:S:s:";
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#
# 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.
#
36
my $sudo;
37
my $zipper = "/usr/local/bin/imagezip";
38
my $uploader = "/usr/local/bin/frisupload";
39 40 41 42
my $slice  = "";
my $device;
my $filename;

43 44 45 46 47 48 49
for my $path (qw#/usr/local/bin /usr/bin#) {
	if (-e "$path/sudo") {
		$sudo = "$path/sudo";
		last;
	}
}

50 51 52 53
# Frisbee master server params
my $iserver = "boss";	# XXX
my $imageid;

54 55 56 57 58 59 60 61 62 63 64 65
#
# 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();
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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'");
    }
}

83 84 85
if (defined($options{"s"})) {
    my $num = $options{"s"};

86 87 88 89 90
    if ($num =~ /(\d)/) {
	$num = $1;
    }
    else {
	die("Tainted slice number: $num");
91 92 93
    }
    
    $slice = "-s $num";
94 95 96 97

    # XXX for now we do not generate relocation info on slices
    # XXX there are still some issues with LILO/GRUB
    $slice = "-N $slice";
98 99
}
$device   = $ARGV[0];
100 101 102 103 104
if (defined($imageid)) {
    $filename = "-";
} else {
    $filename = $ARGV[1];
}
105 106 107 108 109 110 111 112 113

#
# Untaint the arguments.
#
# Note different taint check (allow /).
if ($device =~ /^([-\w.\/]+)$/) {
    $device = $1;
}
else {
114
    die("Tainted device name: $device");
115
}
116
if ($filename =~ /^([-\w.\/\+]+)$/) {
117 118 119
    $filename = $1;
}
else {
120
    die("Tainted output filename: $filename");
121 122
}

123 124 125 126 127 128
# 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.

129
if ($^O eq 'linux') {
130 131 132 133 134 135 136 137
	$device =~ m#/dev/(\D+)(\d+)#;
	($dtype, $dunit) = ($1, $2);
	$dunit -= 4 if ($dtype eq 'ad' && $dunit > 3);
	$dunit =~ y/01234567/abcdefgh/;

	$device = "/dev/sd$dunit";
}

138 139 140 141 142 143 144 145
#
# If imageid is defined, we use the frisbee uploader.
#
my $cmd = "$zipper $slice $device $filename";
if (defined($imageid)) {
    $cmd .= " | $uploader -S $iserver -F $imageid -";
}

146 147 148 149 150
#
# 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.
#
151
if (system("$sudo $cmd")) {
152
    print STDERR "*** Failed to create image!\n";
153
    print STDERR "    command: '$sudo $cmd'\n";
154 155 156 157
    exit 1;
}

exit 0;