Thursday, July 21, 2016

Setting up filesystem on Amazon EBS Volume

Setting up a filesystem on Amazon EBS volume and verifying the persistance of files created on filesystem after instance is terminated using CLI

In order to complete the desired task, I have chosen to create two instances of Amazon-linux and two instances of NetBSD using AWS CLI on my linux machine. A complete step-by-step guide to create an instance can be found on my previous blog. [After creating instances, follow the steps written below in similar order.] 

1.Create a 1GB volume using AWS CLI

aws ec2 create-volume --size 1 --availability-zone us-west-2b --volume-type gp2 

The availability zone can be found from "aws ec2 describe-instances" command.

2.Now, we can check the available volumes by using below command, the newly created volume will be shown as "available"

aws ec2 describe-volumes

3.Now, attach the newly created volume to desired OS instance by using below command

aws ec2 attach-volume --volume-id vol-1234abcd --instance-id i-abcd1234 --device /dev/sdf

Where /dev is directory containing special device files and /dev/sdf indicates SCSI drive exposed to instance

4.Now Login to your desired OS instance using SSH and run command "dmesg" to obtain information about the hardwares on system in NetBSD, and "cat /proc/partitions" to obtain information about newly added partition in Amazon-linux. These command will show the recently attached volumes with device location

5.Now create a filesystem on newly attached partition, commands to create filesystem on NetBSD and Amazon-Linux are shown below,

newfs /dev/xbd2a 
(Creates a default bsd filesystem on the given volume. It is optional to lable disk using disklabel(8))

mkfs -t ext4 /dev/xvdf
(Creates ext4 filesystem on the given disk block on Amazon linux)

6.Mounting is the process by which we make filesystem available to system. In every linux system "/mnt" is the generic mount point under which we can mount our filesystem or devices. Create a directory (optional) in "/mnt" and mount the newly created filesystem on the created directory. The commands to mount filesystem is as follows;

mount /dev/xbd2a /mnt/example (NetBSD)
mount /dev/xvdf /mnt/example (Amazon-linux) 

7.Now create a text file in the mounted directory so that when we detach the volume and attach it to other instance, we get to verify whether the volume attached is the same or not;

vi test.txt

8.Now unmount the filesystem using commands shown below;

umount /dev/xbd2a /mnt/example (NetBSD)
umount /dev/xvdf /mnt/example (Amazon-linux)

9.Exit from the instance and detach the volume using below command;

aws ec2 detach-volume --volume-id vol-1234abcd

10.Now attach the volume to other instance of same OS, if you have detached volume from NetBSD then attach it to other NetBSD instance using command shown below;

aws ec2 attach-volume --volume-id vol-1234abcd --instance-id i-abcd1234 --device /dev/sdf

11.Now login to your instance where you attached the volume through ssh and mount the volume as shown in step 6 as per your OS.

12.You can verify if the volume is same by listing files under mounted directory through "ls" command.

This blog helps in learning how we can attach a new volume to a linux machine, create a filesystem on it and how we can use the mounted filesystem as regular directory in OS.



No comments:

Post a Comment