You created an AWS instance and decided to make a small root volume. Maybe you were shortsighted or just being cheap. Now the root volume is nearly full and you can’t expand it because you didn’t use LVM.
[[email protected] ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/xvda1 8256952 8173060 8 100% /
To add insult to injury, AWS recently cut the price of EBS storage in half. This issue can be fixed with a handful of commands from the CLI.
Before you begin, take note of a few important details about the instance. You need to know the instance-id and the availability-zone that your instance is running in. You can get the instance-id from the running instance by querying the instance metadata service. More details about the metadata service are available here.
[[email protected] ~]$ curl http://169.254.169.254/latest/meta-data/instance-id/ i-2db6840d
Determine which availability-zone your instance is running in.
[email protected]:~$ aws ec2 describe-instances --instance-id i-2db6840d --output text | egrep ^PLACEMENT PLACEMENT default None us-east-1a
Shut down the instance. You can’t get a reliable snapshot of a volume while it’s attached to a running instance.
[email protected]:~$ aws ec2 stop-instances --instance-ids i-2db6840d --output text i-2db6840d CURRENTSTATE 64 stopping PREVIOUSSTATE 16 running RESPONSEMETADATA e8b8f0ab-7cd1-478f-a7c1-bb5bf81b2355
To determine the volume-id of the root volume run describe-instances:
[email protected]:~$ aws ec2 describe-instances --instance-ids i-2db6840d --output text | egrep ^EBS EBS attached True vol-b6b98bfb 2014-02-11T20:36:00.000Z
Take a snapshot of vol-b6b98bfb:
[email protected]:~$ aws ec2 create-snapshot --volume-id vol-b6b98bfb --output text None vol-b6b98bfb pending 8 None 2014-02-11T21:10:41.000Z snap-e76a3b25 647956677678 RESPONSEMETADATA 03f83444-a473-4bc1-b138-1065f6d5cee0
Now that you have a snapshot (snap-e76a3b25) of the root volume, you can create an exact larger copy of it. If the snapshot state is still pending, you will get an error. Do not forget to specify the size or the new volume will be the same size as the old.
[email protected]:~$ aws ec2 create-volume --availability-zone us-east-1a --size 40 --snapshot-id snap-e76a3b25 --output text us-east-1a standard vol-b4aa98f9 creating snap-e76a3b25 2014-02-11T21:14:51.280Z 40 RESPONSEMETADATA 2257dac5-1685-488c-b545-2f8f9417a6ac
Finally detach the original volume (vol-b6b98bfb) and attach the newly created volume (vol-b4aa98f9).
[email protected]:~$ aws ec2 detach-volume --volume-id vol-b6b98bfb --output text 2014-02-11T20:36:00.000Z i-2db6840d vol-b6b98bfb detaching /dev/sda1 RESPONSEMETADATA ea10cbbd-2adf-43c9-b5e3-cca19c1e5c26 [email protected]:~$ aws ec2 attach-volume --volume-id vol-b4aa98f9 --instance-id i-2db6840d --device /dev/sda1 --output text 2014-02-11T21:20:00.318Z i-2db6840d vol-b4aa98f9 attaching /dev/sda1 RESPONSEMETADATA 146b0ecc-6916-49aa-931c-24c6cdbaca8c
Start your instance and verify that your root volume is now 40 GB.
[email protected]:~$ aws ec2 start-instances --instance-id i-2db6840d --output text i-2db6840d CURRENTSTATE 0 pending PREVIOUSSTATE 80 stopped RESPONSEMETADATA 9320cb43-589f-4ab5-a1ce-d23718c02297 [email protected]:~$ ssh -i .ssh/prd.pem [email protected] ssh: connect to host 172.16.14.78 port 22: Connection refused [email protected]:~$ ssh [email protected] Last login: Tue Feb 11 21:06:04 2014 from __| __|_ ) _| ( / Amazon Linux AMI ___|\___|___| [[email protected] ~]$ df -h / Filesystem Size Used Avail Use% Mounted on /dev/xvda1 7.9G 7.8G 92M 99% /
The root volume is still 8 GB. This is because the volume was created based on a snapshot. Run resize2fs to resize the volume to 40 GB.
[[email protected] ~]$ sudo resize2fs /dev/xvda1 resize2fs 1.42.3 (14-May-2012) Filesystem at /dev/xvda1 is mounted on /; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 3 The filesystem on /dev/xvda1 is now 10485760 blocks long. [[email protected] ~]$ df -h / Filesystem Size Used Avail Use% Mounted on /dev/xvda1 40G 7.8G 32G 20% /
For details on how to grow an EBS based LVM filesystem, see this article that I wrote.
If you have any questions, please post them in the comments below.