1/n LVM Story

I have to create a LVM setup on two machines. I did not use LVM for a long time, so this will be a short story about re-learning how to use/setup LVM.
Maybe a Linux-Newbie finds it useful.

2/n LVM Story

So my systems are 18.04 LTS installations (not that this matters at all here), with 8 drives in total.
Two of these drives are 300 GB HDDs, which are configured to run in RAID1 and are used for the installation of the OS and /home. Nothing special here and I do not touch these, just noted for the sake of completeness.

3/n LVM Story

The other six drives are 900 GB HDDs, probably some Server-Drives (I did not open the hardware before pushing it into the rack). So we have /dev/sd{b,c,d,e,f,g} to use as physical volumes.

3/n LVM Story

A physical volume can either be a partition which already exists on a drive or a complete drive.

In my scenario, it only will be complete drives.

We need to tell LVM about physical volumes. Later, we can group these together (as a so called "Volume Group") and allocate "Logical Volumes" on them.

A Logical Volume, or LV, can be allocated without actually knowing on which Physical Volume (one or many) the LV really is.

4/n LVM Story

And exactly that is the benefit: You can put, for example, six 900 GB drives into one volume group (VG) and then allocate LVs from it, even larger than 900GB!

And that's also my usecase. I do not know yet, though, how big my LV will be in the end. But that's also no problem, as you can resize them later on.

Just be careful: A LV also needs a Filesystem (FS). And it is recommended to use EXT{2,3,4} here.

5/n LVM Story

I will need three LVs with EXT4 FS on them. So lets go:

`$ sudo lvmdiskscan` gives me a list of drives that LVM can see and use.

After that, lets start allocating PVs: `sudo pvcreate /dev/sd{b,c,d,e,f,g}`.

This succeeds, but we can check what happened afterwards with `sudo pvs`.

6/n LVM Story

Now we need to create a VG for the host.

Make sure to use "speaking names" as the name for the volume group. Itn does not hurt and might help avoiding confusion later on:

`sudo vgcreate vg-$(hostname)-main /dev/sd{b,c,d,e,f,g}`

7/n LVM Story

After creating the VG, we can again check what is the current state of our setup with `sudo pvs`.

Now we have to create the LVs we need. In my case, we need a "scratch space" which should be at least 400GB, so let me allocate 600GB which should be plenty for now:

`sudo lvcreate -L 600G -n lv-$(hostname)-scr vg-$(hostname)-main`

This tells LVM to create a 600GB lv with the name "lv-<hostname>-scr` in the VG we created beforehand.

8/n LVM Story

Next, we'll need a LV for docker, which should be 150GB at least. Let me allocate 300GB to be sure:

`sudo lvcreate -L 300G -n lv-$(hostname)-docker vg-$(hostname)-main`

I remember that docker can use LVM devices out of the box but I've never done that and I am not sure I want it. From what I see in the documentation for docker, it would auto-expand the LV. I most certainly don't want that ever to happen.

9/n LVM Story

Last but not least, we need a 2TB LV for data. The reader might already know how this works at this point.

To check what's there, we can use `sudo vgs -o +lv_size,lv_name` and we'll see the layout of our LVs.

Now, we can create the filesystems:

`sudo mkfs.ext4 -L fs-$(hostname)-docker /dev/vg-$(hostname)-main/lv-$(hostname)-docker` (adapt for the other LVs).

Follow

10/n LVM Story

And our last step is to create the mountpoints: `sudo mkdir /docker` (for example) and mount the /dev/vg-.../lv... to it.

Adding the entries to the /etc/fstab so that the drives are mounted on boot is optional, but I'll need it in my scenario, of course.

After mounting `df -h` shows which LVs mounted where. In my scenario, I see that the data LV is mounted from three different disks (because it does not fit on less, of course).

· · Web · 1 · 1 · 0

11/11 LVM Story

That was my rather quick take on LVM setup.

I guess I'll write a more detailed blog article about the whole thing at some point.

Please keep in mind that I'm just re-learing the whole thing and am no way a professional. Everything you saw here is LVM _basics_! It can do _so much more_! Reading the manpages might be enlightening in its own way!

Have a good day!

Addendum: Setting up the second of my two hosts was just copying the history of the first host, because I used $(hostname) for the names everywhere, so it was essentially a no-brainer.

Sign in to participate in the conversation
Mastodon for Tech Folks

This Mastodon instance is for people interested in technology. Discussions aren't limited to technology, because tech folks shouldn't be limited to technology either!