KVM – Resize guest LVM disk

#
## Logon to KVM host
#

#
## Resize guest volume:
#

# If LVM guest volume:
lvextend +50G /dev/vg_kvm/ubuntu

# If qemu guest volume
qemu-img resize ubuntu.qcow2 +50G

#
## Refresh storage pool :
#

virsh pool-refresh vg_kvm

#
## Logon to KVM guest
#

#
## Check actual disk layout:
#
root@ubuntu:/# fdisk -l

Disk /dev/vda: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 cylinders, total 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00043482

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          501758   209713151   104605697    5  Extended
/dev/vda5          501760   209713151   104605696   8e  Linux LVM

Disk /dev/mapper/ubuntu-root: 98.6 GB, 98612281344 bytes
255 heads, 63 sectors/track, 11988 cylinders, total 192602112 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/ubuntu-root doesn’t contain a valid partition table

Disk /dev/mapper/ubuntu-swap_1: 8480 MB, 8480882688 bytes
255 heads, 63 sectors/track, 1031 cylinders, total 16564224 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/ubuntu-swap_1 doesn’t contain a valid partition table

root@ubuntu:~# fdisk /dev/vda

Command (m for help): p

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00043482

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          501758   209713151   104605697    5  Extended
/dev/vda5          501760   209713151   104605696   8e  Linux LVM

Command (m for help): d
Partition number (1-5): 5

Command (m for help): n

root@ubuntu:~# fdisk /dev/vda

Command (m for help): p

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00043482

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          501758   209713151   104605697    5  Extended
/dev/vda5          501760   209713151   104605696   8e  Linux LVM

#
## Edit disk layout, delete LVM and Extended partition
#

Command (m for help): d
Partition number (1-5): 5

Command (m for help): d
Partition number (1-5): 2

#
## Recreate LVM partition
#

Command (m for help): n
Partition type:
p   primary (1 primary, 0 extended, 3 free)
e   extended
Select (default p): p
Partition number (1-4, default 2):
Using default value 2
First sector (499712-314572799, default 499712): 501760
Last sector, +sectors or +size{K,M,G} (501760-314572799, default 314572799):
Using default value 314572799

Command (m for help): p

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00043482

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          501760   314572799   157035520   83  Linux

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

Command (m for help): p

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00043482

Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          501760   314572799   157035520   8e  Linux LVM

#
## Reboot
#

reboot

#
## Resize PV
#

root@ubuntu:~# pvresize /dev/vda2
Physical volume “/dev/vda2” changed
1 physical volume(s) resized / 0 physical volume(s) not resized

#
## Extend LV
#

root@ubuntu:~# lvextend -L +30G /dev/ubuntu/root
Extending logical volume root to 121.84 GiB
Logical volume root successfully resized

#
## Resize filesysten
#

root@ubuntu:~# resize2fs /dev/ubuntu/root
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/ubuntu/root is mounted on /; on-line resizing required
old_desc_blocks = 6, new_desc_blocks = 8
Performing an on-line resize of /dev/ubuntu/root to 31939584 (4k) blocks.
The filesystem on /dev/ubuntu/root is now 31939584 blocks long.

#
## Check free space
#

root@ubuntu:~# df -h
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu-root     122G   76G   40G  66% /
udev                             3.9G  4.0K  3.9G   1% /dev
tmpfs                            1.6G  260K  1.6G   1% /run
none                             5.0M     0  5.0M   0% /run/lock
none                             3.9G     0  3.9G   0% /run/shm
/dev/vda1                        228M   49M  168M  23% /boot
smtlaqnap1.garda.ca:/GARDA/home   55T   14T   41T  25% /home
root@ubuntu:~#