LVM

From Leo's Notes
Revision as of 19:16, 1 January 2019 by Leo (talk | contribs) (→Quick Usage)
This page was last edited on 1 January 2019, at 19:16.

The Logical Volume Manager or LVM is a volume manager used on Linux. It facilitates the management of storage volumes which are similar to disk partitions but with the added flexibility of:

  • Resizing volumes
  • Snapshotting volumes for consistent backups
  • Span multiple disks together into a single large volume similar to JBOD.

Quick Usage

First, create a Volume Group (VG) that specifies which storage (the Physical Volumes, PV) to use. A VG will contain 4MB Physical Extents (PE) that are 'blocks' of storage that can be assigned to a Logical Volume (LV). PEs are mapped to Logical Extents (LE) inside LVs and are mapped by LVM with the mapping data saved in the VG's layout header. Since PEs are assigned, LVs can be resized by adding or removing PEs. However, the filesystem utilizing the volume must be shrunk first or expanded after the change using something like fs2resize..

To quickly get a filesystem up and running using LVM:

root@nsb:/# vgcreate ug_vg /dev/sdb
  Volume group "ug_vg " successfully created
  
  
root@nsb:/# vgdisplay ug_vg 
  --- Volume group ---
  VG Name               ug_vg 
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               836.62 GiB
  PE Size               4.00 MiB
  Total PE              214175
  Alloc PE / Size       0 / 0
  Free  PE / Size       214175 / 836.62 GiB
  VG UUID               WeggQU-zQf3-j3mk-OAFd-iUCQ-EDTo-ezxhcb

root@nsb:/# lvcreate -n ug -l 90%FREE ug_vg
  Logical volume "ug" created.

## After creating a LV, the available free PEs decrease as expected
root@nsc:~# vgdisplay ug_vg
...
  VG Size               836.62 GiB
  PE Size               4.00 MiB
  Total PE              214175
  Alloc PE / Size       192757 / 752.96 GiB
  Free  PE / Size       21418 / 83.66 GiB

# LVM volumes are placed under /dev with the VG name and LV name.
root@nsc:~# mkfs.ext4 /dev/ug_vg/ug

## If a VG needs to be renamed, use the vgrename command:
root@nsb:/# vgrename uga ug_vg


Recovering Volumes

Use vgscan to rescan for volume groups. This will create a file of the same name as the volume in /etc/lvm/backup/.

To restore a volume, use the vgcfgrestore command. For example, if there is a volume VolGroup01:

With the volume groups restored, use pvscan to look for physical volumes.

Activate the volume using vgchange VolGroup01 -a y

With the volume activated, look for logical volumes using lvscan. Logical volumes should show up in /dev/VolGroup01/LogVol00 for example and can be mounted.

In summary:

# vgscan                                  # Generates /etc/lvm/backup/VolGroup01
# cd /etc/lvm/backup
# vgcfgrestore -f VolGroup01 VolGroup01   # Restore VG using /etc/lvm/backup/VolGroup01
# vgscan                                  # verifies volume groups
# pvscan                                  # Looks for physical volumes
# vgchange VolGroup01 -a y                # Activates the volume group
# lvscan                                  # Looks for logical volumes
# mount /dev/VG01/LV00 /mnt/foo           # LVM volumes should be in /dev/vg/lv and should be mountable

See also:


Mounting / Unmounting Volumes

If you have a LVM partition, you can see what physical volumes there are under the LVM by running:

# pvs
PV               VG                Fmt    Attr    PSize       PFree
/dev/hda2        VolGroup01        lvm2   a-      148.94G     32.00M

The volume group (VG) is VolumeGroup01. To view the logical volumes under this volume group, run:

# lvdisplay /dev/VolumeGroup01

Once you have located the logical volume you want to mount, say LogVol00, run:

# mount /dev/VolumeGroup01/LogVol00 /mnt/disk