ISheep

ISheep

Badminton | Coding | Writing | INTJ
github

How to enter emergency mode for AWS EC2 Linux instances without deleting the instance and restoring it

Correct way to handle automatic mounting of volumes when restarting instances

Background#

  1. Operating System: Amazon Linux 2023
  2. Instance Type: t2.micro

I wanted to mount a new volume to my EC2 instance, but when I restarted the instance, the mounted volume disappeared. So, I searched online for how to automatically mount it upon reboot. I tried modifying the /etc/fstab file and then restarted, but I couldn't connect via SSH. After checking the logs, I realized that I had entered emergency mode... I couldn't even access the interactive interface. "Clearly, I messed up the first time."

image
Initially, I thought it was a problem with my proxy, but even after disabling it, I still couldn't connect.

image

I checked the system logs.

image

image

But AWS also provided a solution for failed startups, and there was a small line of text below.

image

Result:

image

Initially, I kept looking at the documentation, trying to find out which type of instance would support it, and even tried changing the instance type, but for some reason, I still couldn't connect... Finally, I found a method to recover by temporarily starting a new instance for recovery and modifying the damaged file.

Recovery Method#

Basically, it involves the following steps:

  1. Stop the failed instance.
  2. Detach the root volume from the failed instance.
  3. Create a new EC2 instance in the same availability zone.
  4. Attach the root volume to the new instance.
  5. Modify the /etc/fstab file.

Stop the Instance#

Just wait for it to stop. The purpose is to detach the volume.
image

Detach the Volume#

Find the volume mounted on the root ("/") directory and click on "Detach".

image

Start a New Instance#

Configure it with the simplest settings, as the purpose is just to attach the previously detached volume.

Remember to specify the subnet (to select the same availability zone as the volume, for example, mine is us-west-1b).

image

Attach the Volume#

In the AWS management console, attach the previously detached volume to the new instance, and SSH into the new instance to mount it.

image

Here, you can see the content that we mistakenly modified earlier.

image

Use vim to directly modify the fstab file. After making the necessary changes, detach the volume and attach it back to the previously non-bootable instance. Remember to enter "xvda" as the name when attaching it back, as that was the default name I saw for the root volume in AWS.

image

Finally, start the old instance, and the newly created instance can be deleted.

image
Success!

References#

Troubleshoot EC2 Linux instances in emergency mode | AWS

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.