Common Operations of Linux LVM

Common Operations of Linux LVM

LVM (Logical Volume Manager) is a logical volume manager in Linux used for managing disk storage. It abstracts physical hard disk partitions into Physical Volumes (PV), combines them into Volume Groups (VG), and then divides them into Logical Volumes (LV). LVM supports dynamic resizing of volumes, snapshots, striping, and other features, providing flexible storage management.

Creating LV and Mounting Demonstration

1. Prerequisites

To use LVM for online resizing of a file system, the file system to be resized must be created using LVM.

If the lvm command is not available on the Linux server, it can be installed using the following commands:

# Debian-based systems
apt install lvm2
# Red Hat-based systems
yum install lvm2 or dnf install lvm2

2. Environment Preparation

Virtualization platform: VMware Workstation
Operating system: Rocky Linux 9.4
Add a hard disk for LVM experiments in VMware Workstation. Shut down the virtual machine and add the hard disk.

After adding the hard disk, use the command lsblk to view the current disks. nvme0n2 is the newly added hard disk.

[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0          11:0    1  1.5G  0 rom
nvme0n1     259:0    0   20G  0 disk
├─nvme0n1p1 259:1    0    1G  0 part /boot
└─nvme0n1p2 259:2    0   19G  0 part
  └─rl-root 253:0    0   19G  0 lvm  /
nvme0n2     259:3    0   20G  0 disk

3. Creating PV (Physical Volume)

Use the command pvcreate to create a physical volume. After successful creation, you can use pvs to view all physical volumes.

[root@localhost ~]# pvcreate /dev/nvme0n2
  Physical volume "/dev/nvme0n2" successfully created.
[root@localhost ~]# pvs
  PV             VG Fmt  Attr PSize   PFree
  /dev/nvme0n1p2 rl lvm2 a--  <19.00g     0
  /dev/nvme0n2      lvm2 ---   20.00g 20.00g
  • /dev/nvme0n2: The newly added hard disk.

4. Creating VG (Volume Group)

Use the command vgcreate to create a volume group. After successful creation, you can use vgs to view all volume groups.

[root@localhost ~]# vgcreate appvg /dev/nvme0n2
  Volume group "appvg" successfully created
[root@localhost ~]# vgs
  VG    #PV #LV #SN Attr   VSize   VFree
  appvg   1   0   0 wz--n- <20.00g <20.00g
  rl      1   1   0 wz--n- <19.00g      0
  • appvg: The name of the volume group, which can be chosen freely.

  • /dev/nvme0n2: The newly created PV.

5. Creating LV (Logical Volume)

Use the command lvcreate to create a logical volume. After successful creation, you can use lvs to view all logical volumes.

[root@localhost ~]# lvcreate -L 10G -n applv appvg
  Logical volume "applv" created.
[root@localhost ~]# lvs
  LV    VG    Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  applv appvg -wi-a-----  10.00g
  root  rl    -wi-ao---- <19.00g
  • -L: Specifies the size of the created LV, I set it to 10G. If you want to allocate by percentage, use -l.

  • -n: Specifies the name of the LV, I named it applv.

  • appvg: Specifies the volume group name from which to create the LV, in this case, it is created from the appvg volume group.

6. Formatting LV

[root@localhost ~]# mkfs.xfs /dev/appvg/applv
meta-data=/dev/appvg/applv       isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1 nrext64=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=16384, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
  • mkfs.xfs: Formats the file system to XFS type.

7. Creating a Directory (Mount Point)

I will create a directory /app to mount the logical volume applv.

[root@localhost ~]# mkdir /app

8. Mounting the Logical Volume

/etc/fstab is a configuration file in Linux systems used to define the mounting information for disk partitions, storage devices, or remote file systems. The system reads this file at startup to automatically mount the specified file systems. Edit /etc/fstab and add the following content on the last line:

[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# tail -n 1 /etc/fstab
/dev/appvg/applv        /app                    xfs     defaults        0 0
  • /dev/appvg/applv: The location of the logical volume.

  • /app: The mount point.

  • xfs: The file system type.

  • defaults: Mount default options, including read-write, execute, and other common options.

  • 0 0: The first 0 indicates that this file system does not need to be backed up by the dump tool; the second 0 indicates that the file system does not need to be checked by fsck at startup.

[root@localhost ~]# mount -a
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 4.0M     0  4.0M   0% /dev
tmpfs                    1.8G     0  1.8G   0% /dev/shm
tmpfs                    726M  9.1M  717M   2% /run
/dev/mapper/rl-root       19G  1.8G   18G  10% /
/dev/nvme0n1p1          1014M  302M  713M  30% /boot
tmpfs                    363M     0  363M   0% /run/user/0
/dev/mapper/appvg-applv   10G  104M  9.9G   2% /app

9. Testing

Enter the /app directory to perform read and write operations.

[root@localhost app]# echo 'hello world' > test.txt
[root@localhost app]# cat test.txt
hello world

Writing and reading works fine without issues.

LVM Online Expansion

1. Logical Volume (LV) in a Volume Group (VG) with Free Space

In this case, you can directly use the command lvextend to expand.

[root@localhost app]# lvextend -l +100%free /dev/appvg/applv
  Size of logical volume appvg/applv changed from 10.00 GiB (2560 extents) to <20.00 GiB (5119 extents).
  Logical volume appvg/applv successfully resized.
[root@localhost app]# lvs
  LV    VG    Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  applv appvg -wi-ao---- <20.00g
  root  rl    -wi-ao---- <19.00g
  • -l +100%free: Expands the remaining space in the VG to applv.

Expand the file system.

[root@localhost app]# xfs_growfs /app
meta-data=/dev/mapper/appvg-applv isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1 nrext64=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=16384, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2621440 to 5241856
[root@localhost app]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 4.0M     0  4.0M   0% /dev
tmpfs                    1.8G     0  1.8G   0% /dev/shm
tmpfs                    726M  9.1M  717M   2% /run
/dev/mapper/rl-root       19G  1.8G   18G  10% /
/dev/nvme0n1p1          1014M  302M  713M  30% /boot
tmpfs                    363M     0  363M   0% /run/user/0
/dev/mapper/appvg-applv   20G  175M   20G   1% /app
  • xfs_growfs: Used to adjust the size of the XFS file system to utilize the newly added space.

  • /app: The mount point of the expanded file system.

2. Current Volume Group (VG) with No Free Space

  1. 1. In this case, we need to add a hard disk and then create a physical volume (PV) from the hard disk.

[root@localhost ~]# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0            11:0    1  1.5G  0 rom
nvme0n1       259:0    0   20G  0 disk
├─nvme0n1p1   259:1    0    1G  0 part /boot
└─nvme0n1p2   259:2    0   19G  0 part
  └─rl-root   253:0    0   19G  0 lvm  /
nvme0n2       259:3    0   20G  0 disk
└─appvg-applv 253:1    0   20G  0 lvm  /app
nvme0n3       259:4    0    5G  0 disk
  • nvme0n3: I added a 5G disk, named nvme0n3.

[root@localhost ~]# pvcreate /dev/nvme0n3  # Create logical volume
  Physical volume "/dev/nvme0n3" successfully created.
[root@localhost ~]# pvs
  PV             VG    Fmt  Attr PSize   PFree
  /dev/nvme0n1p2 rl    lvm2 a--  <19.00g    0
  /dev/nvme0n2   appvg lvm2 a--  <20.00g    0
  /dev/nvme0n3         lvm2 ---    5.00g 5.00g
  1. 2. Then add the physical volume (PV) to the volume group (VG) where the logical volume (LV) resides using the command vgextend to expand the volume group.

[root@localhost ~]# vgextend appvg /dev/nvme0n3
  Volume group "appvg" successfully extended
[root@localhost ~]# vgs
  VG    #PV #LV #SN Attr   VSize   VFree
  appvg   2   1   0 wz--n-  24.99g <5.00g
  rl      1   1   0 wz--n- <19.00g     0

You can see that appvg now has 5G of free space, indicating that the expansion was successful.

  1. 3. Expand the file system.

[root@localhost ~]# lvextend -l +100%free /dev/appvg/applv
  Size of logical volume appvg/applv changed from <20.00 GiB (5119 extents) to 24.99 GiB (6398 extents).
  Logical volume appvg/applv successfully resized.
  [root@localhost ~]# xfs_growfs /app/
meta-data=/dev/mapper/appvg-applv isize=512    agcount=8, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1 nrext64=0
data     =                       bsize=4096   blocks=5241856, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=16384, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 5241856 to 6551552
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 4.0M     0  4.0M   0% /dev
tmpfs                    1.8G     0  1.8G   0% /dev/shm
tmpfs                    726M  9.1M  717M   2% /run
/dev/mapper/rl-root       19G  1.8G   18G  10% /
/dev/nvme0n1p1          1014M  302M  713M  30% /boot
/dev/mapper/appvg-applv   25G  211M   25G   1% /app

You can see that the /app directory has become 25G, completing the expansion.

Summary

This article introduced common operations of LVM in work. LVM is powerful, and for more features, you can search on Google. If you find this useful, please follow me, and I will continue to share operational knowledge.

Leave a Comment