Saturday, May 18, 2013

Logical Volume Manager on Linux

I can say that LVM started to be popular in my company when RHEL4 was introduced. We still have some RHEL3 and 4 built using pure partitions but it's a rare thing now.
LVM is cool and easy to use, it lacks a few features from JFS or VXFS but lets remember - it's free.
Best features are:
  • flexibility of structures you may create
  • simple commands that can show or modify things
  • dynamic volume resizing
  • snapshots creation

How does LVM works?

There are three layers of LVM:
  1. Physical volumes - local or SAN disks that you need to initialize using:
    pvcreate /dev/physical/volume/path
  2. Volume groups - a set created from one or more physical volumes
  3. Logical volumes - slices created in one volume group on which filesystems may reside
You may check the list of volume groups using:
vgs
Similarly you may verify logical volumes list:
lvs
and physical volumes list:
pvs

How to create a volume group?

First you need to initialize physical devices on top of which we will build our volume group:
pvcreate /dev/sdb
Then you can create a volume group using this device:
vgcreate newvg /dev/sdb

How to create a logical volume?

Once volume group is ready you may verify its size (it should be equal to the physical volume size or sum of them if you added multiple devices):
# vgs
  VG         #PV #LV #SN Attr   VSize VFree
  newvg          1      0   0 wz--n- 7.88G 7.88G

Now you can create logical volume using:
lvcreate -L 9G -n lv_home newvg
As an example we've created 9GB big volume named lv_home in newvg volume group.

Finally you may create a filesystem on this logical volume:
mkfs.ext3 /dev/mapper/newvg_lv_home 

How to extend logical volume?

If only you have free space in volume group you may extend logical volume by running:
lvresize -L +1G newvg/lv_home
You may specify to add some space or state exactly how big logical volume should be.
Once done remember to resize filesystem residing on that logical volume - for ext3/4 it may be done online:
resize2fs /dev/mapper/newvg_lv_home
From what I remember online resizing only works on RHEL5/CentOS5 or higher. On older distros you need to perform such changes on unmounted filesystems only (which often leads to booting from rescue CD if you need to extend root's filesystem).

How to shrink/reduce logical volume?

This operation is a little bit complicated.
Growing is easy but reducing might corrupt your underlying filesystem.
Other volume managers (Veritas Storage Foundation or LVM on AIX) can shrink volumes/filesystems online. However you should always take a backup before performing shrinking operation. Rarely it fails and you may be in big trouble later on.
For LVM on Linux you need to reduce the size of underlying filesystem first.
Lets assume you have 10GB filesystem on 10GB logical volume and you want to reduce it to 5GB.
All operations needs to be done on unmounted filesystem and checked filesystem:
umount /dev/mapper/newvg_lv_home
e2fsck -f /dev/mapper/newvg_lv_home
Now reduce the filesystem to something a little smaller than 5GB:
resize2fs /dev/mapper/newvg_lv_home 4G
Why not to 5GB? Just to be sure you didn't crop the filesystem (better safe than sorry).
Then you may reduce the size of logical volume to 5GB
lvreduce -L 5GB /dev/mapper/newvg_lv_home
Finally you may run resize2fs again to fit the filesystem size to the underlying logical volume:
resize2fs /dev/mapper/newvg_lv_home

No comments:

Post a Comment