Monday, 11 May 2015

Configure LVM (Logical Volume Management) in RHEL7


The physical disks, or disk partitions, such as /dev/sda or /dev/sda1 are called "physical volumes". Using LVM, we can combine multiple "physical volumes" into a "volume group". 

The size of a "volume group" is the combined size of all the constituent "physical volumes". For example, 4x10Gb drives can be combined into one 40Gb volume group. "Physical Volumes" can also be added on demand to increase the size of the "volume group".

The total capacity of a volume group can then be allocated to "logical volumes", which are accessed as regular block devices. For example, in a 40Gb volume group, we can create two 20Gb logical volumes. It is the "logical volumes" on which we create a file system and mount for data storage.

Thus, LVM Configuration consists of the following 3 steps:
1) Initialize physical volumes
2) Add the physical volumes to a volume group
3) Create logical volumes from the volume group.

Consider 2 disks '/dev/sda' and  '/dev/sdb' of size 1Gb each. We will combine these 2 disks into a "volume group", which will be of size 2Gb.

From this "volume group", we will create a logical volume of size 1Gb. We will create 'xfs' filesystem on the logical volume, and mount it to store files.

We will add another disk '/dev/sdc' of size '2Gb' to the volume group. The size of the volume group will now be 4Gb.

We will then increase the size of the "logical volume" to 3Gb.

Perform the following steps:

1) Create partition '/dev/sda1' on disk '/dev/sda' and '/dev/sdb1' on disk '/dev/sdb' using 'fdisk' or 'parted'.
    NOTE: Although you can directly initialize a disk as a physical volume, it is recommended that you first create a partition on the disk, and then initialize the partition as a physical volume.

2) Initialize the partitions as physical volumes.
[root@server3 ~]# pvcreate /dev/sda1
  Physical volume "/dev/sda1" successfully created

[root@server3 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created


3) Create a volume group consisting of the  physical volumes initialized above.
[root@server3 ~]# vgcreate vg_linux /dev/sda1 /dev/sdb1
  Volume group "vg_linux" successfully created

The name of the volume group is 'vg_linux' and size is 2Gb (the combined size of both the physical volumes) as shown below.
[root@server3 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_linux
  Format                lvm2
  VG Access             read/write
  VG Status             resizable
  VG Size               1.99 GiB
  PE Size               4.00 MiB
  Total PE              510
  Alloc PE / Size       0 / 0  
  Free  PE / Size       510 / 1.99 GiB
  VG UUID               7e3JSb-YXhr-LLru-vZBr-qsM1-qqxi-bHRb0p


4) Create a logical volume of size 1Gb, which you can mount and actually use.
[root@server3 ~]# lvcreate -L 1G -n lv_data vg_linux
  Logical volume "lv_data" created
 
The logical volume can now be accessed as '/dev/vg_linux/lv_data'.

We can see that from the total size of the volume group, 1Gb has been allocated and 1Gb is free, as shown below.
[root@server3 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_linux
  Format                lvm2
  VG Access             read/write
  VG Status             resizable
  VG Size               1.99 GiB
  PE Size               4.00 MiB
  Total PE              510
  Alloc PE / Size       256 / 1.00 GiB
  Free  PE / Size       254 / 1016.00 MiB
  VG UUID               7e3JSb-YXhr-LLru-vZBr-qsM1-qqxi-bHRb0p


4) Create filesystem on the logical volume 'lv_data'.
[root@server3 ~]# mkfs -t xfs /dev/vg_linux/lv_data

5) Create mount point
[root@server3 ~]# mkdir /var/data

6) Mount the logical volume 'lv_data' and create files in it.
[root@server3 ~]# mount -t xfs /dev/vg_linux/lv_data  /var/data
[root@server3 ~]# touch /var/data/foo{1,2,3}
[root@server3 ~]# ls /var/data/
foo1  foo2  foo3

7) Add the following entry in '/etc/fstab' (to automatically mount the logical volume at boot time).
          /dev/vg_linux/lv_data            /var/data         xfs           defaults        1  2


Increase the size of the Volume Group (VG) 
Consider disk '/dev/sdc' of size 2Gb, which we will add to the volume group

1) Create partition '/dev/sdc1' on disk '/dev/sdc' using 'fdisk' or 'parted'.
    NOTE: Although you can directly initialize a disk as a physical volume, it is recommended that you first create a partition on the disk, and then initialize the partition as a physical volume.

2) Initialize the partition as physical volume.
[root@server3 ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created

3) Add the physical volume to the volume group
[root@server3 ~]# vgextend vg_linux /dev/sdc1
  Volume group "vg_linux" successfully extended

The size of the volume group has now been increased to 4Gb, of which 3Gb is free, as shown below.
[root@server3 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_linux
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  VG Size               3.99 GiB
  PE Size               4.00 MiB
  Total PE              1021
  Alloc PE / Size       256 / 1.00 GiB
  Free  PE / Size       765 / 2.99 GiB
  VG UUID               7e3JSb-YXhr-LLru-vZBr-qsM1-qqxi-bHRb0p


Extend the size of Logical Volume(LV) 

Consider logical volume '/dev/vg_linux/lv_data' mounted on '/var/data'
1) Unmount the logical volume
[root@server3 ~]# umount /dev/vg_linux/lv_data

2) Increase the size of the logical volume to 3Gb.
[root@server3 ~]#  lvextend -L 3G /dev/vg_linux/lv_data
  Extending logical volume lv_data to 3.00 GiB
  Logical volume lv_data successfully resized

3) Mount the logical volume
[root@server3 ~]# mount -t xfs /dev/vg_linux/lv_data  /var/data

4) Resize the filesystem.(XFS filesystem)
[root@server3 ~]# xfs_growfs /dev/vg_linux/lv_data

5) Confirm the new size.
[root@server3 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/vg_linux-lv_data  3.0G   33M  3.0G   2% /var/data


Delete a Logical Volume(LV)  1) You can delete a logical volume as follows:
[root@server3 ~]# lvremove /dev/vg_linux/lv_data
Do you really want to remove active logical volume lv_data? [y/n]: y
  Logical volume "lv_data" successfully removed

No comments:

Post a Comment