126

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.

Usama Abdulrehman
  • 1,041
  • 3
  • 11
  • 21
cone
  • 1,665
  • 3
  • 13
  • 13
  • If you are using AWS web-based console, your instance doesn’t have any other volumes attached, and you are not sure what device ID you are supposed to use for boot volume, you can just attach a volume to your instance without specifying any ID and start the instance. AWS console will complain that there’s nowhere to boot from and tell you which device ID you should use (in my case it was `/dev/sda1`). You can then detach the volume and reattach, this time specifying that device ID. – Anton Strogonoff Jan 11 '21 at 07:56

8 Answers8

229

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 or /dev/xvda (based on AMI used)
  4. Start the instance.

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

Sarath Ak
  • 7,903
  • 2
  • 47
  • 48
Eight-Bit Guru
  • 9,756
  • 2
  • 48
  • 62
  • If I detach the root EBS volume, the instance get terminated! – MUH Mobile Inc. Aug 10 '12 at 03:56
  • The AWS API (which the console uses) won't let you detach the root EBS volume from a running instance - but I'm not sure whether Force Detach obeys the same rule - did you do that by mistake? Or perhaps your instance is not EBS-backed, in which case this question/answer doesn't apply - I don't know what would happen if you tried to detach the boot volume from a non-EBS-backed VM. – Eight-Bit Guru Aug 10 '12 at 08:58
  • 57
    The key is "as the root" -- `/dev/sda1` indeed! – Aseem Kishore Aug 31 '12 at 13:47
  • 6
    Thanks, this is great. One gotcha is that the DNS/IP can change when this happens, so make sure to update any DNS / Elastic IPs, et cetera. I spent a while trying to figure out why I couldn't ssh into it anymore, and this was the reason. – mrooney Jan 17 '13 at 22:54
  • I just did this but `/dev/sda/` worked for me and not `/dev/sda1`. – Matt Connolly Mar 07 '13 at 06:28
  • 1
    Thanks, this answer just saved me so much time! The "/dev/sda1" was very important for me (I tried it as just "sda1" the first time, but no root volume showed up). – Darren Cook Jun 07 '13 at 05:54
  • Here is what you can indicate as letter http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-attaching-volume.html – conecon Nov 01 '13 at 03:10
  • Thank you, is it the same case for reserved instances? – naveed Mar 11 '14 at 08:53
  • 1
    Don't forget to ensure that the new volume has the appropriate label or UUID. For instance, on AWS Linux instances prepare the alternate volume by using `e2label /dev/your-alternate-volume '/'`. – Diomidis Spinellis Apr 19 '14 at 12:52
  • 21
    Neither `/dev/sda` nor `/dev/sda1` worked for me, but `/dev/xvda` did. – bhspencer Feb 05 '15 at 14:37
  • 11
    Man, that was scary. `/dev/sda` -- didn't work. `/dev/sda1`, `dev/sdf` -- worked, but instance did't boot. `/dev/xvda` worked. – Alex Fortuna May 18 '16 at 18:41
  • 16
    The name which you should use is the result of this command: `aws ec2 describe-instances --instance-id i-XXXXXXX | grep -i rootDeviceName` in my case `/dev/xvda` – Neoecos Nov 29 '16 at 23:44
  • 1
    I was thrown off by AWS’s hint, which reads “Windows Devices: xvdf through xvdp”. You can type `/dev/sda1` there and it’ll work. – duozmo May 29 '17 at 16:39
  • just finished resurrecting one of the dead box, we have to provide the device name as /dev/xvda1. AWS doesn't allow you directly to use /dev/sda for the EBS volume. Here is the list which is assigned to instances based on HVM OR PVM. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html – samtoddler Feb 23 '18 at 03:32
  • Note that you can not detach the Volume from the EC2 instance, you have to detach the EC2 instance from the Volume ;-) – bikeman868 Sep 26 '18 at 04:36
  • 1
    The `/dev/sda1` was the key for me. **Do note the old device name** before detaching the old volume. – Antti Haapala -- Слава Україні Feb 13 '19 at 14:14
  • 1
    Somehow, mine was `xvda`... no leading `/dev/` on it, which caused my all kinds of headaches. Thanks @Neoecos for the tip on how to find out what the volume name was expected to be. – Christopher Schultz Jun 18 '19 at 19:21
  • 1
    The key for me was that although amazon blocked attaching as /dev/sda, it allowed me to attach as /dev/sda1 and that worked. So behind the scenes AWS EC2 figures out the boot partition etc. – dllahr Jan 09 '20 at 19:30
  • Is now apparently possible to do it on a running instance: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-restoring-volume.html#considerations – Dario Apr 30 '21 at 09:59
  • Yes, but beware the snapshot has to have come from the volume, so you can only do this once before requiring a new snapshot as the volume id will change. – mckenzm Oct 12 '22 at 03:11
49

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.

byl83
  • 736
  • 5
  • 8
  • I think the machines which are in VPC, device names of format /dev/xv* itself works and you do not need to give names in format /dev/sd*. This is an observation - I am no expert to say this with confidence. – Mayank Jaiswal Apr 10 '15 at 10:45
  • I have an instance matching this description, which is unable to boot ("Unable to find root device") after detaching and reattaching the root device. I want to try this answer, but if I don't attach it as `/dev/sda1` then AWS refuses to start my instance. Is there some other setting I need to configure to use an alternate root device name? – Jack O'Connor Oct 02 '15 at 18:43
  • /dev/sda1 for Linux /dev/xvda for Windows – Luis Oct 13 '15 at 23:48
  • 1
    whoopse, make sure to identify the partition, /dev/sda **1**, `/dev/sda` is invalid but `/dev/sda1` is fine. – ThorSummoner Jun 17 '16 at 03:39
  • And to add why it worked. this is why: https://superuser.com/questions/356533/whats-the-difference-between-dev-sda1-and-dev-xvda1#:~:text=1%20Answer&text=The%20%2Fdev%2Fsda%20device%20is,the%20one%20you%20should%20use. – human Nov 19 '21 at 11:57
  • They probably want us to keep our "grubby" mitts off the bootloaders – mckenzm Oct 12 '22 at 03:13
19

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.

Joey
  • 1,651
  • 15
  • 30
  • 1
    This is so incredibly important, I can't understand why this answer is not more commonly found. – ays0110 Aug 26 '16 at 07:40
6
  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
raka
  • 556
  • 9
  • 18
  • 4
    Also note that the dialog implies that /dev/sda1 cannot be used, but ignore that and continue anyways, it works – Charles L. Oct 06 '21 at 18:15
3

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

yunus
  • 2,445
  • 1
  • 14
  • 12
2

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.

Joe Alamo
  • 133
  • 1
  • 7
0

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.

Prashanth
  • 91
  • 6
0

Last week AWS announced new way to replacing Root volume with less downtime and Without Stop and Start EC2 instance. Please have a look below link. EC2 Replace Root Volume