Home Infrastructure Part 3 – Creating an LVM
An LVM is a `Logical Volume Manager`. You can think of it as a system for managing how the space on your disks is used.
It’s sometimes beneficial to use LVM for your storage because you can’t always plan ahead for how large partitions should be sized. LVM means you can have a “virtual” storage pool, and add space when required to increase its capacity by adding new physical drives. You can move your virtual disks around and resize as you need to.
Some basic partitioning
First you want to find the physical device that you want to install LVM on to. A useful command for this is `lsblk`, this lists your block devices (usually things like USB sticks, hard drives but technically a block device is anything that provides buffered access to hardware devices. From the output you can see that there’s a disk at “sda” and a partition at “sda1” already.
Fdisk is a program that will let you partition your drives. The reason I choose fdisk is that it’s present on mostly all systems, although be aware that some distributions (FreeNAS in particular) don’t include versions that allow you to create GPT formatted partition table , in which case run parted.
Run the fdisk command on the disk (in this case /dev/sda).
What we want to do in this case is create a GPT partition table. This is because the other commonly used type (DOS) has certain restrictions (a 2TB size limit on disks for one, but also a restriction in using the primary/secondary/extended paradigm if you want a large number of partitions).
Configure your disk partitions
Assuming it’s a fresh disk, OR you are happy that you will wipe the disk and it will be irretrievable, follow the steps below. A safe way to try this out would be in a Virtual Machine.
Pick the options :
- ‘l’ to list your partitions (there should be none if you’ve just created a partition type)
- ‘n’ to create a new partition. Pick the partition number (default is 1)
- ‘t’ to set the partition type to Linux LVM (30)
- ‘w’ to write out the partition table to the disk. You should now have a drive with one unformatted partition at its maximum capacity.
Note: we don’t want to create any more partitions at this point, as we’re just using it as a substrate for the LVM storage. More ‘partitions’ would be added through LVM itself as that’s the technology that manages the partitions.
So first we create the physical volume on the Linux LVM partition at /dev/sda1.
Then we want to create the Volume Group. You can think of this as a collection of Logical Volumes which themselves are a bit of virtual storage, like a partition, but that can be moved around and resized at will (well, with some caveats – namely the file system inside)
[root@archbang ablive]# pvcreate /dev/sda1
Physical volume "/dev/sda1" successfully created
[root@archbang ablive]# pvdisplay
"/dev/sda1" is a new physical volume of "40.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda1
PV Size 40.00 GiB
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID sQ1aHi-0gVH-zlAL-MFvp-2vlg-2vDt-CbvtAa
[root@archbang ablive]# vgcreate vg-root /dev/sda1
Volume group "vg-root" successfully created
[root@archbang ablive]# vgdisplay
--- Volume group ---
VG Name vg-root
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 40.00 GiB
PE Size 4.00 MiB
Total PE 10239
Alloc PE / Size 0 / 0
Free PE / Size 10239 / 40.00 GiB
VG UUID TbuJk8-gGeO-0lfG-MuOQ-7L5u-02Pq-ouAgda
Creating your Logical Volumes
Create a Logical Volume, choosing the size (L) and the name (n) and the name of the Volume Group you want to add it to.
lvcreate -L 30G -n root vg-root
You could create a volume that spans the entirity of the available space, or create smaller ones and use them for each partition (/boot, /home, /etc)
Activating your LVM
The Logical Volumes can then be made known to the kernel using the command `lvchange`
lvchange -ay vg-root
This makes them available under `/dev/vg-root` (or equivelent vg name)
mkfs.ext4 /dev/vg-root/root # create a file system (make your own choice here depending on need)
Striping and Mirroring (if you like)
When you write your data to a logical volume, LVM decides how best to store your data on the underlying physical volumes that the volume group resides upon. As you can see above, this means we’re storing out ‘vg-root’ logical volume on the physical volume /dev/sda1 (the LVM partition we made earlier). Don’t worry if you get confused with the terminology, with practice it becomes clear.
Something a bit more advanced would be to create a Striped Logical Volume. This means you can intentionally add multiple physical devices to an LVM and then choose to stripe, which means data can be written and read in parallel with multiple physical drives but it also increases the chances of data loss. This can be thought of as being along the same lines as a Software RAID 1+0.
There’s also a Mirrored Logical Volume, which as the name implies maintains identical copies of the data written onto multiple physical drives. This means that recovery is possible if one of the underlying physical drives fails.
So there you have it, the LVM should be up and running.