-
Notifications
You must be signed in to change notification settings - Fork 21
/
xen-resize-guest
executable file
·447 lines (337 loc) · 8.09 KB
/
xen-resize-guest
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
43
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
121
122
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/perl -w
=encoding utf8
=head1 NAME
xen-resize-guest - Resize a loopback or LVM based xen guest.
=head1 SYNOPSIS
xen-resize-guest [options]
Help Options:
--help Show help information.
--manual Read the manual for this script.
--version Show the version information and exit.
--verbose Show diagnostic output.
General Options:
--add Specify the amount of space to add, e.g. --add=1gb
--dir Specify the path to the loopback image root.
--force Force the resize to happen without a last-chance delay.
--hostname Specify the hostname of the guest to resize.
=head1 OPTIONS
=over 8
=item B<--add>
Specify the amount of storage to add to the primary disk.
=item B<--dir>
Specify the directory where the loopback files are based.
=item B<--force>
Don't pause for 10 seconds prior to commencing.
=item B<--help>
Show help information.
=item B<--hostname>
Specify the hostname to delete.
=item B<--lvm>
Specify the volume group to use.
=item B<--manual>
Read the manual for this script.
=item B<--version>
Show the version number and exit.
=back
=head1 DESCRIPTION
This tool will ease the resizing of Xen guests, whether they are based
upon loopback files or LVM partitions.
Whilst the process of resizing a guest is pretty simple it can be fiddly
to do the steps correctly in the right order:
1. Shutdown the guest.
2. Unmount the volume, if it is mounted.
3. Add to the space.
4. Check the filesystem.
5. Resize the filesystem.
6. Restart the guest.
More than once I've heard of users making mistakes and breaking their
filesystems; hence this tool.
=head1 AUTHORS
Steve Kemp, http://www.steve.org.uk/
Axel Beckert, http://noone.org/abe/
=head1 LICENSE
Copyright (c) 2005-2009 by Steve Kemp, (c) 2010 by The Xen-Tools
Development Team. All rights reserved.
This module is free software;
you can redistribute it and/or modify it under
the same terms as Perl itself.
The LICENSE file contains the full text of the license.
=cut
use strict;
use English;
use Env;
use Getopt::Long;
use Pod::Usage;
use Text::Template;
use Xen::Tools::Common;
#
# Configuration values read from the command line, or configuration file.
#
my %CONFIG;
#
# Release number.
#
my $RELEASE = '4.6.1';
#
# Find xen toolstack command
#
$CONFIG{ 'xm' } = findXenToolstack();
#
# Read the global configuration file.
#
readConfigurationFile("/etc/xen-tools/xen-tools.conf", \%CONFIG);
#
# Parse the command line arguments.
#
parseCommandLineArguments();
#
# Validate our arguments.
#
testArguments();
#
# The path to the file, or device, to resize.
#
my $path = undef;
if ( $CONFIG{ 'dir' } )
{
#
# Make sure we can find the disk
#
$path =
$CONFIG{ 'dir' } . "/domains/" . $CONFIG{ 'hostname' } . "/disk.img";
if ( !-e $path )
{
print <<EOF;
The disk image for the guest domain $CONFIG{'hostname'} was not found
where we expected it to be:
$path
Please check and try again, or report this as a bug.
EOF
exit 1;
}
#
# Ensure the file is an ext3 filesystem.
#
my $type = `file $path`;
if ( $type !~ /ext3/ )
{
print "Filesystem type is not understood.\n";
print "We only handle ext3 right now!\n";
print "Aborting\n";
exit 1;
}
#
# OK we have the size, we have the image.
#
# Resize with DD
#
print "Preparing to resize image: $path\n";
#
# Allow panic
#
if ( !$CONFIG{ 'force' } )
{
foreach my $i ( 1 .. 10 )
{
my $delay = 10 - $i;
print "Sleeping for $delay seconds to allow cancel\n";
sleep 1;
}
}
print "DO NOT INTERRUPT\n";
my $cmd = "dd if=/dev/zero bs=1M count=$CONFIG{'add'}k >> $path";
system($cmd );
}
elsif ( $CONFIG{ 'lvm' } )
{
#
# Make sure we can find the disk
#
$path = "/dev/" . $CONFIG{ 'lvm' } . "/" . $CONFIG{ 'hostname' } . "-disk";
if ( !-e $path )
{
print <<EOF;
The disk image for the guest domain $CONFIG{'hostname'} was not found
where we expected it to be:
$path
Please check and try again, or report this as a bug.
EOF
exit 1;
}
#
# Ensure the file is an ext3 filesystem.
#
my $type = `head $path | file -`;
if ( $type !~ /ext3/ )
{
print "Filesystem type is not understood.\n";
print "We only handle ext3 right now!\n";
print "Aborting\n";
exit 1;
}
#
# OK we have the size, we have the image.
#
# Resize with DD
#
print "Preparing to resize image: $path\n";
#
# Allow panic
#
if ( !$CONFIG{ 'force' } )
{
foreach my $i ( 1 .. 10 )
{
my $delay = 10 - $i;
print "Sleeping for $delay seconds to allow cancel\n";
sleep 1;
}
}
print "DO NOT INTERRUPT\n";
my $cmd = "lvextend -L+$CONFIG{'add'}M $path";
system($cmd );
}
else
{
print "Unknown storage type. (Neither LVM nor loopback.)\n";
exit 1;
}
#
# Check filesystem.
#
print "Checking filesystem\n";
system("fsck.ext3 -f $path");
#
# Run ext3resize.
#
print "Resizing in place\n";
system("resize2fs $path");
#
# Job done.
#
print "All done\n";
exit 0;
=begin doc
Parse the command line arguments this script was given.
=end doc
=cut
sub parseCommandLineArguments
{
my $HELP = 0;
my $MANUAL = 0;
my $VERSION = 0;
#
# Local variables.
#
my %install;
#
# Parse options.
#
GetOptions(
# Misc. options
"add=s", \$CONFIG{ 'add' },
"dir=s", \$install{ 'dir' },
"lvm=s", \$install{ 'lvm' },
"hostname=s", \$CONFIG{ 'hostname' },
"force", \$CONFIG{ 'force' },
# Help options
"help", \$HELP,
"manual", \$MANUAL,
"verbose", \$CONFIG{ 'verbose' },
"version", \$VERSION
);
pod2usage(1) if $HELP;
pod2usage( -verbose => 2 ) if $MANUAL;
if ($VERSION)
{
print("xen-resize-guest release $RELEASE\n");
exit 1;
}
#
# Setup mutually exclusive options in such a way that
# they will allow the configuration values to be overriden by
# the command line.
#
if ( $install{ 'lvm' } )
{
$CONFIG{ 'lvm' } = $install{ 'lvm' };
$CONFIG{ 'dir' } = undef;
delete $CONFIG{ 'dir' };
}
if ( $install{ 'dir' } )
{
$CONFIG{ 'dir' } = $install{ 'dir' };
$CONFIG{ 'lvm' } = undef;
delete $CONFIG{ 'lvm' };
}
}
=begin doc
Test our arguments are complete.
=end doc
=cut
sub testArguments
{
#
# Make sure we received a hostname.
#
if ( !$CONFIG{ 'hostname' } )
{
print <<EOF;
A hostname is mandatory!
Please specify one with --hostname=some.host.name
EOF
exit 1;
}
#
# Make sure we received a size.
#
if ( !$CONFIG{ 'add' } )
{
print <<EOF;
Please specify a size to increase the guest by:
--add=1Gb -> Increase the size of the disk image by 1Gb.
--add=1 -> Increase the size of the disk image by 1Mb.
EOF
exit 1;
}
#
# Make sure the guest isn't running
#
if ( xenRunning( $CONFIG{ 'hostname' }, \%CONFIG ) )
{
print "The guest $CONFIG{'hostname'} appears to be running!\n";
exit 1;
}
#
# We should either have LVM *or* directory - not neither or both.
#
my $options = 0;
$options += 1
if ( defined( $CONFIG{ 'lvm' } ) && length( $CONFIG{ 'lvm' } ) );
$options += 1
if ( defined( $CONFIG{ 'dir' } ) && length( $CONFIG{ 'dir' } ) );
#
# Report
#
if ( $options == 0 )
{
print "Please specify one of --lvm or --dir\n";
exit 1;
}
if ( $options > 1 )
{
print "Please specify only one of --lvm or --dir - not both!\n";
exit 1;
}
#
# Convert from Gb -> Mb;
#
if ( $CONFIG{ 'add' } =~ /^([0-9.]+)Gb*$/i )
{
$CONFIG{ 'add' } = $1 * 1024;
}
if ( $CONFIG{ 'add' } =~ /^([0-9.]+)Mb*$/i )
{
$CONFIG{ 'add' } = $1;
}
}