Cloud Insidr

Cybersecurity in the Age of the Machine

  • Subscribe!
  • Privacy Policy
  • Legal
  • Contact Us

Join us on Twitter: @CloudInsidr

  • news & alerts
    • events
    • industry analysis
    • industry gossip
    • people
  • cloud, edge & co.
    • AWS
    • administration & orchestration
      • web servers in the cloud
      • mail servers
      • databases
  • cybersec & warfare
    • encryption
  • blockchain
Home cloud, edge and everything in between administration and orchestration How to expand an (xfs) EBS volume on AWS EC2
How to expand an (xfs) EBS volume on AWS EC2

Anna E Kobylinska 2015-12-24 Leave a Comment

How to expand an (xfs) EBS volume on AWS EC2

Expanding an EBS volume is not quite as easy as recreating it from a snapshot with a larger volume size. It involves a few more steps.

“Elastic” Block Store is not so elastic after all.

Expanding an EBS volume involves quite a few steps. You may have already tried to:

  1. unmount the EBS volume you want to expand (or freeze write access to it);
  2. write a snapshot;
  3. create a new, larger EBS volume based on the snapshot of the old, smaller volume;
  4. attach the new, larger EBS volume to your EC2 instance;
  5. mount the new volume on the instance;
  6. hit a wall: no amount of xfs_growfs’ing seems to get the job done and expand it even a tiny bit.

Here is how to fix it (fast-forward to step 5 below).

Step 1. Unmount your xfs volume (or freeze write access to it)

On EC2, you can usually get away with just creating the snapshot without much ado. If you are paranoid about data loss, you probably want to unmount the volume (since you may need to do this anyway), or at least lock write access to it.

Unmounting the volume involves the following command:

umount /mount/point

If you prefer to freeze write access to it, use this command instead:

xfs_freeze -f /mount/point

Once you get to the point of being ready to unfreeze it,

xfs_freeze -u /mount/point

will do the trick.

Step 2. Write a snapshot of the EBS volume

Create a snapshot of your EBS volume on AWS EC2. When using the AWS Management Console, navigate to the section Volumes, find your volume, select it, and use the command Create Snapshot from the Actions menu to initiate the process.

Create a snapshot from an EBS volume on AWS EC2 in the AWS Management Console
Create a snapshot from an EBS volume on AWS EC2 in the AWS Management Console

The snapshot will be placed in the Snapshots section of the Console and display the size of the original EBS volume from which it was created.

Creating a snapshot from an EBS volume on AWS EC2 in the Management Console
Creating a snapshot from an EBS volume on AWS EC2 in the AWS Management Console

Step 2. Create a new, larger EBS volume based on the snapshot of the original, smaller EBS volume

Navigate to the section Snapshots of the AWS Management Console for EC2, find your snapshot, select it, and use the command Create Volume from the Actions menu.

Create an EBS volume from snapshot
Create an EBS volume from snapshot

In next dialog that appears, set the new size you want to expand to and select an availability zone (AZ) for the new volume so you can attach it to your instance (the new volume must be located in the same AZ as the instance it will attach to).

The new volume appears, quite predictably, in the Volumes section of the ECS Management Console, and has the new, larger size. Hurraaah! (Not so fast… Son’t celebrate just yet.)

Step 3. Attach the new, larger EBS volume to your EC2 instance

Select your shiny new, larger volume in the Volumes section of the AWS Managment Console and use the command Attach Volume to hook it up to a running instance in the same availability zone.

Attaching an EBS volume to a running EC2 instance
Attach an EBS volume to a running EC2 instance

Select your instance, confirm the device ID and hit Attach.

Attach an EBS volume to a running EC2 instance: size and az selection
Attach an EBS volume to a running EC2 instance: size and az selection

Verify that the volume has attached by taking a cursory glance at the Description tab in the Instances section of the AWS Management Console for your EC2 instance. (You may need to hit the Reload button at the upper right-hand corner of the Console; don’t hit the browser button–that one would reload the entire page and deactivate your current selection of your EC2 instance as well as any view filters.)

An EBS volume attached to an EC2 instance
An EBS volume attached to an EC2 instance

Step 4. Connect to your EC2 instance and mount the volume

Connect using an ssh client such as PuTTY, then switch to root (sudo su).

View block devices

For NVMe, see: How to attach and mount an NVMe EBS volume on EC2.

For legacy SATA drives, the command:

ls /dev/x*

should show something to this effect:

/dev/xvda /dev/xvda1 /dev/xvdf /dev/xvdf1

Your boot volume in the above example is /dev/xvda; the boot partition is /dev/xvda1, the newly attached volume is /dev/xvdf with the data partition being /dev/xvdf1.

Repair the xfs file system on the newly mounted volume (optional, but recommended)

If you need to repair the XFS file system on the volume, use xfs_repair:

xfs_repair /dev/xvdf1

Mount the xfs file system partition

In order to mount the file system you need a directory that will serve as the mount point. Create a new directory (sudo su makes you root):

sudo su 

mkdir /mnt/www

Mount the file system (example mountpoint):

mount /dev/xvdf1 /mnt/www

Verify the capacity of the xfs partition

The volume will show up at its original capacity stored in the AWS snapshot:

df -h

Now what?

Step 5. [OPTION 1] Grow the xfs partition: adjust the partition size using growpart and xfs_growfs

First, use growpart to extend the first partition on the volume:

growpart /dev/xvdf 1

Now, you can extend the XFS file system (while still mounted) using the xfs_growfs command (see manpage for options):

xfs_growfs /mount/point

(Without the -D size option, xfs_growfs will expand the file system to the maximum size supported by the device).

Step 5. [OPTION 2] Grow the xfs partition: adjust the partition size using fdisk

You can also grow the partition using fdisk.

Call fdisk on the newly mounted block device

Call fdisk on the block device that represents the new EBS volume that needs resizing:

# fdisk /dev/xvdf

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 of the disk you selected above using the command p:

Command (m for help): p

Disk /dev/xvdf: 53.7 GB, 53687091200 bytes, 104857600 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: dos
Disk identifier: 0x9e5f5b78

Device Boot Start End Blocks Id System
/dev/xvdf1 2048 41943039 20970496 83 Linux

All of a sudden, fdisk has just confirmed your desired new volume size.

Delete the data partition in fdisk (but don’t write the changes yet!)

Delete the data partition on your volume (don’t worry, your data will be preserved, just make sure you select the right partition, which is partition 1 in the example):

Command (m for help): d
Selected partition 1
Partition 1 is deleted

How does it feel now?

Still worried? Why don’t you create a new partition (see below).

Create a new partition on the newly mounted block device in fdisk

Create a new partition in fdisk by using the n command with option p for primary and all other options confirmed at their default values. This is how it looks in our example:

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set

Here you have your partition back.

Write changes to the device in fdisk

So far, no changes have been written to the disk as of yet. To do this, use the w command (for write):

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

Calling ioctl() to re-read partition table.
Syncing disks.

Mount the newly expanded data partition

Now is the time to mount the newly expanded data partition:

mount /dev/xvdf1  /mnt/www/

Verify that your data is still on it:

ls -l /mnt/www/

The volume size remained unchanged:

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 10G 2.5G 7.6G 25% /
(...)
/dev/xvdf1 20G 246M 20G 2% /mnt/www

Expand the xfs file system on the newly expanded partition

To expand the xfs data partition to the maximum size supported by the device, use this command:

# xfs_growfs -d /mnt/www

You should see roughly this output as a result:

meta-data=/dev/xvdf1 isize=256 agcount=4, agsize=1310656 blks
= sectsz=512 attr=2, projid32bit=0
= crc=0 finobt=0
data = bsize=4096 blocks=5242624, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
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 5242624 to 13106944
Expanding the xfs-formatted EBS volume with xfs_growfs and the result after expansion
Expanding the xfs-formatted EBS volume with xfs_growfs and the result after expansion

Now you can finally get to admire the new, larger volume size:

# df -h
Filesystem Size Used Avail Use% Mounted on
(...)
/dev/xvdf1 50G 247M 50G 1% /mnt/www

How’s that for a reason to celebrate!

Adjust /etc/fstab

If you would like the volume to mount automatically after each reboot, enter this line into /etc/fstab (add spaces as you see fit):

/dev/xvdf1 /your/mountpoint xfs  defaults  0 0

Reboot your system to verify that the volumes mount correctly, and you are done.

Why don’t you subscribe to our newsletter to receive solutions to your problems, tips, and special offers, straight to your email box, for free :

[wysija_form id=”1″]

Alert: With the Elastic File System (Amazon EFS), AWS has created what EBS should have been from the get go: network-attached EC2 volumes-as-a-service that grow as your data grows. They can be accessed from more than one EC2 instance at a time, but they are a bit more expensive than EBS.

Related: How to Restore Your Instance Data from a Backup using Snapshots on AWS EC2/EBS – Cloud Insidr.

Cyber security showdown: AWS versus Microsoft Azure.

Filed Under: administration and orchestration, cloud, edge and everything in between Tagged With: AWS, EBS, EC2, fstab, mountpoint, NVMe, xfs

Leave a Reply Cancel reply

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

Subscribe

SSL/TLS Certificate Square (250 x 250)

Tag Cloud

AWS AWS Billing Dashboard AWS Config AWS Cost Explorer AWS EBS AWS Resource Explorer AWS Tag Editor AWS Trusted Advisor cipher suites cyber security Diffie-Hellman EBS EC2 ECDHE Five Eyes Alliance FLAME HSTS HTTP/2 Java Linux Log4j NGINX NVMe PHP RCE Route 53 SELinux SHA-1 SHA-2 SQL SSL TLS

Pearson Education (InformIT)

SSL/TLS Certificate Medium Rectangle (300 x 250)

©2022 CybrAnalytiqa OÜ

  • Content purchasing and syndication