Skip to content

Instantly share code, notes, and snippets.

@debuglevel
Last active July 30, 2024 08:22
Show Gist options
  • Save debuglevel/58afae625a0c0fa86e87a6e6c99ba179 to your computer and use it in GitHub Desktop.
Save debuglevel/58afae625a0c0fa86e87a6e6c99ba179 to your computer and use it in GitHub Desktop.
Cheatsheet about some basic LVM stuff

LVM cheatsheet

See https://www.thomas-krenn.com/de/wiki/LVM_Grundlagen for the core concepts regarding physical volumes (pv), volume groups (vg) and logival volumes (lv).

Partition table thing that do not have to do with LVM

Show drives, their sizes and their partitions in the boot log: dmesg | grep sd (or nvme on bare-metal; unlikely in virtualized environments)

List a block device tree. Shows in which partition a volume group was placed: lsblk

Grow the third partition to the maximum (if there is empty space afterwards). You may need to reboot afterwards: growpart --verbose /dev/sda 3

Display stuff

  • pvs, pvdisplay, pvscan
  • vgs, vgdisplay, vgscan
  • lvs, lvdisplay, lvscan

Life hacks

  • Use the long form of arguments lvcreate --size instead of lvcreate -L. They describe what they do and yout do not have to look them up in the man page.

Enlarge physical volume (when the partition was enlarged)

When you enlarged a partition (e.g. using the growpart command above), you still have to use pvresize /dev/sda3 to enlarge the physical volume.

Enlarge logical volumes and the file system inside

  1. Show current size of the logical volumes: lvs/lvscan --verbose
  2. Resize the "partition" by 120GB: lvextend --size +120G /dev/vg0/var or
  3. Resize the "partition" to 700GB: lvextend --size 700G /dev/vg0/var
  4. Resize the file system: resize2fs /dev/vg0/var (lvextend --resizefs might do the same for you)
  5. Show the current size of logical volumes: lvs/lvscan --verbose
  6. Show the current size of file systems: df -h

LVM snapshots

Ressources:

Information:

  • Snapshots are copy-on-write (COW). Although the space is allocated beforehand, actual data is only written if it's changed on the underlying logical volume. This means you can use little space for a snapshot of a logical volume which does not change very much - but if it does change too much, your snapshot will break if it needed more space than available. (Also, I guess you cannot just use the "Used" size from df -h, because on the blockdevice, changes yould be made all over the whole space).
  • Snapshots are NOT read-only! In doubt, use mount -o ro to prevent you from destroying your snapshot.
  1. Create a snapshot for the logical volume /dev/vg0/root with 10GB size: lvcreate --size 10G --snapshot --name root_snap /dev/vg0/root.
  2. Display it: lvs, lvdisplay, lvscan
  3. Mount snapshot: mount /dev/vg0/root_snap /mnt/root_snap/ (CAUTION: They are NOT read-only!)
  4. Remove a snapshot: lvremove /dev/vg0/root_snap

Restoring a snapshot

The snapshot of the root filesystem can probably (did not test it yet) be restored on the next reboot:

Sync a logical volume

  • See https://github.com/mpalmer/lvmsync to efficiently sync (the underlying logical volume, not the snapshot!) across a network. (The snapshot is used to determine which blocks changed in the underlying logical volume and have to be considered.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment