How to replace primary storage with new EBS in Amazon EC2 instance


This based on the need, we want create a big EBS, remove the old 8GB EBS and replace with the new one as primary storage. Using new EBS storage as primary is a little bit tricky and you should carefull when doing this. We should be careful when upgrading EBS storage in Amazon EC2. Make sure backup all data and databases before doing upgrading. Also, there no data added / changes in server in middle of migration because it may causes data loss.

First, you should make sure already backup all data in your instance. This is a list that you should notice :

1. /etc – in this folder will be contain bind, nginx, apache and a lot of configuration stuff
2. Backup your database
3. /var
4. Home folder – sometimes you put important stuff there

If you dealing with production server, it’s better you create new instance and “rsync” all stuff into this new one. So, if we have something bad with the server, we have another server that ready to take over.

Now, we ready to replace primary storage with the new one.

A. Create new EBS

Go to Amazon EC2 dashboard panels and choose EBS Volume tab:

1. Create snapshot based on the EBS which we want to increase it size.
2. After snapshot created, you can create new volume by right-click on it.
3. Set new Volume size.

B.Configuring new EBS

We need to attach new EBS into server, mount and editing several files before we use it as primary storage.

1. Attach new EBS into server, eg: /dev/sdf (it will show as /dev/xvdf)
2. Go to server and check if the EBS already there by : sudo fdisk -l
3. Fix size capacity issue by :

1
2
3
sudo su
e2fsck -f /dev/<new-ebs>
resize2fs /dev/<new-ebs>

4. Clone all data from your primary storage into new EBS
You can mount the new EBS into /mnt. Then you can start clone all systems folder (except mnt) into this mnt which contain new EBS.

5. Inside /mnt/etc/fstab
Edit “etc/fstab” inside new EBS and remove LABEL=/ and replace with:

1
/dev/xvda1     /           ext4    defaults,noatime  1   1

Edit “etc/boot/grub.conf” and “etc/grub.conf” inside new EBS and change ROOT LABEL into /dev/xvda1 eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
title Amazon Linux 2012.03 (3.2.20-1.29.6.amzn1.x86_64)
        root (hd0)
        kernel /boot/vmlinuz-3.2.20-1.29.6.amzn1.x86_64 root=/dev/xvda1 console=hvc0 LANG=en_US.UTF-8 KEYTABLE=us
        initrd /boot/initramfs-3.2.20-1.29.6.amzn1.x86_64.img

title Amazon Linux 2012.03 (3.2.12-3.2.4.amzn1.x86_64)
        root (hd0)
        kernel /boot/vmlinuz-3.2.12-3.2.4.amzn1.x86_64 root=/dev/xvda1 console=hvc0 LANG=en_US.UTF-8 KEYTABLE=us
        initrd /boot/initramfs-3.2.12-3.2.4.amzn1.x86_64.img

title Amazon Linux 2011.09 (2.6.35.14-107.1.39.amzn1.x86_64)
        root (hd0)
        kernel /boot/vmlinuz-2.6.35.14-107.1.39.amzn1.x86_64 root=/dev/xvda1 console=hvc0 LANG=en_US.UTF-8 KEYTABLE=us
        initrd /boot/initramfs-2.6.35.14-107.1.39.amzn1.x86_64.img

Edit “etc/ssh/sshd_config” inside new EBS. Uncomment “StrictModes yes” and change it into “StrictModes no”.

C. Attaching the new EBS
1. We should stop the instance and detach EBS that belongs to this instance.
2. Attach new EBS with the instance with “/dev/sda1” as device name.
3. Starting the instance and hope everything is work well.

SSH Problem
When you stop and start the instance, the public DNS will changes even you using elastic IP. Disassociate and re-associate the elastic IP will solve this problem. Remember, when you failed to SSH the server, check if the Reverse Name and IP is correct!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.