DEV Community

Cover image for How to Attach, Modify, and Increase an AWS EC2 EBS Volume from Your Local Linux Machine.
Md Abu Musa
Md Abu Musa

Posted on

How to Attach, Modify, and Increase an AWS EC2 EBS Volume from Your Local Linux Machine.

Expanding the storage of an AWS EC2 instance involves attaching an Elastic Block Store (EBS) volume, modifying it, and resizing the filesystem to use the additional space. This blog walks you through these steps with a detailed explanation of the commands you will use.

Prerequisites

  • AWS EC2 instance: You need an Ubuntu-based EC2 instance running on AWS.
  • Local Linux system: A Linux system (Ubuntu in this case) to SSH into the EC2 instance.
  • AWS EBS Volume: An attached EBS volume to your instance.

Steps to Attach and Modify an EBS Volume

Step 1: Switch to the Root User

sudo su
Enter fullscreen mode Exit fullscreen mode

This command allows you to switch to the root user for administrative privileges required to perform disk operations.


Step 2: Change to the Home Directory

cd
Enter fullscreen mode Exit fullscreen mode

Navigate to the home directory to ensure youโ€™re working in a familiar and safe location.


Step 3: Check Current Disk Usage

df -hT
Enter fullscreen mode Exit fullscreen mode
  • df: Displays the amount of disk space used and available on filesystems.
  • -hT: Displays sizes in a human-readable format (-h) and includes filesystem types (-T).

Use this to verify the current storage and note if the new EBS volume is recognized.


Step 4: List Block Devices

lsblk
Enter fullscreen mode Exit fullscreen mode
  • Displays information about available block devices such as disks and partitions.
  • Confirm that the new EBS volume (/dev/xvdb) is attached to your instance.

Step 5: View Disk Partitions

fdisk -l
Enter fullscreen mode Exit fullscreen mode
  • Lists all disk partitions.
  • Helps you identify the available disk space on the new EBS volume.

Step 6: Create a Partition on the New Volume

fdisk /dev/xvdb
Enter fullscreen mode Exit fullscreen mode
  • This command opens the partition editor for /dev/xvdb.

Follow these prompts:

  • n: Create a new partition.
  • w: Write changes to disk and exit.

You now have a partition (/dev/xvdb1) ready for formatting.


Step 7: Format the Partition with XFS

mkfs -t xfs /dev/xvdb1
Enter fullscreen mode Exit fullscreen mode
  • mkfs: Creates a filesystem on the partition.
  • -t xfs: Specifies the XFS filesystem type, known for scalability and performance.

Step 8: Create a Directory for Mounting

mkdir new_directory
Enter fullscreen mode Exit fullscreen mode

Create a new directory where the EBS volume will be mounted.


Step 9: Mount the Partition

mount /dev/xvdb1 /new_directory
Enter fullscreen mode Exit fullscreen mode

Mount the newly formatted partition to the new_directory.


Step 10: Expand the Partition

If the EBS volume has been resized in AWS, you need to expand the partition to use the additional space.

Resize the Partition Table

growpart /dev/xvdb 1
Enter fullscreen mode Exit fullscreen mode
  • growpart: Extends a partition on a block device.
  • /dev/xvdb 1: Indicates the first partition of the volume.

Resize the Filesystem

xfs_growfs -d new_directory
Enter fullscreen mode Exit fullscreen mode
  • xfs_growfs: Grows the XFS filesystem.
  • -d: Expands the filesystem to the maximum available size.
  • new_directory: Specifies the mount point.

Verification

  1. Check Filesystem Usage:
   df -hT
Enter fullscreen mode Exit fullscreen mode

Verify that the filesystem reflects the increased size.

  1. Inspect the Mounted Volume:
   lsblk
Enter fullscreen mode Exit fullscreen mode

Confirm the updated partition and filesystem.


Conclusion

By following these steps, you can successfully attach, partition, format, and expand an AWS EBS volume on your EC2 instance. This process ensures that your instance can handle increased storage requirements seamlessly.

Happy cloud computing! ๐Ÿš€

Top comments (0)