AWS EC2: extend LVM-managed root volume

DISCLAIMER: Make sure you backup all your instance volume data before following this article.

By default, the 16 GiB LVM managed instance volume (or root disk as it is also known) is split like this:

  • /boot 512 MiB
  • / (root) 4 GiB
  • /tmp 2 GiB
  • /usr 2 GiB
  • /opt 2 GiB
  • /home 2 GiB
  • /var rest of the free space (3.5 GiB).

You can see the default disk topology with the following command:

NOTE: The below lsblk output is for Nitro instance types, in which the NVMe disk device naming is the following: /dev/nvme0n1 is the device name for the first volume, /dev/nvme1n1 is the device name for the second volume, /dev/nvme0n1p1 is the first partition on the first volume, /dev/nvme0n1p3 is the 3rd partition on the first volume, and so on. In case of the Xen instance types, the following disk device naming is used: /dev/xvda is the fist volume and /dev/xvdb is the second volume, while /dev/xvda1 is the first partition of the first volume, /dev/xvda3 is the 3rd partition on the first volume, and so on. Please keep this in mind when reading the following paragraphs.

[ec2-user@ip-172-31-4-8 ~]$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   16G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 15.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    2G  0 lvm  /home
  └─rootvg-varlv  253:5    0  3.5G  0 lvm  /var
[ec2-user@ip-172-31-4-8 ~]$

From the above output:

  • the root disk (device name nvme0n1) size is 16 GiB;
  • the boot partition (device name nvme0n1p2) size is 512 MiB;
  • the LVM partition (device name nvme0n1p3) size is 15.5 GiB;
  • the rootlv logical volume (rootvg-rootlv) size is 4 GiB;
  • the tmplv logical volume (rootvg-tmplv) size is 2 GiB;
  • the usrlv logical volume (rootvg-usrlv) size is 2 GiB;
  • the optlv logical volume (rootvg-optlv) size is 2 GiB;
  • the homelv logical volume (rootvg-homelv) size is 2 GiB;
  • the varlv logical volume (rootvg-varlv) size is 3.5 GiB;

For a Xen instance type, the topology will look like below (note that the disk device names have changed, but the logical volume names remains the same):

[ec2-user@ip-172-31-7-134 ~]$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda              202:0    0   16G  0 disk
├─xvda1           202:1    0    1M  0 part
├─xvda2           202:2    0  512M  0 part /boot
└─xvda3           202:3    0 15.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    2G  0 lvm  /home
  └─rootvg-varlv  253:5    0  3.5G  0 lvm  /var
[ec2-user@ip-172-31-7-134 ~]$

Now, there are certain situations when the 16GiB for the root volume is not enough, as the end user requires more space to store data. You can change the size of the root volume either:

  1. during the VM launch, by configuring the root volume size using the AMI launch wizard;
  2. after the VM launch, by changing the root volume size of your linux instance.

For the purpose of this exercise, we’ll consider that the end user has increased the root volume size to 60 GiB. Below is the new disk topology after this change:

[ec2-user@ip-172-31-4-8 ~]$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   60G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 15.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    2G  0 lvm  /home
  └─rootvg-varlv  253:5    0  3.5G  0 lvm  /var
[ec2-user@ip-172-31-4-8 ~]$

From the above output, the root volume (device name nvme0n1) size has been increased to 60 GiB, but the size of the LVM logical volumes have not, and they need to be changed manually, as described in the steps below:

  1. Increase the LVM partition (nvme0n1p3) size, that is currently 15.5 GiB, to the maximum available space:
[ec2-user@ip-172-31-4-8 ~]$ sudo growpart /dev/nvme0n1 3
CHANGED: partition=3 start=1052672 old: size=32499712 end=33552384 new: size=124776414 end=125829086
[ec2-user@ip-172-31-4-8 ~]$

NOTE: in the above command there is a space between the device name /dev/nvme0n1 and the partition number 3.

NOTE: in case of Xen instance types, the command will look like this (note also in this case the space between the device name and partition number): sudo growpart /dev/xvda 3

Below is the new disk topology after this change:

[ec2-user@ip-172-31-4-8 ~]$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   60G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 59.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    2G  0 lvm  /home
  └─rootvg-varlv  253:5    0  3.5G  0 lvm  /var
[ec2-user@ip-172-31-4-8 ~]$

From the above output, the LVM partition (nvme0n1p3) size has been changed to 59.5 GiB.

  1. Increase the size of the /dev/nvme0n1p3 physical volume (PV) and rootvg volume group (VG), that is currently 15.5 GiB for both of them, with the maximum available free space:
[ec2-user@ip-172-31-4-8 ~]$ sudo pvresize /dev/nvme0n1p3
  Physical volume "/dev/nvme0n1p3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[ec2-user@ip-172-31-4-8 ~]$

NOTE: in case of Xen instance types, the command will look like this: sudo pvresize /dev/xvda3

Verify the new size of the LVM physical volume and volume group

[ec2-user@ip-172-31-4-8 ~]$ sudo pvs
  PV             VG     Fmt  Attr PSize   PFree
  /dev/nvme0n1p3 rootvg lvm2 a--  <59.50g 44.00g
[ec2-user@ip-172-31-4-8 ~]$
[ec2-user@ip-172-31-4-8 ~]$ sudo vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  rootvg   1   6   0 wz--n- <59.50g 44.00g
[ec2-user@ip-172-31-4-8 ~]$

From the above output, the size of both /dev/nvme0n1p3 PV and rootvg VG has been changed to 59.5 GiB. There is also 44GiB free space that can be allocated to logical volumes.

  1. Increase the size of the homelv logical volume (LV) and corresponding filesystem, that both currently have 2 GiB in size, with 5 GiB:
[ec2-user@ip-172-31-4-8 ~]$ sudo lvextend --resizefs --size +5G /dev/rootvg/homelv
  Size of logical volume rootvg/homelv changed from 2.00 GiB (512 extents) to 7.00 GiB (1792 extents).
  Logical volume rootvg/homelv successfully resized.
meta-data=/dev/mapper/rootvg-homelv isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 524288 to 1835008
[ec2-user@ip-172-31-4-8 ~]$

Below is the new disk topology after this change:

[ec2-user@ip-172-31-4-8 ~]$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   60G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 59.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    7G  0 lvm  /home
  └─rootvg-varlv  253:5    0  3.5G  0 lvm  /var
[ec2-user@ip-172-31-4-8 ~]$

From the above output, the homelv LV (rootvg-homelv) size has been changed to 7 GiB. The size of all other LVs has not been changed.

  1. Increase the size of the varlv logical volume and corresponding filesystem, that both currently have 3.5 GiB, with the rest of the free available space.
[ec2-user@ip-172-31-4-8 ~]$ sudo lvextend --resizefs --extents +100%FREE /dev/rootvg/varlv
  Size of logical volume rootvg/varlv changed from <3.50 GiB (895 extents) to <42.50 GiB (10879 extents).
  Logical volume rootvg/varlv successfully resized.
meta-data=/dev/mapper/rootvg-varlv isize=512    agcount=4, agsize=229120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=916480, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 916480 to 11140096
[ec2-user@ip-172-31-4-8 ~]$

Below is the new disk topology after this change:

[ec2-user@ip-172-31-4-8 ~]$ sudo lsblk
NAME              MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1           259:0    0   60G  0 disk
├─nvme0n1p1       259:1    0    1M  0 part
├─nvme0n1p2       259:2    0  512M  0 part /boot
└─nvme0n1p3       259:3    0 59.5G  0 part
  ├─rootvg-rootlv 253:0    0    4G  0 lvm  /
  ├─rootvg-tmplv  253:1    0    2G  0 lvm  /tmp
  ├─rootvg-usrlv  253:2    0    2G  0 lvm  /usr
  ├─rootvg-optlv  253:3    0    2G  0 lvm  /opt
  ├─rootvg-homelv 253:4    0    7G  0 lvm  /home
  └─rootvg-varlv  253:5    0 42.5G  0 lvm  /var
[ec2-user@ip-172-31-4-8 ~]$

From the above output, the varlv LV (rootvg-varlv) size has been changed to 42.5 GiB. The size of all other LVs has not been changed.

If you have any questions related to this article, please contact ProComputers Support as instructed here.

References: