Monday, July 26, 2010

LVM


LVM is an implementation of a logical volume manager for the Linux kernel. It was originally written in 1998 by Heinz Mauelshagen, which was based on Veritas Volume Manager used in HP-UX systems.

LVM includes many of the features expected of a volume manager, including:

Resizing logical groups 
Resizing logical volumes 
Read-only snapshots (LVM2 provides read and write) 
Logical Volume RAID0 

LVM RAID1 or RAID5 is not implemented, so it is recommended to use specific software RAID for these operations, taking over the LV RAID

Advantages of using LVM in a small system 
  • One of the decisions faced by a user installing GNU / Linux is how to partition the hard disk. The need to estimate how much space is required for the system, for temporary or personal data can become problematic and that many users choose to create a partition that fills the disk and enter the data there.Even having correctly estimated how much space is needed for / home, / usr, / tmp, or any other important directory is not uncommon for us to stay out of space in these partitions, when we have plenty of room somewhere else.

  • With the use of a logical volume manager, the entire disc can be assigned to a single logical group and define different logical volumes to store / home or other directories. In case we run out of space, for example, / home, and we have space in / opt, could resize / home and / opt and use the space we've taken from / opt and add it to / home. Keep in mind that to do this, your file system must support resizing the top and bottom, as ReiserFS.

  • Another alternative would be to allow a certain amount of unallocated disk space and where necessary, expand the volume.

Advantages of using LVM in a large system

  • Administering a system with many disks is time-consuming work, and is particularly complex if the system contains records of different sizes. Balancing storage requirements of different users (often conflicting) can be a very laborious and complex task.

  • User groups (call them for example administration, development, etc ...) can have its logical volumes and these can grow as needed, and the administrator can perform the appropriate operations on those volumes.

When a new record is added to the system, it is necessary to move the user data. Simply add the new album to the corresponding logical group and expand the logical volumes all that is appropriate. You can also migrate data from old disks to new ones, completely transparent to the user.



Anatomy of LVM 
  • An LVM is broken down into three parts:

    Physical volumes (PV) are the hard disks, partitions or any other "thing" that looks like a hard disk (raid's)

    Logical Volumes (LV) is the equivalent to a partition on a traditional system. The LV is visible as a standard of blocks, so it can contain filesystems (eg / home) 

    Volume group (VG) is the top of the LVM. It is the "box" where we have our logical volumes (LV) and our physical volumes (PV). It can be seen as an administrative unit in which encompass our resources. It should be noted that while PV is not added to the VG, we can start using it.

In turn, one PV is divided into chunks of data, known as PE (physical extents), which are the same size as the PE of VG. In addition, a LV is also divided into LE (logical extents), whose size is the same for all LV's VG.

For example, with reference to the image of the beginning of the article, suppose we have a VG called CASA, with 4MB of PE, which we add the LV for / tmp /. We added two disk partitions, / dev/sda4 and / dev/sdb3, to be called PV1 and PV2 (you can rename the user's discretion). The PV's can be of different size and therefore have a different number of PE, so suppose we have 99 PE's for PV1 and PV2 248 PE's. Now, we need to create the LV, which can be any size between 1 and 347 (99 +248) PE. This will create a table that relates PE's and LE's and if, for example, write 4 MB at LE LV No. 1, will correspond to the PE 51 of the PV1.

The table that relates PE's and LE's, is called mapping table, and correspondence can be of two types: 

Live: assign a PE's range to another range of LE's in order. For example, the LE's the number 1 through 99 are mapped onto the PE's from 100 to 347, PV2. 

Intertwined: the LE's are intertwined on the PE's of the physical volumes, for example:

Destination
Origin
First chunk of LE1
First chunk of the PV1
Second chunk of LE1
First chunk of PV2
Third chunk of LE1
Second chunk of the PV1
...
...
 
Snapshots (snapshots)
Snapshots (snapshots) allow the administrator to create a new device that will be an exact copy of the LV, frozen at some point in time. Normally this is done automatically, not to alter the normal functioning of the system. When the snapshot is complete, the administrator can remove the device without any problems.Furthermore, it is necessary that the data in the LV are in a consistent state, as many file systems in the 2.6 kernel is done automatically.

An important difference between LVM version 1 (LVM1 from now) and version 2 (LVM2) is that in the first, the snapshots were read-only, while in LVM2 are reading and writing. What is the difference? In LVM1, creating what is called a table of exceptions, which are used to maintain a list of which disk blocks have changed. If a block will be modified at the source is first copied to the snapshot, marked as copied in the table of exceptions and then new data is copied to the original volume. In LVM2, snapshots work as in LVM1, but with the feature that if data is written to the snapshot (once assembled), the block is marked as used in the table of exceptions and not copied to the original volume. This feature is very useful because we can create our instant, assemble and test a new program or a file browser.If something disastrous happens, the tire, we delete it and replace the original volume. 

Putting it all together
A concrete example will help: 

Suppose we have a volume group called VG1, this volume group has a physical extension of 4MB. Within this volume group we put two partitions, / dev/hda1 and / dev/hdb1. These partitions will become physical volumes PV1 and PV2 (can be given more meaningful names depending on the administrator). The PV's are divided into chunks of 4MB, and this is the extent size of the volume group. The discs are of different sizes and 99 extensions will PV1 and PV2 248. Now we can create a logical volume, which may have the size you want between 1 and 347 (248 + 99) extensions. When we created the logical volume defines a mapping between logical and physical extensions, eg. a logical extension could be mapped onto physical extent 51 of PV1, data written in the first 4 MB of the logical volume is written to the extension 51 of PV1. 

Common Tasks

Initializing a disk or partition to use with lvm

pvcreate / dev / sda
pvcreate / dev/sda1
pvcreate / dev/sda5

Volume Group 
As we know the volumes of groups include partitions or disks, therefore, if we want to create a volume group called the foundation and add multiple partitions do the following:

vgcreate foundation / dev/sda1 / dev/sda2 / dev/sdb1


Then we need to activate the volume group

vgchange-ay foundation

to clear output:

vgchange-an Foundation
To clear the founding group wrote:

vgremove Foundation
Note: This will delete the volume group and all its logical volumes.

If we extend our volume group, ie, we need to add another partition or disk, then typed 

vgextend foundation / dev/sda1
This adds the partition / dev/sda1 to our volume group foundation.
To see the changes wrote:

vgdisplay Foundation
And to see the active partition and associated volume group:
 
pvdisplay
Removing physical volumes from a volume group

If we are to / dev/sdc1 of our founding group volume, then:

vgreduce foundation / dev/sdc1
Logical Volumes

To create a logical volume from 1GB to our foundation we group volume:

lvcreate-L +1 G-n LV1 Foundation
where is our logical volume LV1 create
If you want the logical volume is located in a specific partition of the volume of founding group then becomes as follows:

lvcreate-L +1 G-n LV1 foundation / dev/sdb1
After this should create a logical volume at / dev/fundacion/lv1 the format which would give him and then got on our filesystem:

mkfs.ext3 / dev/fundacion/lv1
Then you are ready to be mounted wherever you want

mount / dev/fundacion/lv1 / mnt

Extending a logical volume
If you need more space in our logical volume LVM1 then we extend it to our way taking into account the volume space group we belong to, is, we can not exceed the size of our logical volume free space beyond the volume that has our group. Serious:

First unmount the partition, if fitted:

umount / mnt /

lvextend-L +1 G / dev/fundacion/lv1
This increases the logical volume LV1 1Gb. In addition to this, we must tell the operating system change that volume:

e2fsck-f / dev/fundacion/lv1
to check the filesystem. Then:

resize2fs / dev/fundacion/lv1.
After this you can mount the partition again LV1 with the changes.

Reducing a logical volume:

Note: It can be very dangerous to perform this task.

If our logical volume LV1 has 2Gb of space and we want to reduce it to 1GB, we performed the following:

Dismantle up volume is mounted.

umount / mnt

e2fsck-f / dev/fundacion/lv1

Then I do the resize operating system level

resize2fs / dev/fundacion/lv1 1G
What this does is tell the OS that the LV1 logical volume will now be 1Gb

Finally we reduce it:

lvreduce-L-1G / dev
To confirm that the cash has verified the size of the volume:

lvdisplay / dev/fundacion/lv1
And confirmed in the operating system

fdisk-s / dev/fundacion/lv1

Finally assemble again with the new logical volume size:

mount / dev/fundacion/lv1 / mnt

Bibliography:

http://es.wikipedia.org/wiki/LVM
http://www.tldp.org/HOWTO/LVM-HOWTO.html