Thursday 17 March 2016

Managing Physical and Logical Volumes with LVM in CentOS

Physical Volumes

When creating a physical volume (from disks) the disks MUST have no partition tables (be it MBR or GPT) - see my post about removing GPT/MR partition tables for more info.

To create the physical volume we can issue:

sudo pvcreate /dev/sda 
sudo pvcreate /dev/sdb

We can also create a physical volume from partitions e.g.

sudo pvcreate /dev/sda1 
sudo pvcreate /dev/sdb1

and then review them with:

sudo pvdisplay

To get an overview of disks that can be used we can issue:

sudo lvmdiskscan


sudo pvscan

Logical Volumes

Now that we have created our physical volumes we can now add them to our logical volumes - but we must first create a volume group with:

vgcreate myVolumeGroup /dev/sda /dev/sdb /dev/sdc

We can now create a (100GB) logical volume:

lvcreate -L 100G myVolumeGroup

(outputs - lvol0)

We can then partition the logical disk like so:

fdisk /dev/myVolumeGroup/lvol0

create a partition and then create the filesystem:

mke2fs -j -t ext4 /dev/myVolumeGroup/lvol0

We can now dynamically resize if we wish to increase the disk size - if we wanted to do this  we would firstly add a new disk to the logival volume group:

vgextend myVolumeGroup /dev/sdp

and then resize the logical disk:

lvextend -L 500G /dev/myVolumeGroup/lvol0

then resize / recreate the partition with fdisk:

fdisk /dev/myVolumeGroup/lvol0

and finally use resize2fs to resize the file system:

resize2fs /dev/myVolumeGroup/lvol0


Post a Comment