How To Create LVM Using pvcreate, vgcreate, lvcreate, and lvextend Commands

Published September 25, 2014 by unixminx

What is LVM?

LVM is a tool for logical volume management which includes allocating disks, striping, mirroring and resizing logical volumes. With LVM, a hard drive or set of hard drives is allocated to one or more physical volumes.

How to setup LVM in RHEL 7

Once the physical disk space has been made available to the host, run the following command to identify the disk location:

fdisk -l

Disk /dev/sdb: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5eee65f8

The output of the above command identifies the location of the 4 GB disk as being located in /dev/sdb. The next step involves issuing the following command (replace /dev/sdv with the output you receive from the above command):

[root@slave ~]# fdisk /dev/sdb

Typ Type n to create a new partition and press enter three times until you reach the Last sector prompt. Enter +4GB here and press enter. Type in w and press enter to make the changes live. Issuing partprobe makes the partition live without rebooting the host.

[root@slave ~]# partprobe

The next step involves creating a physical volume.

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

Run a pvscan to pickup the changes.

[root@slave ~]# pvscan
  PV /dev/sda2   VG rhel   lvm2 [7.51 GiB / 0    free]
  Total: 1 [7.51 GiB] / in use: 1 [7.51 GiB] / in no VG: 0 [0   ]

We now need to create a volume group.

[root@slave ~]# vgcreate roldy /dev/sdb1
  Volume group "roldy" successfully created

Inside of this volume group, we will now create a logical partition.

[root@slave ~]# lvcreate roldy --name snookicoco /dev/sdb1 -L 200MB
  Logical volume "snookicoco" created

Format the file system with xfs using the below command:

[root@slave ~]# mkfs.xfs /dev/roldy/snookicoco
meta-data=/dev/roldy/snookicoco  isize=256    agcount=4, agsize=12800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=51200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Create a folder on the host which will be mapped to this storage:

[root@slave ~]# mkdir /snookicoco

In order to edit the fstab, we will need to obtain the UUID of the volume. The UUID can be easily obtained via the blkid command.

[root@slave ~]# blkid
/dev/sda1: UUID="46814065-a338-4860-a3f8-781b132987c6" TYPE="xfs"
/dev/sda2: UUID="3gzDCV-lPFf-8hKA-Kojk-XI01-T7SR-xfK8fd" TYPE="LVM2_member"
/dev/sdb1: UUID="QCrBPH-KPEw-PZ3c-xDs0-nUVE-esuL-AZorqV" TYPE="LVM2_member"
/dev/mapper/rhel-root: UUID="dddea344-415f-4b2b-811f-8d7eac492f9e" TYPE="xfs"
/dev/mapper/rhel-swap: UUID="e7128c41-27b0-45ad-8f20-ddf7ce444aa1" TYPE="swap"
/dev/mapper/roldy-snookicoco: UUID="ae3cc85c-158f-4075-9025-5db0998c1d73" TYPE="xfs"

It’s now time to mount the partition using the fstab.

[root@slave ~]# vi /etc/fstab

Copy the UUID across to a new line in the fstab, add the local mount point and specify the file system as per the below entry:

# /etc/fstab
# Created by anaconda on Thu Sep 18 00:41:01 2014
#
# 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
#
/dev/mapper/rhel-root   /                       xfs     defaults        1 1
UUID=46814065-a338-4860-a3f8-781b132987c6 /boot                   xfs     defaults        1 2
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
UUID="ae3cc85c-158f-4075-9025-5db0998c1d73" /snookicoco xfs defaults 1 2
~
~

Verify that the mount point is now listed.

[root@slave ~]# df -hk | grep snookicoco
/dev/mapper/roldy-snookicoco    201388  10400    190988   6% /snookicoco

How to extend a logical volume

Extending logical volumes is possible thanks to the lvextend utility.

lvextend -L +500M /dev/coco/chops

Fin.

Leave a comment