virt-resize: Document guest boot stops at "GRUB" (RHBZ#640961).
[libguestfs.git] / tools / virt-resize
1 #!/usr/bin/perl -w
2 # virt-resize
3 # Copyright (C) 2010 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 use warnings;
20 use strict;
21
22 use Sys::Guestfs;
23 use Sys::Guestfs::Lib qw(feature_available);
24 use Fcntl qw(S_ISREG SEEK_SET);
25 use POSIX qw(floor);
26 use Pod::Usage;
27 use Getopt::Long;
28 use Data::Dumper;
29 use Locale::TextDomain 'libguestfs';
30
31 $Data::Dumper::Sortkeys = 1;
32
33 die __"virt-resize: sorry this program does not work on a 32 bit host\n"
34     if ~1 == 4294967294;
35
36 $| = 1;
37
38 =encoding utf8
39
40 =head1 NAME
41
42 virt-resize - Resize a virtual machine disk
43
44 =head1 SYNOPSIS
45
46  virt-resize [--resize /dev/sdaN=[+/-]<size>[%]]
47    [--expand /dev/sdaN] [--shrink /dev/sdaN]
48    [--ignore /dev/sdaN] [--delete /dev/sdaN] [...] indisk outdisk
49
50 =head1 DESCRIPTION
51
52 Virt-resize is a tool which can resize a virtual machine disk, making
53 it larger or smaller overall, and resizing or deleting any partitions
54 contained within.
55
56 Virt-resize B<cannot> resize disk images in-place.  Virt-resize
57 B<should not> be used on live virtual machines - for consistent
58 results, shut the virtual machine down before resizing it.
59
60 If you are not familiar with the associated tools:
61 L<virt-list-partitions(1)>,
62 L<virt-list-filesystems(1)> and
63 L<virt-df(1)>,
64 we recommend you go and read those manual pages first.
65
66 =head1 EXAMPLES
67
68 Copy C<olddisk> to C<newdisk>, extending one of the guest's partitions
69 to fill the extra 5GB of space.
70
71  truncate -r olddisk newdisk; truncate -s +5G newdisk
72  virt-list-partitions -lht olddisk
73  # Note "/dev/sda2" is a partition inside the "olddisk" file.
74  virt-resize --expand /dev/sda2 olddisk newdisk
75
76 As above, but make the /boot partition 200MB bigger, while giving the
77 remaining space to /dev/sda2:
78
79  virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 olddisk newdisk
80
81 As above, but the output format will be uncompressed qcow2:
82
83  qemu-img create -f qcow2 newdisk.qcow2 15G
84  virt-resize --expand /dev/sda2 olddisk newdisk.qcow2
85
86 =head1 DETAILED USAGE
87
88 =head2 EXPANDING A VIRTUAL MACHINE DISK
89
90 =over 4
91
92 =item 1. Shut down the virtual machine
93
94 =item 2. Locate input disk image
95
96 Locate the input disk image (ie. the file or device on the host
97 containing the guest's disk).  If the guest is managed by libvirt, you
98 can use C<virsh dumpxml> like this to find the disk image name:
99
100  # virsh dumpxml guestname | xpath /domain/devices/disk/source
101  Found 1 nodes:
102  -- NODE --
103  <source dev="/dev/vg/lv_guest" />
104
105 =item 3. Look at current sizing
106
107 Use L<virt-list-partitions(1)> to display the current partitions and
108 sizes:
109
110  # virt-list-partitions -lht /dev/vg/lv_guest
111  /dev/sda1 ext3 101.9M
112  /dev/sda2 pv 7.9G
113  /dev/sda device 8.0G
114
115 (This example is a virtual machine with an 8 GB disk which we would
116 like to expand up to 10 GB).
117
118 =item 4. Create output disk
119
120 Virt-resize cannot do in-place disk modifications.  You have to have
121 space to store the resized output disk.
122
123 To store the resized disk image in a file, create a file of a suitable
124 size:
125
126  # rm -f outdisk
127  # truncate -s 10G outdisk
128
129 Or use L<lvcreate(1)> to create a logical volume:
130
131  # lvcreate -L 10G -n lv_name vg_name
132
133 Or use L<virsh(1)> vol-create-as to create a libvirt storage volume:
134
135  # virsh pool-list
136  # virsh vol-create-as poolname newvol 10G
137
138 =item 5. Resize
139
140 virt-resize takes two mandatory parameters, the input disk (eg. device
141 or file) and the output disk.  The output disk is the one created in
142 the previous step.
143
144  # virt-resize indisk outdisk
145
146 This command just copies disk image C<indisk> to disk image C<outdisk>
147 I<without> resizing or changing any existing partitions.  If
148 C<outdisk> is larger, then an extra, empty partition is created at the
149 end of the disk covering the extra space.  If C<outdisk> is smaller,
150 then it will give an error.
151
152 More realistically you'd want to expand existing partitions in the
153 disk image by passing extra options (for the full list see the
154 L</OPTIONS> section below).
155
156 L</--expand> is the most useful option.  It expands the named
157 partition within the disk to fill any extra space:
158
159  # virt-resize --expand /dev/sda2 indisk outdisk
160
161 (In this case, an extra partition is I<not> created at the end of the
162 disk, because there will be no unused space).
163
164 L</--resize> is the other commonly used option.  The following would
165 increase the size of /dev/sda1 by 200M, and expand /dev/sda2
166 to fill the rest of the available space:
167
168  # virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 \
169      indisk outdisk
170
171 If the expanded partition in the image contains a filesystem or LVM
172 PV, then if virt-resize knows how, it will resize the contents, the
173 equivalent of calling a command such as L<pvresize(8)>,
174 L<resize2fs(8)> or L<ntfsresize(8)>.  However virt-resize does not
175 know how to resize some filesystems, so you would have to online
176 resize them after booting the guest.  And virt-resize also does not
177 resize anything inside an LVM PV, it just resizes the PV itself and
178 leaves the user to resize any LVs inside that PV as desired.
179
180 Other options are covered below.
181
182 =item 6. Test
183
184 Thoroughly test the new disk image I<before> discarding the old one.
185
186 If you are using libvirt, edit the XML to point at the new disk:
187
188  # virsh edit guestname
189
190 Change E<lt>source ...E<gt>, see
191 L<http://libvirt.org/formatdomain.html#elementsDisks>
192
193 Then start up the domain with the new, resized disk:
194
195  # virsh start guestname
196
197 and check that it still works.  See also the L</NOTES> section below
198 for additional information.
199
200 =item 7. Resize LVs etc inside the guest
201
202 (This can also be done offline using L<guestfish(1)>)
203
204 Once the guest has booted you should see the new space available, at
205 least for filesystems that virt-resize knows how to resize, and for
206 PVs.  The user may need to resize LVs inside PVs, and also resize
207 filesystem types that virt-resize does not know how to expand.
208
209 =back
210
211 =head2 SHRINKING A VIRTUAL MACHINE DISK
212
213 Shrinking is somewhat more complex than expanding, and only an
214 overview is given here.
215
216 Firstly virt-resize will not attempt to shrink any partition content
217 (PVs, filesystems).  The user has to shrink content before passing the
218 disk image to virt-resize, and virt-resize will check that the content
219 has been shrunk properly.
220
221 (Shrinking can also be done offline using L<guestfish(1)>)
222
223 After shrinking PVs and filesystems, shut down the guest, and proceed
224 with steps 3 and 4 above to allocate a new disk image.
225
226 Then run virt-resize with any of the C<--shrink> and/or C<--resize>
227 options.
228
229 =head2 IGNORING OR DELETING PARTITIONS
230
231 virt-resize also gives a convenient way to ignore or delete partitions
232 when copying from the input disk to the output disk.  Ignoring a
233 partition speeds up the copy where you don't care about the existing
234 contents of a partition.  Deleting a partition removes it completely,
235 but note that it also renumbers any partitions after the one which is
236 deleted, which can leave some guests unbootable.
237
238 =head2 QCOW2 AND NON-SPARSE RAW FORMATS
239
240 If the input disk is in qcow2 format, then you may prefer that the
241 output is in qcow2 format as well.  Alternately, virt-resize can
242 convert the format on the fly.  The output format is simply determined
243 by the format of the empty output container that you provide.  Thus to
244 create qcow2 output, use:
245
246  qemu-img create [-c] -f qcow2 outdisk [size]
247
248 instead of the truncate command (use C<-c> for a compressed disk).
249
250 Similarly, to get non-sparse raw output use:
251
252  fallocate -l size outdisk
253
254 (on older systems that don't have the L<fallocate(1)> command use
255 C<dd if=/dev/zero of=outdisk bs=1M count=..>)
256
257 =head1 OPTIONS
258
259 =over 4
260
261 =cut
262
263 my $help;
264
265 =item B<--help>
266
267 Display help.
268
269 =cut
270
271 my $version;
272
273 =item B<--version>
274
275 Display version number and exit.
276
277 =cut
278
279 my @resize;
280
281 =item B<--resize part=size>
282
283 Resize the named partition (expanding or shrinking it) so that it has
284 the given size.
285
286 C<size> can be expressed as an absolute number followed by
287 b/K/M/G/T/P/E to mean bytes, Kilobytes, Megabytes, Gigabytes,
288 Terabytes, Petabytes or Exabytes; or as a percentage of the current
289 size; or as a relative number or percentage.  For example:
290
291  --resize /dev/sda2=10G
292
293  --resize /dev/sda4=90%
294
295  --resize /dev/sda2=+1G
296
297  --resize /dev/sda2=-200M
298
299  --resize /dev/sda1=+128K
300
301  --resize /dev/sda1=+10%
302
303  --resize /dev/sda1=-10%
304
305 You can increase the size of any partition.  Virt-resize will expand
306 the direct content of the partition if it knows how (see C<--expand>
307 below).
308
309 You can only I<decrease> the size of partitions that contain
310 filesystems or PVs which have already been shrunk.  Virt-resize will
311 check this has been done before proceeding, or else will print an
312 error (see also C<--resize-force>).
313
314 You can give this option multiple times.
315
316 =cut
317
318 my @resize_force;
319
320 =item B<--resize-force part=size>
321
322 This is the same as C<--resize> except that it will let you decrease
323 the size of any partition.  Generally this means you will lose any
324 data which was at the end of the partition you shrink, but you may not
325 care about that (eg. if shrinking an unused partition, or if you can
326 easily recreate it such as a swap partition).
327
328 See also the C<--ignore> option.
329
330 =cut
331
332 my $expand;
333
334 =item B<--expand part>
335
336 Expand the named partition so it uses up all extra space (space left
337 over after any other resize changes that you request have been done).
338
339 If virt-resize knows how, it will expand the direct content of the
340 partition.  For example, if the partition is an LVM PV, it will expand
341 the PV to fit (like calling L<pvresize(8)>).  Virt-resize leaves any
342 other content it doesn't know about alone.
343
344 Currently virt-resize can resize:
345
346 =over 4
347
348 =item *
349
350 ext2, ext3 and ext4 filesystems when they are contained
351 directly inside a partition.
352
353 =item *
354
355 NTFS filesystems contained directly in a partition, if libguestfs was
356 compiled with support for NTFS.
357
358 The filesystem must have been shut down consistently last time it was
359 used.  Additionally, L<ntfsresize(8)> marks the resized filesystem as
360 requiring a consistency check, so at the first boot after resizing
361 Windows will check the disk.
362
363 =item *
364
365 LVM PVs (physical volumes).  However virt-resize does I<not>
366 resize anything inside the PV.  The user will have to resize
367 LVs as desired.
368
369 =back
370
371 Note that you cannot use C<--expand> and C<--shrink> together.
372
373 =cut
374
375 my $shrink;
376
377 =item B<--shrink part>
378
379 Shrink the named partition until the overall disk image fits in the
380 destination.  The named partition B<must> contain a filesystem or PV
381 which has already been shrunk using another tool (eg. L<guestfish(1)>
382 or other online tools).  Virt-resize will check this and give an error
383 if it has not been done.
384
385 The amount by which the overall disk must be shrunk (after carrying
386 out all other operations requested by the user) is called the
387 "deficit".  For example, a straight copy (assume no other operations)
388 from a 5GB disk image to a 4GB disk image results in a 1GB deficit.
389 In this case, virt-resize would give an error unless the user
390 specified a partition to shrink and that partition had more than a
391 gigabyte of free space.
392
393 Note that you cannot use C<--expand> and C<--shrink> together.
394
395 =cut
396
397 my @ignore;
398
399 =item B<--ignore part>
400
401 Ignore the named partition.  Effectively this means the partition is
402 allocated on the destination disk, but the content is not copied
403 across from the source disk.  The content of the partition will be
404 blank (all zero bytes).
405
406 You can give this option multiple times.
407
408 =cut
409
410 my @delete;
411
412 =item B<--delete part>
413
414 Delete the named partition.  It would be more accurate to describe
415 this as "don't copy it over", since virt-resize doesn't do in-place
416 changes and the original disk image is left intact.
417
418 Note that when you delete a partition, then anything contained in the
419 partition is also deleted.  Furthermore, this causes any partitions
420 that come after to be I<renumbered>, which can easily make your guest
421 unbootable.
422
423 You can give this option multiple times.
424
425 =cut
426
427 my @lv_expand;
428
429 =item B<--LV-expand logvol>
430
431 This takes the logical volume and, as a final step, expands it to fill
432 all the space available in its volume group.  A typical usage,
433 assuming a Linux guest with a single PV C</dev/sda2> and a root device
434 called C</dev/vg_guest/lv_root> would be:
435
436  virt-resize indisk outdisk \
437    --expand /dev/sda2 --LV-expand /dev/vg_guest/lv_root
438
439 This would first expand the partition (and PV), and then expand the
440 root device to fill the extra space in the PV.
441
442 The contents of the LV are also resized if virt-resize knows how to do
443 that.  You can stop virt-resize from trying to expand the content by
444 using the option C<--no-expand-content>.
445
446 Use L<virt-list-filesystems(1)> to list the filesystems in
447 the guest.
448
449 You can give this option multiple times, I<but> it doesn't
450 make sense to do this unless the logical volumes you specify
451 are all in different volume groups.
452
453 =cut
454
455 my $copy_boot_loader = 1;
456
457 =item B<--no-copy-boot-loader>
458
459 By default, virt-resize copies over some sectors at the start of the
460 disk (up to the beginning of the first partition).  Commonly these
461 sectors contain the Master Boot Record (MBR) and the boot loader, and
462 are required in order for the guest to boot correctly.
463
464 If you specify this flag, then this initial copy is not done.  You may
465 need to reinstall the boot loader in this case.
466
467 =cut
468
469 my $extra_partition = 1;
470 my $min_extra_partition = 10 * 1024 * 1024; # see below
471
472 =item B<--no-extra-partition>
473
474 By default, virt-resize creates an extra partition if there is any
475 extra, unused space after all resizing has happened.  Use this option
476 to prevent the extra partition from being created.  If you do this
477 then the extra space will be inaccessible until you run fdisk, parted,
478 or some other partitioning tool in the guest.
479
480 Note that if the surplus space is smaller than 10 MB, no extra
481 partition will be created.
482
483 =cut
484
485 my $expand_content = 1;
486
487 =item B<--no-expand-content>
488
489 By default, virt-resize will try to expand the direct contents
490 of partitions, if it knows how (see C<--expand> option above).
491
492 If you give the C<--no-expand-content> option then virt-resize
493 will not attempt this.
494
495 =cut
496
497 my $debug;
498
499 =item B<-d> | B<--debug>
500
501 Enable debugging messages.
502
503 =cut
504
505 my $dryrun;
506
507 =item B<-n> | B<--dryrun>
508
509 Print a summary of what would be done, but don't do anything.
510
511 =cut
512
513 my $quiet;
514
515 =item B<-q> | B<--quiet>
516
517 Don't print the summary.
518
519 =back
520
521 =cut
522
523 GetOptions ("help|?" => \$help,
524             "version" => \$version,
525             "resize=s" => \@resize,
526             "resize-force=s" => \@resize_force,
527             "expand=s" => \$expand,
528             "shrink=s" => \$shrink,
529             "ignore=s" => \@ignore,
530             "delete=s" => \@delete,
531             "lv-expand=s" => \@lv_expand,
532             "copy-boot-loader!" => \$copy_boot_loader,
533             "extra-partition!" => \$extra_partition,
534             "expand-content!" => \$expand_content,
535             "d|debug" => \$debug,
536             "n|dryrun|dry-run" => \$dryrun,
537             "q|quiet" => \$quiet,
538     ) or pod2usage (2);
539 pod2usage (1) if $help;
540 if ($version) {
541     my $g = Sys::Guestfs->new ();
542     my %h = $g->version ();
543     print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
544     exit
545 }
546
547 die "virt-resize [--options] indisk outdisk\n" unless @ARGV == 2;
548
549 # Check in and out images exist.
550 my $infile = $ARGV[0];
551 my $outfile = $ARGV[1];
552 die __x("virt-resize: {file}: does not exist or is not readable\n", file => $infile)
553     unless -r $infile;
554 die __x("virt-resize: {file}: does not exist or is not writable\nYou have to create the destination disk before running this program.\nPlease read the virt-resize(1) manpage for more information.\n", file => $outfile)
555     unless -w $outfile;
556
557 # Add them to the handle and launch the appliance.
558 my $g;
559 launch_guestfs ();
560
561 sub launch_guestfs
562 {
563     $g = Sys::Guestfs->new ();
564     $g->set_trace (1) if $debug;
565     $g->add_drive_ro ($infile);
566     $g->add_drive ($outfile);
567     $g->set_progress_callback (\&progress_callback) unless $quiet;
568     $g->launch ();
569 }
570
571 my $sectsize = $g->blockdev_getss ("/dev/sdb");
572
573 # Get the size in bytes of each disk.
574 #
575 # Originally we computed this by looking at the same of the host file,
576 # but of course this failed for qcow2 images (RHBZ#633096).  The right
577 # way to do it is with $g->blockdev_getsize64.
578 my $insize = $g->blockdev_getsize64 ("/dev/sda");
579 my $outsize = $g->blockdev_getsize64 ("/dev/sdb");
580
581 if ($debug) {
582     print "$infile size $insize bytes\n";
583     print "$outfile size $outsize bytes\n";
584 }
585
586 # Create a partition table.
587 #
588 # We *must* do this before copying the bootloader across, and copying
589 # the bootloader must be careful not to disturb this partition table
590 # (RHBZ#633766).  There are two reasons for this:
591 #
592 # (1) The 'parted' library is stupid and broken.  In many ways.  In
593 # this particular instance the stupid and broken bit is that it
594 # overwrites the whole boot sector when initializating a partition
595 # table.  (Upstream don't consider this obvious problem to be a bug).
596 #
597 # (2) GPT has a backup partition table located at the end of the disk.
598 # It's non-movable, because the primary GPT contains fixed references
599 # to both the size of the disk and the backup partition table at the
600 # end.  This would be a problem for any resize that didn't either
601 # carefully move the backup GPT (and rewrite those references) or
602 # recreate the whole partition table from scratch.
603
604 my $parttype;
605 create_partition_table ();
606
607 sub create_partition_table
608 {
609     local $_;
610
611     $parttype = $g->part_get_parttype ("/dev/sda");
612     print "partition table type: $parttype\n" if $debug;
613
614     $g->part_init ("/dev/sdb", $parttype);
615 }
616
617 # In reality the number of sectors containing boot loader data will be
618 # less than this (although Windows 7 defaults to putting the first
619 # partition on sector 2048, and has quite a large boot loader).
620 #
621 # However make this large enough to be sure that we have copied over
622 # the boot loader.  We could also do this by looking for the sector
623 # offset of the first partition.
624 #
625 # It doesn't matter if we copy too much.
626 my $max_bootloader = 4096 * 512;
627
628 die __x("virt-resize: {file}: file is too small to be a disk image ({sz} bytes)\n",
629         file => $infile, sz => $insize)
630     if $insize < $max_bootloader;
631 die __x("virt-resize: {file}: file is too small to be a disk image ({sz} bytes)\n",
632         file => $outfile, sz => $outsize)
633     if $outsize < $max_bootloader;
634
635 # Copy the boot loader across.
636 do_copy_boot_loader () if $copy_boot_loader;
637
638 sub do_copy_boot_loader
639 {
640     print "copying boot loader ...\n" if $debug;
641
642     # Don't disturb the partition table that we just wrote.
643     # https://secure.wikimedia.org/wikipedia/en/wiki/Master_Boot_Record
644     # https://secure.wikimedia.org/wikipedia/en/wiki/GUID_Partition_Table
645
646     my $bootsect = $g->pread_device ("/dev/sda", 446, 0);
647     die __"virt-resize: short read" if length ($bootsect) < 446;
648
649     $g->pwrite_device ("/dev/sdb", $bootsect, 0);
650
651     my $start = 512;
652     if ($parttype eq "gpt") {
653         # XXX With 4K sectors does GPT just fit more entries in a
654         # sector, or does it always use 34 sectors?
655         $start = 17408;
656     }
657
658     my $loader = $g->pread_device ("/dev/sda", $max_bootloader, $start);
659     die __"virt-resize: short read" if length ($loader) < $max_bootloader;
660
661     $g->pwrite_device ("/dev/sdb", $loader, $start);
662 }
663
664 my $to_be_expanded = 0;
665
666 # Get the partitions on the source disk.
667 my @partitions;
668 my %partitions;
669 check_source_disk ();
670
671 sub check_source_disk
672 {
673     local $_;
674
675     # Partitions and PVs.
676     my @p = $g->part_list ("/dev/sda");
677     foreach (@p) {
678         my $name = "/dev/sda" . $_->{part_num};
679         push @partitions, $name;
680
681         my %h = %$_;
682         $h{name} = $name;
683         $h{bootable} = $g->part_get_bootable ("/dev/sda", $h{part_num});
684         eval { $h{mbr_id} = $g->part_get_mbr_id ("/dev/sda", $h{part_num}); };
685         $partitions{$name} = \%h;
686     }
687 }
688
689 # Examine each partition.
690 my @pvs_full = $g->pvs_full ();
691 examine_partition ($_) foreach @partitions;
692
693 sub examine_partition
694 {
695     local $_;
696     my $part = shift;
697
698     # What is it?
699     my $type = "unknown";
700     eval {
701         $type = $g->vfs_type ($part);
702     };
703     $partitions{$part}->{type} = $type;
704
705     # Can we get the actual size of this object (ie. to find out if it
706     # is smaller than the container for shrinking)?
707     my $fssize;
708     if ($type eq "LVM2_member") { # LVM PV
709         foreach (@pvs_full) {
710             $fssize = $_->{pv_size}
711               if canonicalize ($_->{pv_name}) eq $part;
712         }
713     } else {                    # Something mountable?
714         eval {
715             $g->mount_ro ($part, "/");
716
717             my %stat = $g->statvfs ("/");
718             $fssize = $stat{bsize} * $stat{blocks};
719         };
720
721         eval {
722             $g->umount_all ();
723         };
724     }
725
726     # This might be undef if we didn't successfully find the size.  In
727     # that case user won't be allowed to shrink this partition except
728     # by forcing it.
729     $partitions{$part}->{fssize} = $fssize;
730
731     # Is it partition content that we know how to expand?
732     $partitions{$part}->{can_expand_content} = 0;
733     if ($expand_content) {
734         if ($type eq "LVM2_member") {
735             $partitions{$part}->{can_expand_content} = 1;
736             $partitions{$part}->{expand_content_method} = "pvresize";
737         } elsif ($type =~ /^ext[234]$/) {
738             $partitions{$part}->{can_expand_content} = 1;
739             $partitions{$part}->{expand_content_method} = "resize2fs";
740         } elsif ($type eq "ntfs" && feature_available ($g, "ntfsprogs")) {
741             $partitions{$part}->{can_expand_content} = 1;
742             $partitions{$part}->{expand_content_method} = "ntfsresize";
743         }
744     }
745 }
746
747 if ($debug) {
748     print "partitions found: ", join (", ", @partitions), "\n";
749     foreach my $part (@partitions) {
750         print "$part:\n";
751         foreach (sort keys %{$partitions{$part}}) {
752             print("\t", $_, " = ",
753                   defined ($partitions{$part}->{$_})
754                   ? $partitions{$part}->{$_} : "undef",
755                   "\n");
756         }
757     }
758 }
759
760 # Examine the LVs (for --lv-expand option).
761 my @lvs = $g->lvs ();
762 my %lvs;
763 examine_lv ($_) foreach @lvs;
764 mark_lvs_to_expand ();
765
766 sub examine_lv
767 {
768     local $_ = shift;
769
770     $lvs{$_}->{name} = $_;
771
772     my $type = "unknown";
773     eval {
774         $type = $g->vfs_type ($_);
775     };
776     $lvs{$_}->{type} = $type;
777
778     if ($expand_content) {
779         if ($type =~ /^ext[234]$/) {
780             $lvs{$_}->{can_expand_content} = 1;
781             $lvs{$_}->{expand_content_method} = "resize2fs";
782         } elsif ($type eq "ntfs" && feature_available ($g, "ntfsprogs")) {
783             $lvs{$_}->{can_expand_content} = 1;
784             $lvs{$_}->{expand_content_method} = "ntfsresize";
785         }
786     }
787 }
788
789 sub mark_lvs_to_expand {
790     local $_;
791
792     foreach (@lv_expand) {
793         die __x("virt-resize: no logical volume called {n}\n",
794                 n => $_)
795             unless exists $lvs{$_};
796
797         if ($lvs{$_}->{can_expand_content}) {
798             $lvs{$_}->{will_expand_content} = 1;
799             $to_be_expanded++;
800         }
801     }
802 }
803
804 sub find_partition
805 {
806     local $_ = shift;
807     my $option = shift;
808
809     $_ = "/dev/$_" unless $_ =~ m{^/dev};
810     $_ = canonicalize ($_);
811
812     unless (exists $partitions{$_}) {
813         die __x("{p}: partition not found in the source disk image, when using the '{opt}' command line option\n",
814                 p => $_,
815                 opt => $option)
816     }
817
818     if ($partitions{$_}->{ignore}) {
819         die __x("{p}: partition ignored, you cannot use it in another command line argument\n",
820                 p => $_)
821     }
822     if ($partitions{$_}->{delete}) {
823         die __x("{p}: partition deleted, you cannot use it in another command line argument\n",
824                 p => $_)
825     }
826
827     return $_;
828 }
829
830 # Handle --ignore.
831 do_ignore ($_) foreach @ignore;
832
833 sub do_ignore
834 {
835     local $_ = shift;
836     $_ = find_partition ($_, "--ignore");
837     $partitions{$_}->{ignore} = 1;
838 }
839
840 # Handle --delete.
841 do_delete ($_) foreach @delete;
842
843 sub do_delete
844 {
845     local $_ = shift;
846     $_ = find_partition ($_, "--delete");
847     $partitions{$_}->{delete} = 1;
848 }
849
850 # Handle --resize and --resize-force.
851 do_resize ($_, 0, "--resize") foreach @resize;
852 do_resize ($_, 1, "--resize-force") foreach @resize_force;
853
854 sub do_resize
855 {
856     local $_ = shift;
857     my $force = shift;
858     my $option = shift;
859
860     # Argument is "part=size" ...
861     my ($part, $sizefield) = split /=/, $_, 2;
862     $part = find_partition ($part, $option);
863
864     if (exists $partitions{$part}->{newsize}) {
865         die __x("{p}: this partition has already been marked for resizing\n",
866                 p => $part);
867     }
868
869     # Parse the size field.
870     my $oldsize = $partitions{$part}->{part_size};
871     my $newsize;
872     if (!defined ($sizefield) || $sizefield eq "") {
873         die __x("{p}: missing size field in {o} option\n",
874                 p => $part, o => $option);
875     } elsif ($sizefield =~ /^([.\d]+)([bKMGTPE])$/) {
876         $newsize = sizebytes ($1, $2);
877     } elsif ($sizefield =~ /^\+([.\d]+)([bKMGTPE])$/) {
878         my $incr = sizebytes ($1, $2);
879         $newsize = $oldsize + $incr;
880     } elsif ($sizefield =~ /^-([.\d]+)([bKMGTPE])$/) {
881         my $decr = sizebytes ($1, $2);
882         $newsize = $oldsize - $decr;
883     } elsif ($sizefield =~ /^([.\d]+)%$/) {
884         $newsize = $oldsize * $1 / 100;
885     } elsif ($sizefield =~ /^\+([.\d]+)%$/) {
886         $newsize = $oldsize + $oldsize * $1 / 100;
887     } elsif ($sizefield =~ /^-([.\d]+)%$/) {
888         $newsize = $oldsize - $oldsize * $1 / 100;
889     } else {
890         die __x("{p}: {f}: cannot parse size field\n",
891                 p => $part, f => $sizefield)
892     }
893
894     $newsize > 0 or
895         die __x("{p}: new size is zero or negative\n", p => $part);
896
897     mark_partition_for_resize ($part, $oldsize, $newsize, $force, $option);
898 }
899
900 sub mark_partition_for_resize
901 {
902     local $_;
903     my $part = shift;
904     my $oldsize = shift;
905     my $newsize = shift;
906     my $force = shift;
907     my $option = shift;
908
909     # Do nothing if the size is the same.
910     return if $oldsize == $newsize;
911
912     my $bigger = $newsize > $oldsize;
913
914     # Check there is space to shrink this.
915     unless ($bigger || $force) {
916         if (! $partitions{$part}->{fssize} ||
917             $partitions{$part}->{fssize} > $newsize) {
918             die __x("{p}: cannot make this partition smaller because it contains a\nfilesystem, physical volume or other content that is larger than the new size.\nYou have to resize the content first, see virt-resize(1).\n",
919                     p => $part);
920         }
921     }
922
923     $partitions{$part}->{newsize} = $newsize;
924
925     if ($partitions{$part}->{can_expand_content} && $bigger) {
926         $partitions{$part}->{will_expand_content} = 1;
927         $to_be_expanded++;
928     }
929 }
930
931 # Handle --expand and --shrink.
932 my $surplus;
933 if (defined $expand && defined $shrink) {
934     die __"virt-resize: you cannot use options --expand and --shrink together\n"
935 }
936 if (defined $expand || defined $shrink) {
937     calculate_surplus ();
938
939     if ($debug) {
940         print "surplus before --expand or --shrink: $surplus (",
941           human_size ($surplus), ")\n";
942     }
943
944     do_expand () if $expand;
945     do_shrink () if $shrink;
946 }
947
948 # (Re-)calculate surplus after doing expand or shrink.
949 calculate_surplus ();
950
951 # Add up the total space required on the target so far, compared
952 # to the size of the target.  We end up with a surplus or deficit.
953 sub calculate_surplus
954 {
955     local $_;
956
957     # We need some overhead for partitioning.  Worst case would be for
958     # EFI partitioning + massive per-partition alignment.
959     my $overhead = $sectsize * (
960         2 * 64 +                   # GPT start and end
961         (64 * (@partitions + 1))   # Maximum alignment
962         ) +
963         ($max_bootloader - 64 * 512); # boot loader
964
965     my $required = 0;
966     foreach (@partitions) {
967         if ($partitions{$_}->{newsize}) {
968             $required += $partitions{$_}->{newsize}
969         } else {
970             $required += $partitions{$_}->{part_size}
971         }
972     }
973
974     # Compare that to the actual target disk.
975     $surplus = $outsize - ($required + $overhead);
976 }
977
978 sub do_expand
979 {
980     local $_;
981
982     unless ($surplus > 0) {
983         die __x("virt-resize: error: cannot use --expand when there is no surplus space to\nexpand into.  You need to make the target disk larger by at least {h}.\n",
984                 h => human_size (-$surplus));
985     }
986
987     my $part = find_partition ($expand, "--expand");
988     my $oldsize = $partitions{$part}->{part_size};
989     mark_partition_for_resize ($part, $oldsize, $oldsize + $surplus,
990                                0, "--expand");
991 }
992
993 sub do_shrink
994 {
995     local $_;
996
997     unless ($surplus < 0) {
998         die __"virt-resize: error: cannot use --shrink because there is no deficit\n(see 'deficit' in the virt-resize(1) man page)\n"
999     }
1000
1001     my $part = find_partition ($shrink, "--shrink");
1002     my $oldsize = $partitions{$part}->{part_size};
1003     mark_partition_for_resize ($part, $oldsize, $oldsize + $surplus,
1004                                0, "--shrink");
1005 }
1006
1007 # Print summary.
1008 print_summary () unless $quiet;
1009
1010 sub print_summary
1011 {
1012     local $_;
1013     print __"Summary of changes:\n";
1014
1015     foreach my $part (@partitions) {
1016         if ($partitions{$part}->{ignore}) {
1017             print __x("{p}: partition will be ignored\n", p => $part);
1018         } elsif ($partitions{$part}->{delete}) {
1019             print __x("{p}: partition will be deleted\n", p => $part);
1020         } elsif ($partitions{$part}->{newsize}) {
1021             print __x("{p}: partition will be resized from {oldsize} to {newsize}\n",
1022                       p => $part,
1023                       oldsize => human_size ($partitions{$part}->{part_size}),
1024                       newsize => human_size ($partitions{$part}->{newsize}));
1025             if ($partitions{$part}->{will_expand_content}) {
1026                 print __x("{p}: content will be expanded using the '{meth}' method\n",
1027                           p => $part,
1028                           meth => $partitions{$part}->{expand_content_method});
1029             }
1030         } else {
1031             print __x("{p}: partition will be left alone\n", p => $part);
1032         }
1033     }
1034
1035     foreach my $lv (@lv_expand) {
1036         print __x("{n}: LV will be expanded to maximum size\n",
1037                   n => $lv);
1038     }
1039
1040     foreach my $lv (@lvs) {
1041         if ($lvs{$lv}->{will_expand_content}) {
1042             print __x("{n}: content will be expanded using the '{meth}' method\n",
1043                       n => $lv,
1044                       meth => $lvs{$lv}->{expand_content_method});
1045         }
1046     }
1047
1048     if ($surplus > 0) {
1049         print __x("There is a surplus of {spl} bytes ({h}).\n",
1050                   spl => $surplus,
1051                   h => human_size ($surplus));
1052         if ($extra_partition) {
1053             if ($surplus >= $min_extra_partition) {
1054                 print __"An extra partition will be created for the surplus.\n";
1055             } else {
1056                 print __"The surplus space is not large enough for an extra partition to be created\nand so it will just be ignored.\n";
1057             }
1058         } else {
1059             print __"The surplus space will be ignored.  Run a partitioning program in the guest\nto partition this extra space if you want.\n";
1060         }
1061     } elsif ($surplus < 0) {
1062         die __x("virt-resize: error: there is a deficit of {def} bytes ({h}).\nYou need to make the target disk larger by at least this amount,\nor adjust your resizing requests.\n",
1063                 def => -$surplus,
1064                 h => human_size (-$surplus));
1065     }
1066 }
1067
1068 exit 0 if $dryrun;
1069
1070 # Repartition the target disk.
1071 my $nextpart = 1;
1072 repartition ();
1073
1074 sub repartition
1075 {
1076     local $_;
1077
1078     # Work out where to start the first partition.
1079     die __"virt-resize: source disk does not have a first partition\n"
1080         unless exists ($partitions{"/dev/sda1"});
1081     my $start = $partitions{"/dev/sda1"}->{part_start} / $sectsize;
1082
1083     # Align to 64.
1084     $start = ($start + 63) & ~63;
1085
1086     print "starting to partition from $start\n" if $debug;
1087
1088     # Create the new partitions.
1089     foreach my $part (@partitions) {
1090         unless ($partitions{$part}->{delete}) {
1091             # Size in sectors.
1092             my $size;
1093             if ($partitions{$part}->{newsize}) {
1094                 $size = ($partitions{$part}->{newsize} + $sectsize - 1)
1095                     / $sectsize;
1096             } else {
1097                 $size = ($partitions{$part}->{part_size} + $sectsize - 1)
1098                     / $sectsize;
1099             }
1100
1101             # Create it.
1102             my ($target, $end, $part_num) = add_partition ($start, $size);
1103             $partitions{$part}->{target} = $target;
1104
1105             if ($partitions{$part}->{bootable}) {
1106                 $g->part_set_bootable ("/dev/sdb", $part_num, 1);
1107             }
1108
1109             if ($partitions{$part}->{mbr_id}) {
1110                 $g->part_set_mbr_id ("/dev/sdb", $part_num,
1111                                      $partitions{$part}->{mbr_id});
1112             }
1113
1114             # Start of next partition + alignment.
1115             $start = $end + 1;
1116             $start = ($start + 63) & ~63;
1117         }
1118     }
1119
1120     # Create surplus partition.
1121     if ($extra_partition && $surplus >= $min_extra_partition) {
1122         add_partition ($start, $outsize / $sectsize - 64 - $start);
1123     }
1124 }
1125
1126 # Add a partition.
1127 sub add_partition
1128 {
1129     local $_;
1130     my $start = shift;
1131     my $size = shift;
1132
1133     my ($target, $end, $part_num);
1134
1135     if ($nextpart <= 3 || $parttype ne "msdos") {
1136         $target = "/dev/sdb$nextpart";
1137         $end = $start + $size - 1;
1138         $g->part_add ("/dev/sdb", "primary", $start, $end);
1139         $part_num = $nextpart++;
1140     } else {
1141         if ($nextpart == 4) {
1142             $g->part_add ("/dev/sdb", "extended", $start, -1);
1143             $part_num = $nextpart++;
1144             $start += 64;
1145         }
1146         $target = "/dev/sdb$nextpart";
1147         $end = $start + $size - 1;
1148         $g->part_add ("/dev/sdb", "logical", $start, $end);
1149         $part_num = $nextpart++;
1150     }
1151
1152     return ($target, $end, $part_num);
1153 }
1154
1155 # Copy over the data.
1156 copy_data ();
1157
1158 sub copy_data
1159 {
1160     foreach my $part (@partitions)
1161     {
1162         unless ($partitions{$part}->{ignore}) {
1163             my $target = $partitions{$part}->{target};
1164             if ($target) {
1165                 my $oldsize = $partitions{$part}->{part_size};
1166                 my $newsize;
1167                 if ($partitions{$part}->{newsize}) {
1168                     $newsize = $partitions{$part}->{newsize};
1169                 } else {
1170                     $newsize = $partitions{$part}->{part_size};
1171                 }
1172
1173                 if (!$quiet && !$debug) {
1174                     print __x("Copying {p} ...\n", p => $part);
1175                 }
1176
1177                 $g->copy_size ($part, $target,
1178                                $newsize < $oldsize ? $newsize : $oldsize);
1179             }
1180         }
1181     }
1182 }
1183
1184 # After copying the data over we must shut down and restart the
1185 # appliance in order to expand the content.  The reason for this may
1186 # not be obvious, but it's because otherwise we'll have duplicate VGs
1187 # (the old VG(s) and the new VG(s)) which breaks LVM.
1188 #
1189 # The restart is only required if we're going to expand something.
1190
1191 if ($to_be_expanded > 0) {
1192     restart_appliance ();
1193     expand_partitions ();
1194     expand_lvs ();
1195     expand_lvs_content ();
1196 }
1197
1198 sub restart_appliance
1199 {
1200     # Sync disk and exit.
1201     $g->umount_all ();
1202     $g->sync ();
1203     undef $g;
1204
1205     $g = Sys::Guestfs->new ();
1206     $g->set_trace (1) if $debug;
1207     $g->add_drive ($outfile);
1208     $g->launch ();
1209
1210     # Target partitions have changed from /dev/sdb to /dev/sda,
1211     # so change them.
1212     foreach my $part (@partitions)
1213     {
1214         my $target = $partitions{$part}->{target};
1215         if ($target) {
1216             if ($target =~ m{/dev/(.)db(.*)}) {
1217                 $partitions{$part}->{target} = "/dev/$1da$2";
1218             } else {
1219                 die "internal error: unexpected partition target: $target";
1220             }
1221         }
1222     }
1223 }
1224
1225 sub expand_partitions
1226 {
1227     foreach my $part (@partitions)
1228     {
1229         unless ($partitions{$part}->{ignore}) {
1230             my $target = $partitions{$part}->{target};
1231             if ($target) {
1232                 # Expand if requested.
1233                 if ($partitions{$part}->{will_expand_content}) {
1234                     if (!$quiet && !$debug) {
1235                         print __x("Expanding {p} using the '{meth}' method\n",
1236                                   p => $part,
1237                                   meth => $partitions{$part}->{expand_content_method});
1238                     }
1239                     expand_target_partition ($part)
1240                 }
1241             }
1242         }
1243     }
1244 }
1245
1246 sub expand_target_partition
1247 {
1248     local $_;
1249     my $part = shift;
1250
1251     # Assertions.
1252     die unless $part;
1253     die unless $partitions{$part}->{can_expand_content};
1254     die unless $partitions{$part}->{will_expand_content};
1255     die unless $partitions{$part}->{expand_content_method};
1256     die unless $partitions{$part}->{target};
1257     die unless $expand_content;
1258
1259     my $target = $partitions{$part}->{target};
1260     my $method = $partitions{$part}->{expand_content_method};
1261     if ($method eq "pvresize") {
1262         $g->pvresize ($target);
1263     }
1264     elsif ($method eq "resize2fs") {
1265         $g->e2fsck_f ($target);
1266         $g->resize2fs ($target);
1267     }
1268     elsif ($method eq "ntfsresize") {
1269         $g->ntfsresize ($target);
1270     }
1271     else {
1272         die "internal error: unknown method: $method";
1273     }
1274 }
1275
1276 sub expand_lvs
1277 {
1278     local $_;
1279
1280     foreach (@lv_expand) {
1281         $g->lvresize_free ($_, 100);
1282     }
1283 }
1284
1285 sub expand_lvs_content
1286 {
1287     local $_;
1288
1289     foreach (@lvs) {
1290         if ($lvs{$_}->{will_expand_content}) {
1291             my $method = $lvs{$_}->{expand_content_method};
1292             if (!$quiet && !$debug) {
1293                 print __x("Expanding {p} using the '{meth}' method\n",
1294                           p => $_, meth => $method);
1295                     }
1296             if ($method eq "resize2fs") {
1297                 $g->e2fsck_f ($_);
1298                 $g->resize2fs ($_);
1299             } elsif ($method eq "ntfsresize") {
1300                 $g->ntfsresize ($_);
1301             } else {
1302                 die "internal error: unknown method: $method";
1303             }
1304         }
1305     }
1306 }
1307
1308 # Sync disk and exit.
1309 $g->umount_all ();
1310 $g->sync ();
1311 undef $g;
1312
1313 exit 0;
1314
1315 sub sizebytes
1316 {
1317     local $_ = shift;
1318     my $unit = shift;
1319
1320     $_ *= 1024 if $unit =~ /[KMGTPE]/;
1321     $_ *= 1024 if $unit =~ /[MGTPE]/;
1322     $_ *= 1024 if $unit =~ /[GTPE]/;
1323     $_ *= 1024 if $unit =~ /[TPE]/;
1324     $_ *= 1024 if $unit =~ /[PE]/;
1325     $_ *= 1024 if $unit =~ /[E]/;
1326
1327     return floor($_);
1328 }
1329
1330 # Convert a number of bytes to a human-readable number.
1331 sub human_size
1332 {
1333     local $_ = shift;
1334
1335     my $sgn = "";
1336     if ($_ < 0) {
1337         $sgn = "-";
1338         $_ = -$_;
1339     }
1340
1341     $_ /= 1024;
1342
1343     if ($_ < 1024) {
1344         sprintf "%s%dK", $sgn, $_;
1345     } elsif ($_ < 1024 * 1024) {
1346         sprintf "%s%.1fM", $sgn, ($_ / 1024);
1347     } else {
1348         sprintf "%s%.1fG", $sgn, ($_ / 1024 / 1024);
1349     }
1350 }
1351
1352 # The reverse of device name translation, see
1353 # BLOCK DEVICE NAMING in guestfs(3).
1354 sub canonicalize
1355 {
1356     local $_ = shift;
1357
1358     if (m{^/dev/[hv]d([a-z]\d)$}) {
1359         return "/dev/sd$1";
1360     }
1361     $_;
1362 }
1363
1364 # Not as sophisticated as the guestfish progress bar, because
1365 # I intend to use an external library for this at some point (XXX).
1366 sub progress_callback
1367 {
1368     my $proc_nr = shift;
1369     my $serial = shift;
1370     my $position = shift;
1371     my $total = shift;
1372
1373     my $ratio = $position / $total;
1374     if ($ratio < 0) { $ratio = 0 }
1375     elsif ($ratio > 1) { $ratio = 1 }
1376
1377     my $dots = int ($ratio * 76);
1378
1379     print "[", "#"x$dots, "-"x(76-$dots), "]\r";
1380     print "\n" if $ratio == 1;
1381 }
1382
1383 =head1 NOTES
1384
1385 =head2 "Partition 1 does not end on cylinder boundary."
1386
1387 Virt-resize aligns partitions to multiples of 64 sectors.  Usually
1388 this means the partitions will not be aligned to the ancient CHS
1389 geometry.  However CHS geometry is meaningless for disks manufactured
1390 since the early 1990s, and doubly so for virtual hard drives.
1391 Alignment of partitions to cylinders is not required by any modern
1392 operating system.
1393
1394 =head2 RESIZING WINDOWS VIRTUAL MACHINES
1395
1396 In Windows Vista and later versions, Microsoft switched to using a
1397 separate boot partition.  In these VMs, typically C</dev/sda1> is the
1398 boot partition and C</dev/sda2> is the main (C:) drive.  We have not
1399 had any luck resizing the boot partition.  Doing so seems to break the
1400 guest completely.  However expanding the second partition (ie. C:
1401 drive) should work.
1402
1403 Windows may initiate a lengthy "chkdsk" on first boot after a resize,
1404 if NTFS partitions have been expanded.  This is just a safety check
1405 and (unless it find errors) is nothing to worry about.
1406
1407 =head2 GUEST BOOT STUCK AT "GRUB"
1408
1409 If a Linux guest does not boot after resizing, and the boot is stuck
1410 after printing C<GRUB> on the console, try reinstalling grub.  This
1411 sometimes happens on older (RHEL 5-era) guests, for reasons we don't
1412 fully understand, although we think is to do with partition alignment.
1413
1414  guestfish -i -a newdisk
1415  ><fs> cat /boot/grub/device.map
1416  # check the contents of this file are sensible or
1417  # edit the file if necessary
1418  ><fs> grub-install / /dev/vda
1419  ><fs> exit
1420
1421 For more flexible guest reconfiguration, including if you need to
1422 specify other parameters to grub-install, use L<virt-rescue(1)>.
1423
1424 =head1 ALTERNATIVE TOOLS
1425
1426 There are several proprietary tools for resizing partitions.  We
1427 won't mention any here.
1428
1429 L<parted(8)> and its graphical shell gparted can do some types of
1430 resizing operations on disk images.  They can resize and move
1431 partitions, but I don't think they can do anything with the contents,
1432 and they certainly don't understand LVM.
1433
1434 L<guestfish(1)> can do everything that virt-resize can do and a lot
1435 more, but at a much lower level.  You will probably end up
1436 hand-calculating sector offsets, which is something that virt-resize
1437 was designed to avoid.  If you want to see the guestfish-equivalent
1438 commands that virt-resize runs, use the C<--debug> flag.
1439
1440 =head1 SHELL QUOTING
1441
1442 Libvirt guest names can contain arbitrary characters, some of which
1443 have meaning to the shell such as C<#> and space.  You may need to
1444 quote or escape these characters on the command line.  See the shell
1445 manual page L<sh(1)> for details.
1446
1447 =head1 SEE ALSO
1448
1449 L<virt-list-partitions(1)>,
1450 L<virt-list-filesystems(1)>,
1451 L<virt-df(1)>,
1452 L<guestfs(3)>,
1453 L<guestfish(1)>,
1454 L<lvm(8)>,
1455 L<pvresize(8)>,
1456 L<lvresize(8)>,
1457 L<resize2fs(8)>,
1458 L<ntfsresize(8)>,
1459 L<virsh(1)>,
1460 L<parted(8)>,
1461 L<truncate(1)>,
1462 L<fallocate(1)>,
1463 L<grub(8)>,
1464 L<grub-install(8)>,
1465 L<virt-rescue(1)>,
1466 L<Sys::Guestfs(3)>,
1467 L<http://libguestfs.org/>.
1468
1469 =head1 AUTHOR
1470
1471 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
1472
1473 =head1 COPYRIGHT
1474
1475 Copyright (C) 2010 Red Hat Inc.
1476
1477 This program is free software; you can redistribute it and/or modify
1478 it under the terms of the GNU General Public License as published by
1479 the Free Software Foundation; either version 2 of the License, or
1480 (at your option) any later version.
1481
1482 This program is distributed in the hope that it will be useful,
1483 but WITHOUT ANY WARRANTY; without even the implied warranty of
1484 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1485 GNU General Public License for more details.
1486
1487 You should have received a copy of the GNU General Public License
1488 along with this program; if not, write to the Free Software
1489 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.