Virt-resize lets you resize existing virtual machines (not live however).
$ rm -f /tmp/centos.img $ truncate -s 12G /tmp/centos.img $ virt-resize --resize sda1=200% --resize sda2=11.2G \ /dev/vg_trick/CentOS5x32 /tmp/centos.img Summary of changes: /dev/sda1: partition will be resized from 101.9M to 203.9M /dev/sda2: partition will be resized from 9.9G to 11.2G There is a surplus of 644971316 bytes (615.1M). An extra partition will be created for the surplus. Copying /dev/sda1 ... done Copying /dev/sda2 ... done
$ rm -f /tmp/centos.img $ truncate -s 12G /tmp/centos.img $ virt-resize --resize sda1=200% --expand sda2 \ /dev/vg_trick/CentOS5x32 /tmp/centos.img Summary of changes: /dev/sda1: partition will be resized from 101.9M to 203.9M /dev/sda2: partition will be resized from 9.9G to 11.8G Copying /dev/sda1 ... done Copying /dev/sda2 ... done
After some discussion on the list we decided to start with a simple / primitive tool and work upwards. So virt-resize as implemented now will not resize filesystems and PVs. You have to do that as a separate step after running the tool (either running pvresize/resize2fs in the guest or using guestfish for offline changes.
The new tool’s man page after the cut.
NAME virt-resize - Resize a virtual machine disk SYNOPSIS virt-resize [--options] indisk outdisk DESCRIPTION Virt-resize is a tool which can resize a virtual machine disk, making it larger or smaller overall, and resizing or deleting any partitions contained within. Virt-resize cannot resize disk images in-place. Virt-resize should not be used on live virtual machines - for consistent results, shut the virtual machine down before resizing it. If you are not familiar with the associated tools: virt-list-partitions(1), virt-list-filesystems(1) and virt-df(1), we recommend you go and read those manual pages first. BASIC USAGE This describes the common case where you want to expand an image to give your guest more space. Shrinking images is considerably more complicated (unfortunately). 1. Locate disk image Locate the disk image that you want to resize. It could be in a local file or device. If the guest is managed by libvirt, you can use "virsh dumpxml" like this to find the disk image name: # virsh dumpxml guestname | xpath /domain/devices/disk/source Found 1 nodes: -- NODE -- <source dev="/dev/vg/lv_guest" /> 2. Look at current sizing Use virt-list-partitions(1) to display the current partitions and sizes: # virt-list-partitions -lh /dev/vg/lv_guest /dev/sda1 ext3 101.9M /dev/sda2 pv 7.9G (This example is a virtual machine with an 8 GB disk which we would like to expand up to 10 GB). 3. Create destination disk Virt-resize cannot do in-place disk modifications. You have to have space to store the resized destination disk. To store the resized disk image in a file, create a file of a suitable size: # rm -f outdisk # truncate -s 10G outdisk Use lvcreate(1) to create a logical volume: # lvcreate -L 10G -n lv_name vg_name Or use virsh(1) vol-create-as to create a libvirt storage volume: # virsh pool-list # virsh vol-create-as poolname newvol 10G 4. Resize virt-resize indisk outdisk This command just copies disk image "indisk" to disk image "outdisk" *without* resizing or changing any existing partitions. If "outdisk" is larger, then an extra, empty partition is created at the end of the disk covering the extra space. If "outdisk" is smaller, then it will give an error. To resize, you need to pass extra options (for the full list see the "OPTIONS" section below). "--expand" is the most useful option. It expands the named partition within the disk to fill any extra space: virt-resize --expand /dev/sda2 indisk outdisk (In this case, an extra partition is *not* created at the end of the disk, because there will be no unused space). If /dev/sda2 in the image contains a filesystem or LVM PV, then this content is not automatically resized. You can resize it afterwards either using guestfish(1) (offline) or using commands inside the guest (online resizing). "--resize" is the other commonly used option. The following would increase the size of /dev/sda1 by 200M, and expand /dev/sda2 to fill the rest of the available space: virt-resize --resize /dev/sda1=+200M --expand /dev/sda2 \ indisk outdisk Other options are covered below. 5. Test Thoroughly test the new disk image *before* discarding the old one. If you are using libvirt, edit the XML to point at the new disk: # virsh edit guestname Change <source ...>, see libvirt documentation Then start up the domain with the new, resized disk: # virsh start guestname and check that it still works. Note that to see the extra space in the guest, you may need to use guest commands to resize PVs, LVs and/or filesystems to fit the extra space available. Three common guest commands for doing this for Linux guests are pvresize(8), lvresize(8) and resize2fs(8). It is also possible to do this offline (eg. for scripting changes) using guestfish(1). OPTIONS --help Display help. --version Display version number and exit. --resize part=size Resize the named partition (expanding or shrinking it) so that it has the given size. "size" can be expressed as an absolute number followed by b/K/M/G/T/P/E to mean bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, Petabytes or Exabytes; or as a percentage of the current size; or as a relative number or percentage. For example: --resize /dev/sda2=10G --resize /dev/sda4=90% --resize /dev/sda2=+1G --resize /dev/sda2=-200M --resize /dev/sda1=+128K --resize /dev/sda1=+10% --resize /dev/sda1=-10% You can increase the size of any partition. You can *only* decrease the size of partitions that contain filesystems or PVs which have already been shrunk. Virt-resize will check this has been done before proceeding, or else will print an error (see also "--resize-force"). You can give this option multiple times. --resize-force part=size This is the same as "--resize" except that it will let you decrease the size of any partition. Generally this means you will lose any data which was at the end of the partition you shrink, but you may not care about that (eg. if shrinking an unused partition, or if you can easily recreate it such as a swap partition). See also the "--ignore" option. --expand part Expand the named partition so it uses up all extra space (space left over after any other resize changes that you request have been done). Any filesystem inside the partition is *not* expanded. You will need to expand the filesystem (or PV) to fit the extra space either using guestfish(1) (offline) or online guest tools. Note that you cannot use "--expand" and "--shrink" together. --shrink part Shrink the named partition until the overall disk image fits in the destination. The named partition must contain a filesystem or PV which has already been shrunk using another tool (eg. guestfish(1) or other online tools). Virt-resize will check this and give an error if it has not been done. The amount by which the overall disk must be shrunk (after carrying out all other operations requested by the user) is called the "deficit". For example, a straight copy (assume no other operations) from a 5GB disk image to a 4GB disk image results in a 1GB deficit. In this case, virt-resize would give an error unless the user specified a partition to shrink and that partition had more than a gigabyte of free space. Note that you cannot use "--expand" and "--shrink" together. --ignore part Ignore the named partition. Effectively this means the partition is allocated on the destination disk, but the content is not copied across from the source disk. The content of the partition will be blank (all zero bytes). You can give this option multiple times. --delete part Delete the named partition. It would be more accurate to describe this as "don't copy it over", since virt-resize doesn't do in-place changes and the original disk image is left intact. Note that when you delete a partition, then anything contained in the partition is also deleted. Furthermore, this causes any partitions that come after to be *renumbered*, which can easily make your guest unbootable. You can give this option multiple times. --no-copy-boot-loader By default, virt-resize copies over some sectors at the start of the disk (up to the beginning of the first partition). Commonly these sectors contain the Master Boot Record (MBR) and the boot loader, and are required in order for the guest to boot correctly. If you specify this flag, then this initial copy is not done. You may need to reinstall the boot loader in this case. --no-extra-partition By default, virt-resize creates an extra partition if there is any extra, unused space after all resizing has happened. Use this option to prevent the extra partition from being created. If you do this then the extra space will be inaccessible until you run fdisk, parted, or some other partitioning tool in the guest. Note that if the surplus space is smaller than 10 MB, no extra partition will be created. -d | --debug Enable debugging messages. -n | --dryrun Print a summary of what would be done, but don't do anything. -q | --quiet Don't print the summary. SEE ALSO virt-list-partitions(1), virt-list-filesystems(1), virt-df(1), guestfs(3), guestfish(1), lvm(8), pvresize(8), lvresize(8), resize2fs(8), virsh(1), Sys::Guestfs(3). AUTHOR Richard W.M. Jones COPYRIGHT Copyright (C) 2010 Red Hat Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
One response to “New tool: virt-resize”
Pingback: Quick quiz: Resizing VMs « Richard WM Jones