DEV Community

Rudra Prasad Behera
Rudra Prasad Behera

Posted on

AWS BLOCK STORAGE: Types and Operations on EBS Volume

AMAZON BLOCK STORAGE

TYPES OF BLOCK STORAGE

Operations On Ebs Volume

1.Extend/Increase the Volume

  • If you are familiar with Amazon EC2 and EBS volume, you might have experiences modifying the size of EBS volume. AWS provide an easy way to increase the size of your EBS volume. In fact, we could increase it without detaching the volume or restarting the instance. That’s quite a nice work done there because we would not need to have a downtime for our instance. Let’s exercise. Assume we have volume name my-volume.

Create Snapshot
It is best practice to create a snapshot of your volume in case something bad happens.

  • Go to Snapshots from your EC2 Dashboard.
  • Click Create Snapshot.
  • Choose the Resource type - volume
  • Add the Volume Id - Select the volume for extension.
  • Add key : Name and value : snapshot-my-volume.
  • Click button Create Snapshot.
  • Go grab some snack while it is in progress.

Snapshot

Increase the volume

  • In AWS console, Select the Volume to be modified, then go to actions, click on Modify Volume

Modify Volume

Modify Volume 2

Note:-
To know which volume is associated with your instance, check for Attached instances in that Volume and match it with the EC2 Instance ID.

  • Then increase the volume as per your requirement, then click on modify.

After you finish modifying volume, you need to extend OS file system in order to see your increased volume size.

  • In Xshell, connect to the instance by using SSH Client (Select the instance, then click on Connect go to SSH Client, then copy the url and paste it on Xshell, and lastly when asked give the .pem if you have created it) and type the command lsblk to see the increased volume.

lsblk command

  • Type df -hT (it will show the volume along with its type of volume), we can only see the previous volume(i.e. the increased volume is still not updated.)

df -h command

  • For volumes that have a partition (type lsblk to know the partition), such as the volumes shown in the previous step, use the growpart command to extend the partition. Notice that there is a space between the device name and the partition number.
sudo growpart /dev/xvda 1
Enter fullscreen mode Exit fullscreen mode
  • To modify it, we have a command To extend the file system on each volume, use the correct command for your file system, as follows:

-- [XFS volumes] To extend the file system on each volume, use the xfs_growfs command. In this example, / and /data are the volume mount points shown in the output for df -h.

sudo xfs_growfs -d /
sudo xfs_growfs -d /data
Enter fullscreen mode Exit fullscreen mode

If the XFS tools are not already installed, you can install them as follows.

sudo yum install xfsprogs

growfs command

resize

-- [ext4 volumes] To extend the file system on each volume, use the resize2fs command.
resize2fs /<path-volume>/<volume-folder-name> e.g. resize2fs /dev/xvds

2.Adding Extra Disk to the instance

  • Navigate to Volumes, Click on Create Volumes

NOTE:-
While creating volume, the Availability zone must be in the same zone as the deployed instance.

Create Volume

  • Select the Volume, click on Actions, Select Attach Volume, then select the instance on the same zone.

Attach Volume

  • Open Xshell, connect to the instance, type command df -h to know how much volume is in use now.

    NOTE:-
    The attached volume is still not in use, for that we have to mount the volume to the instance. Before mounting the volume we have to format the disk first.

  • Make a directory in the root by using the command mkdir /<folder-name> eg. mkdir /rudra

  • Change to that dir using cd /rudra (it's the directory where the volume is to be mounted).

  • To Format the volume by using the command mkfs.ext4 /<vol-path> eg. mkfs.ext4 /dev/xvdf
    (Type lsblk command to know the volume path)

  • Type the command to mount the volume mount <vol-path> <mounting-directory> e.g. mount /dev/xvdf /rudra/

Xshell command

  • So, if we stop an instance and again start it, the volume stays associated with it but the extra volume is not mounted. Then we have to again mount it to the server. To solve this problem: Type the command vi /etc/fstab and them insert go to the last line and insert
/dev/xvdf         /rudra          ext4 defaults 0 0
Enter fullscreen mode Exit fullscreen mode

then save it and exit from editor.

vi editor

3.Volume Delete On Termination

  • Launch an EC2 instance, and on Add Storage, we can set the Volume Type - GP2 or GP3 or anything else as per your requirements.
  • Now Set delete on termination - Enable
  • Launch another EC2 instance, and set delete on termination - Disable

Delete On Termination

  • > Now if you terminate both the servers, the server where the delete on termination is enabled both the server and the volume gets deleted whereas, where the delete on termination is disabled only the server is terminated but the volume does not gets terminated. >

4.Attaching Volume to Different Instances

Detach a Volume from an instance and attach it to another instance

  • Go to root where the folder or directory where the volume is attached.
  • Then for mounting type umount /<directory-name> e.g. umount /rudra

Umount

  • But still, the volume is associated with the instance to know the type lsblk command
  • Then in AWS Console select the volume and go to actions then force deattach
  • Then in Actions, go to Attach and then attach the volume to another instance.
  • In Xshell, type the following commands to mount it:-
mkdir \<directory-name>
mount <path> /<directory-name>
// Example 
mkdir \rudra
mount /dev/xvdf /rudra
Enter fullscreen mode Exit fullscreen mode

5.Setting Volume to Another Instance in another Availability Zone

  • First unmount the volume using created in a instance attached to it and deattach it in the Volumes in AWS Console using Detach Volume.
  • Then create a Snapshot from Actions menu.
  • Then go to Snapshots Tab, then select the created snapshot, go to Actions and click Create Volume from Snapshot and select the Availability Zone same as the new instance to avoid conflict.
  • Then navigate to Volumes, Attach the Volume to the instance and follow the previous steps to mount it into the server.

6.Reduce the Volume

NOTE:
To copy all the files from a volume to another use a command

rsync -aHAXxSP /<dir-from-copy> /<dir-to-copy>
Enter fullscreen mode Exit fullscreen mode
  • Create a new reduced volume in Volumes (i.e. how much of GB storage volume you want) in the same Availability Zone.
  • Then use the above rsync command to copy the contents from another volume. Then umount the other volume and deattach it from the instance in the Volumes in AWS Console.

Top comments (0)