Skip to content

Instantly share code, notes, and snippets.

@adilsoncarvalho
Last active November 19, 2023 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adilsoncarvalho/2bfbbc76b8dfb8b39c1c to your computer and use it in GitHub Desktop.
Save adilsoncarvalho/2bfbbc76b8dfb8b39c1c to your computer and use it in GitHub Desktop.
Mounting volumes attached to a EC2 instance

Mounting volumes attached to a EC2 instance

Quick and dirty notes from the original AWS Documentation.

TL;DR

[[ ! -z $(sudo file -s /dev/xvdf | grep -e 'data$') ]] && sudo mkfs -t ext4 /dev/xvdf
[[ ! -z $(sudo file -s /dev/xvdg | grep -e 'data$') ]] && sudo mkfs -t ext4 /dev/xvdg
sudo mkdir -p /mnt/{data,tmplog}
sudo mount /dev/xvdf /mnt/data
sudo mount /dev/xvdg /mnt/tmplog
echo -e "/dev/xvdf\t/mnt/data\text4\tdefaults,nofail\t0\t2" | sudo tee --append /etc/fstab
echo -e "/dev/xvdg\t/mnt/tmplog\text4\tdefaults,nofail\t0\t2" | sudo tee --append /etc/fstab
sudo mount -a

NOTE: this assumes two volumes, one to be used for data and the other for logs and tmp that are uninitialized. We're checking first if the is a filesystem on the volume to then initialize it with a new filesystem.

Attach the volumes

Attach your volumes to the instance using the names /dev/xvdf, /dev/xvdg and so on.

Check if your volumes were properly attached

Use the lsblk command to check the attached volumes.

$ lsblk

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
`-xvda1 202:1    0   8G  0 part /
xvdf    202:80   0  20G  0 disk
xvdg    202:96   0  20G  0 disk

Preparing the volume

Before you can actually use it you must get it ready with a filesystem on it.

Checking the volume

For each volume run the command sudo file -s /dev/xvdf to inspect if it is raw or was previously initialized.

[ec2-user ~]$ sudo file -s /dev/xvdf
/dev/xvdf: data

raw

[ec2-user ~]$ sudo file -s /dev/xvda1
/dev/xvda1: Linux rev 1.0 ext4 filesystem data, UUID=1701d228-e1bd-4094-a14c-8c64d6819362 (needs journal recovery) (extents) (large files) (huge files)

already initialized

Putting a filesystem on it

You will use mkfs to get a filesystem on it.

sudo mkfs -t ext4 /dev/xvdf

Adding it to the system

Mounting

Create a mounting point and mount it there

sudo mkdir -p /mnt/data
mount /dev/xvdf /mnt/data

Allowing it to mount on system startup

To allow the system to automatically mount the volumes on reboot you must add the following lines to the /etc/fstab file.

/dev/xvdf       /data     ext4    defaults,nofail        0       2
/dev/xvdg       /tmplog   ext4    defaults,nofail        0       2

Right after that you must check if the file is ok by running the following command:

sudo mount -a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment