Logical Volume Manager (LVM) is an advanced storage management system in Linux that provides greater flexibility in allocating disk space across multiple physical storage devices. With LVM, you can easily create, resize, delete and move disk partitions without unmounting them or rebooting the system. In this comprehensive LVM tutorial for Ubuntu, we will cover everything you need to know to effectively set up and manage LVM.

LVM Components

LVM has three main components:

  1. Physical Volumes (PV) – The underlying block devices on which LVM operates, like partitions or whole disks.

  2. Volume Groups (VG) – A collection of one or more physical volumes grouped together. This pool of storage space is used to create logical volumes.

  3. Logical Volumes (LV) – Virtual block devices that are allocated space from an underlying volume group. Filesystems are created on logical volumes.

The key advantage of LVM is that file systems can span across multiple disks since the logical volumes act as abstract layers between the file systems and underlying physical storage.

Installing LVM Utilities

To manage LVM on Ubuntu, you need to install the lvm2 package which provides command line utilities for LVM operations:

sudo apt install lvm2

These utilities allow you to create, modify and delete LVM entities like physical volumes (pv), volume groups (vg) and logical volumes (lv).

Creating Physical Volumes

The first step is to initialize block devices as LVM physical volumes that can be later added to a volume group. This can be an entire disk, a partition or a RAID array.

Check the block devices on your system:

sudo lvmdiskscan

For demonstration, we‘ll use /dev/sdb disk to create a physical volume.

Use pvcreate command:

sudo pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created

Verify with pvs:

sudo pvs
  PV         VG     Fmt  Attr PSize    PFree 
  /dev/sdb       lvm2 ---    5.00g  5.00g

This initializes /dev/sdb as an LVM physical volume with 5GB free space available now.

Creating Volume Groups

Next, create a volume group by combining one or more physical volumes. The space will be pooled from underlying PVs to allocate to logical volumes later.

Use vgcreate command to create a volume group:

sudo vgcreate data /dev/sdb
  Volume group "data" successfully created

Verify details with vgs:

sudo vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  data   1   0   0 wz--n- 5.00g 5.00g

The above output shows volume group "data" was created using one PV having total space of 5GB.

You can extend the VG later by adding more PVs using the vgextend command.

Creating Logical Volumes

With volume group in place, next step is create logical volumes (like virtual partitions) which will actually hold the filesystems.

Use lvcreate utility to create logical volumes by allocating space from the underlying VG:

sudo lvcreate -L 2G -n videos data
  Logical volume "videos" created.

This creates a 2GB LV named "videos" from "data" volume group.

Verify it:

sudo lvs
  LV     VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  videos data -wi-ao----   2.00g                                                    

You can create multiple logical volumes by allocating remaining free space in the volume group.

For example, create another 3GB LV named "music":

sudo lvcreate -L 3G -n music data 
  Logical volume "music" created.

Verify:

sudo lvs
  LV     VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  music  data -wi-ao---- <3.00g>              
  videos data -wi-ao----   2.00g  

Now you can go ahead and directly format the logical volumes with filesystems without knowing the underlying physical storage structure.

This enables flexibility in resizing logical volumes on demand later.

Extending and Reducing LVM Components

One of the key advantages of LVM is the ability to easily resize components on live filesystems.

  • Use lvextend to increase capacity of a logical volume

  • Use lvreduce to shrink the LV size

  • Free up space from volume group with vgreduce

  • Extend by adding more storage to volume group using vgextend

For example, to extend the "videos" LV to 4GB :

sudo lvextend -L +2G /dev/data/videos 
  Size of logical volume data/videos changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).

No need to unmount or reboot. The filesystem will be automatically resized on the next write.

Similarly you can easily shrink volumes with lvreduce or add/remove physical volumes from volume groups dynamically.

Creating Snapshots

LVM allows you to take snapshots of active logical volumes – this creates a read-only point-in-time copy that can be used for backups.

For example, take a snapshot of the "videos" LV:

sudo lvcreate -L 1G -s -n videos-snap /dev/data/videos

This will create a 1GB snapshot volume named "videos-snap" by synchronising contents from the original "videos" LV.

You can mount this snapshot and copy files for backup purposes without affecting the live filesystem.

Later, you can merge snapshots back to restore changed blocks to the original volume.

Monitoring LVM Status

There are a variety of LVM utilities to query status of components:

  • pvs – Display information on physical volumes

  • vgs – Get details of volume group(s)

  • lvs – List configured logical volume(s)

  • lvscan – List all logical volumes detected on the system

You can also view full LVM configuration details with:

sudo pvs && sudo vgs && sudo lvs

Use these tools to monitor space usage and track your LVM setup.

Removing LVM Components

  • To delete a logical volume, use lvremove:
sudo lvremove /dev/data/videos
  • To remove an empty volume group, use vgremove:
sudo vgremove data
  • To remove a physical volume from system use pvremove:
sudo pvremove /dev/sdb

This will wipe LVM metadata from the disk to return it back as a non-LVM device.

That concludes this LVM beginners tutorial!

As you can see, Logical Volume Management really simplifies storage administration on Linux by creating an abstract layer over your physical disks. With LVM, you can flexibly allocate disk space, take snapshots for backups and easily extend or shrink volumes on live systems.

Similar Posts