Attach and mount a new EBS volume to Amazon Linux 2 EC2 Instance

Vignesh A Sathiyanantham
Mar 24 · 3 min read

AWS allows you to create new EBS volumes and you can attach it to EC2 instance for extra storage. However, to make it usable as storage inside the instance, you need to mount it to a specific folder.

First create a new EBS volume and attach to the EC2 Instance by providing the preferred size , type and device mountpoint.

Connect to the Amazon Linux EC2 instance using the ec2-user Read here about default username on the EC2 instances

Read here about setting up the password authentication for Linux

Identify and format the disk with ext4

Run the below command to list the disk you attached to your instance.

[ec2-user@ip-172-31-40-130 ~]$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 8G 0 disk
└─xvda1 202:1 0 8G 0 part /
xvdb 202:16 0 16G 0 disk

Check if the volume has any data using the following command.

[ec2-user@ip-172-31-40-130 ~]$ sudo file -s /dev/xvdb
/dev/xvdb: data

If the response shows “/dev/xvdb: data”, it means your volume is empty.

Format the volume to ext4 filesystem

[ec2-user@ip-172-31-40-130 ~]$ sudo mkfs -t ext4 /dev/xvdb
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1048576 inodes, 4194304 blocks
209715 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
128 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Mount the Filesystem to the mountpoint

Create a new folder to mount the filesystem

[ec2-user@ip-172-31-40-130 ~]$ sudo mkdir /datadir

Mount the file system on the created directory

[ec2-user@ip-172-31-40-130 ~]$ sudo mount /dev/xvdb /datadir/

Confirm the Disk mounted

[ec2-user@ip-172-31-40-130 ~]$ cd /datadir/
[ec2-user@ip-172-31-40-130 datadir]$ df -h .
Filesystem Size Used Avail Use% Mounted on
/dev/xvdb 16G 45M 15G 1% /datadir

Setup Auto mount on every start

Open /etc/fstab file and make an entry like below

/dev/xvdb /datadir ext4 defaults,nofail 0 0

/dev/xvdb                                     /datadir    ext4   defaults,nofail   0   0

And verify if there are any errors by running mount -a

[ec2-user@ip-172-31-40-130 datadir]$ sudo mount -a
[ec2-user@ip-172-31-40-130 datadir]$

If the above command shows no error, your FSTAB command is correct and you are good to go


Originally published at asvignesh.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬.

To join our community Slack team chat 🗣️ read our weekly Faun topics 🗞️, and connect with the community 📣 click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

Vignesh A Sathiyanantham

Written by

7 years of experience in building data protection, copy data management and storage management applications and plugins for Private datacenter and public cloud

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts. Medium’s largest DevOps publication.

More From Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade