Can I change the root EBS device of my amazon EC2 instance?

Amazon Ec2Amazon Web-Services

Amazon Ec2 Problem Overview


I have an EBS backed Amazon EC2 instance. I would like to change the root device on this instance. Can you please advise the best way to go about this?

I only find documentation on changing several attributes of block devices, but they don't seem to include setting it as the root device.

Amazon Ec2 Solutions


Solution 1 - Amazon Ec2

Yep, it's dead easy:

  1. Stop the instance.
  2. Detach the root EBS volume.
  3. Attach the alternate EBS volume as the root: /dev/sda1
  4. Start the instance.

This presupposes that your alternate EBS volume is bootable, of course - it has to contain the bootable OS image.

Solution 2 - Amazon Ec2

I don't have enough rep to add a comment to the selected answer, but I do want to point out that for me, /dev/sda1 did not work (did not attach as root), but using /dev/xvda worked (attached as root). The instance is one of the newer t2.micro ones using HVM.

Solution 3 - Amazon Ec2

To elaborate on Diomidis Spinellis's comment in the the accepted answer's comments thread, it's important to check the filesystem label of the device you're attempting to switch in as your new root device. While troubleshooting my own server migration, I had to do the following before my instance would boot up:

Use the e2label command to change the label on the ext2/ext3/ext4 filesystem you've created for your new root device.

First, check the filesystem label for your current root device.

$ sudo e2label /dev/xvda1
cloudimg-rootfs

Set the new device to have the same filesystem label.

$ sudo e2label /dev/xvdg 'cloudimg-rootfs'

In my case, the label was cloudimg-rootfs. Sometimes it will simply be /.

It's important to understand how e2label works; check man e2label on your machine or visit http://linux.die.net/man/8/e2label for more information.

Solution 4 - Amazon Ec2

  1. Stop the EC2 instance.
  2. On Navigation panel, click 'Volumes' under 'Elastic Block Store'.
  3. Choose the existing volume, click 'Actions' and 'Detach volume'. Complete the confirmation.
  4. Choose the new volume, click 'Actions' >> 'Attach volume'
    • In the Attach Volume dialogue box,
    • Instance: Enter Instance ID
    • Device: Enter /dev/sda1

Solution 5 - Amazon Ec2

This is the aws suggested solution You can detach the root volume from the original instance after stopping it. The root volume is attached at /dev/sda1. Once this is detached, please attach it to the new instance. After the volume is attached, you may have to mount it from the OS. After it's mounted, you should see the data within it.

After you've done adding the new key, you can detach it and attach to the original instance at /dev/sda1.

I suggest creating a snapshot of the root volume before making any changes.

Before trying out any solutions just try out in the not important instances or spot instances

Solution 6 - Amazon Ec2

If you are wanting to do this via CloudFormation you will need to do the following:

  1. Create snapshot from existing root volume (via console or CLI)
  2. Create new AMI from this snapshot - increase the root volume size here, make sure you select correct virtualisation time (paravirtual or HVM).
  3. On your AWS::EC2::Instance resource, set the ImageId parameter to the new AMI you have just made

Deploy your stack. This will recreate your instance, so make sure you are using an Elastic IP address or have DNS access.

The reason you have to do this workaround is because CF will not let you adjust root volume size on EC2 BlockDeviceMappings, or to adjust SnapshotId of Root Volume.

Solution 7 - Amazon Ec2

When your volume is mounted, it gets a post-fix with numbers, eg: when /dev/sda is mounted, its mounted as /dev/sda1, /dev/sda2 depending on the partitions you make. As we are mounting the root device itself, it assumes the device is already mounted, so we need to give /dev/sda1 for mounting the volume as root device. Note: There shouldn't be any root volume attached.

Follow these steps:

  1. Go to your volumes, select attach volumes from Action.
  2. Select your instance
  3. For mounting as root, give the device name as /dev/sda1
  4. Start your instance.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionconeView Question on Stackoverflow
Solution 1 - Amazon Ec2Eight-Bit GuruView Answer on Stackoverflow
Solution 2 - Amazon Ec2byl83View Answer on Stackoverflow
Solution 3 - Amazon Ec2JoeyView Answer on Stackoverflow
Solution 4 - Amazon Ec2rakaView Answer on Stackoverflow
Solution 5 - Amazon Ec2yunusView Answer on Stackoverflow
Solution 6 - Amazon Ec2Joe AlamoView Answer on Stackoverflow
Solution 7 - Amazon Ec2PrashanthView Answer on Stackoverflow