Saturday, September 27, 2008

Introduction to LVM2

This is a very basic introduction to LVM2 - Logical Volume Manager version 2. "In computer storage, logical volume management or LVM is a method of allocating space on mass storage devices that is more flexible than conventional partitioning schemes. In particular, a volume manager can concatenate, stripe together or otherwise combine partitions into larger virtual ones that can be resized or moved, possibly while it is being used." - Source: Wikipedia

Be sure to create backups before proceeding with the conversion to LVM.

There are three elements in LVM. They are volume group, physical volume and logical volume. If you want to use LVM, you have to enable support for LVM in the kernel. Most distributions have their kernels compiled with LVM support.

A physical volume can be any device/partition that we are able to find in /dev. For example, /dev/sda2, /dev/hda1, et cetera. A physical volume is indeed physical in this sense. We have to create physical volumes from these partitions. One partition corresponds to one physical volume. In the following tutorial, we will convert partitions named sda5 and sdb8 to LVM2 volumes.

Creation of physical volumes:

[alanhaggai@archer ~]$ sudo pvcreate /dev/sda5 /dev/sdb8
  No physical volume label read from /dev/sda5
  Physical volume "/dev/sda5" successfully created
  No physical volume label read from /dev/sdb8
  Physical volume "/dev/sdb8" successfully created

Physical volumes have to be added to a volume group. A volume group is nothing but a collection of physical volumes and logical volumes. We will create one volume group named main. Physical volumes add storage space to the volume group.

[alanhaggai@archer ~]$ sudo vgcreate main /dev/sda5 /dev/sdb8 Volume group "main" successfully created

Logical volumes are to be created within a volume group:

[alanhaggai@archer ~]$ sudo lvcreate -L40G -nhome main
  Logical volume "home" created

The above command creates a logical volume of size 40 GB within the volume group main. A file system is to be created for the logical volume:

[alanhaggai@archer ~]$ sudo mke2fs -j /dev/main/home

An ext3 filesystem is created. The device nodes are arranged as /dev/{volume_group}/{logical_volume} which are linked to /dev/mapper/{volume_group}-{logical_volume}.

Resizing an lvm volume involves resizing the logical volume as well as the filesystem within it. Warning: Be careful while shrinking. Do not shrink to a size less than the size of the data in the volume. If shrunk so, it will result in loss of data. An expansion involves passing a + value to the -L flag, and a shrink involves passing a - value.

[alanhaggai@archer ~]$ sudo lvresize /dev/main/home -L+10G
  Extending logical volume home to 50.00 GB
  Logical volume home successfully resized

[alanhaggai@archer ~]$ sudo resize2fs /dev/main/home
resize2fs 1.40.8 (13-Mar-2008)
Filesystem at /dev/main/home is mounted on /home; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 4
Performing an on-line resize of /dev/main/home to 13108224 (4k) blocks.
The filesystem on /dev/main/home is now 13108224 blocks long.

Volume groups have to be activated:

[alanhaggai@archer ~]$ sudo vgchange -ay 1 logical volume(s) in volume group "main" now active
For an in-depth tutorial, refer: http://tldp.org/HOWTO/LVM-HOWTO/

0 comments: