3 # Copyright (C) 2010 Red Hat Inc.
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.
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.
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.
23 use Sys::Guestfs::Lib qw(feature_available);
24 use Fcntl qw(S_ISREG SEEK_SET);
29 use Locale::TextDomain 'libguestfs';
31 $Data::Dumper::Sortkeys = 1;
33 die __"virt-resize: sorry this program does not work on a 32 bit host\n"
42 virt-resize - Resize a virtual machine disk
46 virt-resize [--resize /dev/sdaN=[+/-]<size>[%]]
47 [--expand /dev/sdaN] [--shrink /dev/sdaN]
48 [--ignore /dev/sdaN] [--delete /dev/sdaN] [...] indisk outdisk
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
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.
60 If you are not familiar with the associated tools:
61 L<virt-list-partitions(1)>,
62 L<virt-list-filesystems(1)> and
64 we recommend you go and read those manual pages first.
68 Copy C<olddisk> to C<newdisk>, extending one of the guest's partitions
69 to fill the extra 5GB of space.
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
76 As above, but make the /boot partition 200MB bigger, while giving the
77 remaining space to /dev/sda2:
79 virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 olddisk newdisk
81 As above, but the output format will be uncompressed qcow2:
83 qemu-img create -f qcow2 newdisk.qcow2 15G
84 virt-resize --expand /dev/sda2 olddisk newdisk.qcow2
88 =head2 EXPANDING A VIRTUAL MACHINE DISK
92 =item 1. Shut down the virtual machine
94 =item 2. Locate input disk image
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:
100 # virsh dumpxml guestname | xpath /domain/devices/disk/source
103 <source dev="/dev/vg/lv_guest" />
105 =item 3. Look at current sizing
107 Use L<virt-list-partitions(1)> to display the current partitions and
110 # virt-list-partitions -lht /dev/vg/lv_guest
111 /dev/sda1 ext3 101.9M
115 (This example is a virtual machine with an 8 GB disk which we would
116 like to expand up to 10 GB).
118 =item 4. Create output disk
120 Virt-resize cannot do in-place disk modifications. You have to have
121 space to store the resized output disk.
123 To store the resized disk image in a file, create a file of a suitable
127 # truncate -s 10G outdisk
129 Or use L<lvcreate(1)> to create a logical volume:
131 # lvcreate -L 10G -n lv_name vg_name
133 Or use L<virsh(1)> vol-create-as to create a libvirt storage volume:
136 # virsh vol-create-as poolname newvol 10G
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
144 # virt-resize indisk outdisk
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.
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).
156 L</--expand> is the most useful option. It expands the named
157 partition within the disk to fill any extra space:
159 # virt-resize --expand /dev/sda2 indisk outdisk
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).
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:
168 # virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 \
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.
180 Other options are covered below.
184 Thoroughly test the new disk image I<before> discarding the old one.
186 If you are using libvirt, edit the XML to point at the new disk:
188 # virsh edit guestname
190 Change E<lt>source ...E<gt>, see
191 L<http://libvirt.org/formatdomain.html#elementsDisks>
193 Then start up the domain with the new, resized disk:
195 # virsh start guestname
197 and check that it still works. See also the L</NOTES> section below
198 for additional information.
200 =item 7. Resize LVs etc inside the guest
202 (This can also be done offline using L<guestfish(1)>)
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.
211 =head2 SHRINKING A VIRTUAL MACHINE DISK
213 Shrinking is somewhat more complex than expanding, and only an
214 overview is given here.
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.
221 (Shrinking can also be done offline using L<guestfish(1)>)
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.
226 Then run virt-resize with any of the C<--shrink> and/or C<--resize>
229 =head2 IGNORING OR DELETING PARTITIONS
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.
238 =head2 QCOW2 AND NON-SPARSE RAW FORMATS
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:
246 qemu-img create [-c] -f qcow2 outdisk [size]
248 instead of the truncate command (use C<-c> for a compressed disk).
250 Similarly, to get non-sparse raw output use:
252 fallocate -l size outdisk
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=..>)
275 Display version number and exit.
281 =item B<--resize part=size>
283 Resize the named partition (expanding or shrinking it) so that it has
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:
291 --resize /dev/sda2=10G
293 --resize /dev/sda4=90%
295 --resize /dev/sda2=+1G
297 --resize /dev/sda2=-200M
299 --resize /dev/sda1=+128K
301 --resize /dev/sda1=+10%
303 --resize /dev/sda1=-10%
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>
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>).
314 You can give this option multiple times.
320 =item B<--resize-force part=size>
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).
328 See also the C<--ignore> option.
334 =item B<--expand part>
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).
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.
344 Currently virt-resize can resize:
350 ext2, ext3 and ext4 filesystems when they are contained
351 directly inside a partition.
355 NTFS filesystems contained directly in a partition, if libguestfs was
356 compiled with support for NTFS.
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.
365 LVM PVs (physical volumes). However virt-resize does I<not>
366 resize anything inside the PV. The user will have to resize
371 Note that you cannot use C<--expand> and C<--shrink> together.
377 =item B<--shrink part>
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.
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.
393 Note that you cannot use C<--expand> and C<--shrink> together.
399 =item B<--ignore part>
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).
406 You can give this option multiple times.
412 =item B<--delete part>
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.
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
423 You can give this option multiple times.
429 =item B<--LV-expand logvol>
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:
436 virt-resize indisk outdisk \
437 --expand /dev/sda2 --LV-expand /dev/vg_guest/lv_root
439 This would first expand the partition (and PV), and then expand the
440 root device to fill the extra space in the PV.
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>.
446 Use L<virt-list-filesystems(1)> to list the filesystems in
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.
455 my $copy_boot_loader = 1;
457 =item B<--no-copy-boot-loader>
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.
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.
469 my $extra_partition = 1;
470 my $min_extra_partition = 10 * 1024 * 1024; # see below
472 =item B<--no-extra-partition>
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.
480 Note that if the surplus space is smaller than 10 MB, no extra
481 partition will be created.
485 my $expand_content = 1;
487 =item B<--no-expand-content>
489 By default, virt-resize will try to expand the direct contents
490 of partitions, if it knows how (see C<--expand> option above).
492 If you give the C<--no-expand-content> option then virt-resize
493 will not attempt this.
499 =item B<-d> | B<--debug>
501 Enable debugging messages.
507 =item B<-n> | B<--dryrun>
509 Print a summary of what would be done, but don't do anything.
515 =item B<-q> | B<--quiet>
517 Don't print the summary.
523 =item B<--format> raw
525 Specify the format of the input disk image. If this flag is not
526 given then it is auto-detected from the image itself.
528 If working with untrusted raw-format guest disk images, you should
529 ensure the format is always specified.
531 Note that this option I<does not> affect the output format.
532 See L</QCOW2 AND NON-SPARSE RAW FORMATS>.
538 =item B<--output-format> raw
540 Specify the format of the output disk image. If this flag is not
541 given then it is auto-detected from the image itself.
543 If working with untrusted raw-format guest disk images, you should
544 ensure the format is always specified.
546 Note that you still need to create the output disk with the right
547 format. See L</QCOW2 AND NON-SPARSE RAW FORMATS>.
553 GetOptions ("help|?" => \$help,
554 "version" => \$version,
555 "resize=s" => \@resize,
556 "resize-force=s" => \@resize_force,
557 "expand=s" => \$expand,
558 "shrink=s" => \$shrink,
559 "ignore=s" => \@ignore,
560 "delete=s" => \@delete,
561 "lv-expand=s" => \@lv_expand,
562 "copy-boot-loader!" => \$copy_boot_loader,
563 "extra-partition!" => \$extra_partition,
564 "expand-content!" => \$expand_content,
565 "d|debug" => \$debug,
566 "n|dryrun|dry-run" => \$dryrun,
567 "q|quiet" => \$quiet,
568 "format=s" => \$format,
569 "output-format=s" => \$output_format,
571 pod2usage (1) if $help;
573 my $g = Sys::Guestfs->new ();
574 my %h = $g->version ();
575 print "$h{major}.$h{minor}.$h{release}$h{extra}\n";
579 die "virt-resize [--options] indisk outdisk\n" unless @ARGV == 2;
581 # Check in and out images exist.
582 my $infile = $ARGV[0];
583 my $outfile = $ARGV[1];
584 die __x("virt-resize: {file}: does not exist or is not readable\n", file => $infile)
586 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)
589 # Add them to the handle and launch the appliance.
595 $g = Sys::Guestfs->new ();
596 $g->set_trace (1) if $debug;
597 my @args = ($infile);
598 push @args, readonly => 1;
599 push @args, format => $format if defined $format;
600 $g->add_drive_opts (@args);
602 push @args, format => $output_format if defined $output_format;
603 $g->add_drive_opts (@args);
604 $g->set_progress_callback (\&progress_callback) unless $quiet;
608 my $sectsize = $g->blockdev_getss ("/dev/sdb");
610 # Get the size in bytes of each disk.
612 # Originally we computed this by looking at the same of the host file,
613 # but of course this failed for qcow2 images (RHBZ#633096). The right
614 # way to do it is with $g->blockdev_getsize64.
615 my $insize = $g->blockdev_getsize64 ("/dev/sda");
616 my $outsize = $g->blockdev_getsize64 ("/dev/sdb");
619 print "$infile size $insize bytes\n";
620 print "$outfile size $outsize bytes\n";
623 # Create a partition table.
625 # We *must* do this before copying the bootloader across, and copying
626 # the bootloader must be careful not to disturb this partition table
627 # (RHBZ#633766). There are two reasons for this:
629 # (1) The 'parted' library is stupid and broken. In many ways. In
630 # this particular instance the stupid and broken bit is that it
631 # overwrites the whole boot sector when initializating a partition
632 # table. (Upstream don't consider this obvious problem to be a bug).
634 # (2) GPT has a backup partition table located at the end of the disk.
635 # It's non-movable, because the primary GPT contains fixed references
636 # to both the size of the disk and the backup partition table at the
637 # end. This would be a problem for any resize that didn't either
638 # carefully move the backup GPT (and rewrite those references) or
639 # recreate the whole partition table from scratch.
642 create_partition_table ();
644 sub create_partition_table
648 $parttype = $g->part_get_parttype ("/dev/sda");
649 print "partition table type: $parttype\n" if $debug;
651 $g->part_init ("/dev/sdb", $parttype);
654 # In reality the number of sectors containing boot loader data will be
655 # less than this (although Windows 7 defaults to putting the first
656 # partition on sector 2048, and has quite a large boot loader).
658 # However make this large enough to be sure that we have copied over
659 # the boot loader. We could also do this by looking for the sector
660 # offset of the first partition.
662 # It doesn't matter if we copy too much.
663 my $max_bootloader = 4096 * 512;
665 die __x("virt-resize: {file}: file is too small to be a disk image ({sz} bytes)\n",
666 file => $infile, sz => $insize)
667 if $insize < $max_bootloader;
668 die __x("virt-resize: {file}: file is too small to be a disk image ({sz} bytes)\n",
669 file => $outfile, sz => $outsize)
670 if $outsize < $max_bootloader;
672 # Copy the boot loader across.
673 do_copy_boot_loader () if $copy_boot_loader;
675 sub do_copy_boot_loader
677 print "copying boot loader ...\n" if $debug;
679 # Don't disturb the partition table that we just wrote.
680 # https://secure.wikimedia.org/wikipedia/en/wiki/Master_Boot_Record
681 # https://secure.wikimedia.org/wikipedia/en/wiki/GUID_Partition_Table
683 my $bootsect = $g->pread_device ("/dev/sda", 446, 0);
684 die __"virt-resize: short read" if length ($bootsect) < 446;
686 $g->pwrite_device ("/dev/sdb", $bootsect, 0);
689 if ($parttype eq "gpt") {
690 # XXX With 4K sectors does GPT just fit more entries in a
691 # sector, or does it always use 34 sectors?
695 my $loader = $g->pread_device ("/dev/sda", $max_bootloader, $start);
696 die __"virt-resize: short read" if length ($loader) < $max_bootloader;
698 $g->pwrite_device ("/dev/sdb", $loader, $start);
701 my $to_be_expanded = 0;
703 # Get the partitions on the source disk.
706 check_source_disk ();
708 sub check_source_disk
712 # Partitions and PVs.
713 my @p = $g->part_list ("/dev/sda");
715 my $name = "/dev/sda" . $_->{part_num};
716 push @partitions, $name;
720 $h{bootable} = $g->part_get_bootable ("/dev/sda", $h{part_num});
721 eval { $h{mbr_id} = $g->part_get_mbr_id ("/dev/sda", $h{part_num}); };
722 $partitions{$name} = \%h;
726 # Examine each partition.
727 my @pvs_full = $g->pvs_full ();
728 examine_partition ($_) foreach @partitions;
730 sub examine_partition
736 my $type = "unknown";
738 $type = $g->vfs_type ($part);
740 $partitions{$part}->{type} = $type;
742 # Can we get the actual size of this object (ie. to find out if it
743 # is smaller than the container for shrinking)?
745 if ($type eq "LVM2_member") { # LVM PV
746 foreach (@pvs_full) {
747 $fssize = $_->{pv_size}
748 if canonicalize ($_->{pv_name}) eq $part;
750 } else { # Something mountable?
752 $g->mount_ro ($part, "/");
754 my %stat = $g->statvfs ("/");
755 $fssize = $stat{bsize} * $stat{blocks};
763 # This might be undef if we didn't successfully find the size. In
764 # that case user won't be allowed to shrink this partition except
766 $partitions{$part}->{fssize} = $fssize;
768 # Is it partition content that we know how to expand?
769 $partitions{$part}->{can_expand_content} = 0;
770 if ($expand_content) {
771 if ($type eq "LVM2_member") {
772 $partitions{$part}->{can_expand_content} = 1;
773 $partitions{$part}->{expand_content_method} = "pvresize";
774 } elsif ($type =~ /^ext[234]$/) {
775 $partitions{$part}->{can_expand_content} = 1;
776 $partitions{$part}->{expand_content_method} = "resize2fs";
777 } elsif ($type eq "ntfs" && feature_available ($g, "ntfsprogs")) {
778 $partitions{$part}->{can_expand_content} = 1;
779 $partitions{$part}->{expand_content_method} = "ntfsresize";
785 print "partitions found: ", join (", ", @partitions), "\n";
786 foreach my $part (@partitions) {
788 foreach (sort keys %{$partitions{$part}}) {
789 print("\t", $_, " = ",
790 defined ($partitions{$part}->{$_})
791 ? $partitions{$part}->{$_} : "undef",
797 # Examine the LVs (for --lv-expand option).
798 my @lvs = $g->lvs ();
800 examine_lv ($_) foreach @lvs;
801 mark_lvs_to_expand ();
807 $lvs{$_}->{name} = $_;
809 my $type = "unknown";
811 $type = $g->vfs_type ($_);
813 $lvs{$_}->{type} = $type;
815 if ($expand_content) {
816 if ($type =~ /^ext[234]$/) {
817 $lvs{$_}->{can_expand_content} = 1;
818 $lvs{$_}->{expand_content_method} = "resize2fs";
819 } elsif ($type eq "ntfs" && feature_available ($g, "ntfsprogs")) {
820 $lvs{$_}->{can_expand_content} = 1;
821 $lvs{$_}->{expand_content_method} = "ntfsresize";
826 sub mark_lvs_to_expand {
829 foreach (@lv_expand) {
830 die __x("virt-resize: no logical volume called {n}\n",
832 unless exists $lvs{$_};
834 if ($lvs{$_}->{can_expand_content}) {
835 $lvs{$_}->{will_expand_content} = 1;
846 $_ = "/dev/$_" unless $_ =~ m{^/dev};
847 $_ = canonicalize ($_);
849 unless (exists $partitions{$_}) {
850 die __x("{p}: partition not found in the source disk image, when using the '{opt}' command line option\n",
855 if ($partitions{$_}->{ignore}) {
856 die __x("{p}: partition ignored, you cannot use it in another command line argument\n",
859 if ($partitions{$_}->{delete}) {
860 die __x("{p}: partition deleted, you cannot use it in another command line argument\n",
868 do_ignore ($_) foreach @ignore;
873 $_ = find_partition ($_, "--ignore");
874 $partitions{$_}->{ignore} = 1;
878 do_delete ($_) foreach @delete;
883 $_ = find_partition ($_, "--delete");
884 $partitions{$_}->{delete} = 1;
887 # Handle --resize and --resize-force.
888 do_resize ($_, 0, "--resize") foreach @resize;
889 do_resize ($_, 1, "--resize-force") foreach @resize_force;
897 # Argument is "part=size" ...
898 my ($part, $sizefield) = split /=/, $_, 2;
899 $part = find_partition ($part, $option);
901 if (exists $partitions{$part}->{newsize}) {
902 die __x("{p}: this partition has already been marked for resizing\n",
906 # Parse the size field.
907 my $oldsize = $partitions{$part}->{part_size};
909 if (!defined ($sizefield) || $sizefield eq "") {
910 die __x("{p}: missing size field in {o} option\n",
911 p => $part, o => $option);
912 } elsif ($sizefield =~ /^([.\d]+)([bKMGTPE])$/) {
913 $newsize = sizebytes ($1, $2);
914 } elsif ($sizefield =~ /^\+([.\d]+)([bKMGTPE])$/) {
915 my $incr = sizebytes ($1, $2);
916 $newsize = $oldsize + $incr;
917 } elsif ($sizefield =~ /^-([.\d]+)([bKMGTPE])$/) {
918 my $decr = sizebytes ($1, $2);
919 $newsize = $oldsize - $decr;
920 } elsif ($sizefield =~ /^([.\d]+)%$/) {
921 $newsize = $oldsize * $1 / 100;
922 } elsif ($sizefield =~ /^\+([.\d]+)%$/) {
923 $newsize = $oldsize + $oldsize * $1 / 100;
924 } elsif ($sizefield =~ /^-([.\d]+)%$/) {
925 $newsize = $oldsize - $oldsize * $1 / 100;
927 die __x("{p}: {f}: cannot parse size field\n",
928 p => $part, f => $sizefield)
932 die __x("{p}: new size is zero or negative\n", p => $part);
934 mark_partition_for_resize ($part, $oldsize, $newsize, $force, $option);
937 sub mark_partition_for_resize
946 # Do nothing if the size is the same.
947 return if $oldsize == $newsize;
949 my $bigger = $newsize > $oldsize;
951 # Check there is space to shrink this.
952 unless ($bigger || $force) {
953 if (! $partitions{$part}->{fssize} ||
954 $partitions{$part}->{fssize} > $newsize) {
955 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",
960 $partitions{$part}->{newsize} = $newsize;
962 if ($partitions{$part}->{can_expand_content} && $bigger) {
963 $partitions{$part}->{will_expand_content} = 1;
968 # Handle --expand and --shrink.
970 if (defined $expand && defined $shrink) {
971 die __"virt-resize: you cannot use options --expand and --shrink together\n"
973 if (defined $expand || defined $shrink) {
974 calculate_surplus ();
977 print "surplus before --expand or --shrink: $surplus (",
978 human_size ($surplus), ")\n";
981 do_expand () if $expand;
982 do_shrink () if $shrink;
985 # (Re-)calculate surplus after doing expand or shrink.
986 calculate_surplus ();
988 # Add up the total space required on the target so far, compared
989 # to the size of the target. We end up with a surplus or deficit.
990 sub calculate_surplus
994 # We need some overhead for partitioning. Worst case would be for
995 # EFI partitioning + massive per-partition alignment.
996 my $overhead = $sectsize * (
997 2 * 64 + # GPT start and end
998 (64 * (@partitions + 1)) # Maximum alignment
1000 ($max_bootloader - 64 * 512); # boot loader
1003 foreach (@partitions) {
1004 if ($partitions{$_}->{newsize}) {
1005 $required += $partitions{$_}->{newsize}
1007 $required += $partitions{$_}->{part_size}
1011 # Compare that to the actual target disk.
1012 $surplus = $outsize - ($required + $overhead);
1019 unless ($surplus > 0) {
1020 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",
1021 h => human_size (-$surplus));
1024 my $part = find_partition ($expand, "--expand");
1025 my $oldsize = $partitions{$part}->{part_size};
1026 mark_partition_for_resize ($part, $oldsize, $oldsize + $surplus,
1034 unless ($surplus < 0) {
1035 die __"virt-resize: error: cannot use --shrink because there is no deficit\n(see 'deficit' in the virt-resize(1) man page)\n"
1038 my $part = find_partition ($shrink, "--shrink");
1039 my $oldsize = $partitions{$part}->{part_size};
1040 mark_partition_for_resize ($part, $oldsize, $oldsize + $surplus,
1045 print_summary () unless $quiet;
1050 print __"Summary of changes:\n";
1052 foreach my $part (@partitions) {
1053 if ($partitions{$part}->{ignore}) {
1054 print __x("{p}: partition will be ignored\n", p => $part);
1055 } elsif ($partitions{$part}->{delete}) {
1056 print __x("{p}: partition will be deleted\n", p => $part);
1057 } elsif ($partitions{$part}->{newsize}) {
1058 print __x("{p}: partition will be resized from {oldsize} to {newsize}\n",
1060 oldsize => human_size ($partitions{$part}->{part_size}),
1061 newsize => human_size ($partitions{$part}->{newsize}));
1062 if ($partitions{$part}->{will_expand_content}) {
1063 print __x("{p}: content will be expanded using the '{meth}' method\n",
1065 meth => $partitions{$part}->{expand_content_method});
1068 print __x("{p}: partition will be left alone\n", p => $part);
1072 foreach my $lv (@lv_expand) {
1073 print __x("{n}: LV will be expanded to maximum size\n",
1077 foreach my $lv (@lvs) {
1078 if ($lvs{$lv}->{will_expand_content}) {
1079 print __x("{n}: content will be expanded using the '{meth}' method\n",
1081 meth => $lvs{$lv}->{expand_content_method});
1086 print __x("There is a surplus of {spl} bytes ({h}).\n",
1088 h => human_size ($surplus));
1089 if ($extra_partition) {
1090 if ($surplus >= $min_extra_partition) {
1091 print __"An extra partition will be created for the surplus.\n";
1093 print __"The surplus space is not large enough for an extra partition to be created\nand so it will just be ignored.\n";
1096 print __"The surplus space will be ignored. Run a partitioning program in the guest\nto partition this extra space if you want.\n";
1098 } elsif ($surplus < 0) {
1099 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",
1101 h => human_size (-$surplus));
1107 # Repartition the target disk.
1115 # Work out where to start the first partition.
1116 die __"virt-resize: source disk does not have a first partition\n"
1117 unless exists ($partitions{"/dev/sda1"});
1118 my $start = $partitions{"/dev/sda1"}->{part_start} / $sectsize;
1121 $start = ($start + 63) & ~63;
1123 print "starting to partition from $start\n" if $debug;
1125 # Create the new partitions.
1126 foreach my $part (@partitions) {
1127 unless ($partitions{$part}->{delete}) {
1130 if ($partitions{$part}->{newsize}) {
1131 $size = ($partitions{$part}->{newsize} + $sectsize - 1)
1134 $size = ($partitions{$part}->{part_size} + $sectsize - 1)
1139 my ($target, $end, $part_num) = add_partition ($start, $size);
1140 $partitions{$part}->{target} = $target;
1142 if ($partitions{$part}->{bootable}) {
1143 $g->part_set_bootable ("/dev/sdb", $part_num, 1);
1146 if ($partitions{$part}->{mbr_id}) {
1147 $g->part_set_mbr_id ("/dev/sdb", $part_num,
1148 $partitions{$part}->{mbr_id});
1151 # Start of next partition + alignment.
1153 $start = ($start + 63) & ~63;
1157 # Create surplus partition.
1158 if ($extra_partition && $surplus >= $min_extra_partition) {
1159 add_partition ($start, $outsize / $sectsize - 64 - $start);
1170 my ($target, $end, $part_num);
1172 if ($nextpart <= 3 || $parttype ne "msdos") {
1173 $target = "/dev/sdb$nextpart";
1174 $end = $start + $size - 1;
1175 $g->part_add ("/dev/sdb", "primary", $start, $end);
1176 $part_num = $nextpart++;
1178 if ($nextpart == 4) {
1179 $g->part_add ("/dev/sdb", "extended", $start, -1);
1180 $part_num = $nextpart++;
1183 $target = "/dev/sdb$nextpart";
1184 $end = $start + $size - 1;
1185 $g->part_add ("/dev/sdb", "logical", $start, $end);
1186 $part_num = $nextpart++;
1189 return ($target, $end, $part_num);
1192 # Copy over the data.
1197 foreach my $part (@partitions)
1199 unless ($partitions{$part}->{ignore}) {
1200 my $target = $partitions{$part}->{target};
1202 my $oldsize = $partitions{$part}->{part_size};
1204 if ($partitions{$part}->{newsize}) {
1205 $newsize = $partitions{$part}->{newsize};
1207 $newsize = $partitions{$part}->{part_size};
1210 if (!$quiet && !$debug) {
1211 print __x("Copying {p} ...\n", p => $part);
1214 $g->copy_size ($part, $target,
1215 $newsize < $oldsize ? $newsize : $oldsize);
1221 # After copying the data over we must shut down and restart the
1222 # appliance in order to expand the content. The reason for this may
1223 # not be obvious, but it's because otherwise we'll have duplicate VGs
1224 # (the old VG(s) and the new VG(s)) which breaks LVM.
1226 # The restart is only required if we're going to expand something.
1228 if ($to_be_expanded > 0) {
1229 restart_appliance ();
1230 expand_partitions ();
1232 expand_lvs_content ();
1235 sub restart_appliance
1237 # Sync disk and exit.
1242 $g = Sys::Guestfs->new ();
1243 $g->set_trace (1) if $debug;
1244 my @args = ($outfile);
1245 push @args, format => $output_format if defined $output_format;
1246 $g->add_drive_opts (@args);
1249 # Target partitions have changed from /dev/sdb to /dev/sda,
1251 foreach my $part (@partitions)
1253 my $target = $partitions{$part}->{target};
1255 if ($target =~ m{/dev/(.)db(.*)}) {
1256 $partitions{$part}->{target} = "/dev/$1da$2";
1258 die "internal error: unexpected partition target: $target";
1264 sub expand_partitions
1266 foreach my $part (@partitions)
1268 unless ($partitions{$part}->{ignore}) {
1269 my $target = $partitions{$part}->{target};
1271 # Expand if requested.
1272 if ($partitions{$part}->{will_expand_content}) {
1273 if (!$quiet && !$debug) {
1274 print __x("Expanding {p} using the '{meth}' method\n",
1276 meth => $partitions{$part}->{expand_content_method});
1278 expand_target_partition ($part)
1285 sub expand_target_partition
1292 die unless $partitions{$part}->{can_expand_content};
1293 die unless $partitions{$part}->{will_expand_content};
1294 die unless $partitions{$part}->{expand_content_method};
1295 die unless $partitions{$part}->{target};
1296 die unless $expand_content;
1298 my $target = $partitions{$part}->{target};
1299 my $method = $partitions{$part}->{expand_content_method};
1300 if ($method eq "pvresize") {
1301 $g->pvresize ($target);
1303 elsif ($method eq "resize2fs") {
1304 $g->e2fsck_f ($target);
1305 $g->resize2fs ($target);
1307 elsif ($method eq "ntfsresize") {
1308 $g->ntfsresize ($target);
1311 die "internal error: unknown method: $method";
1319 foreach (@lv_expand) {
1320 $g->lvresize_free ($_, 100);
1324 sub expand_lvs_content
1329 if ($lvs{$_}->{will_expand_content}) {
1330 my $method = $lvs{$_}->{expand_content_method};
1331 if (!$quiet && !$debug) {
1332 print __x("Expanding {p} using the '{meth}' method\n",
1333 p => $_, meth => $method);
1335 if ($method eq "resize2fs") {
1338 } elsif ($method eq "ntfsresize") {
1339 $g->ntfsresize ($_);
1341 die "internal error: unknown method: $method";
1347 # Sync disk and exit.
1359 $_ *= 1024 if $unit =~ /[KMGTPE]/;
1360 $_ *= 1024 if $unit =~ /[MGTPE]/;
1361 $_ *= 1024 if $unit =~ /[GTPE]/;
1362 $_ *= 1024 if $unit =~ /[TPE]/;
1363 $_ *= 1024 if $unit =~ /[PE]/;
1364 $_ *= 1024 if $unit =~ /[E]/;
1369 # Convert a number of bytes to a human-readable number.
1383 sprintf "%s%dK", $sgn, $_;
1384 } elsif ($_ < 1024 * 1024) {
1385 sprintf "%s%.1fM", $sgn, ($_ / 1024);
1387 sprintf "%s%.1fG", $sgn, ($_ / 1024 / 1024);
1391 # The reverse of device name translation, see
1392 # BLOCK DEVICE NAMING in guestfs(3).
1397 if (m{^/dev/[hv]d([a-z]\d)$}) {
1403 # Not as sophisticated as the guestfish progress bar, because
1404 # I intend to use an external library for this at some point (XXX).
1405 sub progress_callback
1407 my $proc_nr = shift;
1409 my $position = shift;
1412 my $ratio = $position / $total;
1413 if ($ratio < 0) { $ratio = 0 }
1414 elsif ($ratio > 1) { $ratio = 1 }
1416 my $dots = int ($ratio * 76);
1418 print "[", "#"x$dots, "-"x(76-$dots), "]\r";
1419 print "\n" if $ratio == 1;
1424 =head2 "Partition 1 does not end on cylinder boundary."
1426 Virt-resize aligns partitions to multiples of 64 sectors. Usually
1427 this means the partitions will not be aligned to the ancient CHS
1428 geometry. However CHS geometry is meaningless for disks manufactured
1429 since the early 1990s, and doubly so for virtual hard drives.
1430 Alignment of partitions to cylinders is not required by any modern
1433 =head2 RESIZING WINDOWS VIRTUAL MACHINES
1435 In Windows Vista and later versions, Microsoft switched to using a
1436 separate boot partition. In these VMs, typically C</dev/sda1> is the
1437 boot partition and C</dev/sda2> is the main (C:) drive. We have not
1438 had any luck resizing the boot partition. Doing so seems to break the
1439 guest completely. However expanding the second partition (ie. C:
1442 Windows may initiate a lengthy "chkdsk" on first boot after a resize,
1443 if NTFS partitions have been expanded. This is just a safety check
1444 and (unless it find errors) is nothing to worry about.
1446 =head2 GUEST BOOT STUCK AT "GRUB"
1448 If a Linux guest does not boot after resizing, and the boot is stuck
1449 after printing C<GRUB> on the console, try reinstalling grub. This
1450 sometimes happens on older (RHEL 5-era) guests, for reasons we don't
1451 fully understand, although we think is to do with partition alignment.
1453 guestfish -i -a newdisk
1454 ><fs> cat /boot/grub/device.map
1455 # check the contents of this file are sensible or
1456 # edit the file if necessary
1457 ><fs> grub-install / /dev/vda
1460 For more flexible guest reconfiguration, including if you need to
1461 specify other parameters to grub-install, use L<virt-rescue(1)>.
1463 =head1 ALTERNATIVE TOOLS
1465 There are several proprietary tools for resizing partitions. We
1466 won't mention any here.
1468 L<parted(8)> and its graphical shell gparted can do some types of
1469 resizing operations on disk images. They can resize and move
1470 partitions, but I don't think they can do anything with the contents,
1471 and they certainly don't understand LVM.
1473 L<guestfish(1)> can do everything that virt-resize can do and a lot
1474 more, but at a much lower level. You will probably end up
1475 hand-calculating sector offsets, which is something that virt-resize
1476 was designed to avoid. If you want to see the guestfish-equivalent
1477 commands that virt-resize runs, use the C<--debug> flag.
1479 =head1 SHELL QUOTING
1481 Libvirt guest names can contain arbitrary characters, some of which
1482 have meaning to the shell such as C<#> and space. You may need to
1483 quote or escape these characters on the command line. See the shell
1484 manual page L<sh(1)> for details.
1488 L<virt-list-partitions(1)>,
1489 L<virt-list-filesystems(1)>,
1506 L<http://libguestfs.org/>.
1510 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
1514 Copyright (C) 2010 Red Hat Inc.
1516 This program is free software; you can redistribute it and/or modify
1517 it under the terms of the GNU General Public License as published by
1518 the Free Software Foundation; either version 2 of the License, or
1519 (at your option) any later version.
1521 This program is distributed in the hope that it will be useful,
1522 but WITHOUT ANY WARRANTY; without even the implied warranty of
1523 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1524 GNU General Public License for more details.
1526 You should have received a copy of the GNU General Public License
1527 along with this program; if not, write to the Free Software
1528 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.