wiki:extend-disk-on-kvm-guest

Version 16 (modified by Jamie McClelland, 12 years ago) ( diff )

--

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

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.

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.

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:~#

The, 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.

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.

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

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

will add 100% of the VG's free space to the specified LV.

Finally you would normally need to resize the filesystem as well. This is done for ext2/ext3/ext4 like that:

e2fsck -f /dev/mapper/LVNAME
resize2fs /dev/mapper/LVNAME
Note: See TracWiki for help on using the wiki.