Friday, July 29, 2011

How to increase disk space on existing AWS EC2 Linux (Ubuntu) Instance without losing data

By Semir H.

Let’s say you have a Linux Amazon Web Services (AWS) EC2 Instance up and running and you start running out of space. I’ll show you how to quickly clone its disk (EBS volume) onto a bigger disk (also EBS), replace the smaller disk with the bigger one and boot off the bigger (new) disk. No need for any third party tools like Acronis or Ghost or even rsync.


If you find this tutorial useful, please consider making a donation to support future work like this:



Thank you :)

Create Snapshot of the original disk:

First thing we’ll do is to create a Snapshot of the original disk so that we can then create a new, bigger volume out of that Snapshot. There are a couple of things we have to note down first: the Instance number and then the volume attached to that Instance. The easiest way to fine the Instance number is in the Instances part of the AWS Management Console. It will start with “i” and be under the “Instance” column. Now navigate to the Volumes section (Elastic Block Store section) and locate the volume that is attached to your Instance. You can see the Instance number under the “Attachment Information” column. The first half of that string will be the Instance number. Please see the images below for an example.

Instance (in this case number is i-1920b74c):


Please also note which Zone your Instance is in. This will be important for later. In my case the Zone is “ap-southeast-1b” in the Singapore Region. The Zone can be seen by selecting the Instance and looking under its Description tab (bottom).

Volume (note the Instance number):



As you’ll notice in the examples above, the size of the original Volume (EBS Disk) is 8 GB. I’ll increase that to 25 GB.

Creating a Snapshot

To create a snapshot of the Volume you simply right-click on it and click on Create Snapshot. Or you can select the Volume and click on the Create Snapshot button at the top.


Please note that I am creating a Snapshot of the running virtual machine. If you have a database on it or some other application that has a lot of transactions happening then it would be best to turn the virtual machine off (schedule some downtime) and do the Snapshot.

Give the Snapshot a meaningful description and a name. Click on “Yes, Create”.


Once done, you will be able to see your Snapshot in the Snapshots part of the Elastic Block Store area. See example below. Please note the Name, Description and Capacity (original 8 GB).


Creating a new (clone) Volume

Next, we’ll create a Volume out of the Snapshot. Simply right-click on the Snapshot and select “Create Volume from Snapshot” from the resulting menu or you can use the button at the top.



Put in the desired Size of the new Volume and make sure the Availability Zone coresponds to the AZ that the original Instance is in. Click on “Yes, Create”.


If you navigate to the Volumes section you will see the new 25 GB volume there. You’ll notice that it is not in use.


Attach new Volume to existing Instance:

And now for the fun part. We will stop the Instance, detach the original 8 GB Volume, attach the new 25 GB Volume and make sure we are able to boot. Please note the new Volume will have the data up to the point when the Snapshot was taken. If the original machine had more data put onto it since the Snapshot then that will have to be dealt with. This is beyond the scope of this tutorial.

Stop the Instance (if it is running).

Go to the EBS Volumes section, select the original Volume, right click on it and select “Detach Volume”. You can also use the Detach Volume button. When prompted, please select “Yes, Detach”.


To attach the new Volume right-click on it and select “Attach Volume”.


Make sure you select the correct Instance. In the example below I am also modifying the Device to be the same as the original one. The example is for a Linux (Ubuntu) Instance. This is important. Click on “Yes, Attach”.



The Status should change to “in-use”.

Go back to your Instance and start it up. Log back into your Linux Instance and run the following command (for ext3 file system):

    resize2fs /dev/xvda1

Please note that the device is not called sda1 but xvda1. The device was renamed by the Kernel.
After it is done you should be able to see the full 25 GB. use the “du -h” command. Example:


Cleanup:

Once you’ve made sure all works as it should and that all your data is fine on the new Volume, you can remove the Snapshot and the old Volume if you do not intend to use them anymore. Go to the relevant AWS Management Console sections and remove (delete) them.

24 comments:

  1. Great article, saved me a lot of time resizing some Ubuntu EC2 instances. For other readers please note that

    resize2fs /dev/sda1

    works fine - nothing gets renamed (at least when using Alestic Ubuntu AMIs). Just remember to add that in the Attach Volume window under Device.

    ReplyDelete
    Replies
    1. You have saved my 4-5 days of work

      Delete
  2. Hi

    Thanks for you article. i did the same and everything works fine except FTP. in security groups port 21 is opened but server refusing 21. any idea?

    Thanks
    SAB

    ReplyDelete
  3. Hi,
    It's very nice, simple & very very useful article for resizing the root disk on Linux.

    Thanks a lot for such nice article.

    Thanks& Regards,
    Anand kanade

    ReplyDelete
  4. So nice article it really helps me a lot and i want to know if i start and stop the instance the ipaddress is changing it should not change when i do this please give me suggestions what to do for not to change ipaddress sequentially when i stop and start the new instance

    ReplyDelete
  5. I have 3 instances, 2 windows and 1 redhat linux

    I did exactly as described here to increase space for the linux instance, but when trying to attach it I cannot select the instance I want. I can only select the windows instances.

    ReplyDelete
  6. Confirm, this works with the current Amazon AMI. perfect.

    ReplyDelete
  7. My device was named as /dev/xvde1 en RHEL 6.2 for example. So it could be any name.

    ReplyDelete
  8. Hi,

    Why am I getting permission denied when trying to resize?

    I tried resize2fs -f /dev/xvda1
    and got back
    open: Permission denied while opening /dev/xvda1

    I tried resize2fs -f /dev/sda1
    and got back
    open: No such file or directory while opening /dev/sda1

    I am using ami-baba68d3 which is an Ubuntu 11.10 64-bit AMI.

    Filesystem Size Used Avail Use% Mounted on
    /dev/xvda1 7.9G 764M 6.8G 10% /
    udev 1.9G 4.0K 1.9G 1% /dev
    tmpfs 752M 152K 752M 1% /run
    none 5.0M 0 5.0M 0% /run/lock
    none 1.9G 0 1.9G 0% /run/shm
    /dev/xvdb 394G 199M 374G 1% /mnt

    ReplyDelete
    Replies
    1. You should have resolved this already, but for the sake of those who might run into this problem in the future.
      Just run 'ls -la /dev' in the console
      You should scroll to the bottom and look for something like (in my case) 'xvde1'
      NOTE: You might see others like xvdj e.t.c.

      Delete
  9. Thanks so much, il helped me a lot !

    ReplyDelete
  10. Thanks for this excellent article.
    Is it important to attach to the same device that the original volume was attached to?
    If so, you could add a note to make sure to record the device before the original volume is detached.

    ReplyDelete
  11. Thanks for this excellent article. in the very last step you have "use the “du -h” command. Example:" I think you mean df-h
    thanks again.

    ReplyDelete
  12. This was great ! Worked well. Much appreciated

    ReplyDelete
  13. Thanks for this excellent and detailed article.
    There is one vital step missing (for newbies) - this comes after the Attach Volume step with the screenshot of the volume.
    Users logged in as ec2-user (like Joey above) will not be able to resize the boot volume size, the error message they will get is "Permission denied".
    here is the extra step needed - change to super user ("su to root" in jargon).
    The command for this is: "sudo su".
    After this, the next step as mentioned above works flawlessly:
    "resize2fs /dev/xvda1"

    ReplyDelete
  14. Is this will work root device. Suppose My os installed with 8GB space. Can increase volume of Os Volume also

    ReplyDelete
  15. Great step by step explanation! This article helped a ton :)

    ReplyDelete
  16. I was stuck at the last stage, the resize worked for me finally with
    sudo resize2fs /dev/sda1

    Hope this helps someone else!

    ReplyDelete
  17. 2 years after the original post and I just wanted to add my heartfelt thanks and confirm that these instructions still work very well; helped me solve a problem I hadn't even been able to understand correctly.

    ReplyDelete
  18. Thank you for this article.
    When trying to resize the filesystem (either as user and root) I get the following error:
    Filesystem at /dev/xvda1 is mounted on /; on-line resizing required
    old_desc_blocks = 1, new_desc_blocks = 7
    resize2fs: Permission denied to resize filesystem

    This is my output of df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/xvda1 9.9G 7.0G 2.5G 74% /
    udev 1.9G 8.0K 1.9G 1% /dev
    tmpfs 376M 176K 375M 1% /run
    none 5.0M 0 5.0M 0% /run/lock
    none 1.9G 0 1.9G 0% /run/shm
    none 100M 0 100M 0% /run/user
    overflow 1.0M 0 1.0M 0% /tmp

    I am having a lot f trouble to find the solution.
    Any clues?
    Thank you

    ReplyDelete
  19. root@ip-10-167-161-18:/home/ubuntu# resize2fs /dev/xvda1
    resize2fs 1.42.9 (4-Feb-2014)
    The filesystem is already 26212055 blocks long. Nothing to do!

    No any effect same size still
    what wrong with me.

    ReplyDelete