How do I create an LVM logical volume on an entire EBS volume?
Last updated: 2020-06-24
How can I use Logical Volume Manager (LVM) to create a logical volume (LV) on my entire Amazon Elastic Block Store (Amazon EBS) volume?
Short description
LVM allows you to allocate disk space and strip, re-mirror, and resize logical volumes. Using LVM, you can allocate an EBS volume or a set of EBS volumes to one or more physical volumes.
To use LVM on your EBS volume and extend the partitions:
- Create physical volumes (PV) from your EBS volume.
- Create a volume group (VG) and add the physical volumes into the volume group.
- Create a logical volume (LV) and mount directory on the LVM.
- Create and mount a file system.
- Resize the logical volume.
Note: For more information on using LVM with Nitro-based instances, see Chapter 11. LVM (Logical Volume Manager) on the Red Hat Customer Portal website.
Resolution
Note: If you've already created LVM on your volume and mounted it for use, then follow the instructions beginning at Extend the logical volume.
Create physical volumes from your EBS volumes
The underlying physical storage unit of an LVM logical volume is a block device such as a partition of an EBS volume or an entire EBS volume. You can create a PV on a single EBS volume or multiple EBS volumes.
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're using a Nitro-based instance, then replace the device names in the following steps with the appropriate device name. For more information on device naming, see Device name considerations.
1. Open the Amazon Elastic Compute Cloud console.
2. Create your EBS volume, and then attach the volume to your instance.
3. Use the pvcreate command to create a physical volume. The following example uses one volume, /dev/xvdf, to create the physical volume.
$ sudo pvcreate /dev/xvdf
Physical volume "/dev/xvdf" successfully created.
Use the pvs or pvdisplay command to view the physical volume's details:
$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/xvdf lvm2 --- 10.00g 10.00g
Create volume groups and add the physical volumes into the volume group
Use the vgcreate command to create a volume group to combine the new physical volumes. The following example uses one physical volume to create the volume group Dock.
$ sudo vgcreate Dock /dev/xvdf
Volume group "Dock" successfully created
Use the vgs or vgdisplay to view the new volume group's details:
$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
Dock 1 0 0 wz--n- <10.00g <10.00g
Create a logical volume (LV) and a mount directory
1. Use the lvcreate command to create logical volumes (partitions) from your volume group. The following example creates one 9 GB logical volume, station1, from the Dock volume group.
$ sudo lvcreate -n station1 -L 9G Dock
Logical volume "station1" created.
Use the lvs or lvdisplay command to view the logical volume's details:
$ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
station1 Dock -wi-a----- 9.00g
2. Use the mkdir command to create a mount directory. The following example creates the directory /mnt1.
$ sudo mkdir /dev/Dock/station1 /mnt1
Create and mount a file system
1. Use the following commands to create a file system and mount the partitions for use:
Run the mkfs -t command to create the file system. Note: Replace xfs with your file system type, if different. For example, ext2, ext3, or ext4.
$ sudo mkfs -t xfs /dev/Dock/station1
Run the lsblk -f command to verify creation of the new file system. Note: The file system type you created in the previous step appears under FSTYPE.
$ lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
xvda
├─xvda1
└─xvda2 xfs 66e5e079-770e-4359-a9da-5205c3d8d5af /
xvdf LVM2_member YeDuj5-YHmY-U0Hx-xJyt-R1BP-SPIj-3uCcGF
└─Dock-station1 xfs Feiuj5-kH9Y-UxHx-zJ9t-R7cP-5PIY-7ugcuM
Run the mount command to mount the file system on the mount directory you created in the previous step:
$ sudo mount /dev/Dock/station1 /mnt1
2. Edit the mount options in the /etc/fstab file so that the new mount persists after reboot.
Note: Replace xfs with your file system type in the /etc/fstab entry, if different.
/dev/Dock/station1 /mnt1 xfs defaults,nofail 0 0
Extend the logical volume
You have two options for extending logical volumes:
- Option 1: Increase the size of the existing EBS volume.
- Option 2: Add additional EBS volumes to your volume group.
Note: When increasing the size of your existing 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. Depending on your volume type, performance changes might take from a few minutes to a few hours. For more information, see Monitoring the progress of volume modifications.
Option 1: Increase the size of the existing EBS volume
1. Modify the size of the existing EBS volume.
2. Run the pvresize command to resize the physical volume:
$ sudo pvresize /dev/xvdf
Physical volume "/dev/xvdf" changed
1 physical volume(s) resized / 0 physical volume(s) not resized
Use the pvs or pvdisplay to view the physical volume details:
$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/xvdf Dock lvm2 a-- <20.00g <11.00g
Use the vgs or vgdisplay to view the volume group's details:
$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
Dock 1 1 0 wz--n- <20.00g <11.00g
3. Run the lvextend command to extend the logical volume:
$ sudo lvextend -L 19G /dev/Dock/station1
Use the lvs or lvdisplay command to view the logical volume's details:
$ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
station1 Dock -wi-a----- 19.00g
4. Extend the file system:
Note: Depending on your use case, follow the steps for XFS or Ext2, Ext3, and Ext4 file systems.
Ext2, Ext3, and Ext4 file systems:
$ sudo resize2fs /dev/Dock/station1
XFS file systems:
$ sudo yum install xfsprogs
$ sudo xfs_growfs /dev/Dock/station1
Option 2: Add additional EBS volume to the volume group
1. Create another EBS volume of 10 GB and attach the volume to the instance. In the following example, the volume's block device name is /dev/xvdi.
$ sudo pvcreate /dev/xvdi
2. Use the vgextend command to extend the volume group and add the new volume. The following example extends the volume group Dock to include the volume /dev/xvdi.
$ sudo vgextend Dock /dev/xvdi
Physical volume "/dev/xvdi" successfully created.
Volume group "Dock" successfully extended
To confirm the extension, use the vgs or vgdisplay command. The following example indicates that there are now two PV in the Dock volume group.
$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
Dock 2 1 0 wz--n- 29.99g 20.99g
3. Use the lvextend command to extend the logical volume:
$ sudo lvextend -L 29G /dev/Dock/station1
4. Resize the file system.
Note: Depending on your use case, follow the steps for XFS or Ext2, Ext3, and Ext4 file systems.
Ext2, Ext3, and Ext4 file systems:
$ sudo resize2fs /dev/Dock/station1
XFS file systems:
$ sudo xfs_growfs /dev/Dock/station1
Related information
Did this article help you?
Anything we could improve?
Need more help?