Skip to content

Instantly share code, notes, and snippets.

@pcolazurdo
Last active March 23, 2021 08:52
Show Gist options
  • Save pcolazurdo/c855407a7d31e1b29d4f17215fb0a55b to your computer and use it in GitHub Desktop.
Save pcolazurdo/c855407a7d31e1b29d4f17215fb0a55b to your computer and use it in GitHub Desktop.
Resize Cloud9 storage
#!/bin/bash
# Specify the desired volume size in GiB as a command-line argument. If not specified, default to 20 GiB.
SIZE=${1:-20}
# Install the jq command-line JSON processor.
sudo yum -y install jq >/dev/null
# Get the ID of the envrionment host Amazon EC2 instance.
INSTANCEID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
LOCALREGION=$(curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)
# Get the ID of the Amazon EBS volume associated with the instance.
VOLUMEID=$(aws ec2 describe-instances --instance-id "${INSTANCEID}" --region "${LOCALREGION}" | jq -r .Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId )
# Resize the EBS volume.
aws ec2 modify-volume --volume-id "${VOLUMEID}" --size "${SIZE}" --region "${LOCALREGION}"
# Wait for the resize to finish.
while [ "$(aws ec2 describe-volumes-modifications --volume-id "${VOLUMEID}" --region "${LOCALREGION}" --filters Name=modification-state,Values="optimizing","completed" | jq '.VolumesModifications | length')" != "1" ]; do
sleep 1
done
xfs_resize() {
sudo xfs_growfs -d /
}
ext4_resize(){
# Expand the size of the file system.
sudo resize2fs /dev/"${PART}"
}
DISK=$(lsblk | grep disk | cut -d\ -f1 | head)
PART=$(lsblk | grep "part /" | cut -d\ -f1 | cut -d─ -f2 | head)
TYPE=$(df -Th / | grep "/" | awk '{print $2}')
echo -e "\nResizing ${DISK} - ${PART} of type: ${TYPE} to ${SIZE} on ${INSTANCEID} using volume: ${VOLUMEID}\n"
# Rewrite the partition table so that the partition takes up all the space that it can.
sudo growpart /dev/"${DISK}" 1
if [ "${TYPE}" = "xfs" ]; then
echo "Filesystem is xfs - resizing"
xfs_resize
else
echo "Filesystem is not xfs - resizing"
ext4_resize
fi
@pcolazurdo
Copy link
Author

# For EC2 with XFS
# Optionally, you can use lsblk to be sure of dev names
sudo sudo growpart /dev/nvme0n1 1
sudo xfs_growfs /dev/nvme0n1p1

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