How to configure LVM on Linux (centos/Redhat)
LVM stands for Logical Volume Manager. LVM is a tool for logical volume management. LVM can be used to create easy to maintain logical volumes.
LVM has basically three terms, Physical Volume PV, Volume Group VG, Logical Volume LV.
- PV – It’s a raw hard drive that it initialized to work with LVM, such as /dev/sdb, /dev/sdc, /dev/sdb1 etc.
- VG – Many PV is combined into one VG. You can create many VGs and each of them has a unique name.
- LV – You can create many LVs from a VG. You can extend, reduce the LV size on the fly. The LV also has unique names. You format the LV into ext4, zfs, btrfs etc filesystems, mount it and use it as you do other ordinary partitions.
First, we need to check current disk status and for that, you need to run the following command.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 28.9G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 1.5G 0 part [SWAP]
├─sda3 8:3 0 21.5G 0 part /
sdb 8:16 0 21.2G 0 disk
sdc 8:32 0 21.2G 0 disk
sdd 8:48 0 21.2G 0 disk
Step 1: create a new Partiton using fdisk tool and select partition type LVM
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xa37176bf.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-44498687, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-44498687, default 44498687):
Using default value 44498687
Partition 1 of type Linux and of size 21.2 GiB is set
Command (m for help): p
Disk /dev/sdb: 22.8 GB, 22783328256 bytes, 44498688 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: 0xa37176bf
Device Boot Start End Blocks Id System
/dev/sdb1 2048 44498687 22248320 83 Linux
Command (m for help): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Command (m for help): 8e
8: unknown command
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): p
Disk /dev/sdb: 22.8 GB, 22783328256 bytes, 44498688 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: 0xa37176bf
Device Boot Start End Blocks Id System
/dev/sdb1 2048 44498687 22248320 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe
Do same for /dev/sdc and /dev/sdd also
once it is done, now we will create pv as below
Step 2: Create physical volume
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc1" successfully created.
Physical volume "/dev/sdd1" successfully created.
For scanning disks run below command
[root@localhost ~]# lvmdiskscan
To display physical voumes
[root@localhost ~]# pvdisplay
You can list the newly created PVs with
[root@localhost ~]# pvs
Step 3: Create volume group
Now we can create the volume group named “vg00". you can give any other name.
[root@localhost ~]# vgcreate vg00 /dev/sdb1 /dev/sdc1 /dev/sdd1
Volume group "vg00" successfully created
Now you can list all the vgs with the following command
[root@localhost ~]# vgscan
The following command shows the brief details about the volume group "vg00"
[root@localhost ~]# vgdisplay vg00
--- Volume group ---
VG Name vg00
System ID
Format lvm2
Metadata Areas 3
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 3
Act PV 3
VG Size 63.64 GiB
PE Size 4.00 MiB
Total PE 16293
Alloc PE / Size 0 / 0
Free PE / Size 16293 / 63.64 GiB
VG UUID MN3eZK-hGxy-J8OD-UUuU-ylCG-HLVJ-dpJO2L
If you wish you can add more PV to an existing vg00 with following command.
vgextend vg00 /dev/sde1
Step 4: Create Logical volume
Use of the following command creates the LV called “data” size of 10 GB in the VG.
-L – Size in the KB/MB/GB.
-l – Size in PE (Physical Extend 1PE=4MB)
-n – name of the LV
[root@localhost ~]# lvcreate -L 10G -n data vg00
Logical volume "data" created.
you can scan logical volume and display information with below commands
[root@localhost ~]# lvscan
[root@localhost ~]# lvdisplay
Step 5: Create the mount point and mount the new LVM
Now make directory for mounting.
[root@localhost ~]# mkdir /test
Before each logical volume can be used, we need to create a filesystem.
[root@localhost ~]# mkfs.ext4 /dev/vg00/data
you can check BlockID with below command.
[root@localhost ~]# blkid
now mount with /test as follows
[root@localhost ~]# mount /dev/vg00/data /test
verify new disk layout
[root@localhost ~]# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 23G 3.8G 18G 18% /
devtmpfs 637M 0 637M 0% /dev
tmpfs 653M 0 653M 0% /dev/shm
tmpfs 653M 11M 643M 2% /run
tmpfs 653M 0 653M 0% /sys/fs/cgroup
/dev/sda1 1.1G 130M 823M 14% /boot
tmpfs 131M 13k 131M 1% /run/user/989
tmpfs 131M 0 131M 0% /run/user/0
/dev/mapper/vg00-data 11G 38M 9.9G 1% /test
Put on /etc/fstab to automount the partition when system starts.
[root@localhost ~]# vi /etc/fstab
############### Add below line at the end of the file #################
/dev/mapper/vg00-data /test ext4 defaults 0 0
sample output:
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Aug 21 20:30:01 2019
#
# 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=a7d9ee27-2cb2-4d85-ae8b-f89ae638887c / ext4 defaults 1 1
UUID=e25dcfd8-b259-46cf-92a9-681595f9d2aa /boot ext4 defaults 1 2
UUID=4060b8d5-45ff-4c15-8489-a6cf03c13b5e swap swap defaults 0 0
/dev/mapper/vg00-data /test ext4 defaults 0 0
Thats it! Now we have successfully created LVM.
Step 6: Extend Logical volumes
If you are running out of space in logical volume then increase it.
Now i want to increase 2GB /dev/mapper/vg00-data as below
[root@localhost ~]# lvextend -L +2G -r /dev/vg00/data
[root@localhost ~]# df -H
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 23G 3.8G 18G 18% /
devtmpfs 637M 0 637M 0% /dev
tmpfs 653M 0 653M 0% /dev/shm
tmpfs 653M 11M 643M 2% /run
tmpfs 653M 0 653M 0% /sys/fs/cgroup
/dev/sda1 1.1G 130M 823M 14% /boot
tmpfs 131M 13k 131M 1% /run/user/989
tmpfs 131M 0 131M 0% /run/user/0
/dev/mapper/vg00-data 13G 42M 12G 1% /test