By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How do I use LVM to create a logical volume on an EBS volume's partition?

7 minute read
1

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:

  1. Open the Amazon Elastic Compute Cloud (Amazon EC2) console.

  2. Create your EBS volume, and then attach the volume to your Amazon EC2 instance.

  3. 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
  4. 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.
  5. 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
  6. 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:

  1. 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
  2. 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:

  1. 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
  2. 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
  3. 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:

  1. 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.

  2. 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.

  3. Run the mount command to mount the file system on the mount directory:

     sudo mount /dev/examplegroup1/lvexample1 /mnt1
  4. 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:

  1. Modify the size of the existing EBS volume.

  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. Run the lvextend command to extend the LV:

    sudo lvextend -L 19G /dev/examplegroup1/lvexample1
  8. 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
  9. 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:

  1. Create another EBS volume that's 10 GB, and then attach the volume to the instance.

  2. Create a partition on the /dev/xvdi device.

  3. 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.
  4. 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
  5. 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
  6. Run the lvextend command to extend the LV:

    sudo lvextend -L 29G /dev/examplegroup1/lvexample1
  7. 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
AWS OFFICIAL
AWS OFFICIALUpdated 15 days ago
2 Comments

Great guide! Instructions were incredibly clear and was able to get through the entire demo! Would recommend that you include certain parameters like how large should EBS volumes be when they are created and how many GB to extend them by. I was able to derive the amounts from lsblk commands throughout the tutorial, but it would be nice to have them directly.

Also a cool note, you can add the --resizefs/-r flag to lvextend to automatically extend the file system. From the man page, I've derived that it utilises the fsadm tool behind the scenes which supports the ext2, ext3, ext4, ReiserFS and XFS filesystem. This saves us from executing separate resize2fs or xfs_growfs commands

replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a year ago