Amazon EC2

Expanding an EBS volume on a linux VM

Once you have modified the EBS volume in EC2 now you will need to expand the filesystem to utilize the extra storage. To do this we need to login to the VM and identify the device. We can do this wiht the lsblk command.

$lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0         7:0    0 87.9M  1 loop /snap/core/5328
loop1         7:1    0 12.7M  1 loop /snap/amazon-ssm-agent/495
loop2         7:2    0   91M  1 loop /snap/core/6350
loop3         7:3    0   18M  1 loop /snap/amazon-ssm-agent/930
loop4         7:4    0   91M  1 loop /snap/core/6405
loop5         7:5    0 17.9M  1 loop /snap/amazon-ssm-agent/1068
nvme0n1     259:0    0  1.9T  0 disk /data
nvme1n1     259:1    0  100G  0 disk
└─nvme1n1p1 259:2    0  100G  0 part /

We see device nvme0n1 is a 1.9T EBS disk. We are going to modify the EBD in EC2 and make it 4T. Now when we run lsblk we will see the change reflected.

$lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0         7:0    0 87.9M  1 loop /snap/core/5328
loop1         7:1    0 12.7M  1 loop /snap/amazon-ssm-agent/495
loop2         7:2    0   91M  1 loop /snap/core/6350
loop3         7:3    0   18M  1 loop /snap/amazon-ssm-agent/930
loop4         7:4    0   91M  1 loop /snap/core/6405
loop5         7:5    0 17.9M  1 loop /snap/amazon-ssm-agent/1068
nvme0n1     259:0    0  3.9T  0 disk /data
nvme1n1     259:1    0  100G  0 disk
└─nvme1n1p1 259:2    0  100G  0 part /

if we run a df -h we will see that the filesystem is still 2.0T

Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           788M  7.4M  781M   1% /run
/dev/nvme1n1p1   97G  1.9G   96G   2% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/loop0       88M   88M     0 100% /snap/core/5328
/dev/loop1       13M   13M     0 100% /snap/amazon-ssm-agent/495
/dev/nvme0n1    2.0T  1.8T  82G  49% /data
/dev/loop2       91M   91M     0 100% /snap/core/6350
/dev/loop3       18M   18M     0 100% /snap/amazon-ssm-agent/930
/dev/loop4       91M   91M     0 100% /snap/core/6405
/dev/loop5       18M   18M     0 100% /snap/amazon-ssm-agent/1068
tmpfs           788M     0  788M   0% /run/user/1000

So we need to expand the filesystem with resize2fs

$sudo resize2fs /dev/nvme0n1

resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/nvme0n1 is mounted on /data; on-line resizing required
old_desc_blocks = 250, new_desc_blocks = 500
The filesystem on /dev/nvme0n1 is now 1048576000 (4k) blocks long.

Now if we run df -h we can see the extra space is now available

Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           788M  7.4M  781M   1% /run
/dev/nvme1n1p1   97G  1.9G   96G   2% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/loop0       88M   88M     0 100% /snap/core/5328
/dev/loop1       13M   13M     0 100% /snap/amazon-ssm-agent/495
/dev/nvme0n1    3.9T  1.8T  2.0T  49% /data
/dev/loop2       91M   91M     0 100% /snap/core/6350
/dev/loop3       18M   18M     0 100% /snap/amazon-ssm-agent/930
/dev/loop4       91M   91M     0 100% /snap/core/6405
/dev/loop5       18M   18M     0 100% /snap/amazon-ssm-agent/1068
tmpfs           788M     0  788M   0% /run/user/1000