Saturday, 5 June 2021

Extending a logical volume in a virtual machine running Red Hat or Cent OS

To extend the logical volume:

Recommends taking a complete backup of the virtual machine prior to making these changes.

 This provides steps for extending the root partition residing in a logical volume created with Logical Volume Manager (LVM) in a virtual machine running Red Hat/CentOS

My Virtual mechi have only 20GB I have incread to 40 GB like as below screenshort.

1.         Power off the virtual machine.
2.         Edit the virtual machine settings and extend the virtual disk size. (Refer to the above picture)
3.         Power on the virtual machine.
4.         Identify the device name, which is by default /dev/sda, and confirm the new size by running the command:

fdisk -l





















Create a new primary partition:

1.         fdisk /dev/sda (depending on the results of step 4)

2.         Press p to print the partition table to identify the number of partitions. By default, there are 2: sda1 and     sda2.

3.         Press n to create a new primary partition.

4.         Press p for primary.

5.         Press 3 for the partition number, depending on the output of the partition table print.

6.         Press Enter two times.

7.         Press to change the system's partition ID.

8.         Press 3 to select the newly creation partition.

9.         Type 8e to change the Hex Code of the partition for Linux LVM.

10.     Press to write the changes to the partition table.

11.     I have done the above points. See the 2 screenshots below for our reference



























 Increasing the logical volume

We use the pvcreate command which creates a physical volume for later use by the logical volume manager (LVM). In this case, the physical volume will be our new /dev/sda3 partition.


Next, we need to confirm the name of the current volume group using the vgdisplay command. The name will vary depending on your setup, for me it is the name of my test server. vgdisplay provides lots of information on the volume group, I have only shown the name and the current size of it for this example.















Now we extend the ‘vg_ibikz’ volume group by adding in the physical volume of /dev/sda3 which we created using the pvcreate command earlier.












Using the pvscan command we scan all disks for physical volumes, this should confirm the original /dev/sda5 partition and the newly created physical volume /dev/sda3


Next, we need to increase the logical volume (rather than the physical volume) which basically means we will be taking our original logical volume and extending it over our new partition/physical volume of /dev/sda3.

Firstly confirm the path of the logical volume using lvdisplay. This pathname will vary depending on your setup.

The logical volume is then extended using the lvextend command.

 








There is then one final step which is to resize the file system so that it can take advantage of this additional space, this is done using the resize2fs command for ext-based file systems. Note that this may take some time to complete, it took about 30 seconds for my additional space.












 

Alternatively, if you’re running the XFS file system (default as of RedHat/CentOS 7) you can grow the file system with “xfs_growfs /dev/vg_ibikz/Iv_root”.

That’s it, now with the ‘df’ command, we can see that the total available disk space has been increased.



Summary

With this method, we have increased the virtual disk drive through VMware, created a new partition out of this newly unallocated space within the guest OS, turned it into a physical volume, extended the volume group, and then finally extended the original logical volume over the newer physical volume resulting in overall disk space being increased successfully.