Monday, 8 August 2016

How to add disks to LVM volume on Linux

How to add disks to LVM volume on Linux

    
 
Question: I have new unused hard drives, and want to add them to an existing LVM volume to increase its size. How can I add disks to LVM to extend an LVM volume?
The biggest advantage of LVM over traditional disk partitions is its support for "dynamic partitions"; you can create and resize (grow or shrink) LVM volumes dynamically as needed. There is no notion of physical disk boundary in LVM logical volumes, so you can create a large LVM volume that spans across multiple smaller physical disks. Such flexible partitioning allows you to manage storage space more efficiently as disk usage patterns change over time.
If you want to add new disks to an existing LVM volume to expand its size, you can easily do it, and here is how.

Preparation

First, use fdisk to check what disks you have on your system.
$ sudo fdisk -l

In this example, three disks are detected (/dev/sda, /dev/sdb, /dev/sdc). The first disk (/dev/sda) is used by LVM, while the other two (/dev/sdb and /dev/sdc) are not added to LVM, and do not have any partition in them.
Check available LVM volume groups and logical volumes with the lvs command.
$ sudo lvs

In the example, one volume group ("yoda-vg") exists, and two logical volumes ("root" and "swap") are created in this volume group.
$ df

According to the df output, the "root" logical volume is mapped to /dev/mapper/yoda--vg-root by the device mapper.
Using this information, I will show how to add two disks /dev/sdb and /dev/sdc to the "root" logical volume in the rest of the tutorial.

Step One: Create Partitions on New Disks (Optional)

The first step is to create a partition on each new disk before adding them to LVM. This step is needed only if you want to allocate only part of the disk to LVM. If you want to add the whole disk to LVM, creating a partition is not necessary, and you can skip this step.
$ sudo fdisk /dev/sdb
Create a primary-type partition which takes up the whole disk (2TB).

Repeat the same step with /dev/sdc.
Use fdisk to verify newly created partitions (/dev/sdb1 and /dev/sdc1).
$ sudo fdisk -l

Step Two: Create Physical Volumes on New Disks

Next, create a physical volume on each new disk partition (/dev/sdb1 and /dev/sdc1).
$ sudo pvcreate /dev/sdb1
$ sudo pvcreate /dev/sdc1
If you haven't created any partition on each disk, run the following commands instead.
$ sudo pvcreate /dev/sdb
$ sudo pvcreate /dev/sdc
Using lvmdiskscan, verify that physical volumes are created successfully.
$ sudo lvmdiskscan -l

Newly created LVM physical volumes are named the same as the original partitions (e.g., /dev/sdb1, /dev/sdc1).

Step Three: Extend an Existing Logical Volume

Next, find the volume group which contains the logical volume to expand, and extend the group by adding newly created physical volumes to it.
In our example, the "root" logical volume belongs to the "yoda-vg" volume group. Let's add a physical volume /dev/sdb1 to this group first.
$ sudo vgextend yoda-vg /dev/sdb1
Again, if there is no partition in /dev/sdb, the following command instead.
$ sudo vgextend yoda-vg /dev/sdb
Given the resized volume group, now extend the "root" logical volume itself:
$ sudo lvm lvextend -l +100%FREE /dev/yoda-vg/root
The above command tells the "root" logical volume to use all available additional free space in its volume group. Note that /dev/yoda-vg/root is the device the "root" volume is mapped to.
The final step is to enlarge the filesystem created inside the "root" volume. Otherwise, the filesystem will not recognize additional free space in the volume.
$ sudo resize2fs -p /dev/mapper/yoda--vg-root
The resize2fs command supports resizing ext2/ext3/ext4 filesystems. It also supports online resizing in case you expand a filesystem. No need to unmount the filesystem.
At this point, the filesystem should be expanded to take up 100% of the resized "root" logical volume.

Repeat the step three with /dev/sdc.

No comments: