Difference between revisions of "Logical Volume Manager"

From Christoph's Personal Wiki
Jump to: navigation, search
Line 2: Line 2:
  
 
''Note: This article will cover how to use LVM under RedHat-style distros. Everything was tested using [[CentOS]] 6.5.''
 
''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==
 
==Examples==
Line 10: Line 28:
 
  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
Line 21: Line 41:
 
  #~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):
 
  mkfs.ext4 /dev/vgname/lvname
 
  mkfs.ext4 /dev/vgname/lvname
 +
 +
*Test mount it:
 
  mkdir /data
 
  mkdir /data
 +
mount /dev/vgname/lvname /data
 +
umount /data  # un-mount it before proceeding
  
*Make it persistent by adding the following line to your <code>/etc/fstab</code> file:
+
*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==
 +
*[http://www.tldp.org/HOWTO/LVM-HOWTO/ LVM HOWTO] &mdash; by The Linux Documentation Project (TLDP)
  
 
[[Category:Linux Command Line Tools]]
 
[[Category:Linux Command Line Tools]]

Revision as of 04:06, 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)