LVM commands

Device names for LVM volumes follow the convention:

/dev/<volume group>/<logical volume>

Logical Volumes commands

  • Create a logical volum: lvcreate –L 10G –n <lv_name> <vg_name>
  • Create a logical volum on a specific disk: lvcreate –L 10G –n <lv_name> <vg_name> <pv_name>
  • Display attributes of a logical volume: lvdisplay /dev/<vg_name>/<lv_name>
  • Display the mapping of logical extents to physical volumes and physical extents: lvdisplay -m /dev/<vg_name>/<lv_name>
  • Remove a logical volume: lvremove /dev/<vg_name>/<lv_name>
  • Extend the size of a logical volume by 5 GB: lvextend -L 5G /dev/<vg_name>/<lv_name>
  • Extend the size of a logical volume with all the remaining free space in the VG: lvextend -l +100%FREE /dev/<vg_name>/<lv_name>
  • Extends the size of logical volume by the amount of free space on physical volume: lvextend /dev/<vg_name>/<lv_name> /dev/sdb or lvextend -l +100%PVS /dev/<vg_name>/<lv_name>
  • Rename a logical volume: lvrename /dev/<vg_name>/<lv_name> /dev/<vg_name>/<lv_new_name>
  • Reduce the size of a logical volume by 8 GB: lvreduce -L 8G /dev/<vg_name>/<lv_name>
  • Resize underlying filesystem together with the logical volume by 8 GB: lvreduce -r -L 8G /dev/<vg_name>/<lv_name>
  • Deactivate a logical volume: lvchange -a n /dev/<vg_name>/<lv_name>
  • Scan all disks for Logical Volumes: lvscan
  • Show which LV is using the PV: lvs -o +devices or lvs -a -o +devices (all, even those not accessible)
  • Show tags: lvs -o +tags

LV attributes

m   (m)irrored
M   (M)irrored without intial sync
o   (o)rgin
p   (p)vmove
s   (s)napshot
S   invalid (S)napshot
v   (v)irtual
i   mirror (i)mage
l   mirror (I)mage without sync
c   under (c)onstruction
–   Simple Volume

Physical Volumes commands

  • Initialize a disk or partition for use by LVM: pvcreate /dev/sdb
  • Display attributes of a physical volume: pvdisplay /dev/sdb
  • Display the mapping of physical extents to logical volumes and logical extents: pvdisplay -m
  • Remove a physical volume: pvremove /dev/sdb
  • Move physical extents to free physical extents elsewhere in the VG: pvmove /dev/sdb
  • Move all physical extents to a specific destination device: pvmove /dev/sdb /dev/sdc
  • Run the daemon in the background: pvmove -b /dev/sdb /dev/sdc
  • Report progress as a percentage at regular intervals: pvmove -i /dev/sdb
  • Move only the extents belonging to Logical Volume: pvmove -n <lv_name> /dev/sdc
  • Move a range of Physical Extents: pvmove /dev/sdb:1000-1999
  • Check physical volume metadata: pvck -d -v /dev/sdb
  • Report information about physical volumes: pvs
  • Show PVs and UUID: pvs -o +uuid
  • Full details for PV: pvs --segments -o +seg_size,lv_name,lv_size,seg_pe_ranges
  • Scan all disks for physical volumes: pvscan

Volume Groups commands

  • Create a volume group: vgcreate <vg_name> /dev/sdb
  • Create a volume group using multiple disks: vgcreate <vg_name> /dev/sd[b-e]
  • Display attributes of all volume groups or a specific volume group: vgdisplay or vgdisplay <vg_name>
  • Display VGs having one or more physical volumes missing: vgdisplay --partial --verbose
  • Add physical volumes to a volume group: vgextend <vg_name> /dev/sdc
  • Reduce a volume group: vgreduce <vg_name> /dev/sdc
  • Removes all missing physical volumes from the VG: vgreduce --removemissing <vg_name>
  • Removes all missing physical volumes and logical volumes from the VG: vgreduce --removemissing <vg_name> --force
  • Remove a volume group: vgremove <vg_name>
  • Deactivate all LVs: vgchange -a n <vg_name>
  • Activate all LVs: vgchange -a y <vg_name>
  • Activate all known volume groups in the system: vgchange -a y
  • Proceed with read-only metadata operations even if the locking module fails: vgchange -ay --ignorelockingfailure
  • Activate LVs even if portions are missing due to a missing or failed PV: vgchange -an --partial <vg_name>
  • Scan all disks for volume groups and rebuild caches: vgscan
  • Report information about volume groups: vgs
  • Show VGs and PVs: vgs -o +pv_name
  • Check tags for a VG: vgs -o vg_tags /dev/vg_r6vm02dev
  • Backup volume group descriptor area (/etc/lvm/backup directory): vgcfgbackup
  • Backup volume group descriptor area in /tmp: vgcfgbackup -f /tmp/<file_name> <vg_name>
  • Restore volume group descriptor area: vgcfgrestore -f /etc/lvm/backup/<file_name> <vg_name>

VG attributes

r,w (r)ead & (w)rite permissions
z   resi(z)eable
x   e(x)ported
p   (p)artial
c,n,a,i allocation policy (c)ontiguous, c(l)ing, (n)ormal, (a)nywhere, (i)nherited
c   (c)luster

Other commands

  • Display LVM configuration: lvmconfig
  • Validate current configuration used: lvmconfig --validate or lvm dumpconfig --validate
  • Create lvm2 information dumps for diagnostic purposes: lvmdump -a -m
  • Scan for all devices visible to LVM2: lvmdiskscan
  • Outputs some brief information: dmsetup info -c
  • Show low level of device names: dmsetup ls
  • Show low level of device names between devices as a tree: dmsetup ls --tree
  • Remove a LV from device mapper table: dmsetup remove <vg_name>-<lv_name>
  • Destroy the partition table on the disk: dd if=/dev/zero of=/dev/sdb bs=512 count=1

  • Generates a suggested lvm filter based on the LVM devices found

pvs -a --config 'devices { filter = [ "a|.*|" ] }' --noheadings \
    -opv_name,fmt,vg_name | awk 'BEGIN { f = ""; } \
    NF == 3 { n = "\42a|"$1"|\42, "; f = f n; } END \
        { print "Suggested filter line for /etc/lvm/lvm.conf:\n \
        filter = [ "f"\"r|.*|\" ]" }'

Recipes

Extend a Logical Volume by adding a new disk

pvcreate /dev/sdc
vgextend <vg_name> /dev/sdc
lvextend -L +10GB /dev/<vg_name>/<lv_name>
resize2fs /dev/<vg_name>/<lv_name>
df -h /<filesystem>

Extend a Logical Volume by resizing the disk

pvresize /dev/sdc
lvextend -L +10GB /dev/<vg_name>/<lv_name>
resize2fs /dev/<vg_name>/<lv_name>
df -h /<filesystem>

Shrink a Logical Volume

umount /<filesystem>
e2fsck -f /dev/<vg_name>/<lv_name>
lvreduce -r -L 10G /dev/<vg_name>/<lv_name>
e2fsck -f /dev/<vg_name>/<lv_name>
mount /<filesystem>
lvdisplay /dev/<vg_name>/<lv_name>
df -h /<filesystem>

Extend swap partition

swapoff -v /dev/vgroot/swap
lvresize -L +8GB /dev/vgroot/swap
mkswap /dev/vgroot/swap
swapon -va
swapon -s

Recover LVM from corrupted physical volume

vgdisplay --partial --verbose
pvremove -ff /dev/sdc
pvcreate --uuid=xxxxxx /dev/sdc --restorefile=/etc/lvm/archive/<vg_name>.vg
vgcfgrestore -f /etc/lvm/archive/<vg_name>.vg <vg_name>
vgchange -ay <vg_name>
e2fsck /dev/<vg_name>/<lv_name>

Note: This procedure will not restore any data lost from a physical volume that has failed and been replaced. If a physical volume has been partially overwritten (for example, the label or metadata regions have been damaged or destroyed) then user data may still exist in the data area of the volume and this may be recovered using standard tools after restoring access to the volume group using these steps.

Files

  • Metadata map: /etc/lvm/cache/.cache

Links