Wednesday, 17 September 2014

Working with LVMs

Working with LVMs

LVMs or Logical Volume Management is a really useful alternative towards creating fixed partitions and file systems. Unlike traditional fixed sized partitions, LVM are designed to be added or removed as need be without having to re-organize or spread the data over multiple filesystems.

LVM provides a flexible and high level approach to managing disk space. Instead of each disk drive being split into partitions of fixed sizes onto which fixed size file systems are created, LVM provides a way to group together disk space into logical volumes which can be easily resized and moved. In addition LVM allows administrators to carefully control disk space assigned to different groups of users by allocating distinct volume groups or logical volumes to those users. When the space initially allocated to the volume is exhausted the administrator can simply add more space without having to move the user files to a different file system.

In this tutorial, I'll be showing you simple steps using which you can easily extend your root partition using LVMs. But before we begin, here a look at few LVM related terms and components:

Volume Group (VG)
The Volume Group is the high level container which holds one or more logical volumes and physical volumes.

Physical Volume (PV)
A physical volume represents a storage device such as a disk drive or other storage media.

Logical Volume (LV)
A logical volume is the equivalent to a disk partition and, as with a disk partition, can contain a file system.

Physical Extent (PE)

Each physical volume (PV) is divided into equal size blocks known as physical extents.

Logical Extent (LE)
Each logical volume (LV) is divided into equal size blocks called logical extents.

In the following guide, I'll be demonstrating the steps on a CentOS 6.5 OS. The steps should be the same for most of the popular Linux distributions as well.

First thing to note is that my root partition is already mounted to a logical volume (named /vg_lvm-lv_root). This is essential as the following steps will not work if your root partion is places on a standard partition.

Note down where the root partition is mounted using the following command:

# df -h

you can alternatively check the same using the mount command:

# mount

If you want to see info regarding the existing volume groups(VG), you can use the following command:

# vgdisplay

From the screenshot below, we can see that my Volume Group vg_lvm has a Physical Extent (PE) size of 4MB, and has a total of 7GB available as a total Volume Group (VG) size.

To know more about the individual Logical Volumes(LV) created in a Volume Group, use the following command:

# lvdisplay

As you can see from the screenshot, there are two logical volumes here. Firstly, we have a root logical volume (lv_root) and it is 6.71GB in size. The other is a logical volume for swap (lv_swap) and that is 816MB in size. For the purpose of this tutorial, we will increase the root logical volumes size.

Now that we know what space is being used it is often helpful to understand which devices are providing the space (in other words which devices are being used as physical volumes). To obtain this information we need to run the following command:

# pvdisplay

The following screenshot shows that the space for Volume Group (vg_lvm) is provided by a Physical Volume (PV) located at /dev/sda2

Adding additional space to root partition
I have attached a 32GB HDD to my CentOS machine. I can view my newly added HDD as a disk using the fdisk command:

# fdisk -l 

Before I use this new disk as a logical volume, its always good to format it and create a Linux partition on it. 

# fdisk /dev/sdb

Make sure the new disk got formatted properly by using the fdisk command as shown below:

# fdisk -l

Now that the new disk is formatted and ready, lets add that o our Volume Group.

First, we convert this disk into a physical volume. This is achieved using the pvcreate command:

# pvcreate /dev/sdb1

With the physical volume created we now need to add it to the volume group (vg_lvm) using the vgextend command:

# vgextend vg_lvm /dev/sdb1

Use the pvdisplay command to view the newly created Physical Volume

# pvdisplay /dev/sdb1

The new physical volume has now been added to the volume group and is ready to be allocated to a logical volume. 

We use the lvextend tool to provide the size by which we wish to extend the volume. In this case we want to extend the size of lv_root by approx 31 GB (Since we added a HDD of 32GB, use a disk space of approx 31GB for the logical volume). 

NOTE that we need to provide the path to the logical volume which can be obtained from the lvdisplay command

# lvextend -L+31G /dev/vg_lvm/lvm_root

Last step in the process is to simply resize the file system on the new logical volume so that it now starts using the additional 30GB of free space as well.

# resize2fs /dev/vg_lvm/lv_root

Check your root partitions size now, and it should show you ample of new space to work with!!

# df -h

No comments :

Post a Comment

Note: only a member of this blog may post a comment.