How to extend EC2 root partition in LVM

In this tutorial, I am using CentOS 6 (but also applicable on other Linux distro) that has ext4 partition lv_root mounted as / and lv_swap as swap from the volume group VolGroup (which is default), we pretend that we are running out of space in lv_root(/) and volume group (VolGroup) doesn’t have any free space. We added a new volume with 150 GB space. Now, we need to assign this 150 GB space to volume group and then extend lv_root(/).

WARNING: Backup your data before attempting this.

First creating a new volume on EC2 management console and attaching to EC2 instance.

3

Get the information about newly added hard disk using the following command:

sudo fdisk -l

13

Note: /dev/xvdh is the newly added disk!

To create the partition on the second disk, use the following command and follow the “on screen” instructions and change the partition type from Linux to LVM (8e):

sudo fdisk /dev/xvdh

5

Identify the already mounted lvm file system type:

df -T

14

Format the newly created partition using the following command:

sudo mkfs.ext4 /dev/xvdh

7

Initialize the newly created partition as a physical volume:

sudo pvcreate /dev/xvdh

8

Check the volume groups using the following command and extend

Note: vg_centos6 is the volume group that I want to extend, you can change according to your volume group.

Extend the VG (VolGroup) with new PV (/dev/xvdh):

sudo vgs
sudo vgextend VolGroup /dev/xvdh

9

Extend the logical volume (lv_root) with all the free space of the VG(VolGroup):

sudo lvextend -l +100%FREE /dev/VolGroup/lv_root

10

Finally, resize the file-system:

sudo resize2fs /dev/VolGroup/lv_root

11

Verify the file-system is larger using the following commands:

sudo df -h
sudo lvs
sudo vgs

12

GL & HF!

Leave a Reply

Your email address will not be published.