LoginSignup
0
0

AWS CLIでEBSボリュームを拡張する

Last updated at Posted at 2023-08-13

概要

AWS CloudShell から コマンドライン(AWS CLI)で EBS ボリュームサイズを拡張します

環境

  • AWS CloudShell
  • aws-cli 2.13.7
  • Ubuntu 22.04 LTS

手順の流れ

  1. EBS ボリュームIDの確認
  2. EBS ボリュームの拡張
  3. 進捗状況の確認
  4. EBS ボリュームのパーティション拡張
  5. EBS ボリュームのファイルシステム拡張

手順

1. EBS ボリュームIDの確認

AWS マネジメントコンソールから
AWS CloudShell を起動し、 EBS のボリューム ID を取得します

コマンド

$ aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, State.Name, Tags[?Key==`Name`].Value | [0], BlockDeviceMappings[*].Ebs.VolumeId]' --output text

実行結果

# ヘッダーは出力されません
InstanceId           State.Name Tags.Value
-------------------- ---------- ----------
BlockDeviceMappings[*].Ebs.VolumeId
-----------------------------------
i-1234567890abcdef1     running ec2-instance1
vol-1234567890abcdef1 # -> ec2-instance1 に割り当てられたEBSボリュームID
i-1234567890abcdef2     running ec2-instance2
vol-1234567890abcdef2 # -> ec2-instance2 に割り当てられたEBSボリュームID

2. EBS ボリュームの拡張

EBS ボリュームを拡張します

コマンド

$ aws ec2 modify-volume --size 10 --volume-id vol-1234567890abcdef1
# --size 10                         # 拡張後のサイズ(GB)
# --volume-id vol-1234567890abcdef1 # EBSボリュームID

実行結果

{
    "VolumeModification": {
        "VolumeId": "vol-1234567890abcdef1",
        "ModificationState": "modifying",
        "TargetSize": 10,
        "TargetIops": 100,
        "TargetVolumeType": "gp2",
        "TargetMultiAttachEnabled": false,
        "OriginalSize": 8,
        "OriginalIops": 100,
        "OriginalVolumeType": "gp2",
        "OriginalMultiAttachEnabled": false,
        "Progress": 0,
        "StartTime": "2023-08-13T18:35:59+00:00"
    }
}

3. 進捗状況の確認

EBS ボリューム拡張の進捗状況を確認します
Progress で 進捗状況のパーセンテージを確認できます
ModificationState が completed になるまで待機します

コマンド

$ aws ec2 describe-volumes-modifications --volume-id vol-1234567890abcdef1 --output table
# --volume-id vol-1234567890abcdef1 # EBSボリュームID
# --output table                    # 出力形式

実行結果

進行中
---------------------------------------------------------------
|                DescribeVolumesModifications                 |
+-------------------------------------------------------------+
||                   VolumesModifications                    ||
|+-----------------------------+-----------------------------+|
||  ModificationState          |  optimizing                 ||
||  OriginalIops               |  100                        ||
||  OriginalMultiAttachEnabled |  False                      ||
||  OriginalSize               |  8                          ||
||  OriginalVolumeType         |  gp2                        ||
||  Progress                   |  5                          ||
||  StartTime                  |  2023-08-13T18:35:59+00:00  ||
||  TargetIops                 |  100                        ||
||  TargetMultiAttachEnabled   |  False                      ||
||  TargetSize                 |  10                         ||
||  TargetVolumeType           |  gp2                        ||
||  VolumeId                   |  vol-1234567890abcdef1      ||
|+-----------------------------+-----------------------------+|
完了
---------------------------------------------------------------
|                DescribeVolumesModifications                 |
+-------------------------------------------------------------+
||                   VolumesModifications                    ||
|+-----------------------------+-----------------------------+|
||  EndTime                    |  2023-08-13T18:41:05+00:00  ||
||  ModificationState          |  completed                  ||
||  OriginalIops               |  100                        ||
||  OriginalMultiAttachEnabled |  False                      ||
||  OriginalSize               |  8                          ||
||  OriginalVolumeType         |  gp2                        ||
||  Progress                   |  100                        ||
||  StartTime                  |  2023-08-13T18:35:59+00:00  ||
||  TargetIops                 |  100                        ||
||  TargetMultiAttachEnabled   |  False                      ||
||  TargetSize                 |  10                         ||
||  TargetVolumeType           |  gp2                        ||
||  VolumeId                   |  vol-1234567890abcdef1      ||
|+-----------------------------+-----------------------------+|

4. EBS ボリュームのパーティション拡張

EC2 インスタンスにログインし、EBS ボリュームのパーティションを拡張します
ModificationState が optimizing になると実行可能になります

コマンド

# パーティションのサイズを確認
ubuntu@ip-xx-xx-xx-xx:~$ sudo lsblk
NAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0      7:0    0  24.4M  1 loop /snap/amazon-ssm-agent/6312
loop1      7:1    0  24.8M  1 loop /snap/amazon-ssm-agent/6563
loop2      7:2    0  55.6M  1 loop /snap/core18/2745
loop3      7:3    0  55.7M  1 loop /snap/core18/2785
loop4      7:4    0  63.3M  1 loop /snap/core20/1879
loop5      7:5    0  63.4M  1 loop /snap/core20/1974
loop6      7:6    0 111.9M  1 loop /snap/lxd/24322
loop7      7:7    0  53.2M  1 loop /snap/snapd/19122
loop8      7:8    0  53.3M  1 loop /snap/snapd/19457
xvda     202:0    0    10G  0 disk
├─xvda1  202:1    0   7.9G  0 part / # このパーティションを拡張します
├─xvda14 202:14   0     4M  0 part
└─xvda15 202:15   0   106M  0 part /boot/efi

警告
デバイス名 (xvda) とパーティション番号 (1) の間のスペースに注意してください

# パーティションのサイズを拡張
ubuntu@ip-xx-xx-xx-xx:~$ sudo growpart /dev/xvda 1
CHANGED: partition=1 start=227328 old: size=16549855 end=16777183 new: size=20744159 end=20971487

ubuntu@ip-xx-xx-xx-xx:~$ sudo lsblk
NAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0      7:0    0  24.4M  1 loop /snap/amazon-ssm-agent/6312
loop1      7:1    0  24.8M  1 loop /snap/amazon-ssm-agent/6563
loop2      7:2    0  55.6M  1 loop /snap/core18/2745
loop3      7:3    0  55.7M  1 loop /snap/core18/2785
loop4      7:4    0  63.3M  1 loop /snap/core20/1879
loop5      7:5    0  63.4M  1 loop /snap/core20/1974
loop6      7:6    0 111.9M  1 loop /snap/lxd/24322
loop7      7:7    0  53.2M  1 loop /snap/snapd/19122
loop8      7:8    0  53.3M  1 loop /snap/snapd/19457
xvda     202:0    0    10G  0 disk
├─xvda1  202:1    0   9.9G  0 part / # パーティションが拡張されました
├─xvda14 202:14   0     4M  0 part
└─xvda15 202:15   0   106M  0 part /boot/efi

5. EBS ボリュームのファイルシステム拡張

EC2 インスタンスにログインし、EBS ボリュームのファイルシステムを拡張します
ModificationState が optimizing になると実行可能になります

コマンド

拡張前
# 拡張対象の Filesystem、Type、Size、Mounted on を取得
ubuntu@ip-xx-xx-xx-xx:~$ df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/root      ext4   7.6G  7.4G  184M  98% /  # このファイルシステムを拡張します
tmpfs          tmpfs  479M     0  479M   0% /dev/shm
tmpfs          tmpfs  192M  836K  191M   1% /run
tmpfs          tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/xvda15    vfat   105M  6.1M   99M   6% /boot/efi
tmpfs          tmpfs   96M  4.0K   96M   1% /run/user/1000
# ファイルシステム拡張
# Type が xfs の場合
ubuntu@ip-xx-xx-xx-xx:~$ sudo xfs_growfs -d /

# Type が ext4 の場合
ubuntu@ip-xx-xx-xx-xx:~$ sudo resize2fs /dev/xvda1

出力結果

拡張後
ubuntu@ip-xx-xx-xx-xx:~$ df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/root      ext4   9.6G  7.4G  2.2G  78% /  # ファイルシステムが拡張されました
tmpfs          tmpfs  479M     0  479M   0% /dev/shm
tmpfs          tmpfs  192M  836K  191M   1% /run
tmpfs          tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/xvda15    vfat   105M  6.1M   99M   6% /boot/efi
tmpfs          tmpfs   96M  4.0K   96M   1% /run/user/1000

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0