LVM

Setup

Create logical volume

sudo lvm vgdisplay
sudo lvm lvcreate -L 100G -n /dev/vg0/data vg0
sudo mkfs.ext3 -j /dev/vg0/data
sudo mkdir /data
sudo mount /dev/vg0/data /data

Create phyical volume

  • Volume group name: cvg01.
  • Phyisical disk: /dev/sdb1.
sudo fdisk /dev/sdb
sudo lvm pvcreate /dev/sdb1
sudo lvm vgcreate -s 32M cvg01 /dev/sdb1

Maintenance

Extending a physical volume

With available space on the same disk, remove the last parition on that disk and re-create the parition with the same start point to extend it to the maximum available space. fdisk works just fine this. Even is it's the current / disk, this works.

Extending a volume group

sudo vgextend /dev/vg0 /dev/sdb1

Extending a logical volume

sudo lvm vgdisplay # Check available disk space
sudo lvm lvextend -L30G /dev/vg0/var # Extending lv var
sudo lvxtend -l 100%FREE /dev/vg0/var # Extending lv var to max.
sudo ext2online -v /dev/vg0/var # Extending on RedHat 4.x
sudo resize2fs -p /dev/vg0/var  # Extending on CentOS 5.x
sudo xfs_growfs /dev/vg0/var # CentOS 7

resize2fs is part of the package e2fsprogs.

Shrink a logical volume

  • Can not be done online.
sudo init1
umount /usr
e2fsck -f /dev/vg0/usr
resize2fs /dev/vg0/usr 2G
mount /usr
init 3

Free LVM space in MB

sudo pvs --units m

Snapshots

General steps:

  1. Stop all access to the database.
  2. Create a snapshot image within LVM on a free LVM space.
  3. Enable all access to the database.
  4. Mount the snapshot and access the files.

Preperations

$ mysqladmin -u root -p variables | grep datadir
| datadir         | /var/lib/mysql/

$ df /var/lib/mysql
Filesystem    1K-blocks   Used Available Use% Mounted on
/dev/mapper/vg_mysqllvmtest-lv_root
                   8063408961448   6692360  13% /

$ lvscan
ACTIVE   Original '/dev/vg_mysqllvmtest/lv_root' [7,81 GiB] inherit
ACTIVE            '/dev/vg_mysqllvmtest/lv_swap' [3,94 GiB] inherit

$ vgdisplay vg_mysqllvmtest | grep Free
Free  PE / Size       1986 / 7,76 GiB

Step 1

Lock database access:

mysql> FLUSH TABLES WITH READ LOCK;

Step 2

In additional Mysql Session create the Snapshot

# Use the rest of available space for the snapshot
lvcreate -l100%FREE -s -n mysql-backup /dev/vg_mysqllvmtest/lv_root

Step 3

Remove the lock on the database:

mysql> UNLOCK TABLES;

Step 4

$ lvscan
ACTIVE   Original '/dev/vg_mysqllvmtest/lv_root' [7,81 GiB] inherit
ACTIVE            '/dev/vg_mysqllvmtest/lv_swap' [3,94 GiB] inherit
ACTIVE   Snapshot '/dev/vg_mysqllvmtest/mysql-backup' [7,76 GiB] inherit

$ sudo mkdir -p /mnt/snapshot
$ sudo mount /dev/vg_mysllvmtest/lv_root /mnt/snapshot