1
|
#!/bin/bash
|
2
|
|
3
|
# Specify the desired volume size in GiB as a command-line argument. If not specified, default to 20 GiB.
|
4
|
SIZE=${1:-20}
|
5
|
|
6
|
# Get the ID of the environment host Amazon EC2 instance.
|
7
|
INSTANCEID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
|
8
|
|
9
|
# Get the ID of the Amazon EBS volume associated with the instance.
|
10
|
VOLUMEID=$(aws ec2 describe-instances \
|
11
|
--instance-id $INSTANCEID \
|
12
|
--query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \
|
13
|
--output text)
|
14
|
|
15
|
# Resize the EBS volume.
|
16
|
aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE
|
17
|
|
18
|
# Wait for the resize to finish.
|
19
|
while [ \
|
20
|
"$(aws ec2 describe-volumes-modifications \
|
21
|
--volume-id $VOLUMEID \
|
22
|
--filters Name=modification-state,Values="optimizing","completed" \
|
23
|
--query "length(VolumesModifications)"\
|
24
|
--output text)" != "1" ]; do
|
25
|
sleep 1
|
26
|
done
|
27
|
|
28
|
#Check if we're on an NVMe filesystem
|
29
|
if [ $(readlink -f /dev/xvda) = "/dev/xvda" ]
|
30
|
then
|
31
|
# Rewrite the partition table so that the partition takes up all the space that it can.
|
32
|
sudo growpart /dev/xvda 1
|
33
|
|
34
|
# Expand the size of the file system.
|
35
|
# Check if we are on AL2
|
36
|
STR=$(cat /etc/os-release)
|
37
|
SUB="VERSION_ID=\"2\""
|
38
|
if [[ "$STR" == *"$SUB"* ]]
|
39
|
then
|
40
|
sudo xfs_growfs -d /
|
41
|
else
|
42
|
sudo resize2fs /dev/xvda1
|
43
|
fi
|
44
|
|
45
|
else
|
46
|
# Rewrite the partition table so that the partition takes up all the space that it can.
|
47
|
sudo growpart /dev/nvme0n1 1
|
48
|
|
49
|
# Expand the size of the file system.
|
50
|
# Check if we're on AL2
|
51
|
STR=$(cat /etc/os-release)
|
52
|
SUB="VERSION_ID=\"2\""
|
53
|
if [[ "$STR" == *"$SUB"* ]]
|
54
|
then
|
55
|
sudo xfs_growfs -d /
|
56
|
else
|
57
|
sudo resize2fs /dev/nvme0n1p1
|
58
|
fi
|
59
|
fi
|