AWS BLOCK STORAGE: Types and Operations on EBS Volume
Tags: aws, devops, tutorial, ebs
Published: 2022-06-05 15:08:24

## AMAZON BLOCK STORAGE

![TYPES OF BLOCK STORAGE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rtmfatxgyzr5shctssos.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxl1hxiun8y70jxbj24j.jpg)

**Increase the volume**
- In AWS console, Select the Volume to be modified, then go to actions, click on Modify Volume

![Modify Volume](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tuqlcxyus568vr0d402v.jpg)

![Modify Volume 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kghd4ryly6gqxxq6zka7.jpg)
>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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cdtbsg3pkkifck0290i.jpg)
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q44tyx7qvwy0zgfembvp.jpg)
- 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
```
- 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
```
If the XFS tools are not already installed, you can install them as follows.
```sudo yum install xfsprogs```


![growfs command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxtezhh8swsv94z4c39h.jpg)

![resize](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqw8e2ezkcctwb5durts.jpg)

-- [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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ly4l82zmbh0amvuagay.jpg)
>
 
- Select the Volume, click on Actions, Select Attach Volume, then select the instance on the same zone.

![Attach Volume](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zimhn3r4kcud1z14vrg6.jpg)
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3f61iulayevl310g19f.jpg)
- 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
```
then save it and exit from editor.

![vi editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a43gnrs5yzqquvm50xki.jpg)

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfalk4mqrwccoty2ekyq.jpg)
- > 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pv6w7mh1hcsw88u0i78.jpg)
- 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
```

#### 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>
```

- 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.