I want to use Logical Volume Manager (LVM) to create an LV on my Amazon Elastic Block Store (Amazon EBS) volume's partition.
Resolution
If you already created and mounted an LV, then proceed to the Extend the LV section.
Create PVs on your volume's partition
The underlying physical storage unit of an LV is a block device, such as a partition of a volume or an entire volume.
Note: Nitro-based instances expose volumes as NVMe devices. The block device names follow the pattern /dev/nvme1n1, /dev/nvme2n1, /dev/nvme3n1, and so on. If you use a Nitro-based instance, then replace the device names in the following steps with the appropriate device name. For more information, see Device names for volumes on Amazon EC2 instances.
To create physical volumes on your volume's partition, complete the following steps:
-
Open the Amazon Elastic Compute Cloud (Amazon EC2) console.
-
Create your EBS volume, and then attach the volume to your Amazon EC2 instance.
-
Run the following command to install LVM:
sudo yum install lvm2 -y
Note: For Debian or Ubuntu-based systems, run the following command to install the growpart utility:
sudo apt-get install -y lvm2
-
Run the gdisk command to create a partition. For the Command prompt, enter n. For the variable Hex code or GUID, enter 8e00.
The following example creates the /dev/xvdh1 partition on /dev/xvdh:
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
Example output:
Changed type of partition to 'Linux LVM'
OK; writing new GUID partition table (GPT) to /dev/xvdh.
The operation has completed successfully.
-
Run the lsblk command to confirm that the partition is created:
lsblk
Example output:
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
-
Run the pvcreate command to create a physical volume (PV) from the partition. The following example creates a PV from /dev/xvdh1:
sudo pvcreate /dev/xvdh1
Example output:
Physical volume "/dev/xvdh1" successfully created.
Create volume groups and add the PV into the volume group
Complete the following steps:
-
Run the vgcreate command to create a volume group that combines the new PVs. The following example uses one PV to create the examplegroup1 volume group:
sudo vgcreate examplegroup1 /dev/xvdh1
Example output:
group "examplegroup1" successfully created
-
Run the vgs or vgdisplay command to view the volume group details:
sudo vgs
Example output:
VG #PV #LV #SN Attr VSize VFree
examplegroup1 1 0 0 wz--n- <10.00g <10.00g
Create an LV and a mount directory
Complete the following steps:
-
Run the lvcreate command to create LVs from your volume group. The following example creates one 9GB LV, lvexample1, from the examplegroup1 volume group:
sudo lvcreate -n lvexample1 -L 9G examplegroup1
Example output:
volume "lvexample1" created
-
Run the lvs or lvdisplay command to view the LV details:
sudo lvs
Example output:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvexample1 examplegroup1 -wi-a----- 9.00g
-
Run the mkdir command to create a mount directory. The following example creates the /mnt1 directory:
sudo mkdir /mnt1
Create and mount a file system
Complete the following steps:
-
Run the mkfs -t command to create the file system:
sudo mkfs -t xfs /dev/examplegroup1/lvexample1
Note: Replace xfs with your file system type. For example, use ext2, ext3, or ext4.
-
Run the lsblk -f command to verify that the file system is created:
lsblk -f
Example output:
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
└─examplegroup1-lvexample1 xfs 5db36052-81d5-4762-8502-6986ff3964e7
Note: The file system type appears under FSTYPE.
-
Run the mount command to mount the file system on the mount directory:
sudo mount /dev/examplegroup1/lvexample1 /mnt1
-
Edit the mount options in the /etc/fstab file so that the new mount persists after reboot:
/dev/examplegroup1/lvexample1 /mnt1 xfs defaults,nofail 0 0
Note: Replace xfs ,with your file system type.
Extend the LV
To extend the LV, either increase the size of the existing EBS volume or add EBS volumes to the volume group.
Increase the size of the existing EBS volume
Note: When you increase the size of your existing volume, size changes usually take effect within seconds after the volume enters the Optimizing state. When the volume is in the Optimizing state, the volume's performance remains between the source and target configuration specifications. Depending on your volume type, performance changes might take a few minutes to a few hours. For more information, see Monitor the progress of Amazon EBS volume modifications.
To increase the size of an existing EBS volume, complete the following steps:
-
Modify the size of the existing EBS volume.
-
Install the growpart utility:
sudo yum install cloud-utils-growpart -y
Note: To install the growpart utility on Debian or Ubuntu-based systems, run the following command:
sudo apt install -y cloud-guest-utils
-
Run the growpart command to extend the partition. In the following example, the /dev/xvdh1 partition is extended:
sudo growpart /dev/xvdh 1
Example output:
CHANGED: disk=/dev/xvdh partition=1: start=2048 old: size=20971519,end=16777182 new: size=41940958,end=41943006
-
Run the pvresize command to resize the PV:
sudo pvresize /dev/xvdh1
Example output:
Physical volume "/dev/xvdh1" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
-
Run the pvs or pvdisplay command to view the PV details:
sudo pvs
Example output:
PV VG Fmt Attr PSize PFree
/dev/xvdh1 examplegroup1 lvm2 a-- <20.00g <13.00g
-
Run the vgs or vgdisplay command to view the volume group details:
sudo vgs
Example output:
VG #PV #LV #SN Attr VSize VFree
examplegroup1 1 1 0 wz--n- <20.00g <13.00g
-
Run the lvextend command to extend the LV:
sudo lvextend -L 19G /dev/examplegroup1/lvexample1
-
Run the lvs or lvdisplay command to view the LV details:
sudo lvs
Example output:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvexample1 examplegroup1 -wi-a----- 19.00g
-
Run one of the following commands to extend the file system.
Ext2, Ext3, and Ext4 file systems:
sudo resize2fs /dev/examplegroup1/lvexample1
xfs file system:
sudo yum install xfsprogs
Add EBS volumes to the volume group
Complete the following steps:
-
Create another EBS volume that's 10 GB, and then attach the volume to the instance.
-
Create a partition on the /dev/xvdi device.
-
Run the pvcreate command to create a PV. In the following example, the volume's block device name is /dev/xvdi1:
sudo pvcreate /dev/xvdi1
Example output:
Physical volume "/dev/xvdi1" successfully created.
-
Run the vgextend command to extend the volume group and add the new volume. The following example extends the examplegroup1 volume group to include the /dev/xvdi1 volume:
sudo vgextend examplegroup1 /dev/xvdi1
Example output:
Volume group "examplegroup2" successfully extended
-
Run the vgs or vgdisplay command to confirm the extension:
sudo vgs
The following example output shows that there are now two PVs in the examplegroup1 volume group:
#PV #LV #SN Attr VSize VFree
examplegroup1 2 1 0 wz--n- 29.99g 20.99g
-
Run the lvextend command to extend the LV:
sudo lvextend -L 29G /dev/examplegroup1/lvexample1
-
Run the one of the following commands to extend the file system.
Ext2, Ext3, and Ext4 file systems:
sudo resize2fs /dev/examplegroup1/lvexample1
XFS file systems:
sudo xfs_growfs /dev/examplegroup1/lvexample1