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

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

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use English;
use Getopt::Std;

#
# Create a disk image. Caller must have sudo permission!
#
sub usage()
{
    print STDOUT "Usage: create-image [-s slice] <device file> <filename>\n";
    exit(-1);
}
my  $optlist = "rs:";

#
# 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.
#
my $sudo   = "/usr/local/bin/sudo";
my $zipper = "/usr/local/bin/imagezip";
my $slice  = "";
my $device;
my $filename;

#
# 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();
}

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

57
58
59
60
61
    if ($num =~ /(\d)/) {
	$num = $1;
    }
    else {
	die("Tainted slice number: $num");
62
63
64
    }
    
    $slice = "-s $num";
65
66
67
68

    # XXX for now we do not generate relocation info on slices
    # XXX there are still some issues with LILO/GRUB
    $slice = "-N $slice";
69
70
71
72
73
74
75
76
77
78
79
80
}
$device   = $ARGV[0];
$filename = $ARGV[1];

#
# Untaint the arguments.
#
# Note different taint check (allow /).
if ($device =~ /^([-\w.\/]+)$/) {
    $device = $1;
}
else {
81
    die("Tainted device name: $device");
82
}
83
if ($filename =~ /^([-\w.\/\+]+)$/) {
84
85
86
    $filename = $1;
}
else {
87
    die("Tainted output filename: $filename");
88
89
90
91
92
93
94
95
96
97
98
99
100
}

#
# 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 $zipper $slice $device $filename")) {
    print STDERR "*** Failed to create image!";
    exit 1;
}

exit 0;