Skip to content

Instantly share code, notes, and snippets.

@holmberd
Last active February 21, 2024 14:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save holmberd/c7585b019e4533d871509b431c130278 to your computer and use it in GitHub Desktop.
Save holmberd/c7585b019e4533d871509b431c130278 to your computer and use it in GitHub Desktop.
Attaching and Mounting EBS Volume to an EC2 Instance

Attaching and Mounting EBS Volume to EC2 Instance

Create

  • Create new EBS Volume in the correct Availability Zone
  • Attach new EBS Volume to EC2 Instance

Mount

  • Check filesystem type (ext4) sudo file -s /dev/xvd*
  • Find new disk sudo fdisk -l
  • Create filesystem sudo mkfs -t ext4 /dev/xvdf
  • Mount new fs sudo mount /dev/xvdf1 /mnt/vol -t ext4
  • Move/Bind
    sudo mount --bind /mnt/vol /home/user/vol available in both
    sudo mount --move /mnt/vol /home/user/vol available in moved
  • Confirm: df -h

Quick Automatic Mount At Boot

EBS Devices become unmounted again when the EC2 Instance reboots.
Append to the file /etc/rc.local the mount command used:
sudo mount /dev/xvdf1 /vol -t ext4 above exit 0

Stable UUID Automatic Mount At Boot

  • Find UUID for device: ls -al /dev/disk/by-uuid/
  • Create backup of fstab: cp /etc/fstab /etc/fstab.orig
  • Add entry to fstab: sudo vim /etc/fstab  UUID=de9a1ccd-a2dd-44f1-8be8-0123456abcdef /mnt/data-01 ext4 defaults,nofail 0 2
  • Check mount errors by mounting all filesystems in /etc/fstab: sudo mount -a

Warm up a new snapshot EBS volume ("lazily")

Tips

Install the ebsmount package and create a hidden directory on the EBS volume and configure the system to automount using udev when the EBS volume is attached to the EC2 instance.

Practice

  • Databases mount to /var/data
  • Web files /srv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment