Add extra space to KVM Guest root partition

Step1: Add a second virtual disk using virt-manager.

Open the virtualmachine and click View->Details and click the button at the bottom left "+Add Hardware" select Storage and ensure Bus_Type is virtio.

if you now do dmesg you will see that a new disk has been created.

doing fdisk -l will show that we have a new disk vdb

[root@rhel-lab1 ~]# fdisk -l

Disk /dev/vda: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00010ce0

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       16645     7875584   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/vg_rhellab1-lv_root: 7205 MB, 7205814272 bytes
255 heads, 63 sectors/track, 876 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_rhellab1-lv_swap: 855 MB, 855638016 bytes
255 heads, 63 sectors/track, 104 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/vdb: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Create 1 partition on /dev/vdb

Normally this should not be needed and we could just use /dev/vdb but it is good practice to use a partition

[root@rhel-lab1 ~]# fdisk /dev/vdb

Extend the VolumeGroup that contains the / to this new PhysicalVolume

[root@rhel-lab1 ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created
[root@rhel-lab1 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               vg_rhellab1
  PV Size               7.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              1922
  Free PE               0
  Allocated PE          1922
  PV UUID               5V7W0j-e28x-08OO-oZnh-Xkjv-8e09-tYd30m
   
  "/dev/vdb1" is a new physical volume of "8.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               8.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               9uigVa-ZsEF-I51J-snNL-2tXn-LvLo-vseIGm

Since our task is to extend the add extra space to the root partition, we need to extend the VolumeGroup that is used by the root partition onto this physical volume.

[root@rhel-lab1 ~]# vgextend vg_rhellab1 /dev/vdb1 
  Volume group "vg_rhellab1" successfully extended

We can use the pvscan command to display our physical volumes and confirm that we now have our VolumeGroup called vg_rhellab1 extended onto the /dev/vdb1

[root@rhel-lab1 ~]# pvscan
  PV /dev/vda2   VG vg_rhellab1   lvm2 [7.51 GiB / 0    free]
  PV /dev/vdb1   VG vg_rhellab1   lvm2 [8.00 GiB / 8.00 GiB free]
  Total: 2 [15.50 GiB] / in use: 2 [15.50 GiB] / in no VG: 0 [0   ]

Extend the Logical Volume.

At this point if you do df, the root partition is still the same size as it was. We have the final step of extending the logical volume for the root partitionthat resides on vg_rhellab1.

[root@rhel-lab1 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg_rhellab1/lv_root
  LV Name                lv_root
  VG Name                vg_rhellab1
  LV UUID                WZOdt5-iZuV-ZD0j-aEG1-kGMT-MJ5m-rqTlQp
  LV Write Access        read/write
  LV Creation host, time rhel-lab1.localhost, 2014-12-16 09:23:07 +0100
  LV Status              available
  # open                 1
  LV Size                6.71 GiB
  Current LE             1718
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vg_rhellab1/lv_swap
  LV Name                lv_swap
  VG Name                vg_rhellab1
  LV UUID                sa8S0F-g2Mk-TRw6-5VSa-bKce-9jvA-DodNiI
  LV Write Access        read/write
  LV Creation host, time rhel-lab1.localhost, 2014-12-16 09:23:10 +0100
  LV Status              available
  # open                 1
  LV Size                816.00 MiB
  Current LE             204
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
  • And now we arrive to the final step lvextend*
[root@rhel-lab1 ~]# lvextend -L +4G /dev/vg_rhellab1/lv_root /dev/vdb1 
  Extending logical volume lv_root to 10.71 GiB
  Logical volume lv_root successfully resized
[root@rhel-lab1 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_rhellab1/lv_root
  LV Name                lv_root
  VG Name                vg_rhellab1
  LV UUID                WZOdt5-iZuV-ZD0j-aEG1-kGMT-MJ5m-rqTlQp
  LV Write Access        read/write
  LV Creation host, time rhel-lab1.localhost, 2014-12-16 09:23:07 +0100
  LV Status              available
  # open                 1
  LV Size                10.71 GiB
  Current LE             2742
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vg_rhellab1/lv_swap
  LV Name                lv_swap
  VG Name                vg_rhellab1
  LV UUID                sa8S0F-g2Mk-TRw6-5VSa-bKce-9jvA-DodNiI
  LV Write Access        read/write
  LV Creation host, time rhel-lab1.localhost, 2014-12-16 09:23:10 +0100
  LV Status              available
  # open                 1
  LV Size                816.00 MiB
  Current LE             204
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

We can see that the lv_root has grown but if we do a du -h the disk size is still small. We have one more step: resize2fs

Filesystem at /dev/vg_rhellab1/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg_rhellab1/lv_root to 2807808 (4k) blocks.
The filesystem on /dev/vg_rhellab1/lv_root is now 2807808 blocks long.

[root@rhel-lab1 ~]# df -h
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/vg_rhellab1-lv_root   11G  1.3G  8.8G  13% /
tmpfs                            499M     0  499M   0% /dev/shm
/dev/vda1                        485M   33M  427M   8% /boot