DEV Community

meera123
meera123

Posted on • Edited on

HOW TO ATTACH AND MOUNT AN EXTRA AMAZON EBS VOLUME TO AN EXISTING INSTANCE?

Creating new EBS volumes can be done in AWS, you can attach them to instances to add additional storage. Mounting an EBS volume to a folder inside the instance is required to use it as
storage inside the instance

MOUNT VOLUME TO THE INSTANCE

Step 1:Create an EC2 instance if you don't have any.
Step 2:Go to the volume section and click on create volume.

Image description
Step 3:Select the volume and click on action button, then select attach button.
Note:Make sure the EBS volume and instance are in the same availability zone
Step 4:Select the instance where you want it to attach and click on attach.Your volume has now been attached to the instance.
Now connect to the root by throwing below commands

_sudo su_
_cd_
Enter fullscreen mode Exit fullscreen mode

Step 6:You can now list the disk available on your EC2 instance by firing the below command.

_lsblk_
Enter fullscreen mode Exit fullscreen mode

Image description

Step 7:For a detailed description of the volumes throw below commands

_fdisk -l_
Enter fullscreen mode Exit fullscreen mode

Step 8:Now check if the disk is mounted

_df -h_
Enter fullscreen mode Exit fullscreen mode

Step 9:Using the following command, format the volume as ext4 filesystem.

_mkfs.ext4 /dev/xvdf_
Enter fullscreen mode Exit fullscreen mode

Image description

Note you can also format it into the xfs filesystem.(Usually used for larger volumes)
Step 10:Create extra directory of your choice to mount out new ext4 volume. I am using the name /extra. You can name it something of your choice.

_mkdir /extra_
Enter fullscreen mode Exit fullscreen mode

Step 11:Mount to the volume to the /extra directory using following command.

_mount /dev/xvdf /extra_
Enter fullscreen mode Exit fullscreen mode

Image description
Note: With _df -h_, you can check
whether it is mounted or not.

_cd /extra_
Enter fullscreen mode Exit fullscreen mode

By using the above command you can go into the extra directory and can create fil e
To unmount the volume use the unmount command as shown below

_unmount /dev/xvdf_
Enter fullscreen mode Exit fullscreen mode

Now here is the problem is if you stop and start your instance the data will be gone... So we have to permanently mount the volume..

PERMANENTLY MOUNT THE VOLUME TO THE INSTANCE

Step 1:After mounting your volume click the following command

_blkid_
Enter fullscreen mode Exit fullscreen mode

Step 2:After clicking this you will get a UUID. Copy that UUID of your extra
volume.

Image description

Step 3:Then login to vi editor firing below command

_vi /etc/fstab_
Enter fullscreen mode Exit fullscreen mode

** paste the UUID and the path where
you want to mount and the filesystem you
format**

Image description

That's it your file is permanently mounted. Now if you stop your instance and start again your data won't be lost.

That's it you can mount and unmount as much as file of your choice by following these steps.

If you have any doubt feel free to comment.

Top comments (0)