How to Configure LVM (Logical Volume Manager) in Debian 12 Bookworm System

In this guide, we will walk through the process of configuring LVM (Logical Volume Manager) on a Debian 12 “Bookworm” system.

Logical Volume Manager (LVM) is a powerful and flexible storage management solution in Linux. It allows users to dynamically manage disk space, create and resize volumes, and implement advanced storage configurations. In this guide, we will walk through the process of configuring LVM on a Debian 12 “Bookworm” system.

Prerequisites

Before proceeding with the configuration, ensure that:

  • You have a Debian 12 Bookworm system installed.
  • You have root or sudo privileges.
  • You have at least one additional storage device or free disk space.
  • You have lvm2 installed (if not, we will cover installation below).

Step 1: Install LVM2 Package

LVM functionality is provided by the lvm2 package. First, check if it is installed by running:

sudo lvmdiskscan

If the command is not found, install the package using:

sudo apt update && sudo apt install lvm2 -y

Enable and start the LVM service:

sudo systemctl enable lvm2-lvmetad
sudo systemctl start lvm2-lvmetad

Step 2: Identify Available Storage Devices

Use the lsblk or fdisk -l command to identify available storage devices:

lsblk

Example output:

sda      8:0    0  50G  0 disk
sdb      8:16   0 100G  0 disk
sdc      8:32   0 200G  0 disk

In this example, sdb and sdc are the additional disks available for LVM configuration.

Step 3: Create Physical Volumes

To use disks with LVM, they need to be initialized as physical volumes (PVs). Run the following command to initialize them:

sudo pvcreate /dev/sdb /dev/sdc

Verify the creation of physical volumes:

sudo pvdisplay

Step 4: Create a Volume Group

A volume group (VG) is a collection of physical volumes that act as a single storage pool. Create a volume group named vg_data using:

sudo vgcreate vg_data /dev/sdb /dev/sdc

Verify the volume group:

sudo vgdisplay

Step 5: Create Logical Volumes

Once a volume group is created, logical volumes (LVs) can be allocated from it. For example, create a logical volume named lv_storage of size 50GB:

sudo lvcreate -L 50G -n lv_storage vg_data

To create a logical volume using all available space:

sudo lvcreate -l 100%FREE -n lv_backup vg_data

Verify logical volumes:

sudo lvdisplay

Step 6: Format the Logical Volume

To use the logical volume, format it with a file system. For example, format lv_storage with ext4:

sudo mkfs.ext4 /dev/vg_data/lv_storage

For XFS:

sudo mkfs.xfs /dev/vg_data/lv_storage

Step 7: Mount the Logical Volume

Create a mount point and mount the logical volume:

sudo mkdir -p /mnt/storage
sudo mount /dev/vg_data/lv_storage /mnt/storage

To make the mount persistent, add the following entry to /etc/fstab:

/dev/vg_data/lv_storage /mnt/storage ext4 defaults 0 2

Replace ext4 with the correct file system if necessary.

Step 8: Extend a Logical Volume (Optional)

If you need to increase the size of a logical volume, use:

sudo lvextend -L +10G /dev/vg_data/lv_storage

For full size extension:

sudo lvextend -l +100%FREE /dev/vg_data/lv_storage

Then, resize the file system:

For ext4:

sudo resize2fs /dev/vg_data/lv_storage

For XFS:

sudo xfs_growfs /mnt/storage

Step 9: Remove a Logical Volume (Optional)

If you need to remove a logical volume:

sudo umount /mnt/storage
sudo lvremove /dev/vg_data/lv_storage

To remove the volume group:

sudo vgremove vg_data

To remove physical volumes:

sudo pvremove /dev/sdb /dev/sdc

Conclusion

Configuring LVM on Debian 12 Bookworm provides flexibility in managing storage. With LVM, you can dynamically allocate space, resize volumes, and optimize disk usage efficiently. By following these steps, you can create, manage, and extend logical volumes on your system seamlessly.