Extend logical volume on CentOS

In this article we will learn how to extend a logical volume on a Linux machine. Note that I’m using a VM hosted in my VirtualBox environment so you should cover this article with a similar configuration. Before we proceed to the actual configuration steps make sure that the physical disk has been added to the Virtual Machine and the disk is detected by the OS.
To view the available physical disks type pvdisplay:
Linux LVM
The fields shown in the command’s output are the following:
physical volume device name: /dev/sda2
volume group name: vg_linuxvm
physical volume size: 19.51 GiB / not usable 3.00 MiB
physical extent size: 4.00 MiB – refers to the size of blocks that an be allocated on the disk
total number physical extent: 4994 – the total number of blocks from the hard drive
free number physical extent: 0 – number of blocks not allocated on the disk
allocated number of physical extents: 4994
physical volume unique id: GyF1nY-s4rK-DaIV-bMRX-mNP8-4NMf-RJYYdk
Execute fdisk -l to view the available physical disks, physical partitions and logical partitions:
Format disk on CentOS
As you can see from the command’s output, my current disk /dev/sda with 21,5 GB contains two partitions: /dev/sda1 and /dev/sda2. Note that the second partition has the ID set to 8e which indicates Linux LVM.
Execute df -h to view the current setup of the Virtual Machine:
Disk free command
The system may or may not detect the newly added physical disk if you type fdisk -l again. Note that your server may have multiple host bus adapters, to rescan your HBAs, execute the following command:
echo “Channel Target LUN” > /sys/class/scsi_host/hostX/scan
X is the HBA number
C in the channel on the HBA
T is the SCSI target ID
LUN number
View available HBAs by navigating to /sys/class/scsi_host/.
To force your machine to rescan on one HBA on all channels, all targets and LUNs, execute the following command:
echo “- – -” > /sys/class/scsi_host/host0/scan
If your disk is still not recognized, execute the same command for the rest of HBAs:
echo “- – -” > /sys/class/scsi_host/host1/scan
echo “- – -” > /sys/class/scsi_host/host2/scan
Host bus adapter on Linux servers
Now execute fdisk -l to see if the disk was indeed detected by the machine:
logical volume manager for the Linux kernel
My VM has now detected a secondary physical disk named /dev/sdb with a size of 21.5 GB. Now will partition our newly added disk by executing fdisk /dev/sdb:
Partition physical disks on Linux servers
Press n to  add a new partition then press either e for extended or p for primary partition. Create a primary partition, set the partition number and the first and last cylinders. Once the partition has been created, press p when returned to the fdisk menu to view the newly created partition on the physical disk:
How to format Linux physical disk
Remember that our /dev/sda2 partition had a 8e (Linux LVM) ID so to extend this partition we’ll need to set the same ID on our disk. Press t when in fdisk menu to change a partition’s system id, l to view the list codes and then type the needed partition ID:
Linux physical disk partition ID
If you now press p again you will see that the partition ID has been changed to 8e (Linux VM):
CentOS physical disk partition ID
To write changes to disk press w. Execute fdisk -l to to view the changes applied to the disk:
How to extend logical volumes on CentOS
Now we’ll need to create a physical volume for our new  /dev/sdb1 partition. To achieve this result execute pvcreate /dev/sdb1. Then type pvdisplay to view the newly created physical volume:
Linux physical volume
On the physical volumes we attach volume groups. Note that you can assign multiple physical volumes to a volume group. Type vgdisplay to view the existing volume groups:
Display volumes groups on CentOS
As you can see from the command’s output, my volume group is named vg_linuxvm. You can create a new volume group on a physical volume by executing vgcreate name physical_volumes just like in the following example: vgcreate volume_group1 /dev/sdb4 /dev/sdc3
Use pvscan to rescan for physical volumes and view the newly created one:
How to scan physical volumes
To extend the vg_linuxvm volume group, we’ll have to execute the vgextend vg_linuxvm /dev/sdb1 command. After this operation is completed, type pvdisplay again to see if the VG name has been allocated to the /dev/sdb1 physical volume:
Physical volumes on Linux servers
Now execute vgdisplay to view the new size of the vg_linuxvm volume group:
Display CentOS volume groups
On top of the volume groups we create the logical volumes, you can view them by executing lvdisplay command:
View logical volumes on Linux servers
We can extend the logical volume to a certain size or use the whole /dev/sdb1 partition:
lvextend -L+18G /dev/vg_linuxvm/lv_root
lvextend /dev/vg_linuxvm/lv_root /dev/sdb1
Extend logical volume on CentOs
The last thing we’ll have to do is resize the file system on the /dev/vg_linuxvm/lv_root logical volume so the newly added size is recognized by the Operating System. Execute resize2fs /dev/vg_linuxvm/lv_root and then df -h to see how the logical volume size has increased:
Resize file system command on CentOs
The operation has now been completed and the logical volume has been extended. To summarize this operation you will need to remember that there are four layers when talking about extending a logical volume: hard drives / disk partitions / physical volumes / volume groups and logical volumes. You add a new physical disk to a machine, you assign one or more partitions on that physical disk then you create physical volumes from a particular partition. The physical volumes created are assigned to one volume group (you can assign multiple physical volumes to a single volume group) and from the volume groups we create the logical volumes. You can view this hierarchy by executing lsblk:
Linux logical volumes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s