调整环境使用的 Amazon EBS 卷大小 - AWS Cloud9

AWS Cloud9 不再向新客户提供。 AWS Cloud9 的现有客户可以继续正常使用该服务。了解更多

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

调整环境使用的 Amazon EBS 卷大小

此步骤说明如何调整 Amazon EBS 卷大小。

  1. 为您要调整大小的亚马逊 EBS 卷打开与亚马逊 EC2 实例关联的环境。

  2. 在环境的 AWS Cloud9 IDE 中,创建一个包含以下内容的文件,然后使用扩展名保存该文件.sh(例如,resize.sh)。

    注意

    此脚本适用于与运行 AL2 023、亚马逊 Linux 2、亚马逊 Linux 或 EC2 Ubuntu 服务器和已配置为使用 IMDSv2。

    该脚本还会调整作为 NVMe 区块设备公开的 Amazon EBS 卷的大小 Nitro基于的实例。有关基于 Nitro 系统的实例列表,请参阅 NitroAmazon EC2 用户指南中的基于实例的实例。

    #!/bin/bash # Specify the desired volume size in GiB as a command line argument. If not specified, default to 20 GiB. SIZE=${1:-20} # Get the ID of the environment host Amazon EC2 instance. TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60") INSTANCEID=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null) REGION=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/placement/region 2> /dev/null) # Get the ID of the Amazon EBS volume associated with the instance. VOLUMEID=$(aws ec2 describe-instances \ --instance-id $INSTANCEID \ --query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \ --output text \ --region $REGION) # Resize the EBS volume. aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE # Wait for the resize to finish. while [ \ "$(aws ec2 describe-volumes-modifications \ --volume-id $VOLUMEID \ --filters Name=modification-state,Values="optimizing","completed" \ --query "length(VolumesModifications)"\ --output text)" != "1" ]; do sleep 1 done # Check if we're on an NVMe filesystem if [[ -e "/dev/xvda" && $(readlink -f /dev/xvda) = "/dev/xvda" ]] then # Rewrite the partition table so that the partition takes up all the space that it can. sudo growpart /dev/xvda 1 # Expand the size of the file system. # Check if we're on AL2 or AL2023 STR=$(cat /etc/os-release) SUBAL2="VERSION_ID=\"2\"" SUBAL2023="VERSION_ID=\"2023\"" if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]] then sudo xfs_growfs -d / else sudo resize2fs /dev/xvda1 fi else # Rewrite the partition table so that the partition takes up all the space that it can. sudo growpart /dev/nvme0n1 1 # Expand the size of the file system. # Check if we're on AL2 or AL2023 STR=$(cat /etc/os-release) SUBAL2="VERSION_ID=\"2\"" SUBAL2023="VERSION_ID=\"2023\"" if [[ "$STR" == *"$SUBAL2"* || "$STR" == *"$SUBAL2023"* ]] then sudo xfs_growfs -d / else sudo resize2fs /dev/nvme0n1p1 fi fi
  3. 从 IDE 中的终端会话中,切换到包含 resize.sh 文件的目录。然后,运行以下任一命令,同时将 20 替换为要将 Amazon EBS 卷调整至的期望大小(以 GiB 为单位):

    • bash resize.sh 20
    • chmod +x resize.sh ./resize.sh 20