Amazon EBS Volumeを拡張する方法

どなたかの役に立つかも知れないのでメモを公開。


Note: For the latest and complete guideline, follow the official such as this.

1. Configure AWS Access Key

Follow here.

2. Set credentials for AWS CLI

$ aws configure

AWS Access Key ID [None]: ******************UD
AWS Secret Access Key [None]: ******************IX
Default region name [None]: ap-northeast-1
Default output format [None]: json

3. Check which hypervisor the instance is based on (i.e. Xen or Nitro), since device and partition naming differs

$ aws ec2 describe-instance-types --instance-type t2.micro --query "InstanceTypes[].Hypervisor"

["xen"]

4. Check whether the volume has a partition (use lsblk command)

$ sudo lsblk

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  20G  0 disk 
└─xvda1 202:1    0  12G  0 part /

5. Extend the partition (use growpart command: specify the device name and the partition number)

$ sudo growpart /dev/xvda 1

CHANGED: partition=1 start=4096 old: size=25161695 end=25165791 new: size=41938911 end=41943007

6. Verify that the partition has been extended (use lsblk command: the partition size should now be equal to the volume size)

$ sudo lsblk

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  20G  0 disk 
└─xvda1 202:1    0  20G  0 part /

7. Get the name, size, type, and mount point for the file system that you need to extend (use df -hT command)

$ df -hT

Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  478M     0  478M   0% /dev
tmpfs          tmpfs     486M     0  486M   0% /dev/shm
tmpfs          tmpfs     486M  712K  485M   1% /run
tmpfs          tmpfs     486M     0  486M   0% /sys/fs/cgroup
/dev/xvda1     xfs        12G   11G  1.3G  90% /
tmpfs          tmpfs      98M     0   98M   0% /run/user/1000

8. Perform the extension of the mounted file system

Now that we know the file system is xfs, on which the root dir is mounted, then use xfs_growfs command to specify the mount point of the file system to be extended.

$ sudo xfs_growfs -d /

meta-data=/dev/xvda1             isize=512    agcount=7, agsize=524159 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=0, rmapbt=0
         =                       reflink=0    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=3145211, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 3145211 to 5242363

9. Verify that the file system has been extended (confirm that the file system size is equal to the volume size)

$ df -hT

Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  478M     0  478M   0% /dev
tmpfs          tmpfs     486M     0  486M   0% /dev/shm
tmpfs          tmpfs     486M  712K  485M   1% /run
tmpfs          tmpfs     486M     0  486M   0% /sys/fs/cgroup
/dev/xvda1     xfs        20G   11G  9.3G  54% /
tmpfs          tmpfs      98M     0   98M   0% /run/user/1000

Done!