How to Add a Second Disk to an EC2 Instance in AWS – scottlinux.com | Linux Blog

How to Add a Second Disk to an EC2 Instance in AWS – scottlinux.com | Linux Blog

In AWS, one can hot-add a second disk or volume to an existing EC2 Linux instance. Here is how to do that!


1. First, create the new volume

Navigate to: EC2 > Elastic Block Store > Volumes

Create desired additional Volume in the availability zone of the EC2 instance.

01-aws-new-volume

Next, go to Action > Attach Volume

02-aws-new-volume

Search for the name of the EC2 instance and attach, accepting default device name listed in the dialog.

03-aws-new-volume

Again, this can be attached hot (live) to a running Linux instance or when the instance is powered off.

If you do not see the EC2 instance listed, start over and make sure to create the volume in the same availability zone as the EC2 instance.


2. Next, ssh into the EC2 instance to verify attached volume

When hot adding, run sudo dmesg and check the output which will have something like this:

[12099233.050459] blkfront: xvdf: barrier or flush: disabled  [12099233.054541]  xvdf: unknown partition table  

Ah-ha! Our new volume is /dev/xvdf (Note the F as in frank!)

Compare that (in this example) to blkid output (shows current volumes, in this case existing /dev/xvda, /dev/xvdb):

admin@ip-10-31-243-35:~$ sudo blkid  /dev/xvda1: UUID="8d0f0ad5-0e3d-40cd-9af5-fb68a7f47ea6" TYPE="ext4"   /dev/xvdb: UUID="4e341886-5a48-42d9-94df-537aafa96f81" TYPE="ext3"  

One can also run an ls -l to see the volumes connected to the instance, and their device name (in this example, /dev/xvdf):

$ ls -l /dev/xvd*  brw-rw---T 1 root disk 202,  0 Dec  7 14:31 /dev/xvda1  brw-rw---T 1 root disk 202,  0 Dec  7 14:31 /dev/xvdb  brw-rw---T 1 root disk 202, 80 Apr 26 16:25 /dev/xvdf  

3. Finally, format and mount the new volume

Ok now that the new volume name has been determined, format the new volume and mount it.

Note: This is following the above example, where my new volume is /dev/xvdf. The device on your instance may be different!

Format ext4:

sudo mkfs.ext4 /dev/xvdf  

Make a mount point:

sudo mkdir /mnt/data  

Mount the volume there:

sudo mount /dev/xvdf /mnt/data/  

Set permissions for the default EC2 user (in this case, for an ubuntu EC2 instance):

sudo chown -R ubuntu:ubuntu /mnt/data/  

Verify new volume:

ubuntu@ip-10-179-146-76:/mnt/data$ sudo blkid  /dev/xvda1: LABEL="cloudimg-rootfs" UUID="8d0f0ad5-0e3d-40cd-9af5-fb68a7f47ea6" TYPE="ext4"   /dev/xvdb: UUID="4e341886-5a48-42d9-94df-537aafa96f81" TYPE="ext3"   /dev/xvdf: UUID="bf7ebcf6-63bc-4ba9-ba26-6a6450e0cfff" TYPE="ext4"   

Also one can verify by running `mount` or `df -h` to see the new volume.


4. (Optional) Add new volume to /etc/fstab to auto-mount on boot

If the new volume is permanent, add an entry to /etc/fstab for the new volume.

Edit /etc/fstab and add a new entry for the UUID of the new volume, including the desired mount point.

Example:

LABEL=cloudimg-rootfs   /        ext4   defaults        0 0  /dev/xvdb       /mnt    auto    defaults,nobootwait,comment=cloudconfig 0       2  UUID=bf7ebcf6-63bc-4ba9-ba26-6a6450e0cfff /mnt/data     ext4    defaults,noatime,errors=remount-ro 0 0  

Done.