Home / How to create LVM in CentOS, AlmaLinux, and RockyLinux?

How to create LVM in CentOS, AlmaLinux, and RockyLinux?

An LVM is a tool for managing logical volumes. It can assign disks, stripes, mirrors, and change the size of logical volumes. With LVM, a hard drive or set of hard drives is assigned to one or more physical volumes. LVM physical volumes can be put on other block devices, which could include two or more disks.

Scan and verify the disks/LUNs first with the following commands.

# lvmdiskscan

# fdisk -l

# echo "- - -" > /sys/class/scsi_host/hostX/scan

You can create a new partition in fdisk or use the whole disk/LUN.

# fdisk /dev/sdi
Command (m for help): **n**
Command action
e extended
p primary partition (1-4)
**p**
Partition number (1-4): **1**
First cylinder (1-1018, default 1): **PRESS ENTER KEY TO USE DEFAULT VALUE**
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1018, default 1018): **+2G**

Command (m for help): **p** <-- Print the partition table to view the new partition created.

Disk /dev/sdi: 5368 MB, 5368709120 bytes
166 heads, 62 sectors/track, 1018 cylinders
Units = cylinders of 10292 * 512 = 5269504 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x96e56989

Device Boot Start End Blocks Id System
**/dev/sdi1** 1 409 2104683 83 Linux

Command (m for help): **w** <-- Write changes.
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
#


Using pvcreate command, create LV structure on the new partition.

# pvcreate /dev/sdi [If you want to use whole disk/LUN]
# pvcreate /dev/sdi1
Physical volume "/dev/sdi1" successfully created

Create a Volume group.

# vgcreate <vgname> /dev/sdi1
Volume group "vgname" successfully created

Create a Logical Volume.

# lvcreate -n <lvname> -l 150 <vgname> <-- Create Logical Volume specifying extents with -l
OR
# lvcreate -n <lvname> -L 2G <vgname> <-- Create Logical Volume specifying size with -L

Create a filesystem on Logical Volume.

# mkfs.ext4 /dev/mapper/<vgname>-<lvname>

Now mount the Logical Volume and make it persistent across future reboots. For this create a new directory.

# mkdir /<dirname>

Open /etc/fstab and add the following line.

/dev/mapper/<vgname>-<lvname> /<dirname> ext4 defaults 0 0

Save /etc/fstab and quit vi.

Mount all filesystems in /etc/fstab as follows.

# mount -a

Check mounted Logical volume as follows.

# df -h /<dirname>

Leave a Reply