Logical Volume Management (LVM)

Logical volume management is essentially a supercharged and abstracted version of disk partitioning.

  1. It groups individual storage devices into volume groups.
  2. The blocks in a volume group can then be allocated to logical volumes which are represented by block device files and act like disk partitions.

Logicial volumes has more megical operations that the volume manager lets you carry out:

  • Move logical volumes among different physical devices
  • Grow and shrink logical volumes on the fly
  • Take copy-on-write “snapshots” of logical volumes
  • Replace on-line drives without interrupting service
  • Incorporate mirroring or striping in your logical volumes

Management

LVM Commands Reference

EntityOperationCommand
Physical volumeCreatepvcreate
Inspectpvdisplay
Modifypvchange
Checkpvck
Volume groupCreatevgcreate
Modifyvgchange
Extendvgextend
Inspectvgdisplay
Checkvgck
Enablevgscan
Logical volumeCreatelvcreate
Modifylvchange
Resizelvresize
Inspectlvdisplay

You can use these commands or use lvm and it’s subcommands, they’re identical and just call the same logic.

Note

Physical volumes in LVM are misleading because physical volumes dont need direct correspondence to physical devices. They can be disks, but they can also be disk partitions or RAID arrays. LVM doesn’t care.

Configuration

Core phases:

  1. define (label) and initilize physical volumes
  2. add physical volumes to a volume group
  3. create logical volumes on the volume group

LVM commands start with letters that make it clear at which level of abstraction they operate:

  • pv - physical volumes
  • vg - volume groups
  • lv - logical volumes
  • lvm - the system as a whole

Configuration Example

  • We set up a 1TB hard disk (/dev/sdb) for use with LVM and create a logical volume.
  • We assume that the disk has been partitioned with all space being assigned to a single partition, /dev/sdb1.

We could omit the partitioning step entirely and just use the raw disk as our physical device, but there is no performance benefit to doing so. Partitioning makes the disk comprehensible to the broadest variety of software and operating systems.

  1. label sdb1 parition as a LVM physical volume so it can be used in a volume group

    Bash
    sudo pvcreate /dev/sdb1
    # Physical volume "/dev/sdb1" successfully created.
  2. Add the new physical volume to a volume group

    Bash
    sudo vgcreate vg0 /dev/sdb1
    # Volume group "vg0" successfully created

    vg0 is just a convention for volume group 0 it can be named anything.

    We could add more phyiscal devices to our volume group now if we wanted more storage, we can do that later as well.

    Bash
    sudo vgdisplay vg0
    --- Volume group ---
    VG Name               vg0 # volume group name
    System ID             
    Format                lvm2        # Metadata format (always lvm2)
    Metadata Areas        1 
    Metadata Sequence No  1
    VG Access             read/write  # accessible for r/w
    VG Status             resizable   # can be resized
    MAX LV                0           # max logical volumes (0 = unlimtied)
    Cur LV                0           # current logical volumes created
    Open LV               0           # logical volumes mounted 
    Max PV                0           # max physical volumes (0 = unlimited)
    Cur PV                1           # current physical volunes in group
    Act PV                1           # active physical volumes
    VG Size               1020.00 MiB # Total capacity 
    PE Size               4.00 MiB    # allocation unit size (smallest logical volume we can make 4MiB)
    Total PE              255
    Alloc PE / Size       0 / 0   
    Free  PE / Size       255 / 1020.00 MiB
    VG UUID               YlCGhF-5Red-2yvm-qk64-rgsr-qrjm-Xuj95Q
  3. Create the logical volume with vg0

    sudo lvcreate -L 100M -n website1 vg0
    # Logical volume "website1" created.

    Fixed Size (-L) - K (Kilobytes), M (Megabytes), G (Gigabytes), T (Terabytes)

    Now we’re at the same view as paritions, the lv is a block device we map a file system to.

  4. Create filesystem on the logical volume

    sudo mkfs.ext4 /dev/vg0/website1 
    # ...
    
    sudo mkdir /opt/website1
    sudo mount /dev/vg0/website1 /opt/website1 
    # fstab if we want to persistance

    Done!

Filesystem Resizing

The logical volume manager doesn’t know anything about the contents of its volumes, so you must do your resizing at both the volume and filesystem levels.

The order depends on the specific operation.

  • Reductions must be filesystem-first
  • enlargements must be volume-first.

Important

Don’t memorize these rules: just think about what’s actually happening and use common sense.

Resizing example

I’m going to add 100MB to my previous example lv/fs

Bash
sudo umount /opt/website1
sudo lvdisplay
➜ sand /opt sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/website1
  LV Name                website1
  VG Name                vg0
  LV Size                100.00 MiB

sudo lvresize -L +100M /dev/vg0/website1
sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/website1
  LV Name                website1
  VG Name                vg0
  LV Size                200.00 MiB

The original filesystem is still 100MiB, we need to resize it using resize2fs, which is meant for ext2+ filesystems’ (other filesystems have different utilities).
resize2fs determines the size of the new fs from the volume so we only need to specify the new size when shrinking.

Bash
sudo resize2fs /dev/vg0/website1
resize2fs 1.47.0 (5-Feb-2023)
Resizing the filesystem on /dev/vg0/website1 to 51200 (4k) blocks.
The filesystem on /dev/vg0/website1 is now 51200 (4k) blocks long.

We can confirm with df

Bash
sudo mount /dev/vg0/website1 /opt/website1
df /opt/website1 -h
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/vg0-website1  184M   24K  170M   1% /opt/website1

Cloud Disks / Hypervisor Disks

“Disks” you allocate and attach to virtual machines in the cloud are essentially logical volumes, although the volume manager itself lives elsewhere in the cloud. These volumes are usually resizable through the cloud provider’s management console or command-line utility.

The procedure for resizing cloud filesystems is much the same as the one outlined above, but keep in mind that in the background the cloudprovider is just using lvm to give you new logical disks which you’re segmenting.

Proof in Proxmox

I can this on my proxmox host, as we see the drive we’ve been working with is just a logical volume itself.

$ lvdisplay

 --- Logical volume ---
  LV Path                /dev/pve/vm-108-disk-1
  LV Name                vm-108-disk-1
  VG Name                pve
  LV Size                1.00 GiB

The hypervisor presents it to the VM as a virtual block device, all the vm sees is /dev/sdb. Awesome right?