LVM, short for Logical Volume Manager, is awesome – and it doesn’t seem to get the attention it deserves. In this one-shot tutorial, I’ll show you why you should care about LVM, how to get started, some of the commands you can use to manage it, and more. You’ll even learn how to use it to take snapshots of your system!
Commands Used
Resizing a logical volume
First, get a tiny bit of space back for tmp files (this command clears apt caches and etc):
sudo apt clean
Extend the logical volume
lvextend --resizefs -l +100%FREE /dev/mapper/vg_ubuntu-lv_root
Adding a disk to LVM
After adding a physical or virtual disk to the server, run the following commands.
Convert the new disk to an LVM physical volume
Note: Make sure you change “sdb” to the identifier of your drive):
pvcreate /dev/sdb
Add the new physical volume to the volume group
vgextend vg_ubuntu /dev/sdb
Check the status
vgdisplay
Extend the physical volume by 10GB (or however many “GB” you want)
lvextend -L +10G /dev/mapper/vg_ubuntu-lv_root
Grow the logical volume with ALL of the available space, without specifying a particular size
lvextend --resizefs -l +100%FREE /dev/mapper/vg_ubuntu-lv_root
Grow the filesystem to match the newly available space
resize2fs /dev/mapper/vg_ubuntu-lv_root
Check current available space
df -h
Creating a brand-new LVM setup
Add a new virtual or physical disk to the server.
Note: For all of the below commands, change “sdc” to match your disk’s identifier
Convert the new disk to be an LVM physical volume
pvcreate /dev/sdc
Create the volume group
vgcreate vg_extra /dev/sdc
Check the volume group
vgdisplay
Create a logical volume (named lv_logs in this example)
lvcreate vg_extra -L 5G -n lv_logs
Format the logical volume
mkfs.ext4 /dev/mapper/vg_extra-lv_logs
Create a directory to mount the new logical volume
mkdir /mnt/extra/logs
Mount the logical volume
mount /dev/mapper/vg_extra-lv_logs /mnt/extra/logs
Find the “block id” of the new logical volume
blkid /dev/mapper/vg_extra-lv_logs
Back up your fstab file to be safe
cp /etc/fstab /etc/fstab.bak
Edit the fstab file
nano /etc/fstab
Add a line to the fstab to mount the volume, similar to this
UUID=<BLOCK ID FOR LOGICAL VOLUME> /mnt/extra/logs ext4 defaults 0 2
Test the new mount, first making sure it’s not mounted
umount /mnt/extra/logs
Then test your fstab file (BEFORE rebooting)
mount -a
If no errors, then you’re all set.
Snapshots
Create a new snapshot
lvcreate /dev/mapper/<SOURCE VOLUMEGROUP NAME> -L 1G -s -n snapshot_name
View used space of snapshots
lvs
Mount a snapshot
mount /dev/mapper/vg_extra-web_snapshot_20200421 /mnt/extra/snapshot
Restore a snapshot
Umount the original volume:
umount /path/to/mounted/logical-volume
Restore the snapshot:
lvconvert --merge /dev/mapper/snapshot_name
Deactivate/reactivate to fresh it:
lvchange -an /dev/mapper/vg_extra-lv_web lvchange -ay /dev/mapper/vg_extra-lv_web
In this video at around 46:00 it shows that there’s a snapshot logical volume created with 1Gb of space. However, it’s taking the snapshot of another logical volume of 5Gb. In this example there was only one file so I guess this may seem like a silly question… but do LVM snapshots ignore unused/free space in the target logical partition?
LVM snapshots require free space in the Volume Group, not on the Logical Volume. Run
vgs
and check your vg’s size. It should say that free space is not 0, in order to take snapshots. If you run out of space on the vg, the snapshot becomes useless, because it can’t move the old data to another location. You should keep at least 15% free space in the volume group for lvm snapshots and 10% free to allocate to logical volumes in a pinch if you run production, although you can just add a new disk, or for a VM increase the size of the current disk, create a new partition and expand the logical volume that way.Thanks, I still haven’t tested this but will it give a try. I was just confused about that difference in size reported.
Volume management is not too hard. The partition is how you partition the disk. Then you create physical volumes on the partitions.
Then you make the volume groups. To each volume group, you can have as many as you want, you assign an amount of space from the physical volume. But to a logical volume, it is like a virtual partition, you consider the volume group like a custom size storage device, like a custom virtual hdd, and you partition it in smaller chunks for different mounts, called logical volumes.
Using “virtual hdds” is easier to manage, especially when you want to move things around. It also eliminated some of the old problems with MBR (like limited number of partitions).
The theory seems pretty straight forward, although I’m curious about practical use cases. For example, now that we have modern file systems like btrfs, wouldn’t it be better to use that for snapshots?