Create and Extend LVM Logical Volumes in RHEL

Red Hat Enterprise LinuxBeginner
Practice Now

Introduction

In this lab, you will learn the fundamental process of creating and managing LVM (Logical Volume Manager) storage on a Red Hat Enterprise Linux system. You will start by preparing a physical disk, partitioning it with parted, and initializing it as an LVM Physical Volume (PV) using pvcreate. Following this, you will pool this PV into a Volume Group (VG) with vgcreate, and then create a flexible Logical Volume (LV) from that group using lvcreate. The initial setup concludes with formatting the new LV with an XFS filesystem and mounting it persistently for system use.

Building on the initial configuration, you will then explore one of LVM's key advantages: the ability to dynamically resize storage. You will learn how to extend an existing Volume Group by adding a new Physical Volume with vgextend. Subsequently, you will expand the Logical Volume to utilize this new space using lvextend and resize the XFS filesystem online with xfs_growfs to make the additional capacity immediately available to the operating system without downtime.

Prepare Physical Volumes with parted and pvcreate

In this step, you will begin managing storage by preparing a physical disk for use with the Logical Volume Manager (LVM). This involves two key stages: first, partitioning the disk using the parted utility, and second, initializing these partitions as LVM Physical Volumes (PVs) with the pvcreate command.

Logical Volume Manager (LVM) Overview

LVM is a powerful storage management tool in Linux that provides a flexible layer over physical storage devices. Instead of using disks and partitions directly, LVM allows you to create abstract "volume groups" from one or more physical devices, and then carve out "logical volumes" from that pooled space. This makes it much easier to resize storage, replace disks, and manage your system's storage without downtime.

The most fundamental component in LVM is the Physical Volume (PV). A PV is a physical storage device, such as a hard disk partition or a whole disk, that has been initialized for use by LVM.

1. Create a Partition for LVM

Before a disk can be used by LVM, you must create a partition on it and set its type to "LVM". We will use the /dev/vdb device for this exercise. You will need sudo privileges to modify disk partitions.

First, create a new GUID Partition Table (GPT) on the /dev/vdb device. A GPT is a modern standard for the layout of the partition table on a physical storage device.

sudo parted /dev/vdb mklabel gpt

Next, create a single partition that is 512 MiB in size. We will name this partition lvm-part1.

sudo parted /dev/vdb mkpart lvm-part1 1MiB 513MiB

Now, set the partition type to lvm. This flag tells the system that this partition is intended for use with the Logical Volume Manager.

sudo parted /dev/vdb set 1 lvm on

To ensure the kernel recognizes the new partition immediately, run the udevadm settle command. This command waits for the udev daemon to process all device events, ensuring that the new partition /dev/vdb1 is available.

sudo udevadm settle

Finally, verify that the partition was created correctly by printing the partition table.

sudo parted /dev/vdb print

You should see output similar to the following, showing one partition with the lvm flag enabled.

Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system  Name       Flags
 1      1049kB  538MB  537MB               lvm-part1  lvm

2. Initialize the Partition as a Physical Volume

With the partition created and correctly typed, the next step is to initialize it as an LVM Physical Volume using the pvcreate command. This command writes LVM metadata onto the partition, formally making it part of the LVM system.

Run the following command to initialize /dev/vdb1:

sudo pvcreate /dev/vdb1

A successful operation will produce the following message:

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

3. Display Physical Volume Information

You can now inspect the newly created Physical Volume. The pvs command provides a compact summary of all PVs on the system, while pvdisplay offers a more detailed view.

Use pvs to see a quick summary:

sudo pvs

The output will list your new PV. Note that it does not yet belong to any Volume Group (VG).

  PV         VG Fmt  Attr PSize   PFree
  /dev/vdb1     lvm2 ---  512.00m 512.00m

For more details, use pvdisplay:

sudo pvdisplay /dev/vdb1

This command shows detailed information, including the PV name, size, and unique identifier (UUID).

  "/dev/vdb1" is a new physical volume of "512.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name
  PV Size               512.00 MiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

You have now successfully prepared a physical partition for LVM. In the next step, you will use this PV to create a Volume Group.

Create a Volume Group from Physical Volumes with vgcreate

In this step, you will use the Physical Volume (PV) you prepared in the previous step to create a Volume Group (VG). A Volume Group is a central component of LVM, acting as a single, manageable pool of disk space from which you can create Logical Volumes.

Understanding Volume Groups and Physical Extents

A Volume Group (VG) aggregates one or more Physical Volumes into a single storage pool. Think of it as combining several smaller containers of water into one large tank. This abstraction is what gives LVM its flexibility.

This storage pool is divided into small, fixed-size chunks called Physical Extents (PEs). By default, a PE is 4 MiB. When you create a Logical Volume later, you are essentially allocating a certain number of these PEs from the Volume Group.

1. Create the Volume Group

Now, you will create a Volume Group named my_vg using the /dev/vdb1 Physical Volume. The command for this is vgcreate.

sudo vgcreate my_vg /dev/vdb1

If the command is successful, you will see a confirmation message:

  Volume group "my_vg" successfully created

This command has created a new storage pool named my_vg that contains all the available space from /dev/vdb1.

2. Display Volume Group Information

Just as you did with Physical Volumes, you can display information about your new Volume Group. The vgs command provides a summary, and vgdisplay provides a detailed view.

To get a compact summary of all Volume Groups on the system, run:

sudo vgs

The output will show your new VG, its size, and the amount of free space.

  VG    #PV #LV #SN Attr   VSize   VFree
  my_vg   1   0   0 wz--n- 508.00m 508.00m

For a more detailed report on your specific Volume Group, use vgdisplay:

sudo vgdisplay my_vg

This output provides comprehensive details, including the PE size, the total number of PEs, and how many are still free.

  --- 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               508.00 MiB
  PE Size               4.00 MiB
  Total PE              127
  Alloc PE / Size       0 / 0
  Free  PE / Size       127 / 508.00 MiB
  VG UUID               xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Notice the PE Size is 4.00 MiB and you have 127 Free PE, which corresponds to the total available space in the Volume Group. You have now successfully created a Volume Group and it is ready to have Logical Volumes created from it.

Create and Format a Logical Volume with lvcreate and mkfs.xfs

In this step, you will carve out a usable block device, known as a Logical Volume (LV), from the Volume Group you created. Once the LV is created, it's still just raw storage space. To store files on it, you must format it with a filesystem.

Understanding Logical Volumes and Filesystems

A Logical Volume (LV) is the LVM equivalent of a traditional disk partition. It is created from the free space within a Volume Group and is presented to the operating system as a standard block device. You can create filesystems on LVs, mount them, and use them to store data. The key advantage is that LVs can be easily resized, which is much more difficult with physical partitions.

A filesystem is a data structure that the operating system uses to control how data is stored and retrieved. It organizes the raw space of a device like an LV into files and directories. For this lab, we will use XFS, which is a high-performance, journaling filesystem that is the default for Red Hat Enterprise Linux.

1. Create the Logical Volume

You will now create a 256 MiB Logical Volume named my_lv from the my_vg Volume Group. The command for this is lvcreate.

  • -n my_lv: Specifies the name for the new LV.
  • -L 256M: Sets the size of the LV to 256 Megabytes.
  • my_vg: The name of the Volume Group to create the LV from.

Execute the following command:

sudo lvcreate -n my_lv -L 256M my_vg

A successful command will produce this output:

  Logical volume "my_lv" created.

2. Verify the Logical Volume Creation

You can inspect your new LV using the lvs and lvdisplay commands.

To see a summary of all LVs, run:

sudo lvs

The output will show your new LV, my_lv, within the my_vg group.

  LV    VG    Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  my_lv my_vg -wi-a----- 256.00m

For a detailed view, you can specify the LV's full path. The path to an LV is typically /dev/VG_NAME/LV_NAME.

sudo lvdisplay /dev/my_vg/my_lv

This provides detailed information, including the LV Path, which you will need for the next step.

  --- Logical volume ---
  LV Path                /dev/my_vg/my_lv
  LV Name                my_lv
  VG Name                my_vg
  LV UUID                xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  LV Write Access        read/write
  LV Creation host, time host, 2023-10-27 10:30:00 +0000
  LV Status              available
  ## open                 0
  LV Size                256.00 MiB
  Current LE             64
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

3. Format the Logical Volume with an XFS Filesystem

Now, you will format the my_lv Logical Volume with the XFS filesystem using the mkfs.xfs command. This prepares the volume to store files.

sudo mkfs.xfs /dev/my_vg/my_lv

The command will output details about the filesystem it is creating.

meta-data=/dev/my_vg/my_lv       isize=512    agcount=4, agsize=16384 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=65536, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Your Logical Volume is now formatted and ready to be mounted and used. You will do this in the next step.

Persistently Mount the Logical Volume using /etc/fstab

In this step, you will make the Logical Volume you created and formatted accessible to the system. This involves two actions: creating a directory to serve as a "mount point" and then configuring the system to automatically attach the Logical Volume to this directory every time it boots.

Understanding Mounting and /etc/fstab

A formatted block device, like your my_lv Logical Volume, is not directly usable until it is mounted. Mounting is the process of attaching a filesystem to a specific directory in the main filesystem tree. Once mounted, you can navigate into that directory to read and write files on the device.

To ensure the filesystem is mounted automatically after a reboot, you must add an entry for it in the /etc/fstab (file system table) file. This file contains a list of all the disks and partitions that the system should mount at boot time.

1. Create a Mount Point

A mount point is simply an empty directory that will serve as the root of your new filesystem. We will create a directory named /data for this purpose.

sudo mkdir /data

2. Add an Entry to /etc/fstab for Persistent Mounting

Now, you need to tell the system to mount your LV at the /data directory automatically. You will add a new line to the /etc/fstab file. Each line in this file has six fields:

  1. Device: The device to mount. In our case, /dev/my_vg/my_lv.
  2. Mount Point: The directory to mount it on. Here, it's /data.
  3. Filesystem Type: The type of the filesystem, which is xfs.
  4. Mount Options: Options for mounting. defaults is a standard set of options suitable for most cases.
  5. Dump: Used by the dump utility to decide whether to back up the filesystem. 0 means disable.
  6. Pass: Used by fsck to determine the order for checking filesystems at boot. 0 means do not check.

We will use the echo command combined with sudo tee -a to append the correct line to /etc/fstab without needing a text editor.

echo '/dev/my_vg/my_lv /data xfs defaults 0 0' | sudo tee -a /etc/fstab

You can verify that the line was added correctly by viewing the contents of the file:

cat /etc/fstab

3. Mount the Filesystem

Now that the entry exists in /etc/fstab, you can use the mount command to mount it immediately. Because the mount point /data is listed in /etc/fstab, you only need to provide the directory name.

sudo mount /data

The system will read /etc/fstab, find the entry for /data, and mount the corresponding device.

4. Verify the Mount

To confirm that the filesystem is successfully mounted, you can use the df (disk free) command with the -h (human-readable) option.

df -h /data

The output should show your logical volume mounted on /data, with its total size and available space.

Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/my_vg-my_lv     251M   28M  224M  11% /data

You can also create a test file to ensure you have write access:

echo "My LVM is working!" | sudo tee /data/test.txt

Then, read the file back to confirm it was written:

cat /data/test.txt
My LVM is working!

Your Logical Volume is now mounted and will be available automatically every time the system starts.

Extend the Volume Group and Logical Volume with vgextend and lvextend

In this step, you will experience one of the most powerful features of LVM: the ability to dynamically increase the size of your storage. You will add a new physical disk partition to your existing Volume Group to expand its total capacity, and then extend your Logical Volume to use some of that new space.

Understanding Storage Extension

A primary reason for using LVM is its flexibility. When you run out of space, you don't need to perform a complex data migration. Instead, you can simply add another physical disk (or partition) to the Volume Group and then grow your Logical Volumes as needed. This can all be done online, without unmounting the filesystem.

  • vgextend: This command adds one or more Physical Volumes to an existing Volume Group, increasing its total size.
  • lvextend: This command increases the size of a Logical Volume, drawing space from the free extents within its Volume Group.

1. Prepare a New Physical Volume

To extend the Volume Group, you first need a new Physical Volume. We will create a second partition on the /dev/vdb device, just like you did in the first step.

First, create a new 512 MiB partition. We'll place it immediately after the first one.

sudo parted /dev/vdb mkpart lvm-part2 513MiB 1025MiB

Next, set the partition type to lvm.

sudo parted /dev/vdb set 2 lvm on

Ensure the kernel recognizes the new partition, /dev/vdb2.

sudo udevadm settle

Finally, initialize this new partition as a Physical Volume.

sudo pvcreate /dev/vdb2

You should see the success message:

  Physical volume "/dev/vdb2" successfully created.

2. Extend the Volume Group

Now, add the new Physical Volume (/dev/vdb2) to your existing Volume Group (my_vg) using the vgextend command.

sudo vgextend my_vg /dev/vdb2

The confirmation message will indicate success:

  Volume group "my_vg" successfully extended

You can verify the change with the vgs command. Notice that the VSize and VFree have increased significantly.

sudo vgs my_vg
  VG    #PV #LV #SN Attr   VSize    VFree
  my_vg   2   1   0 wz--n- 1022.00m 766.00m

The my_vg Volume Group now spans two physical partitions and has more free space available.

3. Extend the Logical Volume

With more space available in the Volume Group, you can now extend your Logical Volume. Let's increase the size of my_lv from 256 MiB to a new total size of 400 MiB.

The lvextend command with the -L option sets the new absolute size of the volume.

sudo lvextend -L 400M /dev/my_vg/my_lv

The output will confirm the resize operation.

  Size of logical volume my_vg/my_lv changed from 256.00 MiB (64 extents) to 400.00 MiB (100 extents).
  Logical volume my_vg/my_lv successfully resized.

Verify the new size of the Logical Volume with lvs:

sudo lvs /dev/my_vg/my_lv
  LV    VG    Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  my_lv my_vg -wi-ao---- 400.00m

Important: You have successfully extended the Logical Volume (the block device), but the XFS filesystem living on it is not yet aware of this new space. If you check the mounted filesystem's size, it will still report the old size.

df -h /data
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/my_vg-my_lv     251M   28M  224M  11% /data

In the next step, you will resize the filesystem to fill the newly available space in the Logical Volume.

Resize the XFS Filesystem with xfs_growfs

In this final step, you will complete the storage extension process by resizing the XFS filesystem to make it aware of the additional space you allocated to its underlying Logical Volume. This is the crucial last step that makes the new space usable for storing files.

Understanding Filesystem Resizing

When you extend a Logical Volume, you are only increasing the size of the container (the block device). The filesystem inside that container remains at its original size and is unaware of the new, unused space at the end of the device.

For an XFS filesystem, the xfs_growfs command is used to expand the filesystem to fill the underlying device. A major advantage of XFS is that this operation can be performed online, while the filesystem is mounted and in use, with no downtime required.

1. Grow the XFS Filesystem

The xfs_growfs command takes the mount point of the filesystem as its argument. In your case, the mount point is /data.

Run the following command to expand the filesystem:

sudo xfs_growfs /data

The command will output information about the change, indicating the old and new number of data blocks.

meta-data=/dev/mapper/my_vg-my_lv isize=512    agcount=4, agsize=16384 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=65536, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 65536 to 102400

The key line is data blocks changed from 65536 to 102400, which confirms the filesystem has grown.

2. Verify the New Filesystem Size

Now, run the df -h command again to verify that the filesystem size reflects the change.

df -h /data

The output should now show the new, larger size, which is approximately 400 MiB.

Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/my_vg-my_lv     395M   29M  367M   8% /data

You can also check that the test file you created earlier is still intact:

cat /data/test.txt
My LVM is working!

Congratulations! You have successfully completed the entire LVM workflow: from partitioning a physical disk and creating Physical Volumes, to building a Volume Group, creating a Logical Volume, formatting and mounting it, and finally, dynamically extending it online without any data loss.

Summary

In this lab, you learned the fundamental workflow for setting up storage using the Logical Volume Manager (LVM) in RHEL. You started by preparing a physical disk, using parted to create a partition with the lvm flag set, and then initializing it as a Physical Volume (PV) with pvcreate. Following this, you pooled the PV into a new Volume Group (VG) using vgcreate. From this VG, you carved out a Logical Volume (LV) with lvcreate, formatted it with an XFS filesystem using mkfs.xfs, and configured it for persistent mounting by adding an entry to /etc/fstab.

The lab also demonstrated the flexibility of LVM by guiding you through the process of extending an existing logical volume. This involved preparing a new physical volume, adding it to the volume group with vgextend to increase the total available space, and then extending the logical volume itself with lvextend. Finally, you resized the XFS filesystem online using xfs_growfs to make the additional storage space usable by the operating system, completing the end-to-end process of dynamic storage management.