Linux: LVM

LVM is a Logical Volume Manager for the Linux operating system.

LVM 2 is almost completely backward compatible with volumes created with LVM 1.

The exception to this is snapshots (You must remove snapshot volumes before upgrading to LVM 2).

LVM 2 uses the device mapper kernel driver.

Terminology

  • Physical volume (PV): Consists of Raw disks or RAID arrays or other storage devices
  • Volume Group (VG): A collection of PV’s that we can use the space from. Combines the physical volumes into storage groups.
  • Logical volume (LV): A partition created from space in a VG. VG’s are divided into LV’s and are mounted as partitions.

Lab

We are going to use a Frdora linux virtual box machine to perform some LVM operations.
Via using the fdisk cli command we can see that only one disk exists in the system (/dev/sda with three partitions)

sudo fdisk -l
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 sectors
Disk model: VBOX HARDDISK   
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: gpt
Disk identifier: 47D76F39-3985-4641-B5C3-3A30D4EF9159

Device       Start      End  Sectors Size Type
/dev/sda1     2048     4095     2048   1M BIOS boot
/dev/sda2     4096  2101247  2097152   1G Linux filesystem
/dev/sda3  2101248 31455231 29353984  14G Linux filesystem

We will add a new virtual disk (2 GiB) and run the same command (fdisk -l):

fdisk -l
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 sectors
Disk model: VBOX HARDDISK   
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: gpt
Disk identifier: 47D76F39-3985-4641-B5C3-3A30D4EF9159

Device       Start      End  Sectors Size Type
/dev/sda1     2048     4095     2048   1M BIOS boot
/dev/sda2     4096  2101247  2097152   1G Linux filesystem
/dev/sda3  2101248 31455231 29353984  14G Linux filesystem


Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VBOX HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

The new disk is shown as /dev/sdb. For simplicity reasons we will not divide the disk in partisions and we will use the whole capacity as is.

Via using the pv* cli commands we will list and assign a psysical volume to the system:

pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
  Creating devices file /etc/lvm/devices/system.devices


pvdisplay
  "/dev/sdb" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               6ejOXT-WUs3-wuHs-p4JE-SW0n-FrgL-fuUBrA

Now we have created the PV, we need to create the volume group:

vgcreate my-backup-group /dev/sdb
  Volume group "my-backup-group" successfully created


vgdisplay
  --- Volume group ---
  VG Name               my-backup-group
  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               <2.00 GiB
  PE Size               4.00 MiB
  Total PE              511
  Alloc PE / Size       0 / 0   
  Free  PE / Size       511 / <2.00 GiB
  VG UUID               wcQJRn-VYLZ-gaQP-6TC6-FNcS-6cuX-VDWz5b

At this point we have created the Volume Group named my-backup-group via adding the /dev/sdb physical volume.

We are ready now to create the Logical Volume:

lvcreate -l 100%FREE -n my-backup-lv my-backup-group
  Logical volume "my-backup-lv" created.

lvdisplay
  --- Logical volume ---
  LV Path                /dev/my-backup-group/my-backup-lv
  LV Name                my-backup-lv
  VG Name                my-backup-group
  LV UUID                ad4zWF-O0Z0-a94E-tG4c-IS0d-NJjJ-3eXld3
  LV Write Access        read/write
  LV Creation host, time linux-lab, 2023-08-17 14:03:48 +0300
  LV Status              available
  # open                 0
  LV Size                <2.00 GiB
  Current LE             511
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

We have created now the Logical Volume named my-backup-lv via using the my-backup-group Volume Group and we assigned the whole size capacity (-l 100%FREE).

It’s time to format our Logical Volume and mount it in a folder to use it.

disk -l
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 sectors
Disk model: VBOX HARDDISK   
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: gpt
Disk identifier: 47D76F39-3985-4641-B5C3-3A30D4EF9159

Device       Start      End  Sectors Size Type
/dev/sda1     2048     4095     2048   1M BIOS boot
/dev/sda2     4096  2101247  2097152   1G Linux filesystem
/dev/sda3  2101248 31455231 29353984  14G Linux filesystem


Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VBOX HARDDISK   
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 /dev/mapper/my--backup--group-my--backup--lv: 2 GiB, 2143289344 bytes, 4186112 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

We are going to use the mkfs tool to format the LV path (/dev/my-backup-group/my-backup-lv) in ext4 type:

mkfs -t ext4 /dev/my-backup-group/my-backup-lv
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 523264 4k blocks and 130816 inodes
Filesystem UUID: 2e0d0457-3c3a-45cd-8fdf-cb62226254f4
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

To mount the Logical Volume in a folder we need to run:

mkdir -p /mnt/my-backp-folder
mount /dev/my-backup-group/my-backup-lv /mnt/my-backp-folder 

Note: To make the mounts persistent, we need to add them to the fstab file.

And via running the df command we can check that the folder has the correct size:

df -h
Filesystem                                    Size  Used Avail Use% Mounted on
devtmpfs                                      4.0M     0  4.0M   0% /dev
tmpfs                                         6.1G     0  6.1G   0% /dev/shm
tmpfs                                         2.5G  1.2M  2.5G   1% /run
/dev/sda3                                      14G  4.0G  9.4G  30% /
tmpfs                                         6.1G  8.0K  6.1G   1% /tmp
/dev/sda2                                     974M  258M  650M  29% /boot
/dev/sda3                                      14G  4.0G  9.4G  30% /home
tmpfs                                         1.3G   56K  1.3G   1% /run/user/42
tmpfs                                         1.3G   40K  1.3G   1% /run/user/1000
/dev/mapper/my--backup--group-my--backup--lv  2.0G   24K  1.9G   1% /mnt/my-backp-folder

Extend

Let’s think now that we need more space at the specific Logical Volume. We will add a new virtual disk (3 GiB) and we will extend the needed volumes.

Again with the fdisk cli tool we will must be able to see the third disk /dev/sdc (3 GiB):
[ pvcreate –> vgextend –> lvextend ]


fdisk -l
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 sectors
Disk model: VBOX HARDDISK   
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: gpt
Disk identifier: 47D76F39-3985-4641-B5C3-3A30D4EF9159

Device       Start      End  Sectors Size Type
/dev/sda1     2048     4095     2048   1M BIOS boot
/dev/sda2     4096  2101247  2097152   1G Linux filesystem
/dev/sda3  2101248 31455231 29353984  14G Linux filesystem


Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 sectors
Disk model: VBOX HARDDISK   
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 /dev/sdc: 3 GiB, 3221225472 bytes, 6291456 sectors
Disk model: VBOX HARDDISK   
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 /dev/mapper/my--backup--group-my--backup--lv: 2 GiB, 2143289344 bytes, 4186112 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

On the previous we performed the disk format after creating the LV, but now we want to do it initially and before add the third disk because we don”t want to loose the existing data in the mounted folder.

mkfs.ext4 /dev/sdc

Now we perform the similar steps to extend the LV, but instead of the *create commands we are gonna use the *extend commands:

pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.

pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               my-backup-group
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              511
  Free PE               0
  Allocated PE          511
  PV UUID               SI3O9d-kook-uGAZ-h91u-DtNS-NXDl-ln4oKY
   
  "/dev/sdc" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc
  VG Name               
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               7SpFcz-oFJe-0xHG-8RRR-ca1w-KQ4S-zry8fI


vgdisplay
  --- Volume group ---
  VG Name               my-backup-group
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <2.00 GiB
  PE Size               4.00 MiB
  Total PE              511
  Alloc PE / Size       511 / <2.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               j3EWJa-WW72-1mCm-S4LV-Ew23-NAUg-rC1D58


vgextend my-backup-group /dev/sdc
  Volume group "my-backup-group" successfully extended

vgdisplay
  --- Volume group ---
  VG Name               my-backup-group
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               4.99 GiB
  PE Size               4.00 MiB
  Total PE              1278
  Alloc PE / Size       511 / <2.00 GiB
  Free  PE / Size       767 / <3.00 GiB
  VG UUID               j3EWJa-WW72-1mCm-S4LV-Ew23-NAUg-rC1D58


lvdisplay
  --- Logical volume ---
  LV Path                /dev/my-backup-group/my-backup-lv
  LV Name                my-backup-lv
  VG Name                my-backup-group
  LV UUID                o387Tv-DYuA-H7vL-QyOh-BQ30-WcYP-Xittt3
  LV Write Access        read/write
  LV Creation host, time linux-lab, 2023-08-17 14:51:41 +0300
  LV Status              available
  # open                 0
  LV Size                <2.00 GiB
  Current LE             511
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0


lvextend -l +100%FREE /dev/my-backup-group/my-backup-lv
  Size of logical volume my-backup-group/my-backup-lv changed from <2.00 GiB (511 extents) to 4.99 GiB (1278 extents).
  Logical volume my-backup-group/my-backup-lv successfully resized.


lvdisplay
  --- Logical volume ---
  LV Path                /dev/my-backup-group/my-backup-lv
  LV Name                my-backup-lv
  VG Name                my-backup-group
  LV UUID                o387Tv-DYuA-H7vL-QyOh-BQ30-WcYP-Xittt3
  LV Write Access        read/write
  LV Creation host, time linux-lab, 2023-08-17 14:51:41 +0300
  LV Status              available
  # open                 0
  LV Size                4.99 GiB
  Current LE             1278
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

More

To list the disks/partitions we can also use the lsblk cli tool:

lsblk
NAME                               MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda                                  8:0    0  15G  0 disk 
├─sda1                               8:1    0   1M  0 part 
├─sda2                               8:2    0   1G  0 part /boot
└─sda3                               8:3    0  14G  0 part /home
                                                           /
sdb                                  8:16   0   2G  0 disk 
└─my--backup--group-my--backup--lv 253:0    0   5G  0 lvm  
sdc                                  8:32   0   3G  0 disk 
└─my--backup--group-my--backup--lv 253:0    0   5G  0 lvm  

The lvmdump command can be used to generate lvm2 information dumps for the diagnostic purposes by a support team. lvmdump is a tool to dump various information concerning LVM2. By default, it creates a tarball suitable for submission along with a problem report.

lvmdump
Creating dump directory: /root/lvmdump-linux-lab-20230817131804
 
Gathering LVM & device-mapper version info...
Gathering dmsetup info...
Gathering process info...
Gathering console messages...
Gathering /etc/lvm info...
Gathering /dev listing...
Gathering /sys/block listing...
Creating report tarball in /root/lvmdump-linux-lab-20230817131804.tgz

With the pvscan we can scan all disks for Physical Volumes.

pvscan
PV /dev/sdb VG my-backup-group lvm2 [<2.00 GiB / 0 free]
PV /dev/sdc VG my-backup-group lvm2 [<3.00 GiB / 0 free]
Total: 2 [4.99 GiB] / in use: 2 [4.99 GiB] / in no VG: 0 [0 ]