Pawan Sharma | May 1, 2012 | Be the first to comment!

LVM Part 2: Creating PV, VG and LV

In this post we will see how to create LVM partitions. LVM is one of the most important objectives of both RHCSA and RHCE. Also it is the most important and useful tool for Linux system administrators as it provides facility of extending and reducing partitions online.
LVM covers following topics.
  1. Creating Physical Volumes
  2. Creating Volume Group.
  3. Creating Logical Volumes.
  4. Rename Volume Group.
  5. Rename Logical Volume.
  6. Extending Logical Volume Online.
  7. Extending Logical Volume Online.
  8. Reducing Volume Group.
  9. Deleting Logical Volume.
  10. Deleting Volume Group
  11. Deleting Physical Volume.
In the previous post I mentioned that the Physical Volume can be a partition or a whole disk.  To see current partitions and disks use fdisk –l. In this example I will use the whole disk to create a Physical Volume.

1. First select the disk from which you have to create the Physical Volume. In this example I am using the second and third disk (sdb and sdc) to create two PV.

2. Use fdisk to create partition on the required hard disc and create a partition of whole disk as shown below.
Partition manager in RHEL 6

3. Now change type of partition to LINUX LVM. For that press t to edit the partition type and select 8e (Linux LVM) and use p to print partition table and save it using wq.

Disk partitioning in RHEL6

Note. It is not required to change the partition type to 8e (Linux LVM), this is just for the refrence.

4. Now use pvcreate command to create the Physical Volume. Ex pvcreate /dev/sdb1, I have created one more PV with disc sdc, and use pvdisplay/pvscan command to view Physical volume.
Disk partitioning in RHEL6
5. Now create Volume Group using these two PVs.

Ex: vgcreate <VG_NAME> /dev/sdb1 /dev/sdc. And use vgscan/vgdisplay command to view the VG.
Disk partitioning in RHEL6

In the above image we can see that there is 3.99GB/1022 PE free space we will create two LVs of 1 GB each.

6. From the above volume group we can create logical volumes as we like, in this example I will create 2 logical volumes each of size 1 GB.

lvcreate –L 1G –n lvdata MYVG0
-L is for size
-n is for lvname.

MYVG01 is VG name from which we have to create the LV.

The above command will create a LV of size 1GB. We can see the created logical volume by lvscan/lvdisplay command.
Disk partitioning in RHEL6

In this example we will create LV using physical extents. In vgdisplay command we can see that Physical Extent size is 4Mb therefore to create LV of size 1Gb we need 256 Physical Extents (PE).
lvcreate –l 256 –n lvdata1 MYVG01
Here –l is for number of PE.

     Disk partitioning in RHEL6

7. Now to use these LVs first we have to create file system (format) of these LVs using mkfs.ext4 command.
  • mkfs.ext4 /dev/MYVG01/lvdata
  • mkfs.ext4 /dev/MYVG01/lvdata1
8. Now to mount these LVs on directories, first create two directories and then use mount command to mount these LVs. To make these changes persistent make entry in fstab.

Disk partitioning in RHEL6

Disk partitioning in RHEL6

In the next post we will see how to rename Volume Groups and Logical Volumes. If you have any questions and queries please post comments.

No comments:

Post a Comment