How to mount another EBS as /var on EC2 (ubuntu)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for new instance type.