Creating and Configuring LVM Logical Volumes on Linux
To begin, examine the current disk configurasion using fdisk -l. This command lists all available disks and their partitions. For example, you might see output similar to:
Disk /dev/sdb: 500 GB, 500000000000 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xabcdef12
Device Boot Start End Blocks Id System
/dev/sdb1 1 60801 488281250 8e Linux LVM
If an existing LVM partition is present, you can remove it for demonstration purposes. Use fdisk /dev/sdb to enter the partition editor. Within the interactive prompt, delete the partition with the d command, then create a new one using n. Set it as a primary partition and accept default values for cylinder ranges. Change the partition type to Linux LVM (hex code 8e) with the t command. Write changes with w and update the kernel partition table using partprobe.
After partitioning, initialize the physical volume (PV) with pvcreate /dev/sdb1. Verify the PV creation by running pvscan or pvdisplay. For instance:
[root@server ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@server ~]# pvscan
PV /dev/sdb1 lvm2 [500.00 GiB]
Total: 1 [500.00 GiB] / in use: 0 [0 ] / in no VG: 1 [500.00 GiB]
Next, create a volume group (VG) named storage_vg that includes the PV: vgcreate storage_vg /dev/sdb1. Check the VG with vgscan or vgdisplay. The output might show:
[root@server ~]# vgcreate storage_vg /dev/sdb1
Volume group "storage_vg" successfully created
[root@server ~]# vgdisplay
--- Volume group ---
VG Name storage_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 500.00 GiB
PE Size 4.00 MiB
Total PE 128000
Alloc PE / Size 0 / 0
Free PE / Size 128000 / 500.00 GiB
VG UUID abc12345-6789-def0-1234-56789abcdef0
Now, create a logical volume (LV) using all available physical extents (PEs). Use lvcreate -l 128000 -n data_lv storage_vg. Confirm the LV with lvdisplay:
[root@server ~]# lvcreate -l 128000 -n data_lv storage_vg
Logical volume "data_lv" created
[root@server ~]# lvdisplay
--- Logical volume ---
LV Path /dev/storage_vg/data_lv
LV Name data_lv
VG Name storage_vg
LV UUID 12345678-90ab-cdef-1234-567890abcdef
LV Write Access read/write
LV Creation host, time server, 2023-10-01 12:00:00 +0000
LV Status available
# open 0
LV Size 500.00 GiB
Current LE 128000
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
Format the LV with an ext4 filesystem: mkfs -t ext4 /dev/storage_vg/data_lv. Create a mount directory, such as /data, and mount the LV: mkdir /data and mount /dev/storage_vg/data_lv /data. Verify the mount with df -h:
[root@server ~]# mkfs -t ext4 /dev/storage_vg/data_lv
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 131072000 4k blocks and 32768000 inodes
Filesystem UUID: 98765432-fedc-ba09-8765-4321fedcba09
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
[root@server ~]# mkdir /data
[root@server ~]# mount /dev/storage_vg/data_lv /data
[root@server ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 2.0G 45G 5% /
tmpfs 4.0G 0 4.0G 0% /dev/shm
/dev/mapper/storage_vg-data_lv 493G 70M 467G 1% /data
To enable automatic mounting at boot, edit /etc/fstab. Add an entry like:
/dev/storage_vg/data_lv /data ext4 defaults 1 2
Test the fstab configuration by unmounting and remounting: umount /data followed by mount -a. Check with df -h to ensure the mount is restored.
Key commands summarized:
# fdisk /dev/sdb
# pvcreate /dev/sdb1
# vgcreate storage_vg /dev/sdb1
# lvcreate -l 128000 -n data_lv storage_vg
# mkfs -t ext4 /dev/storage_vg/data_lv
# mkdir /data
# mount /dev/storage_vg/data_lv /data