DEV Community

mostafamedhat1983
mostafamedhat1983

Posted on • Edited on

HOW TO ATTACH AND USE EBS VOLUME ON EC2 LINUX INSTANCE- A SIMPLE GUIDE

To create new EBS volume navigate to EC2, Elastic Block Store, Volumes then click Create volume.

Image description

-choose the desired volume type and size, make sure the EBS volume is in the same AZ as the EC2 instance it will be attached to. Then click Create.

Image description

-choose the EBS volume you created, Actions then Attach volume

Image description

-select the EC2 instance you want to attach the EBS volume to it. EBS volume name will be /dev/sdf or /dev/xvdf by default

Image description

-EBS volume is successfully attached

Image description

-connect to your EC2 instance (to use EC2 instance connect you must allow SSH in the inbound rules of attached security group).

Image description

Image description

sudo lsblk
view your available disk devices and their mount points.

Image description
the EBS volume we attached is xvdf (1G) and it has no mount point.

sudo lsblk -f
get information about all of the devices attached to the instance.

Image description

sudo file -s /dev/xvdf
show file system for disk xvdf

Image description

sudo mkfs -t xfs /dev/xvdf
create a file system xfs on the volume xdvf
If you get an error that mkfs.xfs is not found, use the following command to install the XFS tools and then repeat the previous command: sudo yum install xfsprogs

Image description

sudo mkdir /ebs
create directory ebs, we will use this directory to mount our EBS volume
sudo mount /dev/xvdf /ebs
mount the volume at the directory you created

Image description

**To mount an attached EBS volume on every system reboot, add an entry for the device to the /etc/fstab file.

You can use the device name, such as /dev/xvdf, in /etc/fstab, but it is recommended to use the device's 128-bit universally unique identifier (UUID) instead. Device names can change, but the UUID persists throughout the life of the partition.**

sudo cp /etc/fstab /etc/fstab.orig
Create a backup of your /etc/fstab to restore it in case of any problems

Image description

sudo blkid
find the UUID of the device

Image description
EBS volume UUID is 0bce0506-dd1d-4ee7-9aef-642eac6501bb

sudo vim /etc/fstab
edit the fstab file
press i to enter insert mode
Add the following entry to /etc/fstab
UUID=0bce0506-dd1d-4ee7-9aef-642eac6501bb /ebs xfs defaults,nofail 0 2
0 to prevent the file system from being dumped, and we specify 2 to indicate that it is a non-root device.

Image description

esc
:wq
w stands for write (save) and q stands for quit.

Image description

to check unmout the device then mount all file systems in /etc/fstab
sudo umount /ebs
sudo mount -a

Image description

Errors in the /etc/fstab file can render a system unbootable. Do not shut down a system that has errors in the /etc/fstab file

restore fstab backup in case of error
sudo mv /etc/fstab.orig /etc/fstab

Top comments (2)

Collapse
 
ahmedattia profile image
Ahmed Attia

Great insights

Collapse
 
waleedelginady profile image
Waleed Elginady

Nice Job