How to configure LVM on Linux
Logical Volume Manager (LVM) is a storage management technology used in Linux and other Unix-like operating systems. It allows users to create, resize, and move logical volumes (LVs) and volume groups (VGs) dynamically without requiring any downtime. With LVM, administrators can abstract physical storage devices and manage them as logical volumes, providing greater flexibility and easier management of storage resources. LVM allows for logical volume snapshots, which are read-only copies of a logical volume that can be used for backups, testing, or other purposes without disrupting the original volume. LVM provides a powerful and flexible solution for managing storage in Linux systems.
The following steps can work on CentOS, AlmaLinux, Rocky and Redhat Linux
- Install the LVM packages if they are not already installed:
# yum install lvm2
- Create a new partition using fdisk or another partitioning tool:
# fdisk /dev/sdb
- Set the partition type to Linux LVM (code 8e):
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list all codes): 8e
Command (m for help): w - Create a physical volume on the new partition:
# pvcreate /dev/sdb1
You will see:
Physical volume "/dev/sdb1" successfully created
- Create a volume group using the physical volume:
# vgcreate my_vg /dev/sdb1
- Check the volume group information:
# vgdisplay my_vg
- Create a logical volume named my_lv with a size of 512 MiB:
# lvcreate -L 512M -n my_lv my_vg
- Check the logical volume information:
# lvdisplay my_vg/my_lv
- Create a file system on the logical volume:
# mkfs.ext4 /dev/my_vg/my_lv
- Create a mount point:
# mkdir /mnt/my_lv
- Mount the logical volume:
# mount /dev/my_vg/my_lv /mnt/my_lv
- Verify that the logical volume is mounted:
# mount | grep /mnt/my_lv
- Edit the /etc/fstab file to automatically mount the logical volume at boot time:
# vi /etc/fstab
Here’s an example of what you will see on the screen during these steps:
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +2G
Created a new partition 1 of type 'Linux' and of size 2 GiB.
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): p
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x5de74d5c
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 4196351 4194304 2G 8e Linux LVM
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
An example output of the command could be:
--- Volume group ---
VG Name my_vg
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size <1.00 GiB
PE Size 4.00 MiB
Total PE 255
Alloc PE / Size 0 / 0
Free PE / Size 255 / <1.00 GiB
VG UUID A94s73-l0Y7-2HrB-lq3B-9XzT-0iQD-4Mq3oI
An example output of the command could be:
--- Logical volume ---
LV Path /dev/my_vg/my_lv
LV Name my_lv
VG Name my_vg
LV UUID t7mZ9X-DdF7-69jP-MCvL-jg90-wQeL-IdwJhB
LV Write Access read/write
LV Creation host, time localhost, 2021-10-25 09:44:21 +0000
LV Status available
# open 0
LV Size 512.00 MiB
Current LE 128
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
An example output of the command could be:
/dev/mapper/my_vg-my_lv on /mnt/my_lv type ext4 (rw,relatime)
Add the following line at the end of the file:
/dev/my_vg/my_lv /mnt/my_lv ext4 defaults 0 0
Save and exit the file.
That's it! The logical volume is now mounted and will automatically mount at boot time.