Resize a gp2 Backed EBS Volume on an HVM Instance in AWS EC2
Unlike PV instances in EC2, HVM instances run as if it were on the native hardware platform. HVM instances however do not support standard/magnetic backed EBS storage types. If you were used to using resize2fs
for resizing your EC2 volumes, you’ve probably realized this doesn’t work with the new instance types.
Online resizing of a gp2/HVM volume can be performed nearly as quickly as standard/magnetic/PV volumes.
The following assumptions are being made about the instance configuration, you may need to modify the steps as needed for your particular configuration:
- ext4 Filesystem
- We will be resizing the boot volume
- Boot volume is
/dev/xvda
- RHEL (CentOS in my case)
- A reboot is necessary (workarounds are available)
- You have
root
orsudo
access to the instance
In this example scenario, the disk size within the host OS is 6GB, but has been provisioned in EC2 as 20GB.
Note: For legibility, I’ve excluded sudo
from the following commands, depending on your system, you may or may not need to use it
Begin by verifying that the system in fact sees the smaller partition size.
$ df -h
Which should show you output similar to the following (depending on your configuration of course).
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 6.0G 2.0G 3.7G 35% /
tmpfs 15G 0 15G 0% /dev/shm
Now verify that fdisk
sees the larger disk.
$ fdisk -l
Which should return the following.
Disk /dev/xvda: 21.5 GB, 21474836480 bytes
97 heads, 17 sectors/track, 25435 cylinders
Units = cylinders of 1649 * 512 = 844288 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0003b587
Open the boot volume in fdisk
(Note: We open xvda
not xvda1
).
$ fdisk /dev/xvda
You should get the following prompt.
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help):
Enter u
to view units as sectors and hit return.
Changing display/entry units to sectors
Command (m for help):
We need to view the start sector of the volume so output the volume data.
Enter p
to print the partition table and hit return.
Disk /dev/xvda: 21.5 GB, 21474836480 bytes
97 heads, 17 sectors/track, 25435 cylinders, total 41943040 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 identifier: 0x0003b587
Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 12584959 6291456 83 Linux
Command (m for help):
The starting sector is 2048
.
Enter d
to delete the boot partition and hit return.
Selected partition 1
Command (m for help):
Enter n
to create a new partition and hit return.
Command action
e extended
p primary partition (1-4)
Enter p
to set the new partition to be the primary partition and hit return.
Partition number (1-4):
Enter 1
to set the partition number and hit return.
First sector (17-41943039, default 17):
Enter the start sector we found in the step above, in this case 2048
, and hit return.
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Hit return to select the default value which effectively is telling fdisk
to use the entire disk.
Using default value 41943039
Command (m for help):
Verify the new partition table uses the entire disk as we set above.
Enter p
to print the partition table and hit return.
Disk /dev/xvda: 21.5 GB, 21474836480 bytes
97 heads, 17 sectors/track, 25435 cylinders, total 41943040 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 identifier: 0x0003b587
Device Boot Start End Blocks Id System
/dev/xvda1 2048 41943039 20970496 83 Linux
Command (m for help):
Enter a
to set the partition bootable flag and hit return, the volume will not boot if you do net set this flag.
Partition number (1-4):
Enter 1
to set the first partition to bootable and hit return.
Command (m for help):
Enter w
to write the changes to disk and hit return.
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.
The disk has been resized, we need to reboot for the kernel to recognize the new partition table.
$ reboot now
When the system has rebooted, run resize2fs
as you normally would.
$ resize2fs /dev/xvda1
Which should output.
Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/xvda1 to 41943039 (4k) blocks.
The filesystem on /dev/xvda1 is now 41943039 blocks long.
Confirm the system can see the full disk size.
$ df -h
Which should output.
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 10G 20G 50% /
tmpfs 1003M 0 1003M 0% /dev/shm