How to resize a VM’s disk size using LVM within KVM – the hard way. This is done if for some reason you are not able to to use the typical virsh tools or the VM itself is having issues on an automatic resize. This can occur if there are block errors in the physical disk that is assigned to the LVM group.

I have found that this is one of the more complex ways of doing this and should only be used if you are unable to expand the disk of the LVM group inside of the VM. One of the big drawbacks here is you have to shut down the VM, the other huge drawback is potential for data loss.

Firstly, the VM should be shut down and we can add a new partition to increase the LVM disk size. Then start the VM using virsh and KVM and go to the console.

fdisk -u /dev/xvda

(-u shows sectors instead of cylinders. This is important)

(Make a note of the start sector of the LVM partition)
Command (m for help): p

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *        2048      499711      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2          501758    16775167     8136705    5  Extended
/dev/xvda5          501760    16775167     8136704   8e  Linux LVM

(delete the LVM and/or Extended partition)
Command (m for help): d
Partition number (1-5): 2

(Create a new partition)
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)5
e (I choose extended but you may choose extended)
Partition number (1-4): 2 (choose one that's available)


(Create a new extended partition)
Command (m for help): n
Command action
 e extended
 p primary partition (1-4)
e (I choose extended but you may choose extended)
Partition number (5-8): 5 (choose one that's available)

(Fill in the start sector of the LVM partition we deleted above)
First sector (63-16777215, default 63): 501760
Last sector, +sectors or +size{K,M,G} (501760-16777215, default 16777215): (Enter)

(change created partition to Linux LVM)
Command (m for help): t 
Partition number (1-7): 5
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *        2048      499711      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2          501758    16775167     8137727    5  Extended
/dev/xvda5          501760    16777215     8137728   8e  Linux LVM

Command (m for help): w (write)

# reboot

(Resize physical volume)
# pvresize /dev/xvda5

(make a note of the VG "Free PE")
# vgdisplay

(make a note of the "LV Name" of the LV you want to resize)
# lvdisplay 

(Resize LV. Fill in the found "Free PE" and "LV Name" (without the brackets))
# lvresize -l +[Free PE] [LV Name]

(resize the file system)
# resize2fs [LV Name] 
# reboot

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.