How to Create an LVM Logical Volume

0
681

In this tutorial, we are going to learn about how to create LVM logical volumes. Before we dive deeper, let us have a look at LVM architecture.

LVM ARCHITECTURE OVERVIEW

The structure of an LVM disk environment is simply illustrated in the figure below.

LVM Logical Volumes Components

The underlying structure of an LVM environment is a block device such a partition or a whole disk which is initialized a Physical Volume (PV). A single or multiple block devices can be grouped together to form a PV.

The physical volumes can be combined into a Volume Group (VG). This creates a pool of disk space out of which LVM logical volumes (LVs) can be allocated. A top logical volumes, regular file systems, such as ext4, xfs, etc. can then be created. This process is comparable to the how disks are divided into partitions.

So, basically, LVM disk environment is made up of three components, i.e Physical Volume, Volume Group and Logical Volume as depicted in the architecture above. In order to create LVM logical volumes, here is a basic four step procedure:

  • Create partitions to be used and initialize them as physical volumes
  • Create a volume group
  • Create a logical volume
  • Create a file system on a logical volume

In the following sections, we will be going through various LVM configuration examples.

Creating an LVM logical volume

We will be following the above outlined steps to create an LVM logical volume. So before you can create any logical volume, you need to initialize the disk as PV using the command, pvcreate.
In our case, we have two uninitialized disks, /dev/sdb1 and /dev/sdc1 and we will be using both of them to create LVM logical volumes.

First, let us initialize our disks as physical volume:

# pvcreate /dev/sdb1 /dev/sdc1
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc1" successfully created.

To confirm this, you can run the command, pvs, to display information about physical volumes.

# pvs
PV            VG     Fmt Attr PSize PFree
/dev/sda2     centos lvm2 a-- <9.00g 0
/dev/sdb1            lvm2 --- <2.00g <2.00g
/dev/sdc1            lvm2 --- <2.00g <2.00g

As you can see, our two physical volumes have no volume group assigned to them yet.  Now, create a volume group that consists of the physical volumes created above. To create a volume group, run the command below;

# vgcreate test_vol_group /dev/sdb1 /dev/sdc1
Volume group "test_vol_group" successfully created

where test_vol_group is the name of our volume group and /dev/sdb1, /dev/sdc1 are our physical volumes created above.

You can use the vgs command to display the attributes of the new volume group.

# vgs
VG               #PV  #LV  #SN     Attr    VSize  VFree
centos            1     2     0    wz--n-  <9.00g   0
test_vol_group    2     0     0    wz--n-  3.99g  3.99g

Now that we have the volume group, let us create a logical volume, test_log_volume, that will occupy 1G of the volume group space.

# lvcreate -L 1G -n test_log_volume test_vol_group
Logical volume "test_log_group" created.

where –L specifies the size and –n specifies the name of the logical volume. test_vol_group is our volume group created above.
You can use the lvs command to display the attributes of the new volume group.

# lvs
LV             VG             Attr       LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root           centos         -wi-ao----  <8.00g
swap           centos         -wi-ao----   1.00g
test_log_group test_vol_group -wi-a-----   1.00g

So we now have a 1G logical volume. Let us make it usable by creating a file system on it. The following command creates a xfs file system on the logical volume. We will use mkfs.xfs command in its simplest form.

# mkfs.xfs /dev/test_vol_group/test_log_group

Next,create a mount point and mount the logical volume.

# mkdir /tmp/mnt
# mount /dev/test_vol_group/test_log_group /tmp/mnt/

Let us check the logical volume filesystem and disk space usage.

#df -hT
Filesystem                                Type  Size Used Avail Use% Mounted on
/dev/mapper/centos-root                   xfs   8.0G  1.1G 7.0G 14% /
...
/dev/mapper/test_vol_group-test_log_group xfs   1014M 33M  982M 4% /tmp/mnt

Upto there, we have created a usable LVM logical volume. In our next  tutorial, we will be learning how to remove a disk from a logical volume.
Stay tunned.

LEAVE A REPLY

Please enter your comment!
Please enter your name here