Logical Volume Creation in Linux

This is a short tutorial for creating LVM in Linux system. By using some simple commands we can create linear volumes, striped volumes, and mirrored volumes.There are different applications are available to create LVM.
Assume, we have two hard disk each having 2GB size.

Use below command in command line to show the total disk.

# fdisk -l 2>/dev/null | grep ‘/dev/sd[a1-b1]’
Disk /dev/sda1: 2147 MB, 2147483646 bytes
Disk /dev/sdb1: 2147 MB, 2147483646 bytes

From this output we can see that two disk with 2GB size. The following steps will show you how the logical volume is creating.

Step: 1
Make sure the volume is a physical volume. You can use below command to create physical volume.

# pvcreate /dev/sda1
Writing physical volume data to disk “/dev/sda1”
Physical volume “/dev/sda1” successfully created

# pvcreate /dev/sdb1
Writing physical volume data to disk “/dev/sdb1”
Physical volume “/dev/sdb1” successfully created

Then we can execute command “lvmdiskscan” to verify that the hard disks are properly created.

# lvmdiskscan
/dev/sda1 (2.00 GiB) LVM physical volume
/dev/sdb1 (2.00 GiB)LVM physical volume
2 disks
2 LVM physical volume whole disks
0 LVM physical volumes

Step: 2

In this step we have to create volume groups, for that we are using the command “vgcreate” as follows.

# vgcreate vg_sda1 /dev/sda1
Volume group “vg_sda” successfully created

# vgcreate vg_sda1 /dev/sdb1
Volume group “vg_sdb1” successfully created

Step 3:

As I mentioned,the logical volume is classified into three :- linear volumes, striped volumes, and mirrored volumes.

Here I am using “lvcreate” command to create the logical volume.

Create linear volume in each volume groups.

# lvcreate –extents –name lv_sda1 vg_sda1
Logical volume “lv_sda1” created

# lvcreate –extents –name lv_sdb1 vg_sdb1
Logical volume “lv_sdb1” created

The “lvcreate” takes a lot of options. Do check its man page to know more.

Step: 4
check/verify our work by using the commands “vgscan” and “lvscan” respectively.

# vgscan
Foundvolumegroup “vg_sdb1” using metadata type lvm2
Foundvolumegroup “vg_sda1” using metadata type lvm2

# lvscan
ACTIVE ‘/dev/vg_sdb1/lv_sdb1’ [2.00 GiB] inherit
ACTIVE ‘/dev/vg_sda1/lv_sda1’ [2.00 GiB] inherit

These logical volumes are in “/dev” directory in the format “/dev/vg/lv”
where vg is volume group, and “lv” is logical volume. In our case it will be –

/dev/vg_sda1/lv_sda1 & /dev/vg_sdb1/lv_sdb1

The run “lvdisplay” to see the details of the logical volume that I created.

# lvdisplay

++Logical volume++

LV Name /dev/vg_sdb1/lv_sdb1
VG Name vg_sdb1
LV UUID ztRveJ-ZCsD-gIXd-Uzfl-UObO-8CYF-xdXb72
LV Write Access read/write
LV Status available
# open 0

LV Size 2.00 GiB
Current LE 513
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 257
Block device 254:4
— Logical volume —
LV Name /dev/vg_sda1/lv_sda1
VG Name vg_sda1
LV UUID gTG0Sz-TiF9-3bmF-C7U5-FlqJ-TbZG-eYULdf
LV Write Access read/write
LV Status available
# open 0
LV Size 2.00 GiB
Current LE 513
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 254:3

Now we can use the logical volumes, the same way we can use physical disks. All these commands for mounting disks, creating file systems, formatting and will work on the logical volumes.

At last we have to do two important things before start using this logical volume

Format the logical volume, and Mount the logical volume in a suitable location

Formatting the logical volume involves creating a file system on it. We use the “mke2fs” command to create a file system.

# mkfs.ext4 /dev/vg_sda1/lv_sda1

# mkfs.ext4 /dev/vg_sdb1/lv_sdb1

Then mount the logical volume in suitable location.

# mkdir /mnt/volume-a
# mount -t ext4 /dev/vg_sda1/lv_sda1/mnt/volume-a

similarly

# mkdir /mnt/volume-b
# mount -t ext4 /dev/vg_sdb1/lv_sdb1 /mnt/volume-b

If we need to mount the logical volume(s) each time we boot into Linux, we will have to enter the proper line in the /etc/fstab file.