How to use LVM to manage storage on Ubuntu & Debian

LVM Manage Storage On Debian CentOS Fedora RedHat Ubuntu 18.04/20.04/22.04

Logical Volume Management (LVM) is a useful tool for managing large storage volumes on Linux. It allows you to create logical volumes that can span multiple physical storage devices. This gives you flexibility in allocating disk space that goes beyond the restrictions of physical disk partitions.

In this guide, we will cover the basics of setting up and managing LVM volumes on Debian & Ubuntu 18.04, 20.04, and 22.04.

Overview of LVM

Some key terms and concepts relating to LVM:

  • Physical volumes (PV) – The underlying physical storage devices that provide the actual storage capacity, such as partitions on hard drives or SSDs.
  • Volume groups (VG) – A collection of physical volumes grouped together. This allows physical storage devices to be combined into larger pools of storage.
  • Logical volumes (LV) – Virtual block devices that are allocated space from the underlying volume group. You can create, resize and delete logical volumes as needed.
  • Extents – Small sized chunks of contiguous space on a physical volume. When allocated to a logical volume, they provide storage to the volume.

The key advantage of LVM is the abstraction between physical disks and logical volumes. By using LVM, you don’t have to worry about the specifics of the physical storage. You can resize, snapshot, and move logical volumes without having to modify the underlying physical devices.

Installing LVM

To get started with LVM, you first need to install the lvm2 package:

$ sudo apt install lvm2

This will install all the tools necessary to manage LVM volumes.

Creating Physical Volumes

The first step is to initialize disks or partitions as physical volumes (PVs) to be used by LVM.

For example, to initialize the /dev/sdb1 partition as a LVM physical volume:

$ sudo pvcreate /dev/sdb1

Output:

  Physical volume "/dev/sdb1" successfully created

You can verify available PVs:

$ sudo pvs

Output:

  PV         VG        Fmt  Attr PSize   PFree
  /dev/sdb1             lvm2 ---  <223.57G <223.57G

This will list all physical volumes that LVM can access.

Creating Volume Groups

Next, you need to combine one or more PVs into a volume group (VG). The VG will be allocated space from the pooled PVs.

For example, to create a volume group called data using the /dev/sdb1 PV:

$ sudo vgcreate data /dev/sdb1

Output:

  Volume group "data" successfully created

You can verify available VGs:

$ sudo vgs

Output:

  VG   #PV #LV #SN Attr   VSize  VFree
  data   1   0   0 wz--n- <223.57G <223.57G

List the details of a specific VG:

$ sudo vgdisplay data

Output:

  --- Volume group ---
  VG Name               data
  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               <223.57 GiB
  PE Size               4.00 MiB
  Total PE              57313
  Alloc PE / Size       0 / 0   
  Free  PE / Size       57313 / <223.57 GiB

Creating Logical Volumes

Once you have a volume group, you can create logical volumes (LVs) within that VG.

For example, to create a 10GB logical volume called logs:

$ sudo lvcreate -L 10G -n logs data

Output:

  Logical volume "logs" created.

This creates a 10GB LV called logs in the VG data.

To create an LV that uses all remaining free space in the VG:

$ sudo lvcreate -l 100%FREE -n apps data 

Output:

  Logical volume "apps" created.

Verify logical volumes:

$ sudo lvs

Output:

  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  apps data -wi-a----- <213.57G                                                    
  logs data -wi-a----- 10.00G  

You can also see the full path for the created LVs with:

$ sudo lvdisplay

Output:

  --- Logical volume ---
  LV Path                /dev/data/apps
  LV Name                apps
  VG Name                data
  LV UUID                XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2023-07-26 15:37:11 +0200
  LV Status              available
  # open                 0
  LV Size                <213.57 GiB
  Current LE             54698
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

The logical volumes will be mapped to /dev/VG/LV devices that can be formatted and mounted, just like regular partitions.

Formatting and Mounting Logical Volumes

To use LVM logical volumes, they need to be formatted with a filesystem like any other block device.

For example, to format the logs LV to ext4 and mount it at /var/log:

$ sudo mkfs.ext4 /dev/data/logs 
$ sudo mkdir /var/log
$ sudo mount /dev/data/logs /var/log

To mount this automatically on reboot, add this to /etc/fstab:

/dev/data/logs /var/log ext4 defaults 0 0

Do the same for any other LVs you want to use permanently.

Extending a Logical Volume

One of the advantages of LVM is that you can easily extend logical volumes.

For example, to grow the logs LV by 5GB, first extend the volume group by 5GB:

$ sudo lvextend -L +5G /dev/data/logs

Output:

  Size of logical volume data/logs changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
  Logical volume data/logs successfully resized.

Then resize the filesystem to match:

$ sudo resize2fs /dev/data/logs 

Output:

resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/data/logs is mounted on /var/log; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/data/logs is now 153600 (4k) blocks long.

The LV is now 5GB larger.

This is much easier than extending disk partitions, which requires painful steps like moving and resizing adjacent partitions.

Reducing a Logical Volume

To shrink an LV, resize the filesystem first:

$ sudo resize2fs /dev/data/logs 20G

Output:

resize2fs 1.45.5 (07-Jan-2020)
Resizing the filesystem on /dev/data/logs to 5242880 (4k) blocks.
The filesystem on /dev/data/logs is now 5242880 (4k) blocks long.

This shrinks the filesystem to 20GB.

Then shrink the LV size to match:

$ sudo lvreduce -L 20G /dev/data/logs

Output:

  Size of logical volume data/logs changed from 15.00 GiB (3840 extents) to 20.00 GiB (5120 extents).
  Logical volume data/logs successfully resized.

The logs LV is now reduced to 20GB.

Creating Snapshots

LVM allows you to create snapshots of logical volumes. This can be used to take backups of LVs.

For example, create a snapshot of the logs LV:

$ sudo lvcreate --size 10G --snapshot --name logs-snap /dev/data/logs

Output:

  Logical volume "logs-snap" created.

This will create a snapshot called logs-snap that is a copy of logs at the time the snapshot was taken.

Initially the snapshot uses no space, but as the original LV changes, the snapshot will grow to store the old blocks as they are overwritten.

You can mount, back up, or restore data from the snapshot just like a regular LV.

Monitoring LVM Usage

It’s important to monitor your LVM volume groups and logical volumes to make sure you don’t run out of space.

To see allocated physical extents per VG:

$ sudo vgdisplay -v data

Output:

  --- Volume group ---
  VG Name               data
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <223.57 GiB
  PE Size               4.00 MiB
  Total PE              57313
  Alloc PE / Size       15360 / 60.00 GiB
  Free  PE / Size       41953 / <163.57 GiB

To monitor LV usage:

$ sudo lvdisplay 

Output:

  --- Logical volume ---
  LV Path                /dev/data/logs
  LV Name                logs
  VG Name                data
  LV UUID                XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2022-03-05 10:17:11 +0200
  LV Status              available
  # open                 0
  LV Size                20.00 GiB
  Current LE             5120
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/data/apps
  LV Name                apps
  VG Name                data
  LV UUID                XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2023-07-26 15:37:11 +0200
  LV Status              available
  # open                 0
  LV Size                <193.57 GiB
  Current LE             49408
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

You can also install an LVM monitoring utility like lvm2 snarf to get notifications when usage crosses thresholds.

Advantages of LVM

Some of the key advantages of LVM include:

  • Flexibility to grow and shrink logical volumes as needed, without partitions restraints.
  • Easily take snapshots and backups of volumes.
  • Pool storage from multiple devices into a common volume group.
  • Migrate data by moving logical volumes between storage devices.
  • Allocate striped, mirrored or encrypted logical volumes.

Overall, LVM provides powerful, flexible storage management for your Ubuntu servers. With a bit of learning, it can be tremendously useful for managing large and growing storage demands.

Conclusion

LVM provides a robust way to manage large-scale and dynamic storage in Ubuntu and Debian. By using LVM, you can effectively handle constantly changing storage needs, while also enabling key functionality like snapshots and thin provisioning. However, it does require learning some new concepts and commands. With some hands-on practice, LVM can become an indispensable part of how you deploy and manage Ubuntu storage. Used properly, it can save you a lot of time and hassle as your needs grow and change. The flexibility of being able to dynamically grow, shrink, and migrate volumes makes LVM well worth the initial effort to learn.

LEAVE A COMMENT