How to resize AWS ec2 ebs root partition without rebooting in 3 steps

Go to the AWS EBS dashboard and modify the volume size. Might be good to create a snapshot of it for safety but haven’t really failed ever doing this.

# 1. Check the device of your partition
$ sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 28.1M 1 loop /snap/amazon-ssm-agent/2012
loop1 7:1 0 97M 1 loop /snap/core/9665
loop2 7:2 0 55M 1 loop /snap/core18/1880
loop3 7:3 0 71.3M 1 loop /snap/lxd/16100
xvda 202:0 0 25G 0 disk
└─xvda1 202:1 0 20G 0 part /
xvdf 202:80 0 1T 0 disk /mnt/ebs/frostwire-files
xvdg 202:96 0 16G 0 disk /mnt/ebs/oldroot

# 2. Grow the partition
$ sudo growpart /dev/xvda 1
CHANGED: partition=1 start=2048 old: size=41940959 end=41943007 new: size=52426719 end=52428767

# 3. Extend the file system
$ sudo resize2fs /dev/xvda1
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
...

# Done, new size is reflected with df
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 25G 19G 5.6G 78% /

 

Troubleshooting growpart

$ sudo growpart /dev/nvme1n1 1
WARN: unknown label 
failed [sfd_dump:1] sfdisk --unit=S --dump /dev/nvme1n1
sfdisk: /dev/nvme1n1: does not contain a recognized partition table
FAILED: failed to dump sfdisk info for /dev/nvme1n1

This can happen if your partition is an xfs partition, in that case try the following to double check:

lsblk -f /dev/nvme1n1
NAME    FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
nvme1n1 xfs                5615a816-53bc-4090-a537-80ba86b1b5f3   49.9G    75% /media/ebs/data

We see it’s an XFS filesystem, therefore we can do this now:

sudo xfs_growfs /dev/nvme1n1
meta-data=/dev/nvme1n1           isize=256    agcount=50, agsize=1310720 blks
         =                       sectsz=512   attr=2, projid32bit=0
         =                       crc=0        finobt=0, sparse=0, rmapbt=0
         =                       reflink=0    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=65536000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

We should now see the changes in df -h

df -h
Filesystem       Size  Used Avail Use% Mounted on
...
/dev/nvme1n1     250G  151G  100G  61% /media/ebs/data
...

Leave a Reply

Your email address will not be published. Required fields are marked *