How to expand LVM storage on a Linux server?

Have you ever wondered how to extend the partition that contains your root directory or home directory with LVM? Your partitions may be insufficient and you need to increase their capacity. We will explore how to extend the storage using Logical Volume Manager in Linux.

What is LVM?

Logical Volume Management is a great way to expand the drive space by simply adding additional hard drives into it. It’s like a dynamic partition, where we can create, resize, and reduce the drive space using LVM.

STEP 1: Disk Extending / Disk Growing

Before extending the /root partition using the LVM method, check the  current disk space on the server by the command,

df -h ([root@localhost ~]# df -h)

Extending disk space in LVM on linux server

([root@localhost ~]# lsblk)

Extending disk space in LVM on linux server

Here, we will take the attached disk of sdb for our usage.

STEP 2: Splitting the disk into 2 separate disks

Now, we need to create 2 primary partitions and like sdb1 and sdb2, for adding a new PV, we have to use fdisk to create the LVM partition.

[root@localhost ~]# fdisk /dev/sdb

splitting the disk in LVM on linux server
  1. To Create a new partition Press n.
  1. Choose the primary partition and use p.
  1. Choose which number of partitions to be selected to create the primary partition.
  1. Press 1 if any other disk is available.
  1. Change the type using t.
  1. Type 8e to change the partition type to Linux LVM.
  1. Press w to write the changes.
splitting the disk in LVM on linux server

Great! The disk sdb1 is created and let’s create a sdb2 disk from sdb main disk.

splitting the disk in LVM on linux server

Hurray! We created both the sdb1 and sdb2 disks successfully. Next, we are going to create a new Physical Volume for the sdb1 disk,

[root@localhost ~]# pvcreate /dev/sdb1

splitting the disk in LVM on linux server

Now, check the available volume groups and find the exact volume group name for /root partition, [root@localhost ~]# vgs

splitting the disk in LVM on linux server

Here, the /root partition’s volume group name is centos.

Add the created sdb1 Physical volume into the centos volume group,

[root@localhost ~]# vgextend centos /dev/sdb1

splitting the disk in LVM on linux server

We can see which PV is used to create a particular Volume group.

splitting the disk in LVM on linux server

[root@localhost ~]# vgdisplay

splitting the disk in LVM on linux server

Now we can use the below command to expand the logical volume /root partition,

[root@localhost ~]# lvextend -l +12800 /dev/centos/root

Or, we can use this lvextend -l +100%FREE /dev/centos/root command to extend the volume group.

splitting the disk in LVM on linux server

Next, we need to identify the file system type of the required partition by using the following command

[root@localhost ~]# df -hT

splitting the disk in LVM on linux server

As we can see, the currently used file system type is xfs, let’s grow the /root partition with the extended logical volume,

[root@localhost ~]# xfs_growfs /dev/centos/root

splitting the disk in LVM on linux server

We need to verify the size of the extended partition.

STEP 3: Mounting /home2 in separate mount-point

Create a PV for the sdb2 disk and a new volume group for the sdb2 disk on the server.

[root@localhost ~]# pvcreate /dev/sdb2

Mounting the disk in LVM on linux server

[root@localhost ~]# vgcreate test /dev/sdb2

(here, the test is a volume group name, you can replace the volume group name test with any other name according to your wish.)

Mounting the disk in LVM on linux server

Now, check the available volume groups on the server,

[root@localhost ~]# vgs

Mounting the disk in LVM on linux server

We are going to check the available free physical extend size under the test volume group, [root@localhost ~]# vgdisplay test

Mounting the disk in LVM on linux server

Next, we need to create a logical volume for the sdb2 disk,

[root@localhost ~]# lvcreate -n home2 -l 100%FREE test

(Here, the home2 is a logical volume group name and it is not a mount point, and you can replace home2 with any other name.)

The -n option is used to indicate a name for the LV, whereas -L sets a fixed size, and -l (lowercase L) is used to indicate a percentage of the remaining space in the container VG.

Mounting the disk in LVM on linux server

Check the newly created logical volume under the test volume group,

[root@localhost ~]# lsblk

Mounting the disk in LVM on linux server

Make /home2 directory on the server, [root@localhost ~]# mkdir /home2

Mounting the disk in LVM on linux server

Mount the /home2 as a separate mount point,

[root@localhost ~]# mount /dev/test/home2 /home2

Mounting the disk in LVM on linux server

After mounting /home2 as a separate mount-point create a file system type for /home2. Here, we create xfs as the file system type for /home2.

[root@localhost ~]# mkfs.xfs /dev/test/home2

Mounting the disk in LVM on linux server

Identify the UUID for the /home2 partition, and permanently mount the /home2 partition on the server.

[root@localhost ~]# blkid /dev/test/home2

Mounting the disk in LVM on linux server

Now, Copy this UUID for the /home2 partition and add it to the fstab file,

[root@localhost ~]# vi /etc/fstab

(Note: take a backup of fstab file before making the changes in it)

Mounting the disk in LVM on linux server

Check the added UUID in fstab for /home2 partition,

[root@localhost ~]# cat /etc/fstab

Mounting the disk in LVM on linux server

Check the newly created and mounted /home2 partition on the server.

Mounting the disk in LVM on linux server

Finally, we extended our disk using LVM in the Linux server. Thank you for taking the time to read our blog on “How to expand LVM storage on a Linux server?”. We hope you found the information valuable and insightful. If you find any issues with the information provided in this blog don’t hesitate to contact us (info@assistanz.com).

View post >>

View post >>