Setting up Logical Volumes with LVM

Category:Storage
This document how to create logical volumes in Alpine using lvm2.
LVM is collection of programs that allow 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
We also want it to be loaded during next reboot.
We also need the userspace programs.
=== Preparing the physical volumes ===
First we need to tell LVM the 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.
=== Preparing the Volume Group ===
We can then create a volume group and add the physical volume /dev/md0
If we need more space later, we can add additional physcal volumes with vgextend. All physcal disks/partitions added need to be prepared with pvcreate.
=== Creating Logical volumes ===
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
=== Display Logical Volumes ===
You can now see 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
=== Rename Logical Volumes ===
=== Extend Logical Volumes ===
If you want to add space and the volume has the room for it...
If you want to set the space to a new larger size...
=== Start LVM during Boot ===
We want lvm to init the logical volumes during boot. There is a boot service named lvm to do this. If your volumes are on raid, make sure that /etc/init.d/lvm is started after mdadm-raid.
on Alpine Linux 1.8 or earlier:
=== Setting up swap ===
Now we have our devices in /dev/vg0 and can use them as normal disk paritions. To set up swap:
Add the following line to your /etc/fstab:
 /dev/vg0/swap none swap sw 0 0
=== Setting up /vservers partition ===
Finally we want to set up an XFS partition for /vservers.
Install xfsprogs.
Create filesystem on /dev/vg0/vservers.
Add the mount information to your /etc/fstab: NOTE:tagxid may cause this to not mount. Try this by hand and check dmesg to see if there are any errors:
 /dev/vg0/vservers /vservers xfs noatime,tagxid 0 0
Note that the tagxid option is specific for setting up vserver disk limits so you might not want it.
The noatime option increases performance, but you will no longer know when files were last accessed.
=== Starting localmount and swap ===
Now we can mount our partition.
We need to run localmount during boot, but it has to happen after lvm. In Alpine Linux 1.9 and later, this should not be needed
on Alpine Linux 1.8 or earlier:
Start the swap service and make sure it starts during next reboot and that it starts after lvm.
on Alpine Linux 1.8 or earlier:
=== More Info on LVM ===
These resources may be helpful:
* the common tasks section in the LVM Howto
* Alpine wiki page for LVM on LUKS
* Arch wiki page on LVM
* Arch wiki page on RAID and LVM