Setting up Logical Volumes with LVM

From Alpine Linux

This document how to create logical volumes in Alpine using lvm2.

LVM is collection of programs that allow larger physical disks to be reassembled into "logical" disks that can be shrunk or expanded as data needs change.

In this document we will use a software raid1 device as physical storage for our logical volumes. We will set up a swap partition and a data partition for vservers

Installing LVM software

First we need to load the kernel driver, dm-mod

modprobe dm-mod

We also want it to be loaded during next reboot.

echo dm-mod >> /etc/modules

We also need the userspace programs.

apk_add lvm2

Preparing the physical volumes

First we need to tell LVM that de partition is available as a physical volume and can be added to a volume group. In this example we use a software raid array as physical volume.

pvcreate /dev/md0

Preparing the Volume Group

We can then create a volume group and add the physical volume /dev/md0

vgcreate vg0 /dev/md0

If we later need more space we can add additional physcal volumes with vgextend. All physcal disks/partitions added need to be prepared with pvcreate.

In the volume group we can create logical volumes. To create a 1GB volume called swap and a 6GB volume called 'vservers on the volume group vg0 we run

lvcreate -n swap -L 1G vg0
lvcreate -n vservers -L 6G vg0

You can now se the logical volumes with the lvdisplay utility.

lvdisplay
 --- Logical volume ---
 LV Name                /dev/vg0/swap
 VG Name                vg0
 LV UUID                a4NYOi-FQP6-Lj5Q-0TYk-Jjtk-Qxjt-nxeBPn
 LV Write Access        read/write
 LV Status              available
 # open                 0
 LV Size                1.00 GB
 Current LE             256
 Segments               1
 Allocation             inherit
 Read ahead sectors     0
 Block device           253:0
  
 --- Logical volume ---
 LV Name                /dev/vg0/vservers
 VG Name                vg0
 LV UUID                16VMmy-7I0s-eeoW-tL2V-JrlN-jM6C-d0wEg0
 LV Write Access        read/write
 LV Status              available
 # open                 0
 LV Size                6.00 GB
 Current LE             1536
 Segments               1
 Allocation             inherit
 Read ahead sectors     0
 Block device           253:1
  

This volume shows up as /dev/vg0/vservers and can be used as device for mounting. (You will need to create a filesystem first)