Getting started with LVM
I recently had to extend a normal disk partition. It wasn't fun. That's why I recommend using LVM, it's a lot easier to manage, extend, shrink and remove partitions on a single or multiple disks.
Common Terms
Physical Volumes (PVs)
are your physical disks attached to a server. These can be different sizes. That's why LVM is so flexible.
Volume Groups (VGs)
are groups of PVs. For example you could have vg00
which includes PVs a
, b
and c
.
Logical Volumes (LVs)
are smaller “partitions” of VGs. You can mount these at different mount points and are easily manageable.
Example
Your server configuration:
- Disk /dev/sda, 200GB
- other partitions like boot
- /dev/sda2, 199GB, PV
- Disk /dev/sdb, 4TB
- /dev/sdb1, 4TB, PV
- Disk /dev/sdc, 150G
- /dev/sdc1, 150G, PV
You can either split those into multiple VGs or combine them into one. I recommend combining them, because you can only assign a PV to single VG.
Currently your total allocatable storage for vg00
would be 4445GB. Now you can split that into one or more LVs and mount them to your system. I usually have different /
, /var
, /home
and swap
partitions.
Requirements
- A server
- An unused disk partition on the server
- LVM2 installed
apt install lvm2
(on debian)
Creating a PV
sudo pvcreate <disk>
Example <disk>
values:
- /dev/sdb
- /dev/sdb1
List PVs
sudo pvs
Creating a VG
sudo vgcreate <VG name> <PVs>
Example:
sudo vgcreate vg00 /dev/sdb
sudo vgcreate vg00 /dev/sda /dev/sdb /dev/sdc1
List VGs
sudo vgs
Creating a LV
sudo lvcreate -n <name> -L <size> <VG name>
Example:
sudo lvcreate -n root -L 30G vg00
sudo lvcreate -n home -L 50G vg00
sudo lvcreate -n var -L 20G vg00
Formatting
You can choose any filesystem you want. I usually select ext4
.
sudo mkfs.ext4 /dev/<VG name>/<LV name>
Example:
sudo mkfs.ext4 /dev/vg00/root
Final Steps
Now you can mount them and store files. Remember to add them to /etc/fstab
in order to automatically have them mounted.
sudo mount /dev/<VG name>/<LV name> /mnt
List LVs
sudo lvs
Extend a LV
sudo lvextend -L +2G /dev/<VG name>/<LV name> -r
The -r
flag automatically resizes the filesystem. If it's not provided, you need to manually run the resize2fs <volume>
command.
Example:
sudo lvextend -L +2G /dev/vg00/root -r
Verify Changes
You can run df -h
and it should show the new amount. If it still shows the old one, you can try running resize2fs /dev/<VG name>/<LV name>
command.