wiki:extend-disk-on-kvm-guest

Version 23 (modified by Ross, 7 years ago) (diff)

--

How do I extend the disk space for a KVM guest?

If you only need to extend a specific logical volume (i.e. vg_stone0-var), follow these directions.

Each guest is running logical volume manager. If you need to allocate more disk space on a guest, run the vgs command to determine if you already have un-allocated disk space available.

This example shows that chavez has space available:

0 chavez:~# vgs
  VG         #PV #LV #SN Attr   VSize   VFree 
  vg_chavez0   1   7   0 wz--n- 499.52g 57.52g
0 chavez:~#

There's no need to allocate more space from the host, since chavez already has 57GB free. Continue with step 2.

Here's an example of a host without any available disk space:

0 mandela:~# vgs
  VG          #PV #LV #SN Attr   VSize  VFree
  vg_mandela0   1   6   0 wz--n- 69.52g    0 
0 mandela:~#

mandela has no available disk space, so additional space must be allocated from the host, ken.

Step 1: Allocate space from the host

Our KVM guests are each allocated a single logical volume from the KVM host's volume group (typically named after the host).

The first and easiest step is to login to the host, check available disk space:

0 ken:~# vgs
  VG      #PV #LV #SN Attr   VSize VFree
  vg_ken0   1  16   0 wz--n- 1.82t 1.29t
0 ken:~# 

ken has 1.29 TB available.

Next check how much space is already allocated to your guest:

0 ken:~# lvs | grep mandela 
  mandela  vg_ken0 -wi-ao  20.00g                                      
0 ken:~#

Then, add more space:

lvextend --size 35G vg_ken0/mandela
  Extending logical volume mandela to 35.00 GiB
  Logical volume mandela successfully resized
0 ken:~#

Next, you will need to reboot the guest before the change in disk space is recognized by the guest (this is apparently a requirement until someone implements online disk resizing). You will need to execute: shutdown -h now, so that the underlying block devices get reinitialized by a new process.

After rebooting, you can confirm the new size on the guest by running:

cat /proc/partitions

You should see that the base disk (e.g. /dev/vda) reflects the new size, although all of the partitions reflect their old sizes (e.g. /dev/vda2).

Next, on the guest, run parted and print the current partition table. It's often easier to manage if you look at units in sectors:

parted /dev/vda unit s p

Note carefully the start sector for the last partition. You will need this exact number in the step below.

Delete the last partition, and re-create it with a higher end sector (yes, you can do this without destroying the data on the disk). Replace STARTSECTOR with the start sector of the output from the command above (including the s after the number). Specifying -1 as the end sector means go as far to the end as possible. If you get a message saying that the sector you requested is not possible, accept a different one, you can safely say yes.

parted /dev/vda

The following commands are executed in interactive mode:

unit s
rm 2
mkpart primary STARTSECTOR -1 
toggle 2 lvm

Next, check to see if your changes are reflected in /proc/partitions:

cat /proc/partitions

You may need to reboot a second time before they will show up. Try rerunning:

parted /dev/vda unit s p

This should show a different end point for the partition for additional verification. Then reboot.

Once your partition table is reflecting the new size, run the following command:

pvresize /dev/vda2

And now your volume group should show additional free space.

Step 2: Assign free space and resize filesystems

The next step would be to assign the free space from the VG to the LV. That's what lvresize does. For example (size in GB is the new size you want):

lvresize --size 50GB /dev/mapper/LVNAME

will change the specified LV to have 50GB.

Finally you would normally need to resize the filesystem as well so that it can make use of the extra space in the logical volume.

If your filesystem is ext3 or ext4, and the filesystem is currently mounted (online), you should be able to do an online resize with just:

resize2fs /dev/mapper/LVNAME

If the filesystem isn't currently mounted, it's good form to check its integrity before resizing, like so:

e2fsck -f /dev/mapper/LVNAME
resize2fs /dev/mapper/LVNAME