Expanding the EC2 root file system

By Brian Fitzgerald

Introduction

Expanding the AWS EC2 Linux root file system size for Red Hat version 7.1 and up can be handled by a few simple AWS EC2 console or CLI steps. For Redhat version 7.0, additional Linux command line steps are required.

Initial Conditions

We’ll start out with a small root file system size, 8G.

[ec2-user@ip-10-0-1-244 ~]$ df -H /
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      8.6G  1.3G  7.4G  15% /

Review the instance and the volume in the AWS console:

ebs-id

If you want to use command line tools, the note these facts:

Instance ID: i-0be13d6ba7d191ebe
EBS ID: vol-0aeecfd5a36a070a1

Procedure

To resize to 100G, for example, from the console, navigate to EC2, select the EC2 instance. Select the root block device.

inst.png

resizvol

or from the CLI, issue:

C:\>aws ec2 modify-volume --volume-id vol-0aeecfd5a36a070a1 --size 100
VOLUMEMODIFICATION      modifying       100     10      gp2     0       2019-06-17T22:42:37.000Z    300 100     gp2     vol-0aeecfd5a36a070a1

Optionally, check on the status:

C:\>aws ec2 describe-volumes-modifications --volume-id vol-0aeecfd5a36a070a1
VOLUMESMODIFICATIONS    optimizing      100     10      gp2     0       2019-06-17T22:42:37.000Z    300 100     gp2     vol-0aeecfd5a36a070a1

If your system is Amazon Linux, or Red Hat Linux version 7.1 and up, reboot the instance and you are done.

Fix 7.0 with parted

On some systems, Redhat Linux 7.0, for example, resizing the volume is not enough. You must fix the partition table with parted and then adjust the root partition size. Start parted on your root device:

[root@ip-10-0-1-244 ~]# parted /dev/xvda
GNU Parted 3.1
Using /dev/xvda
Welcome to GNU Parted! Type 'help' to view a list of commands.
                               display the version number and copyright information of GNU Parted

Print the partition table with “p”:

(parted) p

Parted will find problems and offer to fix them. Respond “f”:

Error: The backup GPT table is not at the end of the disk, as it should be.  This might mean that another operating
system believes the disk is smaller.  Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? f
Warning: Not all of the space available to /dev/xvda appears to be used, you can fix the GPT to use all of the space
(an extra 188743680 blocks) or continue with the current setting?
Fix/Ignore? f
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: pmbr_boot

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  6445MB  6442MB  xfs

Next, quit parted and startup fdisk.:

(parted) q
[root@ip-10-0-1-244 ~]# fdisk /dev/xvda
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Print the partition table:

Command (m for help): p

Disk /dev/xvda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
Disk identifier: C43F888F-F4D2-422F-9DE9-3755F19BB874
# Start End Size Type Name
1 2048 4095 1M BIOS boot
2 4096 12587007 6G Microsoft basic

Delete partition 2 by entering “d”. On the next line, accept the default partition number.

Command (m for help): d
Partition number (1,2, default 2):
Partition 2 is deleted

Re-create partition 2 by entering “n”. Accept the defaults for partition number, first sector, and last sector.

Command (m for help): n
Partition number (2-128, default 2):
First sector (34-209715166, default 4096):
Last sector, +sectors or +size{K,M,G,T,P} (4096-209715166, default 209715166):
Created partition 2

Write out the partition table:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Proceed to the next section. Reboot.

Reboot

Reboot from the AWS EC2 console, the AWS CLI, or the Linux shell:

C:\>aws ec2 reboot-instances --instance-ids i-0be13d6ba7d191ebe

or

[root@ip-10-0-1-244 ~]# reboot

Login and check:

[ec2-user@ip-10-0-1-244 ~]$ df -H /
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      108G  1.4G  106G   2% /

That’s it!

Conclusion

The procedure for resizing the root file system depends on the operating system version. In Redhat 7.1 and up, simply resize the EBS volume and reboot.

In Redhat 7.0:

  • Resize the EBS volume.
  • Fix the partition table with parted.
  • Adjust the partition table with fdisk.
  • Reboot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s