5 virt-resize - Resize a virtual machine disk
9 virt-resize [--resize /dev/sdaN=[+/-]<size>[%]]
10 [--expand /dev/sdaN] [--shrink /dev/sdaN]
11 [--ignore /dev/sdaN] [--delete /dev/sdaN] [...] indisk outdisk
15 Virt-resize is a tool which can resize a virtual machine disk, making
16 it larger or smaller overall, and resizing or deleting any partitions
19 Virt-resize B<cannot> resize disk images in-place. Virt-resize
20 B<should not> be used on live virtual machines - for consistent
21 results, shut the virtual machine down before resizing it.
23 If you are not familiar with the associated tools:
24 L<virt-filesystems(1)> and L<virt-df(1)>, we recommend you go and read
25 those manual pages first.
33 Copy C<olddisk> to C<newdisk>, extending one of the guest's partitions
34 to fill the extra 5GB of space.
36 virt-filesystems --long -h --all -a olddisk
38 truncate -r olddisk newdisk
39 truncate -s +5G newdisk
41 # Note "/dev/sda2" is a partition inside the "olddisk" file.
42 virt-resize --expand /dev/sda2 olddisk newdisk
46 As above, but make the /boot partition 200MB bigger, while giving the
47 remaining space to /dev/sda2:
49 virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 \
54 As in the first example, but expand a logical volume as the final
55 step. This is what you would typically use for Linux guests that use
58 virt-resize --expand /dev/sda2 --LV-expand /dev/vg_guest/lv_root \
63 As in the first example, but the output format will be qcow2 instead
66 qemu-img create -f qcow2 newdisk.qcow2 15G
67 virt-resize --expand /dev/sda2 olddisk newdisk.qcow2
73 =head2 EXPANDING A VIRTUAL MACHINE DISK
77 =item 1. Shut down the virtual machine
79 =item 2. Locate input disk image
81 Locate the input disk image (ie. the file or device on the host
82 containing the guest's disk). If the guest is managed by libvirt, you
83 can use C<virsh dumpxml> like this to find the disk image name:
85 # virsh dumpxml guestname | xpath /domain/devices/disk/source
88 <source dev="/dev/vg/lv_guest" />
90 =item 3. Look at current sizing
92 Use L<virt-filesystems(1)> to display the current partitions and
95 # virt-filesystems --long --parts --blkdevs -h -a /dev/vg/lv_guest
97 /dev/sda1 partition 101M /dev/sda
98 /dev/sda2 partition 7.9G /dev/sda
99 /dev/sda device 8.0G -
101 (This example is a virtual machine with an 8 GB disk which we would
102 like to expand up to 10 GB).
104 =item 4. Create output disk
106 Virt-resize cannot do in-place disk modifications. You have to have
107 space to store the resized output disk.
109 To store the resized disk image in a file, create a file of a suitable
113 # truncate -s 10G outdisk
115 Or use L<lvcreate(1)> to create a logical volume:
117 # lvcreate -L 10G -n lv_name vg_name
119 Or use L<virsh(1)> vol-create-as to create a libvirt storage volume:
122 # virsh vol-create-as poolname newvol 10G
126 virt-resize takes two mandatory parameters, the input disk (eg. device
127 or file) and the output disk. The output disk is the one created in
130 # virt-resize indisk outdisk
132 This command just copies disk image C<indisk> to disk image C<outdisk>
133 I<without> resizing or changing any existing partitions. If
134 C<outdisk> is larger, then an extra, empty partition is created at the
135 end of the disk covering the extra space. If C<outdisk> is smaller,
136 then it will give an error.
138 More realistically you'd want to expand existing partitions in the
139 disk image by passing extra options (for the full list see the
140 L</OPTIONS> section below).
142 L</--expand> is the most useful option. It expands the named
143 partition within the disk to fill any extra space:
145 # virt-resize --expand /dev/sda2 indisk outdisk
147 (In this case, an extra partition is I<not> created at the end of the
148 disk, because there will be no unused space).
150 L</--resize> is the other commonly used option. The following would
151 increase the size of /dev/sda1 by 200M, and expand /dev/sda2
152 to fill the rest of the available space:
154 # virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 \
157 If the expanded partition in the image contains a filesystem or LVM
158 PV, then if virt-resize knows how, it will resize the contents, the
159 equivalent of calling a command such as L<pvresize(8)>,
160 L<resize2fs(8)>, L<ntfsresize(8)> or L<btrfs(8)>. However virt-resize
161 does not know how to resize some filesystems, so you would have to
162 online resize them after booting the guest.
164 Other options are covered below.
168 Thoroughly test the new disk image I<before> discarding the old one.
170 If you are using libvirt, edit the XML to point at the new disk:
172 # virsh edit guestname
174 Change E<lt>source ...E<gt>, see
175 L<http://libvirt.org/formatdomain.html#elementsDisks>
177 Then start up the domain with the new, resized disk:
179 # virsh start guestname
181 and check that it still works. See also the L</NOTES> section below
182 for additional information.
184 =item 7. Resize LVs etc inside the guest
186 (This can also be done offline using L<guestfish(1)>)
188 Once the guest has booted you should see the new space available, at
189 least for filesystems that virt-resize knows how to resize, and for
190 PVs. The user may need to resize LVs inside PVs, and also resize
191 filesystem types that virt-resize does not know how to expand.
195 =head2 SHRINKING A VIRTUAL MACHINE DISK
197 Shrinking is somewhat more complex than expanding, and only an
198 overview is given here.
200 Firstly virt-resize will not attempt to shrink any partition content
201 (PVs, filesystems). The user has to shrink content before passing the
202 disk image to virt-resize, and virt-resize will check that the content
203 has been shrunk properly.
205 (Shrinking can also be done offline using L<guestfish(1)>)
207 After shrinking PVs and filesystems, shut down the guest, and proceed
208 with steps 3 and 4 above to allocate a new disk image.
210 Then run virt-resize with any of the I<--shrink> and/or I<--resize>
213 =head2 IGNORING OR DELETING PARTITIONS
215 virt-resize also gives a convenient way to ignore or delete partitions
216 when copying from the input disk to the output disk. Ignoring a
217 partition speeds up the copy where you don't care about the existing
218 contents of a partition. Deleting a partition removes it completely,
219 but note that it also renumbers any partitions after the one which is
220 deleted, which can leave some guests unbootable.
222 =head2 QCOW2 AND NON-SPARSE RAW FORMATS
224 If the input disk is in qcow2 format, then you may prefer that the
225 output is in qcow2 format as well. Alternately, virt-resize can
226 convert the format on the fly. The output format is simply determined
227 by the format of the empty output container that you provide. Thus to
228 create qcow2 output, use:
230 qemu-img create [-c] -f qcow2 outdisk [size]
232 instead of the truncate command (use I<-c> for a compressed disk).
234 Similarly, to get non-sparse raw output use:
236 fallocate -l size outdisk
238 (on older systems that don't have the L<fallocate(1)> command use
239 C<dd if=/dev/zero of=outdisk bs=1M count=..>)
241 =head2 LOGICAL PARTITIONS
243 Logical partitions (a.k.a. C</dev/sda5+> on disks using DOS partition
244 tables) cannot be resized.
246 To understand what is going on, firstly one of the four partitions
247 C</dev/sda1-4> will have MBR partition type C<05> or C<0f>. This is
248 called the B<extended partition>. Use L<virt-filesystems(1)> to see
249 the MBR partition type.
251 Logical partitions live inside the extended partition.
253 The extended partition can be expanded, but not shrunk (unless you
254 force it, which is not advisable). When the extended partition is
255 copied across, all the logical partitions contained inside are copied
256 over implicitly. Virt-resize does not look inside the extended
257 partition, so it copies the logical partitions blindly.
259 You cannot specify a logical partition (C</dev/sda5+>) at all on the
260 command line. Doing so will give an error.
270 =item B<--align-first auto>
272 =item B<--align-first never>
274 =item B<--align-first always>
276 Align the first partition for improved performance (see also the
277 I<--alignment> option).
279 The default is I<--align-first auto> which only aligns the first
280 partition if it is safe to do so. That is, only when we know how to
281 fix the bootloader automatically, and at the moment that can only be
282 done for Windows guests.
284 I<--align-first never> means we never move the first partition.
285 This is the safest option. Try this if the guest does not boot
288 I<--align-first always> means we always align the first partition (if
289 it needs to be aligned). For some guests this will break the
290 bootloader, making the guest unbootable.
292 =item B<--alignment N>
294 Set the alignment of partitions to C<N> sectors. The default in
295 virt-resize E<lt> 1.13.19 was 64 sectors, and after that is 128
298 Assuming 512 byte sector size inside the guest, here are some
299 suitable values for this:
303 =item I<--alignment 1> (512 bytes)
305 The partitions would be packed together as closely as possible, but
306 would be completely unaligned. In some cases this can cause very poor
307 performance. See L<virt-alignment-scan(1)> for further details.
309 =item I<--alignment 8> (4K)
311 This would be the minimum acceptable alignment for reasonable
312 performance on modern hosts.
314 =item I<--alignment 128> (64K)
316 This alignment provides good performance when the host is using high
319 =item I<--alignment 2048> (1M)
321 This is the standard alignment used by all newly installed guests
330 Enable debugging messages.
334 Debug garbage collection and memory allocation. This is only useful
335 when debugging memory problems in virt-resize or the OCaml libguestfs
338 =item B<--delete part>
340 Delete the named partition. It would be more accurate to describe
341 this as "don't copy it over", since virt-resize doesn't do in-place
342 changes and the original disk image is left intact.
344 Note that when you delete a partition, then anything contained in the
345 partition is also deleted. Furthermore, this causes any partitions
346 that come after to be I<renumbered>, which can easily make your guest
349 You can give this option multiple times.
351 =item B<--expand part>
353 Expand the named partition so it uses up all extra space (space left
354 over after any other resize changes that you request have been done).
356 If virt-resize knows how, it will expand the direct content of the
357 partition. For example, if the partition is an LVM PV, it will expand
358 the PV to fit (like calling L<pvresize(8)>). Virt-resize leaves any
359 other content it doesn't know about alone.
361 Currently virt-resize can resize:
367 ext2, ext3 and ext4 filesystems.
371 NTFS filesystems, if libguestfs was compiled with support for NTFS.
373 The filesystem must have been shut down consistently last time it was
374 used. Additionally, L<ntfsresize(8)> marks the resized filesystem as
375 requiring a consistency check, so at the first boot after resizing
376 Windows will check the disk.
380 LVM PVs (physical volumes). virt-resize does not usually resize
381 anything inside the PV, but see the I<--LV-expand> option. The user
382 could also resize LVs as desired after boot.
386 Btrfs filesystems, if libguestfs was compiled with support for btrfs.
390 Note that you cannot use I<--expand> and I<--shrink> together.
392 =item B<--format> raw
394 Specify the format of the input disk image. If this flag is not
395 given then it is auto-detected from the image itself.
397 If working with untrusted raw-format guest disk images, you should
398 ensure the format is always specified.
400 Note that this option I<does not> affect the output format.
401 See L</QCOW2 AND NON-SPARSE RAW FORMATS>.
403 =item B<--ignore part>
405 Ignore the named partition. Effectively this means the partition is
406 allocated on the destination disk, but the content is not copied
407 across from the source disk. The content of the partition will be
408 blank (all zero bytes).
410 You can give this option multiple times.
412 =item B<--LV-expand logvol>
414 This takes the logical volume and, as a final step, expands it to fill
415 all the space available in its volume group. A typical usage,
416 assuming a Linux guest with a single PV C</dev/sda2> and a root device
417 called C</dev/vg_guest/lv_root> would be:
419 virt-resize indisk outdisk \
420 --expand /dev/sda2 --LV-expand /dev/vg_guest/lv_root
422 This would first expand the partition (and PV), and then expand the
423 root device to fill the extra space in the PV.
425 The contents of the LV are also resized if virt-resize knows how to do
426 that. You can stop virt-resize from trying to expand the content by
427 using the option I<--no-expand-content>.
429 Use L<virt-filesystems(1)> to list the filesystems in the guest.
431 You can give this option multiple times, I<but> it doesn't
432 make sense to do this unless the logical volumes you specify
433 are all in different volume groups.
435 =item B<--machine-readable>
437 This option is used to make the output more machine friendly
438 when being parsed by other programs. See
439 L</MACHINE READABLE OUTPUT> below.
445 Print a summary of what would be done, but don't do anything.
447 =item B<--no-copy-boot-loader>
449 By default, virt-resize copies over some sectors at the start of the
450 disk (up to the beginning of the first partition). Commonly these
451 sectors contain the Master Boot Record (MBR) and the boot loader, and
452 are required in order for the guest to boot correctly.
454 If you specify this flag, then this initial copy is not done. You may
455 need to reinstall the boot loader in this case.
457 =item B<--no-extra-partition>
459 By default, virt-resize creates an extra partition if there is any
460 extra, unused space after all resizing has happened. Use this option
461 to prevent the extra partition from being created. If you do this
462 then the extra space will be inaccessible until you run fdisk, parted,
463 or some other partitioning tool in the guest.
465 Note that if the surplus space is smaller than 10 MB, no extra
466 partition will be created.
468 =item B<--no-expand-content>
470 By default, virt-resize will try to expand the direct contents
471 of partitions, if it knows how (see I<--expand> option above).
473 If you give the I<--no-expand-content> option then virt-resize
474 will not attempt this.
476 =item B<--ntfsresize-force>
478 Pass the I<--force> option to L<ntfsresize(8)>, allowing resizing
479 even if the NTFS disk is marked as needing a consistency check.
480 You have to use this option if you want to resize a Windows
481 guest multiple times without booting into Windows between each
484 =item B<--output-format> raw
486 Specify the format of the output disk image. If this flag is not
487 given then it is auto-detected from the image itself.
489 If working with untrusted raw-format guest disk images, you should
490 ensure the format is always specified.
492 Note that this option I<does not create> the output format. This
493 option just tells libguestfs what it is so it doesn't try to guess it.
494 You still need to create the output disk with the right format. See
495 L</QCOW2 AND NON-SPARSE RAW FORMATS>.
501 Don't print the summary.
503 =item B<--resize part=size>
505 Resize the named partition (expanding or shrinking it) so that it has
508 C<size> can be expressed as an absolute number followed by
509 b/K/M/G to mean bytes, Kilobytes, Megabytes, or Gigabytes;
510 or as a percentage of the current size;
511 or as a relative number or percentage.
514 --resize /dev/sda2=10G
516 --resize /dev/sda4=90%
518 --resize /dev/sda2=+1G
520 --resize /dev/sda2=-200M
522 --resize /dev/sda1=+128K
524 --resize /dev/sda1=+10%
526 --resize /dev/sda1=-10%
528 You can increase the size of any partition. Virt-resize will expand
529 the direct content of the partition if it knows how (see I<--expand>
532 You can only I<decrease> the size of partitions that contain
533 filesystems or PVs which have already been shrunk. Virt-resize will
534 check this has been done before proceeding, or else will print an
535 error (see also I<--resize-force>).
537 You can give this option multiple times.
539 =item B<--resize-force part=size>
541 This is the same as I<--resize> except that it will let you decrease
542 the size of any partition. Generally this means you will lose any
543 data which was at the end of the partition you shrink, but you may not
544 care about that (eg. if shrinking an unused partition, or if you can
545 easily recreate it such as a swap partition).
547 See also the I<--ignore> option.
549 =item B<--shrink part>
551 Shrink the named partition until the overall disk image fits in the
552 destination. The named partition B<must> contain a filesystem or PV
553 which has already been shrunk using another tool (eg. L<guestfish(1)>
554 or other online tools). Virt-resize will check this and give an error
555 if it has not been done.
557 The amount by which the overall disk must be shrunk (after carrying
558 out all other operations requested by the user) is called the
559 "deficit". For example, a straight copy (assume no other operations)
560 from a 5GB disk image to a 4GB disk image results in a 1GB deficit.
561 In this case, virt-resize would give an error unless the user
562 specified a partition to shrink and that partition had more than a
563 gigabyte of free space.
565 Note that you cannot use I<--expand> and I<--shrink> together.
571 Display version number and exit.
575 =head1 MACHINE READABLE OUTPUT
577 The I<--machine-readable> option can be used to make the output more
578 machine friendly, which is useful when calling virt-resize from other
581 There are two ways to use this option.
583 Firstly use the option on its own to query the capabilities of the
584 virt-resize binary. Typical output looks like this:
586 $ virt-resize --machine-readable
593 A list of features is printed, one per line, and the program exits
596 Secondly use the option in conjunction with other options to make the
597 regular program output more machine friendly.
599 At the moment this means:
605 Progress bar messages can be parsed from stdout by looking for this
612 The calling program should treat messages sent to stdout (except for
613 progress bar messages) as status messages. They can be logged and/or
614 displayed to the user.
618 The calling program should treat messages sent to stderr as error
619 messages. In addition, virt-resize exits with a non-zero status code
620 if there was a fatal error.
624 Versions of the program prior to 1.13.9 did not support the
625 I<--machine-readable> option and will return an error.
629 =head2 "Partition 1 does not end on cylinder boundary."
631 Virt-resize aligns partitions to multiples of 128 sectors (see the
632 I<--alignment> parameter). Usually this means the partitions will not
633 be aligned to the ancient CHS geometry. However CHS geometry is
634 meaningless for disks manufactured since the early 1990s, and doubly
635 so for virtual hard drives. Alignment of partitions to cylinders is
636 not required by any modern operating system.
638 =head2 RESIZING WINDOWS VIRTUAL MACHINES
640 In Windows Vista and later versions, Microsoft switched to using a
641 separate boot partition. In these VMs, typically C</dev/sda1> is the
642 boot partition and C</dev/sda2> is the main (C:) drive. Resizing the
643 first (boot) partition causes the bootloader to fail with
644 C<0xC0000225> error. Resizing the second partition (ie. C: drive)
647 Windows may initiate a lengthy "chkdsk" on first boot after a resize,
648 if NTFS partitions have been expanded. This is just a safety check
649 and (unless it find errors) is nothing to worry about.
651 =head2 GUEST BOOT STUCK AT "GRUB"
653 If a Linux guest does not boot after resizing, and the boot is stuck
654 after printing C<GRUB> on the console, try reinstalling grub.
656 guestfish -i -a newdisk
657 ><fs> cat /boot/grub/device.map
658 # check the contents of this file are sensible or
659 # edit the file if necessary
660 ><fs> grub-install / /dev/vda
663 For more flexible guest reconfiguration, including if you need to
664 specify other parameters to grub-install, use L<virt-rescue(1)>.
666 =head1 ALTERNATIVE TOOLS
668 There are several proprietary tools for resizing partitions. We
669 won't mention any here.
671 L<parted(8)> and its graphical shell gparted can do some types of
672 resizing operations on disk images. They can resize and move
673 partitions, but I don't think they can do anything with the contents,
674 and they certainly don't understand LVM.
676 L<guestfish(1)> can do everything that virt-resize can do and a lot
677 more, but at a much lower level. You will probably end up
678 hand-calculating sector offsets, which is something that virt-resize
679 was designed to avoid. If you want to see the guestfish-equivalent
680 commands that virt-resize runs, use the I<--debug> flag.
684 Libvirt guest names can contain arbitrary characters, some of which
685 have meaning to the shell such as C<#> and space. You may need to
686 quote or escape these characters on the command line. See the shell
687 manual page L<sh(1)> for details.
691 This program returns 0 if successful, or non-zero if there was an
696 L<virt-filesystems(1)>,
714 L<virt-alignment-scan(1)>,
715 L<http://libguestfs.org/>.
719 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
723 Copyright (C) 2010-2011 Red Hat Inc.
725 This program is free software; you can redistribute it and/or modify
726 it under the terms of the GNU General Public License as published by
727 the Free Software Foundation; either version 2 of the License, or
728 (at your option) any later version.
730 This program is distributed in the hope that it will be useful,
731 but WITHOUT ANY WARRANTY; without even the implied warranty of
732 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
733 GNU General Public License for more details.
735 You should have received a copy of the GNU General Public License
736 along with this program; if not, write to the Free Software
737 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.