LVM Notes#

Commands to manipulate LVM resources from physical volumes (PV) and volume groups (VG) to logical volume (LV)

|  pvcreate - pvremove  Λ
|  vgcreate - vgremove  |
|  vgextend - vgreduce  |
V  lvcreate - lvremove  |

Clone a Logical Volume#

#!/bin/bash
# Usage: ./clone_lv SOURCE TARGET_VG TARGET_LV_NAME
set -euo pipefail

src=$1
dest_vg=$2
dest_name=$3

target=/dev/${dest_vg}/${dest_name}
# make sure target does not exist, because dd is destructive :>
[[ ! -f $target ]]

bytes=$(lvs --noheadings --units b -o seg_size $src | awk '{ print $1 }')

lvcreate --name $dest_name --size $bytes $dest_vg
time dd if=$src of=$target bs=4M

Add physical volume#

create partition, then set to 8e, then print and write. Remember that empty newlines use the default.

fdisk /dev/sdb
n
p
1


p

t
8e
p
w

partprobe
fdisk -l /dev/sdb

# error: "Can't open /dev/sdb1 exclusively.  Mounted filesystem?"
# fuk it. too lazy
reboot

Create the physical volume

pvcreate /dev/sdb1

Make a new logical volume from half of the physical volume /dev/sdb1. Remember that extending is easy, but reducing is hard.

vgcreate ssd /dev/sdb1
lvcreate --name ssd --extents 50%VG ssd
lvs

Mapper device is /dev/mapper/${vg}-${lv}, e.g.:

fdisk -l /dev/mapper/ssd-ssd

Create FS, mount this thing, set permissions

mkfs.ext4 -L ssd /dev/mapper/ssd-ssd

mkdir -p /media/ssd

mount /dev/mapper/ssd-ssd /media/ssd/
df /media/ssd/
chown felix:felix /media/ssd
umount /media/ssd/

fstab

cat <<'EOF' >> /etc/fstab
/dev/mapper/ssd-ssd /media/ssd         ext4    defaults 0 1
EOF
mount /media/ssd
df /media/ssd

Move Volume to Another Server#

https://serverfault.com/questions/358228/moving-a-logical-volume-directly-from-one-server-to-another-over-the-network

LVM Snapshots for Backups#

You can use them to get consistent backups of data in use. As TLDP states:

A snapshot volume is a special type of volume that presents all the data that was in the volume at the time the snapshot was created.

This means that a snapshot “must be large enough to hold all the changes that are likely to happen to the original volume during the lifetime of the snapshot”.

It also implies that snapshots should be as short lived as possible. So given the following steps we want to minimize the duration of the backup step:

  • create snapshot

  • backup

  • remove snapshot

First, let’s set up our test case, that is: a logical volume prod that we will write to during the backup process. I am using my ssd volume group:

lvcreate --size 3G --name prod ssd
mkfs.ext4 /dev/ssd/prod
mkdir /mnt/prod_live
mount /dev/ssd/prod /mnt/prod_live
while true; do date > /mnt/prod_live/t; sleep 5; done

We expect writes in the order of a few bytes, so 10MiB should be plenty of space

lvcreate --extents 10%ORIGIN --snapshot --name bak_prod /dev/ssd/prod
mkdir /mnt/bak_prod
mount /dev/ssd/bak_prod /mnt/bak_prod

Let’s have a look. Note the columns “Origin” and “Data%” of lvs. Also, /mnt/bak_prod/t does not change while /mnt/prod_live/t does.

lvs
diff /mnt/prod_live/t /mnt/bak_prod/t

Create the backup the way you like, e.g.

tar -czf /tmp/prod_backup_$(date +%F).gz -C /mnt/bak_prod/ .
tar -tf /tmp/prod_backup_*.gz

Remove the snapshot:

umount /mnt/bak_prod/
lvremove /dev/ssd/bak_prod