Bobcares

How to create an LVM logical volume on an EBS volume: AWS EC2

by | Aug 16, 2021

Are you looking for how to create an LVM logical volume on an entire EBS volume? We will help you!

As a part of our AWS Support Services, here, at Bobcares, we often receive similar AWS queries from our customers.

Today, let’s see the steps followed by our Support Techs to create an LVM logical volume on an EBS volume.
 

Create an LVM logical volume on an EBS volume

 
LVM is a tool for logical volume management that includes allocating disks, striping, mirroring, and resizing logical volumes. If we are using logical volumes on the Amazon EBS volume, we must use LVM to extend the logical volume.

Let’s see the steps followed by our Support Techs to use LVM on an EBS volume and extend the partitions.

  1. Firstly, we need to create physical volumes (PV) on the partition of the EBS volume.
  2. Create a volume group (VG) and then add the physical volume that we created into the volume group.
  3. Then create a logical volume (LV) and mount the directory on the LVM.
  4. Then we need to create and mount a file system.
  5. Finally, resize the logical volume.

 

Creating physical volumes on the partition of the EBS volume

 

  1. At first, log in to the AWS Management console and then open the Amazon EC2 console.

2. Create the EBS volume and then attach it to the instance.

3. To create a partition we can use gdisk command.

$ sudo gdisk /dev/xvdh
Command (? for help): n
Partition number (1-1218, default 1): 1
First sector (34-20971486, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to 'Linux LVM'
...
OK; writing new GUID partition table (GPT) to /dev/xvdh.
The operation has completed successfully.

Here, in the example, we create the partition /dev/xvdh1 on /dev/xvdh.

For the variables Hex code or GUID, enter 8e00.

4. To confirm the creation of the partition we can use lsblk command.

$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 8G 0 disk
└─xvda1 202:1 0 8G 0 part /
xvdh 202:80 0 10G 0 disk
└─xvdh1 202:81 0 10G 0 part

5. To create a physical volume from the partion, run the pvcreate command.

$ sudo pvcreate /dev/xvdh1
Physical volume "/dev/xvdh1" successfully created.

Also note that if we are using Nitro-based instance, the block device name are like /dev/nvme1n1, /dev/nvme2n1, etc. So we need to replace the device names in the following steps with the correct device name.
 

Creating volume groups and adding the physical volumes into the volume group

 
The following example uses one physical volume to create volume group samplegroupA.

  1. To create a volume group to add the new physical volume we can use the vgcreate command.

Syntax: $ sudo vgcreate <volume-name> <device-name>

$ sudo vgcreate samplegroupA /dev/xvdh1
Volume group "samplegroupA" successfully created

Here in the following example uses one physical volume to create the volume group ‘samplegroupA

2. To see the new volume group’s details, we can use the vgs or vgdisplay command.

$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
samplegroupA 1 0 0 wz--n- <10.00g <10.00g

 

Creating a logical volume and mounting the directory on LVM

 

  1. To create a logical volume (partition) from the volume group, we can use the lvcreate command.

Syntax: sudo lvcreate -n <logical-volume-name> -L <size-of-volume> <lvm-volume-name>

$ sudo lvcreate -n LVsampleA -L 9G samplegroupA
Logical volume "LVsampleA" created

2. To view the logical volume’s details, we can use the lvs or lvdisplay command.

$ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LVsampleA samplegroupA -wi-a----- 9.00g

3. Then using the mkdir command we can create a mount directory. The following example creates the directory /mnt1.

$ sudo mkdir /mnt1

 

Creating and mounting the file system

 

  1. To create a file system and mount the partitions, use the following commands.

We can use the mkfs -t command to create the file system.

$ sudo mkfs -t xfs /dev/samplegroupA/LVsampleA

Replace xfs with the correct file system type.

2. Then use the lsblk -f command to verify the creation of the new file system.

$ lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
xvda
├─xvda1
└─xvda2 xfs 66e5e079-770e-4359-a9da-5205c3d8d5af /
xvdh
└─xvdh1 LVM2_member 0UnOic-e2ng-XxH5-z0UW-7aTh-RxQK-KMrDqo
└─samplegroupA-LVsampleA xfs 5db36052-81d5-4762-8502-6986ff3964e7

3. Then run the mount command to mount the file system on the mount directory.

$ sudo mount /dev/samplegroupA/LVsampleA /mnt1

4. Then edit the mount options in the /etc/fstab file so that the new mount persists after reboot.

/dev/samplegroupA/LVsampleA /mnt1 xfs defaults,nofail 0 0

 

Resize the logical volume

 
There are two options for extending logical volumes:

  1. By increasing the size of the existing EBS volume.
  2. Adding additional EBS volumes to the volume group.

 

By increasing the size of the existing EBS volume:

 
1. Change the size of the existing EBS volume.

2. Then we need to install the growpart:

$ sudo yum install cloud-utils-growpart

For Debian or Ubuntu systems:

$ sudo apt install -y cloud-guest-utils

3. Then to extend the partition, use the growpart command.

$ sudo growpart /dev/xvdh 1
CHANGED: disk=/dev/xvdh partition=1: start=2048 old: size=20971519,end=16777182 new: size=41940958,end=41943006

4. Then run the pvresize command to resize the physical volume.

$ sudo pvresize /dev/xvdh1
Physical volume "/dev/xvdh1" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized

Here, the partition /dev/xvdh1 is extended.

5. We can use the pvs or pvdisplay to view the physical volume details.

$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/xvdh1 samplegroupA lvm2 a-- <20.00g <13.00g

6. To view the volume group’s details, use the vgs or vgdisplay command.

$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
samplegroupA 1 1 0 wz--n- <20.00g <13.00g

7. Then to extend the logical volume, use the lvextend command.

$ sudo lvextend -L 19G /dev/samplegroupA/LVsampleA

8. To view the logical volume’s details, use the lvs or lvdisplay command.

$ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LVsampleA samplegroupA -wi-a----- 19.00g

9. Then extend the file system:

For XFS file systems:

$ sudo yum install xfsprogs
$ sudo xfs_growfs /dev/samplegroupA/LVsampleA

For Ext4 file systems:

$ sudo resize2fs /dev/samplegroupA/LVsampleA

Note that when increasing the size of the volume, size changes usually take effect within a few seconds when the volume enters the optimizing state. The volume’s performance is affected while in the optimizing state but doesn’t fall below the source configuration specification. Performance changes may take from a few minutes to a few hours depending on the volume type.
 

Adding additional EBS volume to the volume group

 

  1. First, we need to create another EBS volume of 10 GB and then attach the volume to the instance.
$ sudo pvcreate /dev/xvdi1
Physical volume "/dev/xvdi1" successfully created.

2. To extend the volume group, use the vgextend command and then add the new volume.

$ sudo vgextend samplegroupA /dev/xvdi1
Volume group "samplegroup2" successfully extended

3. To view this, run the vgs or vgdisplay command.

$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
samplegroupA 2 1 0 wz--n- 29.99g 20.99g

This shows that there are now two PV in the samplegroupA volume group.

4. We can use the lvextend command to extend the logical volume:

$ sudo lvextend -L 29G /dev/samplegroupA/LVsampleA

5. Then resize the file system.

For XFS file systems:

$ sudo xfs_growfs /dev/samplegroupA/LVsampleA

For Ext4 file systems:

$ sudo resize2fs /dev/samplegroupA/LVsampleA

Note: If we have already created LVM on the volume and mounted it, then we need to follow the steps at Extend the logical volume.

 

[Need help with more AWS queries? We’d be happy to assist]
 

Conclusion

 
To conclude, today we discussed the steps followed by our Support Engineers to help our customers to create an LVM logical volume on an EBS volume.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.