Logical Volume Manager

From Christoph's Personal Wiki
Jump to: navigation, search

This article will explain how to use the Logical Volume Manager (LVM) in Linux.

Note: This article will cover how to use LVM under RedHat-style distros. Everything was tested using CentOS 6.5. Some of the features shown are only available for LVM2.

Logical volume stack

I like to think of logical volumes as a stack, with the physical part on the bottom and you are stacking each element on top:

filesystem (FS)
Logical volume (LV)
Volume group (VG)
Physical volume (PV)

Another way to visualize this is like so (adapted {and inverted} from Erik Bågfors' diagram):

 ext4    ext4  (filesystems)
  |        |
rootlv  homelv (LVs)
   \      /
    \    /
    diskvg     (VG)
    /   \
   /     \
 xvdb1  xvdb2  (PVs; on partitions or whole disks)

Examples

Creating a logical volume

  • Prepare a physical volume:
$ fdisk -cu /dev/xvde
$ pvcreate /dev/xvde1
$ pvdisplay /dev/xvde1
$ pvs
  • Create a volume group:
$ vgcreate vgname /dev/xvde1
$ vgdisplay vgname
$ vgs
  • Create and use a new logical volume:
$ lvcreate -n lvname -L 1G vgname
$ lvdisplay /dev/vgname/lvname
$ lvs
$ ls -l /dev/vgname/lvname
$ #~OR~
$ ls -l /dev/mapper/vgname-lvname
  • Make a filesystem (ext4 in this example) inside the above logical volume:
$ mkfs.ext4 /dev/vgname/lvname
  • Test mount it:
$ mkdir /data
$ mount /dev/vgname/lvname /data
$ umount /data  # un-mount it before proceeding
  • Make it persistent by adding the following line to your /etc/fstab file and test mount it (again):
/dev/mapper/vgname-lvname  /data  ext4  defaults  1 2
$ mount -a

Miscellaneous

If you see the following error message:

vgscan -- no volume groups found

Try the following:

  • Check integrity of your VGs (note: obviously, you can not have an open LVs inside your VGs):
$ vgchange -an  # deactivate any active VGs
$ vgscan
$ vgchange -ay  # re-activate VGs

Or, you could try using (the very old) "uuid_fixer" (Careful! Can be dangerous!)

  • Removing a logical volume (note: a logical volume must be closed before it can be removed):
$ umount /dev/vgname/lvname
$ lvremove /dev/vgname/lvname
lvremove -- do you really want to remove "/dev/vgname/lvname"? [y/n]: y
lvremove -- doing automatic backup of volume group "vgname"
lvremove -- logical volume "/dev/vgname/lvname" successfully removed

External links

  • LVM HOWTO — by The Linux Documentation Project (TLDP)