Resizing /dev/sda1 on Amazon EC2

When working on a medium size EC2 instance, running Debian 6, I ran out of free space. The root partition had fully grown into its 1GB EBS volume. 1GB was a default setting and I should have changed this before I started working.

You can use “df -h” to check how much free disk space you have remaining (man page).

df shows how much free space is remaining

No disk space!

So I cant just mount another disk because all my work is in /dev/sda1 (root).

By work I mean a PostgreSQL database, my Python app and ProFTPD. Normally I could just admit defeat and start a new EC2 instance but then I wouldn’t have a blog post. There could be times when you wouldn’t want to do that anyway.

So.. I’m not at the magical level where I can unmount and re-mount the disk the OS resides on while it is running.

The solution is to create a new EBS volume, copy the file system using the EBS ‘Snapshot’ feature and then attach it to your server instance.

How to fix

It’s an easy job and the downtime is only a few minutes.

  • Stop the EC2 instance
    Turn off the server
  • Take a ‘Snapshot’ of the EC2 instance (in the EBS section)create a copy of your servers hard drive
  • Create a new larger EBS volume and load the ‘Snapshot’
    assign a snapshot of an ec2 instance to the new ebs volume
  • Detach the EBS volume currently being used by the EC2 instance
  • Attach the larger EBS volume, containing the snapshot
  • Start the EC2 instance
  • Assign any Elastic Ip’s you need
  • Log in via SSH
  • Reclaim the new space (The OS won’t do this on its own), see below

Claim the new free space

The command to claim unused space on your hard drive is resize2fs. Read about it first because you can specify some things like which disk to use. Run time may be a few minutes and there wont be any output to stdout until its complete. Allow it to finish or append the command with an &.

As root just execute:

resize2fd /dev/sda1

If that doesn’t work then try:

resize2fs /dev/xvda1

quick note: “xvda” – this is a Xen Virtual Disk, explained

Finally, execute df to check it worked. Output should be like the top picture with a small % under use.

In future

In my haste I used the wizard to set up a Debian ‘Squeeze’ instance. The Amazon Machine Image (AMI) came with a 1GB hard drive (EBS). It’s at that point you could attach a larger EBS just for the job you are working on. That would also be cool as you could detach (unmount) it and attach (mount) on another EC2 instance without having to take servers offline.

More info

This article saved me:
http://alestic.com/2010/02/ec2-resize-running-ebs-root

This issue seams pretty common:
http://stackoverflow.com/questions/6151695/ec2-instace-on-amazon-and-i-am-greeted-with-no-space-left-on-the-disk

Amazon official documentation:
http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/using.html

Remove terrible newspapers from news.google.com

I use news.google.com to quickly check the headlines. Some news corps get in the way with their lies and pseudo-facts. Follow these steps for minimal waft:

  1. Click the pencil at the top right (Personalize your news)
  2. Scroll down to ‘Adjust Sources’ and move the slider(s)

Using urllib2 to POST JSON

import json
import urllib2

data = {'name':'Alex','colour':'Green','number':42,'foo':'bar'}
data = json.dumps(data)

url = "http://somewebsite.zzz/webservice"

req = urllib2.Request(url, data, {'Content-Type': 'application/json'})

f = urllib2.urlopen(req)
response = f.read()
f.close()

Excellent guide to Python and REST webservices: http://developer.yahoo.com/python/python-rest.html