Difference between revisions of "Logical Volume Manager"

From Christoph's Personal Wiki
Jump to: navigation, search
(Creating a logical volume)
Line 24: Line 24:
 
===Creating a logical volume===
 
===Creating a logical volume===
 
*Prepare a physical volume:
 
*Prepare a physical volume:
  fdisk -cu /dev/xvde
+
  $ fdisk -cu /dev/xvde
  pvcreate /dev/xvde1
+
  $ pvcreate /dev/xvde1
  pvdisplay /dev/xvde1
+
  $ pvdisplay /dev/xvde1
  pvs
+
  $ pvs
  
 
*Create a volume group:
 
*Create a volume group:
  vgcreate vgname /dev/xvde1
+
  $ vgcreate vgname /dev/xvde1
  vgdisplay vgname
+
  $ vgdisplay vgname
  vgs
+
  $ vgs
  
 
*Create and use a new logical volume:
 
*Create and use a new logical volume:
  lvcreate -n lvname -L 1G vgname
+
  $ lvcreate -n lvname -L 1G vgname
  lvdisplay /dev/vgname/lvname
+
  $ lvdisplay /dev/vgname/lvname
  lvs
+
  $ lvs
  ls -l /dev/vgname/lvname
+
  $ ls -l /dev/vgname/lvname
  #~OR~
+
  $ #~OR~
  ls -l /dev/mapper/vgname-lvname
+
  $ ls -l /dev/mapper/vgname-lvname
  
 
*Make a filesystem inside the above logical volume (<code>ext4</code> in this example):
 
*Make a filesystem inside the above logical volume (<code>ext4</code> in this example):
  mkfs.ext4 /dev/vgname/lvname
+
  $ mkfs.ext4 /dev/vgname/lvname
  
 
*Test mount it:
 
*Test mount it:
  mkdir /data
+
  $ mkdir /data
  mount /dev/vgname/lvname /data
+
  $ mount /dev/vgname/lvname /data
  umount /data  # un-mount it before proceeding
+
  $ umount /data  # un-mount it before proceeding
  
 
*Make it persistent by adding the following line to your <code>/etc/fstab</code> file and test mount it (again):
 
*Make it persistent by adding the following line to your <code>/etc/fstab</code> file and test mount it (again):
 
  /dev/mapper/vgname-lvname  /data  ext4  defaults  1 2
 
  /dev/mapper/vgname-lvname  /data  ext4  defaults  1 2
  mount -a
+
  $ mount -a
  
 
==External links==
 
==External links==

Revision as of 04:08, 9 March 2014

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.

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 inside the above logical volume (ext4 in this example):
$ 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

External links

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