When using the official CentOS Linux AMI on EC2, it comes (currently) with only an 8GB partition. Want to resize that? Here is how…
First make sure you start a small (t2.micro or similar) instance to do this work with. It should be based on the Amazon Linux AMI. You should have root access to it. Let’s call it “fix-the-disk-instance”.
- Stop the NEW instance
- Go to EC2 > Instances
- Select the Instance
- Make sure the Instance has a name that makes sense (e.g. snap.appcove.net)
- Click “Actions” > “Instance State” > Stop
- Detach the block device
- Go to EC2 > Volumes
- Find the volume by looking under Attachment Information for the instance name (e.g. snap.appcove.net)
- Select the volume
- Click “Actions” > “Detach Volume”
- Attach the block device to “fix-the-disk-instance”
- Go to EC2 > Volumes
- Find the volume by looking under Attachment Information for the instance name (e.g. snap.appcove.net)
- Select the volume
- Click “Actions” > “Attach Volume”
- Attach it to “fix-the-disk-instance” so we can fix it up
- Start “fix-the-disk-instance” and login
- Go to EC2 > Volumes
- Select the Instance “fix-the-disk-instance”
- Click “Actions” > “Instance State” > Start
- Wait for it to start
- SSH into it and escalate to root
- Run `parted` command and print list
- As root, run the following command: parted
- Type the following command: print all<enter>
- Find your disk in the output and note the path (e.g. /dev/xvdf)
- Quit the program by typing: quit<enter>
- Resize the partition:
- Run fdisk on the device path found above (e.g. fdisk /dev/xvdf)
- List the partitions
- Type: p<enter>
- Take note of the “Start” column (e.g. 2048)
- Delete the partition
- Type: d<enter>
- Create a new one by typing:
- n (for new)
- p (for primary)
- 1 (for first partition)
- Type first sector the same as the Start column above (e.g. 2048)
- Last sector should be the default (end of disk)
- w (for writing to disk)
- Now check the partition and resize it
- Run this command making sure to use the right device path (but add a 1 to the end for the first partition)
- e2fsck -f /dev/xvdf1
- Resize the partition making sure to use the right device path (but add a 1 to the end for the first partition)
- resize2fs /dev/xvdf1
- Run this command making sure to use the right device path (but add a 1 to the end for the first partition)
- Shutdown the instance “fix-the-disk-instance”
- Go to EC2 > Instances
- Select the Instance “fix-the-disk-instance”
- Click “Actions” > “Instance State” > Stop
- Detach the block device and reattach it to the original server
- Go to EC2 > Volumes
- Find the volume by looking under Attachment Information for the instance name (e.g. fix-the-device-instance).
- Make sure it is the right volume by looking at the ID/Size.
- Select the volume
- Click “Actions” > “Detach Volume”
- Click “Actions” > “Attach Volume”
- IMPORTANT: enter “/dev/sda1” as the device so it attaches as the root volume
- Attach it to the original server
- Start “fix-the-disk-instance” and login
- Go to EC2 > Volumes
- Select the original instance (e.g. “snap.appcove.net”)
- Click “Actions” > “Instance State” > Start
- Wait for it to start
And that’s all!