Skip to content

Instantly share code, notes, and snippets.

@garyritchie
Forked from stingh711/mount_ebs.sh
Last active December 19, 2019 14:23
Show Gist options
  • Save garyritchie/98bde7bef397ced168dac328150ad0f1 to your computer and use it in GitHub Desktop.
Save garyritchie/98bde7bef397ced168dac328150ad0f1 to your computer and use it in GitHub Desktop.
How to mount another EBS as /var on EC2 (ubuntu)
#!/bin/bash
#attach the EBS to /dev/sdf before running it
UPDATE=yes
while getopts d:u option
do
case "${option}"
in
d) DEVICE=${OPTARG};;
u) UPDATE=${OPTARG};;
esac
done
findroot=$(findmnt -n -o SOURCE --target /)
findvar=$(findmnt -n -o SOURCE --target /var)
if [ "$findroot" = "$findvar" ]; then
echo we need to move /var
else
echo var already separate partition
exit
fi
echo "Using $DEVICE for /var"
#format EBS
mkfs -t ext4 $DEVICE
#copy original /var to $DEVICE
mkdir /mnt/new
mount $DEVICE /mnt/new
cd /var
cp -ax * /mnt/new
cd /
mv var var.old
#mount EBS as new /var
umount $DEVICE
mkdir /var
mount $DEVICE /var
if [ "$UPDATE" = "yes" ]
## Get ID for fstab entry
# lsblk -o +UUID
fs_uuid=$(lsblk -no UUID $DEVICE)
#update fstab file to mount EBS on system startup
echo "$fs_uuid /var ext4 noatime,nofail 0 0" >> /etc/fstab
else
echo "CAUTION: Not updating /etc/fstab."
fi
@garyritchie
Copy link
Author

Updated for new instance type.

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