LVM examples

Posted on Mon 22 December 2008 by Pavlo Khmel

First LVM commands to get overview on server:

pvs # report information about physical volumes
vgs # report information about volume groups
lvs # report information about logical volumes

Add disk to LVM

# Remove disk partition table
dd if=/dev/zero of=/dev/hda bs=512 count=1
# Create physical volume
/usr/sbin/pvcreate /dev/hda
# check
# list volume groups in details
# add physical volume to existent Volume Group
/usr/sbin/vgextend VolGroup00 /dev/hda
# check changes
# Create logical volume
/usr/sbin/lvcreate -L 1G -n phn1 VolGroup00
# check
# create filesystem
/sbin/mkfs.ext3 /dev/VolGroup00/phn1
# mount
mount /dev/VolGroup00/phn1 /mnt
# reduce filesystem syze
umount /mnt
/sbin/resize2fs -p /dev/VolGroup00/phn1 400M
# check
/sbin/e2fsck /dev/VolGroup00/phn1
# reduce logical volume
/usr/sbin/lvresize -L 900M /dev/VolGroup00/phn1
# create one more logical volume
/usr/sbin/lvcreate -L 1G -n phn2 VolGroup00
# check
# remove logical volumes
/usr/sbin/lvremove /dev/VolGroup00/phn1
/usr/sbin/lvremove /dev/VolGroup00/phn2
# remove disk from volme group
/usr/sbin/vgreduce VolGroup00 /dev/hda
/usr/sbin/pvremove /dev/hda
# check

Extend root partition without reboot

/dev/rootvg/rootlv = 4 GB
/dev/rootvg = 10 GB
add 4 GB to /dev/rootvg/rootlv

lvresize /dev/rootvg/rootlv -L+4G
resize2fs -p /dev/mapper/rootvg-rootlv 8G
lvresize /dev/VolGroup00/LogVol00 -L+10G
# For RedHat 4:
ext2online /zxc
/zxc mount point

LVM access from another system.
Boot Install DVD

boot: linux rescue
# Activate logical volumes
lvm vgscan
lvm vgchange -ay
lvm lvs

Rename logical volumes

lvrename vg_name old_lv new_lv

Create logical volume, use all available space

lvcreate -l 100%FREE -n phn_lv phn_vg

Extend logical volume, use all available space

lvextend -l +100%FREE /dev/phn_vg/phn_lv

Extend filesystem on RedHat 4

ext2online /dev/mapper/phn_vg-phn_lv

Another example

fdisk /dev/sdb
pvcreate /dev/sdb1
vgextend rootvg /dev/sdb1
lvextend -L+15G /dev/mapper/rootvg-opt_lv
lvextend -L+5G /dev/mapper/rootvg-rafw_lv
lvextend -l +100%FREE /dev/mapper/rootvg-media_lv
resize2fs -p /dev/mapper/rootvg-opt_lv
resize2fs -p /dev/mapper/rootvg-rafw_lv
resize2fs -p /dev/mapper/rootvg-media_lv

Extend root using another partition disk space:

umount /work
e2fsck -f /dev/mapper/vg0-work
resize2fs -p /dev/mapper/vg0-work 20G
lvreduce -L 26G /dev/mapper/vg0-work
resize2fs -p /dev/mapper/vg0-work
e2fsck -f /dev/mapper/vg0-work
mount /work
lvextend -l +100%FREE /dev/mapper/vg0-root
resize2fs -p /dev/mapper/vg0-root

Reduce volume group

ext4 does not support online size reduce.

Check that files are not in use:

lsof | grep /tmp

Check what minimal size is possible

df -h /tmp

Shrink to minimal size

umount /tmp
e2fsck -f /dev/mapper/vg0-tmp
resize2fs -M /dev/mapper/vg0-tmp

Set new size

lvreduce --size 10G /dev/mapper/vg0-tmp
resize2fs -p /dev/mapper/vg0-tmp
mount -a

Add available size to another volume

lvextend -l +100%FREE /dev/mapper/vg0-var 
resize2fs -p /dev/mapper/vg0-var


xfs_growfs /dev/mapper/centos-root