Created
December 20, 2018 21:33
-
-
Save omar-yassin/5689d8a2f2fbe935330f9c8bafcfbcf8 to your computer and use it in GitHub Desktop.
expand ebs and xfs filesystem
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 | |
# ec2 metadata | |
instance_document=$(curl -s 'http://169.254.169.254/latest/dynamic/instance-identity/document') | |
instance_id=$(echo ${instance_document} | jq -r .instanceId) | |
instance_region=$(echo ${instance_document} | jq -r .region) | |
# AWS COMMAND | |
AWS_BIN="$(which aws) --region ${instance_region}" | |
# ec2 attached volume -exclude root | |
attached_volumes=$(${AWS_BIN} ec2 describe-volumes --filters Name=attachment.instance-id,Values=${instance_id} | jq -r '.Volumes[] | [.Attachments[].VolumeId, .Attachments[].Device, .Size] | @tsv' | grep -v '/dev/sda1') | |
# volumes defined in tag | |
# aws_device_path os_device_path os_mount_point | |
# /dev/sdi /dev/xvdi /data | |
volume_tags=$(${AWS_BIN} ec2 describe-tags --filters Name=resource-id,Values=${instance_id} | jq -r '.Tags[] | select(.Key == "ebs_volumes")|.Value' | jq -r '.volumes[] | [.mount_letter, .mount_path] | @tsv' | awk '{print "/dev/sd"$1, "/dev/xvd"$1, $2}') | |
merged_volume_list="/tmp/merged_volume_list" | |
echo "" > ${merged_volume_list} | |
# merge both lists and print to user | |
echo "Current EBS mount(s) that can be expanded:" | |
IFS=$'\n' | |
option_count=1 | |
for volume in ${volume_tags} ; do | |
v=$(echo "${volume}" | awk '{print $1}') | |
volume_id=$(echo "${attached_volumes}" | grep "${v}" | awk '{print $1}') | |
volume_size=$(echo "${attached_volumes}" | grep "${v}" | awk '{print $3}') | |
echo -e "[${option_count}] ${volume_id} ${volume}" >> ${merged_volume_list} | |
echo -e "[${option_count}] EBS_SIZE: [$volume_size] EBS_ID: [${volume_id}] EBS_ALIASES: [${volume}]" | |
((option_count++)) | |
done | |
unset IFS | |
echo "" | |
echo "Enter which volume to expand (enter integer):" | |
read selection | |
selected_volume=$(cat ${merged_volume_list} | grep "^\[${selection}]") | |
if [ -z "$selected_volume" ] ; then | |
echo "ERROR: selection does not exits" | |
exit 99 | |
fi | |
# Check if a number | |
case $selection in | |
''|*[!0-9]*) | |
echo -e "[ERROR] Integer not detected, exiting.." | |
exit 99 | |
;; | |
*) | |
;; | |
esac | |
echo "" | |
echo "Enter new size in GB for volume (enter integer):" | |
read new_size | |
# Check if a number | |
case $new_size in | |
''|*[!0-9]*) | |
echo -e "[ERROR] Integer not detected, exiting.." | |
exit 99 | |
;; | |
*) | |
;; | |
esac | |
echo "" | |
ebs_volume_id=$(echo "${selected_volume}" | awk '{print $2}') | |
device_path=$(echo "${selected_volume}" | awk '{print $4}') | |
mount_point=$(echo "${selected_volume}" | awk '{print $5}') | |
echo "[*] Attempting to expand volume with the following properties:" | |
echo "ebs_id: ${ebs_volume_id}" | |
echo "device_path: ${device_path}" | |
echo "mount_point: ${mount_point}" | |
echo "NEW_SIZE: ${new_size}" | |
echo "" | |
echo "[STEP 1 OF 3] Expand EBS volume" | |
${AWS_BIN} ec2 modify-volume --volume-id ${ebs_volume_id} --size ${new_size} | |
# wait a bit | |
sleep 120 | |
echo "" | |
echo "[STEP 2 OF 3] Expanding partition" | |
growpart ${device_path} 1 | |
sleep 2 | |
echo "" | |
echo "[STEP 3 OF 3] Expanding filesystem" | |
xfs_growfs -d ${mount_point} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment