Problem

Amazon EC2 (Linux)를 사용할 때 인스턴스의 여유 공간이 부족해지는 문제가 가끔 발생합니다. Amazon Elastic Block Store(Amazon EBS) 인스턴스에 /root 파티션의 볼륨을 동적으로 확장하려면 어떻게 할까요?

Solution

먼저 콘솔에서 확장이 필요한 볼륨을 선택하고 확장한다.

아래 이미지 처럼 원하는 크기로 확장 할 수 있다.

먼저 파티션 크기와 상태를 확인합니다1.

ubuntu@ip-xxx:/var/log$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0         7:0    0 33.3M  1 loop /snap/amazon-ssm-agent/3552
loop1         7:1    0 70.3M  1 loop /snap/lxd/21029
loop2         7:2    0 32.3M  1 loop /snap/snapd/12704
loop3         7:3    0 55.5M  1 loop /snap/core18/2074
loop4         7:4    0 67.6M  1 loop /snap/lxd/20326
loop5         7:5    0   25M  1 loop /snap/amazon-ssm-agent/4046
loop6         7:6    0 55.4M  1 loop /snap/core18/2066
loop9         7:9    0 32.3M  1 loop /snap/snapd/12398
nvme1n1     259:0    0   22G  0 disk /data
nvme0n1     259:1    0  100G  0 disk
└─nvme0n1p1 259:2    0    8G  0 part /

확장이 필요한 파티션을 선택하여 파티션 확장을 시도한다.

ubuntu@ip-xxx:/var/log$ sudo growpart /dev/nvme0n1 1
CHANGED: partition=1 start=2048 old: size=16775135 end=16777183 new: size=209713119 end=209715167
ubuntu@ip-xxx:/var/log$ lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0         7:0    0 33.3M  1 loop /snap/amazon-ssm-agent/3552
loop1         7:1    0 70.3M  1 loop /snap/lxd/21029
loop2         7:2    0 32.3M  1 loop /snap/snapd/12704
loop3         7:3    0 55.5M  1 loop /snap/core18/2074
loop4         7:4    0 67.6M  1 loop /snap/lxd/20326
loop5         7:5    0   25M  1 loop /snap/amazon-ssm-agent/4046
loop6         7:6    0 55.4M  1 loop /snap/core18/2066
loop9         7:9    0 32.3M  1 loop /snap/snapd/12398
nvme1n1     259:0    0   22G  0 disk /data
nvme0n1     259:1    0  100G  0 disk
└─nvme0n1p1 259:2    0  100G  0 part /

resize2fs를 이용해 파티션 반영을 시도한다.

sudo resize2fs /dev/root
ubuntu@ip-xxx:/var/log$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        97G  7.6G   90G   8% /
devtmpfs        973M     0  973M   0% /dev
tmpfs           978M     0  978M   0% /dev/shm
tmpfs           196M  1.2M  195M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           978M     0  978M   0% /sys/fs/cgroup
/dev/loop0       34M   34M     0 100% /snap/amazon-ssm-agent/3552
/dev/nvme1n1     22G  3.1G   19G  15% /data
/dev/loop4       68M   68M     0 100% /snap/lxd/20326
/dev/loop6       56M   56M     0 100% /snap/core18/2066
overlay          97G  7.6G   90G   8% /var/lib/docker/overlay2/426c953f22aaa7fa03151b3056851d74cffccc33b0d9e0c830bed2894156b023/merged
overlay          97G  7.6G   90G   8% /var/lib/docker/overlay2/81b8fbc658c8b072cf7f56d38a759088b29d9263f0ad1e5eac29f93ed480a7d3/merged
overlay          97G  7.6G   90G   8% /var/lib/docker/overlay2/80b61384d1bd93fc8e156064fa838e8f80c5fb2ab62b7aaa9d8b99ff0cbfa79a/merged
overlay          97G  7.6G   90G   8% /var/lib/docker/overlay2/0bf0f0b6c8b531d6ebc5c9c66804066cc9935d1715cc72b7c7afc276d4274bda/merged
overlay          97G  7.6G   90G   8% /var/lib/docker/overlay2/a7da263e825d9b5a010c34425ee874238417b54f6597c37e999de90b5d54cb50/merged
/dev/loop3       56M   56M     0 100% /snap/core18/2074
/dev/loop9       33M   33M     0 100% /snap/snapd/12398
/dev/loop1       71M   71M     0 100% /snap/lxd/21029
/dev/loop2       33M   33M     0 100% /snap/snapd/12704
/dev/loop5       25M   25M     0 100% /snap/amazon-ssm-agent/4046
tmpfs           196M     0  196M   0% /run/user/1000

References