How to Create and Manage LVM in CentOS

Steps to Manage and Create LVM Using vgcreate, lvcreate and lvextend Commands

Logical Volume Management is used for creating multiple logical volumes which means allocating disks space, mirroring and resize volumes in any method without any data loss. LFCS: To Manage and Create LVM Using vgcreate, lvcreate and lvextend Commands is explained in this article.

The structure of the LVM consists of

  • Numerous logical volumes can be created in a volume group with the resize option.
  • The volume group created is a single storage unit.
  • One or more partition or hard disk is configured as physical volumes (PVs).

To Creating Physical Volumes, Volume Groups, and Logical Volumes

Run the following command to create physical volumes on /dev/sda5, /dev/sda6, and /dev/sda7:

[root@linuxhelp ~]# pvcreate /dev/sda5 /dev/sda6 /dev/sda7
  Physical volume " /dev/sda5"  successfully created
  Physical volume " /dev/sda6"  successfully created
  Physical volume " /dev/sda7"  successfully created

and to list the newly created PVs run the following

[root@linuxhelp ~]# pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sda5       lvm2 a--  3.00g 3.00g
  /dev/sda6       lvm2 a--  3.00g 3.00g
  /dev/sda7       lvm2 a--  3.00g 3.00g

To get detailed information about each PV use the following command.

[root@linuxhelp ~]# pvdisplay /dev/sda5
  " /dev/sda5"  is a new physical volume of " 3.00 GiB" 
  --- NEW Physical volume ---
  PV Name               /dev/sda5
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               a0iS9I-ZFIi-bEU0-Jt08-hGu5-BJ4S-knR5ZQ

[root@linuxhelp ~]# pvdisplay /dev/sda6
  " /dev/sda6"  is a new physical volume of " 3.00 GiB" 
  --- NEW Physical volume ---
  PV Name               /dev/sda6
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Zv9U3D-iS2x-8vzk-XAS2-vdVa-j8s9-sXJe2i

[root@linuxhelp ~]# pvdisplay /dev/sda7
  " /dev/sda7"  is a new physical volume of " 3.00 GiB" 
  --- NEW Physical volume ---
  PV Name               /dev/sda7
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               LX8D8N-KTUX-iNp8-bE6L-WSOP-Ledp-63SrbK

For generate volume group with the name vg1 using /dev/sda5 and /dev/sda6 use the following command.

[root@linuxhelp ~]# vgcreate vg1 /dev/sda5 /dev/sda6
  Volume group " vg1"  successfully created

Execute the following command to view the volume group information.

[root@linuxhelp ~]# vgdisplay vg1
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               5.99 GiB
  PE Size               4.00 MiB
  Total PE              1534
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1534 / 5.99 GiB
  VG UUID               NiC09Y-1mLt-k9Mj-gff6-ml0C-OZXO-r21gWX

As vg1 contains two 3 GB disks, a single 6 GB drive appears.

It is necessary to consider space while creating logical volumes.
Here, we have created two LV named lv1 and backup.

[root@linuxhelp ~]# lvcreate -L 3G -n lv1 vg1
  Logical volume " lv1"  created
[root@linuxhelp ~]# lvcreate -l 100%FREE -n backup vg1
  Logical volume " backup"  created

Explanations

The option ' -n' shows the name of the Lv ' -L' is for size of the memory and ' -l' represents the remaining available space.

Run the following command to view the list of LV

[root@linuxhelp ~]# lvs
  LV     VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  backup vg1  -wi-a----- 2.99g                                             
  lv1    vg1  -wi-a----- 3.00g     


Use the following command to display the detailed information.

[root@linuxhelp ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                IqgHAy-B4d0-CHOR-l6IE-q0cW-WRUm-o8W3Ep
  LV Write Access        read/write
  LV Creation host, time linuxhelp, 2016-04-26 09:51:15 +0530
  LV Status              available
  # open                 0
  LV Size                3.00 GiB
  Current LE             768
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vg1/backup
  LV Name                backup
  VG Name                vg1
  LV UUID                SHj1g4-cE1N-Suuo-4yo4-qdVC-vP3f-JUaMOm
  LV Write Access        read/write
  LV Creation host, time linuxhelp, 2016-04-26 09:52:03 +0530
  LV Status              available
  # open                 0
  LV Size                2.99 GiB
  Current LE             766
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

Using mkfs command we are formatting /dev/vg1/lv1 and /dev/vg1/backup with ext4 file system.

[root@linuxhelp ~]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
196608 inodes, 786432 blocks
39321 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=805306368
24 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@linuxhelp ~]# mkfs.ext4 /dev/vg1/backup 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
196224 inodes, 784384 blocks
39219 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=803209216
24 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

To Resize the Logical Volumes and Extending Volume Groups

Run the following command to alter the memory space of one file and to another.

[root@linuxhelp ~]# lvreduce -L -1G -r /dev/vg1/lv1
fsck from util-linux 2.23.2
/dev/mapper/vg1-lv1: clean, 11/196608 files, 31036/786432 blocks
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg1-lv1 to 524288 (4k) blocks.

The filesystem on /dev/mapper/vg1-lv1 is now 524288 blocks long.

Reducing logical volume lv1 to 2.00 GiB
Logical volume lv1 successfully resized

[root@linuxhelp ~]# lvextend -l +100%FREE -r /dev/vg1/backup
fsck from util-linux 2.23.2
/dev/mapper/vg1-backup: clean, 11/196224 files, 31006/784384 blocks
  Extending logical volume backup to 3.99 GiB
  Logical volume backup successfully resized
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vg1-backup to 1046528 (4k) blocks.

The filesystem on /dev/mapper/vg1-backup is now 1046528 blocks long.

don' t forget to include minus or plus while resizing the LV.

Use the following command to add /dev/sd7 to vg1,

[root@linuxhelp ~]# vgextend vg1 /dev/sda7
  Volume group " vg1"  successfully extended

To increase the size of the VG

Run the following command to increase the size of the VG.

[root@linuxhelp ~]# vgdisplay vg1
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               8.99 GiB
  PE Size               4.00 MiB
  Total PE              2301
  Alloc PE / Size       1534 / 5.99 GiB
  Free  PE / Size       767 / 3.00 GiB
  VG UUID               NiC09Y-1mLt-k9Mj-gff6-ml0C-OZXO-r21gWX

To Mount Logical Volumes on Boot and on Demand

Run the following commands to create mount points.

[root@linuxhelp ~]# mkdir /mnt/backup
[root@linuxhelp ~]# mkdir /mnt/lv1

Run the following command on each disk to identify UUID.

[root@linuxhelp ~]# blkid /dev/vg1/backup
/dev/vg1/backup: UUID=" 0fdf65b1-b15d-4c6f-9978-7feca1aa3577"  TYPE=" ext4"  
[root@linuxhelp ~]# blkid /dev/vg1/lv1
/dev/vg1/lv1: UUID=" d4b8c7a4-6a9b-47c7-b14f-32cd427f370f"  TYPE=" ext4" 


Now we will make it as permanent mount by editing in /etc/fstab.

#
# /etc/fstab
# Created by anaconda on Sat Apr 23 10:58:25 2016
#
# Accessible filesystems, by reference, are maintained under ' /dev/disk' 
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=40821247-96e6-4735-9bf7-fd796042e3ac    /             xfs     defaults        1 1
UUID=cfb663ce-ac90-4d4e-99d2-a7c1969c5d89    /boot         xfs     defaults        1 2
UUID=06aced7b-22e6-4910-b486-74369c27ebac    swap          swap    defaults        0 0
UUID=0fdf65b1-b15d-4c6f-9978-7feca1aa3577    /mnt/backup   ext4    defaults        0 0 
UUID=d4b8c7a4-6a9b-47c7-b14f-32cd427f370f    /mnt/lv1      ext4    defaults        0 0 

Save and mount.

[root@linuxhelp ~]# mount -a
[root@linuxhelp ~]# mount | grep mnt
/dev/mapper/vg1-backup on /mnt/backup type ext4 (rw,relatime,seclabel,data=ordered)
/dev/mapper/vg1-lv1 on /mnt/lv1 type ext4 (rw,relatime,seclabel,data=ordered)
FAQ
Q
What is Logical Volume Management?
A
Logical Volume Management is used for creating multiple logical volumes which means allocating disks space, mirroring and resize volumes in any method without any data loss.
Q
What is the command to mount all the partitions in Linux?
A
For mounting all the partitions use "mount -a" command.
Q
What is the command to list and see the UUID of the mounted disk?
A
Use "blkid partition path" to list and see the options.
Q
How to mount the partitions permanently after creating LVM?
A
Make an entry in "/etc/fstab" after LVM creation.
Q
What file system is available for centos/RHEL?
A
For RHEL/Centos 6 better use "xfs" but ext4 can also be used.