Complete guide to extend guest disk from backend, LVM and Filesystem inside KVM guest.

First of all this is my guest system layout. There is a disk called ‘vda’ where my guest filesystem resides.


IN GUEST

[terminal]
[root@localhost ~]# fdisk -l

Disk /dev/vda: 6442 MB, 6442450944 bytes, 12582912 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: 0x000b31c1

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 1026047 512000 83 Linux
/dev/vda2 1026048 12582911 5778432 8e Linux LVM

Disk /dev/mapper/fedora-swap: 4227 MB, 4227858432 bytes, 8257536 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 /dev/mapper/fedora-root: 1686 MB, 1686110208 bytes, 3293184 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
[/terminal]

Two guest logical volumes ‘fedora-swap’ and ‘fedora-root’ are created inside guest physical volume vda. Discard ‘fedora-swap’ for now.

The physical volume layout is shown below:

[terminal]
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 fedora lvm2 a– 5.51g 0

[/terminal]

Now guest image is on a file in host filesystem called ‘nested-guest-f18’.

If your guest system is on a Logical Volume in your host system please refer this article

If I check ‘qemu-img’ information on this guest disk file, I can see

in HOST

[terminal]
[root@humbles-lap vmimgs]# qemu-img info nested-guest-f18.qcow2
image: nested-guest-f18.qcow2
file format: qcow2
virtual size: 6.0G (6442450944 bytes)
disk size: 1.5G
cluster_size: 65536

[/terminal]

So, virtual size is “6.0” G and its a sparse file.

Let me resize the size from qemu-img resize command

[terminal]
[root@humbles-lap vmimgs]# qemu-img resize nested-guest-f18.qcow2 +8G

Image resized.
[/terminal]

Check it now:

[terminal]
[root@humbles-lap vmimgs]# qemu-img info nested-guest-f18.qcow2
image: nested-guest-f18.qcow2
file format: qcow2
virtual size: 14G (15032385536 bytes)
disk size: 1.5G
cluster_size: 65536
[root@humbles-lap vmimgs]#
[/terminal]

Looks good.

Now, the changes have been reflected inside guest as you can see in fdisk output..

[terminal]
[root@localhost ~]# fdisk -l

Disk /dev/vda: 15.0 GB, 15032385536 bytes, 29360128 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: 0x000b31c1

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 1026047 512000 83 Linux
/dev/vda2 1026048 12582911 5778432 8e Linux LVM

Disk /dev/mapper/fedora-swap: 4227 MB, 4227858432 bytes, 8257536 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 /dev/mapper/fedora-root: 1686 MB, 1686110208 bytes, 3293184 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

How-ever LVM layout looks same :

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 fedora lvm2 a– 5.51g 0
[root@localhost ~]#

[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
fedora 1 2 0 wz–n- 5.51g 0

[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
root fedora -wi-ao— 1.57g
swap fedora -wi-ao— 3.94g
[root@localhost ~]#

[/terminal]

Now, lets create a new partition on vda called vda3.

[terminal]

[root@localhost ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.22.1).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): e
Partition number (1-4, default 3):
Using default value 3
First sector (12582912-29360127, default 12582912):
Using default value 12582912
Last sector, +sectors or +size{K,M,G} (12582912-29360127, default 29360127):
Using default value 29360127
Partition 3 of type Extended and of size 8 GiB is set

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]#

Disk /dev/vda: 15.0 GB, 15032385536 bytes, 29360128 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: 0x000b31c1

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 1026047 512000 83 Linux
/dev/vda2 1026048 12582911 5778432 8e Linux LVM
/dev/vda3 12582912 29360127 8388608 5 Extended

Disk /dev/mapper/fedora-swap: 4227 MB, 4227858432 bytes, 8257536 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 /dev/mapper/fedora-root: 1686 MB, 1686110208 bytes, 3293184 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

[/terminal]

Let me toggle the partition type to LVM :

[terminal]
[root@localhost ~]#fdisk /dev/vda
Partition 3 of type Linux and of size 8 GiB is set

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

Command (m for help):

Disk identifier: 0x000b31c1

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 1026047 512000 83 Linux
/dev/vda2 1026048 12582911 5778432 8e Linux LVM
/dev/vda3 12582912 29360127 8388608 8e Linux LVM

Disk /dev/mapper/fedora-swap: 4227 MB, 4227858432 bytes, 8257536 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 /dev/mapper/fedora-root: 1686 MB, 1686110208 bytes, 3293184 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

[/terminal]

Once its done, let us create a PV on vda3

[terminal]
[root@localhost ~]# pvcreate /dev/vda3
Physical volume “/dev/vda3” successfully created
[root@localhost ~]#

[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 fedora lvm2 a– 5.51g 0
/dev/vda3 lvm2 a– 8.00g 8.00g

Its the turn to extend the fedora volume group..

[root@localhost ~]# vgextend fedora /dev/vda3
Volume group “fedora” successfully extended
[root@localhost ~]#
[/terminal]

Now let us extend the logical volume called ‘root’ :

[terminal]
[root@localhost ~]# lvextend -L +6G /dev/fedora/root
Extending logical volume root to 7.57 GiB
Logical volume root successfully resized
[root@localhost ~]#

Once the underlying LVM has enough space, let us try to increase the ext filesystem online..

[root@localhost ~]# resize2fs /dev/fedora/root
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/fedora/root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/fedora/root is now 1984512 blocks long.

[root@localhost ~]#

Disk /dev/vda: 15.0 GB, 15032385536 bytes, 29360128 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: 0x000b31c1

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 1026047 512000 83 Linux
/dev/vda2 1026048 12582911 5778432 8e Linux LVM
/dev/vda3 12582912 29360127 8388608 8e Linux LVM

Disk /dev/mapper/fedora-swap: 4227 MB, 4227858432 bytes, 8257536 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 /dev/mapper/fedora-root: 8128 MB, 8128561152 bytes, 15876096 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

[root@localhost ~]#

[/terminal]

LVM layer lists the new layout with an increased space of logical volumes.

[terminal]
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 fedora lvm2 a– 5.51g 0
/dev/vda3 fedora lvm2 a– 8.00g 2.00g
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
fedora 2 2 0 wz–n- 13.50g 2.00g
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
root fedora -wi-ao— 7.57g
swap fedora -wi-ao— 3.94g
[root@localhost ~]#
[/terminal]

All good? if you have any doubts, please feel free to ask in the comments section.

Digiprove sealCopyright secured by Digiprove © 2020 Humble Chirammal

1 thought on “Complete guide to extend guest disk from backend, LVM and Filesystem inside KVM guest.”

Comments are closed.