DEV Community

Omkar Sharma
Omkar Sharma

Posted on Edited on

Mounting and Making EBS Volume Persistent on EC2: Step-by-Step Guide

๐Ÿ”ง Step 1: Create an EBS Volume

  1. Go to the EC2 Dashboard in AWS Console.
  2. In the left panel, click Elastic Block Store > Volumes.
  3. Click Create Volume.
  4. Fill in:
    • Size (e.g., 10 GiB)
    • Availability Zone (Must match your EC2 instance)
    • Leave other options default
  5. Click Create Volume.

omkarsharma2821

๐Ÿ”— Step 2: Attach Volume to EC2 Instance

  1. Select the newly created volume.
  2. Click Actions > Attach Volume.
  3. Choose your instance and device name (e.g., /dev/xvdf).
  4. Click Attach.

omkarsharma2821

omkarsharma2821

omkarsharma2821

omkarsharma2821

The volume status changes from Available โž In-use.

๐Ÿงช Step 3: Verify the Volume is Attached

lsblk
Enter fullscreen mode Exit fullscreen mode

Output will show /dev/xvdf (or similar) listed without a mountpoint.

omkarsharma2821

๐Ÿงฑ Step 4: Format the Volume with ext4

sudo mkfs -t ext4 /dev/xvdf
or
sudo mkfs.ext4 /dev/xvdf
Enter fullscreen mode Exit fullscreen mode

omkarsharma2821

โ“ Why Do We Format the Volume?

When you create a new EBS volume, it's like a brand-new hard drive โ€” it has no filesystem. Formatting it is essential because:

  • It defines how data is stored and organized.
  • It prepares the volume to store files and directories.
  • It enables the system to mount and use the volume.

โš ๏ธ Without formatting, the volume cannot be used to store any data.

๐Ÿ“‚ Step 5: Create a Mount Directory

sudo su : to swtich to super user
cd / : to go to home directory
mkdir /your-dir-name : to create new directory
Enter fullscreen mode Exit fullscreen mode

Learn Linux commands from scratch

omkarsharma2821

Basis mkdir /test mkdir test
Path type Absolute path Relative path
Location Creates test in the root directory (/). Creates test in the current working directory.
Starting point Starts from / (root). Starts from the current directory.
Example current directory /home/ubuntu /home/ubuntu
Result /test /home/ubuntu/test
Permission May require sudo. Usually requires write permission in the current directory.
Use case When you want to create a directory at a specific absolute location. When you want to create a directory in your current location.

Easy trick to remember:

  • /test โ†’ Root directory.
  • test โ†’ Current directory.

Example:

pwd
# Output: /home/ubuntu

mkdir test
# Creates /home/ubuntu/test

mkdir /test
# Creates /test
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Step 6: Mount the Volume

mount /dev/xvdf /test
Enter fullscreen mode Exit fullscreen mode

omkarsharma2821

if not want to mount at /

omkarsharma2821

Verify the mount:

df -h
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Step 7: Make the Mount Persistent (After Reboot)

To make the mount persistent, add an entry to /etc/fstab.

  1. First, get the UUID:
blkid
Enter fullscreen mode Exit fullscreen mode

omkarsharma2821

  1. Open the fstab file:
if vi /etc/fstab
Enter fullscreen mode Exit fullscreen mode
Note:
press "i" to insert mode
press "esc" to return 
press ":wq!" to write and exit
Enter fullscreen mode Exit fullscreen mode

omkarsharma2821

๐Ÿ”„ Step 8: Reboot and Verify

reboot
Enter fullscreen mode Exit fullscreen mode

After reboot, connect again and run:

df -h
Enter fullscreen mode Exit fullscreen mode

You should see /test mounted. โœ…

To increase the size of the same EBS volume, you must first modify the volume from the AWS Management Console (AWS Portal) and then extend the partition and file system inside the EC2 instance, if required.

sudo resize2fs /dev/nvme1n1
Enter fullscreen mode Exit fullscreen mode

omkarsharma2821

๐Ÿงน Optional: Test Persistence

You can unmount and remount to test:

umount /test
mount -a
df -h
Enter fullscreen mode Exit fullscreen mode
mount -a
Enter fullscreen mode Exit fullscreen mode
  • Reads the file /etc/fstab.
  • Mounts all configured file systems that are not currently mounted and are eligible for automatic mounting.
  • Commonly used to test /etc/fstab entries without rebooting the server.

Top comments (0)