DEV Community

Cover image for How to Attach and Use a New EBS Volume on Your Existing EC2 Instance
Shahid
Shahid

Posted on

How to Attach and Use a New EBS Volume on Your Existing EC2 Instance

Are you running out of disk space on your AWS EC2 instance? Don't worry; you're not alone! As applications grow and data accumulates, the need for additional storage becomes inevitable. The good news is that Amazon Web Services (AWS) makes it easy to attach new Elastic Block Store (EBS) volumes to your existing EC2 instances. In this blog post, we'll walk you through the step-by-step process to seamlessly add more storage to your server.

You can watch along the video
https://www.youtube.com/watch?v=X99yQtxFzzY

πŸ’½ Why Add a New EBS Volume?

Before we dive in, let's understand why you might need an additional EBS volume:

  • Increased Storage Needs: Your application data is growing beyond the current disk capacity.
  • Separation of Data: You want to separate system files from application data for better management.
  • Performance Optimization: Distribute I/O operations across multiple volumes.

Now, let's get started!


πŸ“Œ Step 1: Check Current Disk Usage

First things first, let's see how much disk space you're currently using.

Run the df -h command:

ubuntu@ip-172-31-13-233:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        39G   31G  8.3G  79% /
...
Enter fullscreen mode Exit fullscreen mode

What's happening here?

  • /dev/root is 79% full. That's a warning sign that we need more space!

πŸ“Œ Step 2: List Attached Block Devices

Next, we need to identify the block devices attached to your instance.

Use the lsblk command:

ubuntu@ip-172-31-13-233:~$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
...
nvme1n1     259:0    0 93.1G  0 disk
nvme0n1     259:1    0   40G  0 disk
└─nvme0n1p1 259:2    0   40G  0 part /
Enter fullscreen mode Exit fullscreen mode

Understanding the output:

  • nvme0n1 is your root volume. which is 39 GB mounted on /dev/root . ⚠️ Don't touch that
  • nvme1n1 is the new 93.1G EBS volume you've attached but not yet configured.

πŸ“Œ Step 3: Inspect the New Volume

Let's check the current state of the new volume.

Run:

ubuntu@ip-172-31-13-233:~$ sudo file -s /dev/nvme1n1
/dev/nvme1n1: data
Enter fullscreen mode Exit fullscreen mode

Interpretation:

  • The output data means the volume is raw and doesn't have a filesystem.

πŸ“Œ Step 4: Create a Filesystem

Time to format the new volume so we can use it.

Format with ext4 filesystem:

ubuntu@ip-172-31-13-233:~$ sudo mkfs -t ext4 /dev/nvme1n1
Enter fullscreen mode Exit fullscreen mode

Sample output:

mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 24412160 4k blocks and 6108000 inodes
Filesystem UUID: your-uuid-here
Superblock backups stored on blocks: ...
Enter fullscreen mode Exit fullscreen mode

Note: Formatting will erase all data on the volume. Since it's new, we're safe!

πŸ“Œ Step 5: Mount the New Volume

Let's make this new space accessible.

Create a mount point:

ubuntu@ip-172-31-13-233:~$ sudo mkdir /mnt/data
Enter fullscreen mode Exit fullscreen mode

Mount the volume:

ubuntu@ip-172-31-13-233:~$ sudo mount /dev/nvme1n1 /mnt/data
Enter fullscreen mode Exit fullscreen mode

Verify the mount:

ubuntu@ip-172-31-13-233:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
...
/dev/nvme1n1     92G   61M   87G   1% /mnt/data
Enter fullscreen mode Exit fullscreen mode

Awesome! /dev/nvme1n1 is now mounted at /mnt/data.

πŸ“Œ Step 6: Ensure Persistence Across Reboots

We don't want to remount the volume every time the server restarts.

Get the UUID of the new volume:

ubuntu@ip-172-31-13-233:~$ sudo blkid /dev/nvme1n1
/dev/nvme1n1: UUID="your-uuid-here" TYPE="ext4"
Enter fullscreen mode Exit fullscreen mode

Edit the fstab file:

ubuntu@ip-172-31-13-233:~$ sudo nano /etc/fstab
Enter fullscreen mode Exit fullscreen mode

Add this line at the end:

UUID=1a36ae86-3d85-4b31-addb-asdsad /mnt/data  ext4  defaults,nofail  0  2
Enter fullscreen mode Exit fullscreen mode

Save and exit.

Test the fstab entry:

ubuntu@ip-172-31-13-233:~$ sudo umount /mnt/data
ubuntu@ip-172-31-13-233:~$ sudo mount -a
Enter fullscreen mode Exit fullscreen mode

Verify it's mounted:

ubuntu@ip-172-31-13-233:~$ df -h
...
/dev/nvme1n1     92G   61M   87G   1% /mnt/data
Enter fullscreen mode Exit fullscreen mode

Success! The volume will now auto-mount on reboot.


πŸŽ‰ Conclusion

You've just expanded your EC2 instance's storage without any downtime! By attaching and configuring a new EBS volume, you can:

  • Avoid Disk Space Issues: Prevent application failures due to insufficient storage.
  • Improve Data Management: Organize data efficiently across multiple volumes.
  • Scale Seamlessly: Add storage as your needs grow, without overprovisioning upfront.

Pro Tip: 1 Always ensure you have backups before modifying disk configurations, especially in production environments.
Pro Tip: 2 To check the partition format type (filesystem type) of a disk partition on your Linux/Ubuntu system, you can use below commands. Here's how you can do it:


πŸ› οΈ Methods to Check Partition Format Type

1. Using the lsblk Command with the -f Option

The lsblk command lists information about all available or specified block devices. The -f option displays the filesystem information.

Command:

lsblk -f
Enter fullscreen mode Exit fullscreen mode

Example Output:

NAME        FSTYPE LABEL UUID                                 MOUNTPOINT
nvme0n1
└─nvme0n1p1 ext4         a1b2c3d4-e5f6-7890-abcd-sdsdsd/
nvme1n1     ext4         1a2b3c4d-5e6f-7890-abcd-sdsdsd /mnt/data
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The FSTYPE column shows the filesystem type of each partition.
  • In this example, both partitions are formatted with the ext4 filesystem.

2. Using the blkid Command

The blkid command is used to locate or print block device attributes.

Command:

sudo blkid
Enter fullscreen mode Exit fullscreen mode

Example Output:

/dev/nvme0n1p1: UUID="a1b2c3d4-e5f6-7890-abcd-1234567890ab" TYPE="ext4" PARTUUID="12345678-01"
/dev/nvme1n1: UUID="1a2b3c4d-5e6f-7890-abcd-0987654321fe" TYPE="ext4"
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The TYPE field indicates the filesystem type.
  • Replace /dev/nvme1n1 with your specific device if needed.

3. Using the file Command

The file command tests each argument to determine what it is. When used with the -s option, it reads the block or character special files.

Command:

sudo file -s /dev/nvme1n1
Enter fullscreen mode Exit fullscreen mode

Example Output:

/dev/nvme1n1: Linux rev 1.0 ext4 filesystem data, UUID=1a2b3c4d-5e6f-7890-abcd-0987654321fe
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The output shows that /dev/nvme1n1 has an ext4 filesystem.
  • This method is useful for checking unmounted partitions.

4. Using the df Command with the -T Option

The df command reports file system disk space usage. The -T option displays the filesystem type.

Command:

df -T
Enter fullscreen mode Exit fullscreen mode

Example Output:

Filesystem     Type     1K-blocks     Used Available Use% Mounted on
/dev/nvme0n1p1 ext4      41152832 32505888   6584576  84% /
/dev/nvme1n1   ext4      94371840   65536  89595904   1% /data
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The Type column indicates the filesystem type of each mounted filesystem.

πŸ“Œ Summary

  • lsblk -f: Lists block devices with filesystem information.
  • sudo blkid: Shows UUID and filesystem type of block devices.
  • sudo file -s /dev/your_device: Identifies the filesystem type of a specific device.
  • df -T: Displays mounted filesystems with their types.

πŸ”’ Permissions

  • Some commands may require superuser privileges. Use sudo when necessary.
  • Ensure you have the appropriate permissions to run these commands safely.
  • You need to change the permission to folder /mnt/data/ for non root user to able to write and read .
sudo chmod -R 777 /mnt/data/
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Tips

  • Replace /dev/nvme1n1 with the actual device name you wish to check.
  • For a quick overview, lsblk -f is often the most convenient.

Example in Context:

If you want to check the filesystem type of the new EBS volume you attached (as per your previous steps), you might run:

ubuntu@ip-172-31-13-233:~$ lsblk -f
Enter fullscreen mode Exit fullscreen mode

This will display:

NAME        FSTYPE LABEL UUID                                 MOUNTPOINT
nvme0n1
└─nvme0n1p1 ext4         a1b2c3d4-e5f6-7890-abcd-1234567890ab /
nvme1n1     ext4         1a2b3c4d-5e6f-7890-abcd-0987654321fe /data
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Conclusion

By using these commands, you can easily determine the partition format type of your disk partitions. This is especially useful when managing storage devices, setting up new filesystems, or troubleshooting disk-related issues.


Feel free to reach out if you have more questions or need further assistance!


πŸ”₯ Join the Conversation!

Have questions or tips about managing EBS volumes? Drop a comment below! If you found this guide helpful:

  • πŸ‘ Like and Share: Help others discover this resource.
  • πŸ”” Subscribe: Stay updated with more AWS tutorials.

Thanks for reading, and happy computing!


Top comments (0)