Use LVM on Flatcar Container Linux | Flatcar Container Linux
Use LVM on Flatcar Container Linux
LVM - Logical Volume Management - allows you to create logical volumes, for example to use multiple physical disks as
one volume. This allows you to make the full use of all attached disks.
Flatcar Linux has built-in support for LVM.
This guide covers creation of logical volumes using LVM and how to use them.
Creating LVM
There are two main ways to do this: create everything manually or use an Ignition config. We will first cover
the manual way to get a better grip of what is happening, then we will cover the Ignition way.
Manual
You can find all volumes using the lsblk command. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop3 7:3 0 68.3M 1 loop
loop4 7:4 0 39.3M 1 loop
loop5 7:5 0 4K 1 loop
sda 8:0 0 223.6G 0 disk
|-sda1 8:1 0 128M 0 part
|-sda2 8:2 0 2M 0 part
|-sda3 8:3 0 1G 0 part
|`-usr 254:0 0 1016M 1 crypt /usr
|-sda4 8:4 0 1G 0 part
|-sda6 8:6 0 128M 0 part /oem
|-sda7 8:7 0 64M 0 part
`-sda9 8:9 0 221.3G 0 part /
sdb 8:16 0 447.1G 0 disk
sdc 8:32 0 447.1G 0 disk
sdd 8:48 0 223.6G 0 disk
Now we know that we have /dev/sda, /dev/sdb, /dev/sdc, /dev/sdd available. However, we cannot use /dev/sda in
this scenario, but we can work with the others.
1
2
3
4
5
# pvcreate /dev/sdb /dev/sdc /dev/sdd WARNING: Failed to connect to lvmetad. Falling back to device scanning.
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.
You can verify that everything worked with the following commands:
# pvs PV VG Fmt Attr PSize PFree
/dev/sdb lvm2 --- 447.13g 447.13g
/dev/sdc lvm2 --- 447.13g 447.13g
/dev/sdd lvm2 --- 223.57g 223.57g
# pvdisplay"/dev/sdd" is a new physical volume of "223.57 GiB" --- NEW Physical volume ---
PV Name /dev/sdd
VG Name
PV Size 223.57 GiB
Allocatable NO
PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID x0N0k2-5c6j-HlHZ-vuAX-s82V-Yx8v-Pi3rWa
"/dev/sdc" is a new physical volume of "447.13 GiB" --- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 447.13 GiB
Allocatable NO
PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID dRa71o-rYk9-gJKC-bJdC-tJVV-yarW-LHTwPu
"/dev/sdb" is a new physical volume of "447.13 GiB" --- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 447.13 GiB
Allocatable NO
PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID vu98O9-4UDD-TTGK-PvsY-g4bN-FeL4-lcqLy7
As you can see, you do not yet have a virtual group. You use the vgcreate command to create one and add your PVs.
You need to specify the name of the group and the volumes you want to add to it like so:
1
2
# vgcreate base-layer /dev/sdb /dev/sdc /dev/sdd Volume group "base-layer" successfully created
Now you can go ahead and create a logical volume. We recommend to name it according to its purpose as in this example:
You can verify that everything worked well by issuing the following command:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# lvdisplay --- Logical volume ---
LV Path /dev/base-layer/vol_docker
LV Name vol_docker
VG Name base-layer
LV UUID d0ne0u-zBZQ-29f5-rkd9-XnZv-0vhE-rGmLvA
LV Write Access read/write
LV Creation host, time rrackow-test, 2024-09-18 06:42:09 +0000
LV Status available
# open 0 LV Size 1.09 TiB
Current LE 286163 Segments 3 Allocation inherit
Read ahead sectors auto
- currently set to 256 Block device 254:1
As you can see we now have a total size the sum of the individual disks.
Next we need to use mkfs to create an ext4 filesystem:
Now you can for example mount the volume for use with docker, by mounting it to /var/lib/docker like so:
1
2
# mkdir /var/lib/docker# mount /dev/base-layer/vol_docker /var/lib/docker
Ignition
In your Ignition config you will need two units: one to create the volume group and one to mount the volume.
Additionally, you will also need a script that executes all the required commands.
We will start with the script. It basically packages everything from the manual part into a script like so:
#!/bin/bash
set -euo pipefail
# Function to find all disksfind_volumes(){ lsblk -d -o NAME,TYPE | awk '$2 == "disk" {print "/dev/" $1}'}disks=$(find_volumes)# Create Physical Volumespvcreate "${disks}"# Create Volume Groupvgcreate vg-root "${disks}"# Create Logical Volume for datalvcreate -n vol_root -l 100%FREE vg-root
# Format the data volume with ext4 filesystemmkfs.ext4 /dev/vg-root/vol_root
As you can see, we use a function to list all available disks. If you do not want to use all disks, you need to adjust
the script accordingly. If you want to mount to a different place, e.g. /var/lib/docker instead of /, you need to adjust this
bit as well.
The next step is to create the unit file that executes the script:
1
2
3
4
5
6
7
8
9
10
11
[Unit]Description=LVM Setup
ConditionFirstBoot=yes
Before=local-fs-pre.target
[Service]Type=oneshot
Restart=on-failure
RemainAfterExit=yes
ExecStart=/etc/systemd/multi-user.target/lvm.sh #This is the name and path of the file above[Install]WantedBy=multi-user.target
However, we also need to mount the volume we created:
1
2
3
4
5
6
7
8
9
[Unit]Description=LVM Mount
[Mount]What=/dev/vg-root/vol_root
Where=/
Type=ext4
Options=defaults
[Install]WantedBy=local-fs.target
Now we need to put it all together into a butane yaml: