clone_image.in 7.83 KB
Newer Older
1
2
3
4
5
6
7
8
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
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use strict;
use Getopt::Std;
use Data::Dumper;
use File::Temp qw(tempfile);
use CGI;

#
# Clone an image (descriptor) from a node and then snapshot
# that node into the descriptor. Creates the descriptor if
# if it does not exist. The idea is to use all of the info
# from the current image descriptor that is loaded on the node
# to quickly create a new descriptor by inheriting all of the
# attributes of the original.
#
# We also want to support taking a snapshot of a previously
# created clone. To make everything work properly, require
# that the imagename exist in the experiment project, which
# ensures that we are operating on a clone, not an image in
# some other project or a system image.
#
sub usage()
{
    print("Usage: clone_image [-d] [-e] [-n | -s] <imagename> <node_id>\n".
	  "Options:\n".
	  "       -d     Turn on debug mode\n".
	  "       -e     Create a whole disk image\n".
	  "       -s     Create descriptor but do not snapshot\n".
	  "       -n     Impotent mode\n");
    exit(-1);
}
my $optlist     = "dens";
my $debug       = 0;
my $wholedisk   = 0;
my $impotent    = 0;
my $nosnapshot  = 0;
43
my $isvirtnode  = 0;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

#
# Configure variables
#
my $TB           = "@prefix@";
my $PROJROOT     = "@PROJROOT_DIR@";
my $CREATEIMAGE  = "$TB/bin/create_image";
my $NEWIMAGEEZ   = "$TB/bin/newimageid_ez";

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

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

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use EmulabConstants;
use emutil;
use User;
use Project;
use Image;
use OSinfo;
use Node;

# Protos
sub fatal($);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"e"})) {
    $wholedisk = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"s"})) {
    $nosnapshot = 1;
}
usage()
    if (@ARGV != 2);

my $imagename = shift(@ARGV);
my $node_id   = shift(@ARGV);

#
# Map invoking user to object. 
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}

#
# The node must of course be allocated and the user must have
# permission to clone it. 
#
my $node = Node->Lookup($node_id);
if (!defined($node)) {
    fatal("No such node");
}
121
122
$isvirtnode = $node->isvirtnode();

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
if (!$node->AccessCheck($this_user, TB_NODEACCESS_LOADIMAGE())) {
    fatal("Not enough permission");
}
my $experiment = $node->Reservation();
if (!defined($experiment)) {
    fatal("Node is not reserved");
}
my $pid     = $experiment->pid();
my $group   = $experiment->GetGroup();
my $project = $experiment->GetProject();
if (! (defined($project) && defined($group))) {
    fatal("Could not get project/group for $experiment");
}
my $image = Image->Lookup($project->pid(), $imagename);

#
# The simple case is that the descriptor already exists. So it is just
# a simple snapshot to the image file. 
#
if (defined($image)) {
    #
    # Only EZ images via this interface.
    #
    if (!$image->ezid()) {
	fatal("Cannot clone a non-ez image");
    }
    
    #
    # The access check above determines if the caller has permission
    # to overwrite the image file. 
    # Not that this matters, cause create_image is going to make the
    # same checks.
    #
    # But we do not allow emulab-ops images to ever be overwritten.
    # Might remove this later. Just being careful since this is going
    # to be used from the ProtoGENI RPC interface.
    #
    if ($image->pid eq TBOPSPID()) {
	fatal("Not allowed to snapshot a system image");
    }
    if ($impotent) {
	print "Not doing anything in impotent mode\n";
	exit(0);
    }
    if ($nosnapshot) {
168
169
	print "Not taking a snapshot, as directed\n"
	    if ($debug);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	exit(0);
    }

    my $output = emutil::ExecQuiet("$CREATEIMAGE -p $pid $imagename $node_id");
    if ($?) {
	print STDERR $output;
	fatal("Failed to create image");
    }
    print "Image is being created. This can take 15-30 minutes.\n";
    exit(0);
}

#
# Need to look up the base image; the image that is currently running
# on the node and being cloned.
#
my ($base_osinfo, $base_image) = $node->RunningOsImage();
187
if (! (defined($base_osinfo))) {
188
189
    fatal("Could not determine osid/imageid for $node_id");
}
190
191
print "$node_id is running $base_osinfo" .
    (defined($base_image) ? ",$base_image" : "") . "\n"
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    if ($debug);

#
# Create the image descriptor. We use the backend script to do the
# heavy lifting, but we have to cons up an XML file based on the image
# descriptor that is being cloned.
#
# These are the fields we have to come up with, plus a number
# of mtype_* entries.
#
my %xmlfields =
    ("imagename"	=> $imagename,
     "pid"		=> $project->pid(),
     "gid"		=> $experiment->gid(),
206
     "description"	=> $base_osinfo->description(),
207
208
209
210
211
212
213
     "OS"		=> $base_osinfo->OS(),
     "version"		=> $base_osinfo->version(),
     "path"		=> "$PROJROOT/$pid/images/${imagename}.ndz",
     "osfeatures",	=> $base_osinfo->osfeatures(),
     "op_mode",		=> $base_osinfo->op_mode(),
     "wholedisk",	=> $wholedisk,
);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
if (defined($base_image)) {
    $xmlfields{"mbr_version"}     = $base_image->mbr_version();
    $xmlfields{"reboot_waittime"} = $base_image->reboot_waittime();
    $xmlfields{"loadpart"}        = $base_image->loadpart();
}
elsif ($isvirtnode) {
    $xmlfields{"reboot_waittime"} = 240;
    $xmlfields{"loadpart"}        = 1;
    $xmlfields{"mtype_pcvm"}      = 1;
    $xmlfields{"wholedisk"}       = 1;
}
else {
    fatal("No base image for $node_id");
}
# This needs more thought.
if ($isvirtnode) {
    $xmlfields{"def_parentosid"} =
	$base_osinfo->pid() . "," . $base_osinfo->osname();
}
233
234
235
236

#
# Grab the existing type list and generate new mtype_* variables.
#
237
238
239
240
241
242
243
if (defined($base_image)) {
    my @typelist = $base_image->TypeList($base_osinfo);
    if (! @typelist) {
	fatal("$base_image does not run on any types");
    }
    foreach my $type (@typelist) {
	my $type_id = $type->type();
244
    
245
246
	$xmlfields{"mtype_${type_id}"} = 1;
    }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
}

#
# Create the XML file to pass to newimageid_ez.
#
my ($fh, $filename) = tempfile(UNLINK => 1);
fatal("Could not create temporary file")
    if (!defined($fh));

print $fh "<image>\n";
foreach my $key (keys(%xmlfields)) {
    my $value = $xmlfields{$key};

    print $fh "<attribute name=\"$key\">";
    print $fh "<value>" . CGI::escapeHTML($value) . "</value>";
    print $fh "</attribute>\n";
}
print $fh "</image>\n";
close($fh);

267
my $output = emutil::ExecQuiet("$NEWIMAGEEZ -v $filename");
268
269
270
271
272
273
274
275
276
if ($?) {
    print STDERR $output;
    fatal("Failed to verify image descriptor from $filename");
}
if ($impotent) {
    print "Not doing anything in impotent mode\n";
    system("cat $filename");
    exit(0);
}
277
$output = emutil::ExecQuiet("$NEWIMAGEEZ $filename");
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
if ($?) {
    print STDERR $output;
    my $foo = `cat $filename`;
    print STDERR $foo;
    fatal("Failed to create image descriptor");
}

$image = Image->Lookup($project->pid(), $imagename);
if (!defined($image)) {
    fatal("Cannot lookup newly created image for $imagename");
}
my $osinfo = OSinfo->Lookup($image->imageid());
if (!defined($osinfo)) {
    fatal("Cannot lookup newly created osinfo for $image");
}
if ($debug) {
    print "Created $osinfo\n";
    print "Created $image\n";
}
if ($nosnapshot) {
298
299
    print "Not taking a snapshot, as directed\n"
	if ($debug);
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    exit(0);
}
$output = emutil::ExecQuiet("$CREATEIMAGE -p $pid $imagename $node_id");
if ($?) {
    print STDERR $output;
    fatal("Failed to create image");
}
print "Image is being created. This can take 15-30 minutes.\n";
exit(0);

sub fatal($)
{
    my ($mesg) = @_;

    die("*** $0:\n".
	"    $mesg\n");
}