Fix an invalid GRUB configuration on a Linux instance
Repair an invalid GRUB configuration that prevents a Linux ECS instance from starting.
Prerequisites
The instance health diagnostics tool detected an invalid GRUB configuration on the instance.
Background
Problem description: A Linux instance fails to start due to an invalid GRUB configuration.
Cause: The GRUB configuration contains a root file system parameter. If this parameter does not point to the correct partition on the system disk, such as /dev/vda1, or if the UUID does not match the root file system, the kernel cannot load the root file system and the instance fails to start.
Prerequisites
These solutions apply only to Linux systems in legacy boot mode, not UEFI. Verify the boot mode before you proceed.
-
Find a Linux instance with the same instance type.
If no such instance exists, create one using the wizard.
-
Log on to the instance. See Connect to an instance using a VNC connection.
-
Verify the boot mode.
Check the firmware directory:
ls /sys/firmware/Sample output:
acpi dmi memmap qemu_fw_cfg-
If no EFI directory exists, the instance uses the legacy boot mode. Proceed with Fix the GRUB configuration using a repair disk or Fix the GRUB configuration using an intermediate instance.
-
If the EFI directory exists, the instance uses UEFI boot mode. Submit a ticket for assistance.
-
Fix the GRUB configuration using a repair disk
This solution applies only to Linux systems in legacy boot mode, not UEFI.
- Connect to the problematic instance remotely.When a repair disk is attached to an ECS instance, you can connect to the instance only using a VNC connection. For more information, see Connect to an instance using VNC.
- View the mount information of the original system disk of the problematic instance.On the repair disk, the file system of the original system disk is mounted on a temporary directory. You can find the information about the temporary directory in one of the following ways:
- In the Mounted Instance section on the system disk details page, the temporary directory uses the format
/tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse****, wherebp19bspzms79kqse****is the serial number of the original system disk of the instance. - On the repair disk, run the mount command to view the information. For example, if the device path of the original system disk is /dev/vda, the command is:
mount | grep /dev/vdaThe following output is returned:/dev/vda1 on /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse**** type ext4 (rw,relatime)
- In the Mounted Instance section on the system disk details page, the temporary directory uses the format
-
Mount virtual file systems (/dev, /proc, /sys) to the chroot environment so that grub2-mkconfig can detect the root partition.
Sample commands:
mount --bind /dev /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse****/dev mount --bind /proc /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse****/proc mount --bind /sys /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse****/sys - Run the chroot command to switch the root directory to the temporary path of the original system disk and enter the chroot environment.You must repair the files in the temporary path of the original system disk. For example, if the temporary path is /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse****, the command is:
chroot /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse**** -
Back up the GRUB configuration file.
Back up /boot/grub2/grub.cfg to /root/grub.cfg for rollback if needed.
cp /boot/grub2/grub.cfg /root/grub.cfg -
Regenerate the GRUB configuration file.
Detect the root file system UUID and write it to the GRUB configuration file:
grub2-mkconfig -o /boot/grub2/grub.cfg -
Verify the root file system parameter in the GRUB configuration file.
Sample command:
grep -n -E "(kernel|linux[16]*[efi]*)[[:space:]]+(/boot)?/vmlinuz-.*root=" /boot/grub2/grub.cfg /boot/grub2/grubenvSample output:
/boot/grub2/grub.cfg:99: linux16 /boot/vmlinuz-3.10.0-514.26.2.el7.x86_64 root=UUID=48609326-10e3-40c2-93b3-3f0d9798**** ro crashkernel=auto net.ifnames=0 console=tty0 console=ttyS0,115200n8 /boot/grub2/grub.cfg:114: linux16 /boot/vmlinuz-3.10.0-327.el7.x86_64 root=UUID=48609326-10e3-40c2-93b3-3f0d9798**** ro crashkernel=auto net.ifnames=0 console=tty0 console=ttyS0,115200n8 /boot/grub2/grub.cfg:128: linux16 /boot/vmlinuz-0-rescue-36dc953195bb4389870d2591ff6bfea5 root=UUID=48609326-10e3-40c2-93b3-3f0d9798**** ro crashkernel=auto net.ifnames=0 console=tty0 console=ttyS0,115200n8 -
Exit the repair environment and verify the instance status.
-
Exit the chroot environment.
-
Unmount the virtual file systems.
Sample commands:
umount /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse****/dev umount /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse****/proc umount /tmp/ecs-offline-diagnose_disk-bp19bspzms79kqse****/sys -
On the Offline Diagnosis page in the ECS console, detach the repair disk.
-
Connect to the repaired instance and verify that it works.
-
Fix the GRUB configuration using an intermediate instance
Detach the system disk from the problematic instance, attach it to an intermediate instance (another Linux instance in the same region), and fix the GRUB configuration.
-
Detach the system disk from the problematic instance and attach it as a data disk to the intermediate instance. See Steps 1 to 3 in How to log on to a problematic system disk by attaching it as a data disk to an ECS instance.
NoteStop at the
mount /dev/vdb1 /mntstep. Do not runchroot /mnt. If you already ranchroot /mnt, runexitfirst. -
Mount virtual file systems (/dev, /proc, /sys) to the chroot environment so that grub2-mkconfig can detect the root partition.
Sample commands:
mount --bind /dev /mnt/dev mount --bind /proc /mnt/proc mount --bind /sys /mnt/sys -
Switch to the chroot environment.
chroot /mntThe /dev, /proc, and /sys directories should now contain the same mappings as on a normal instance.
-
Back up the GRUB configuration file.
Back up /boot/grub2/grub.cfg to /root/grub.cfg for rollback if needed.
cp /boot/grub2/grub.cfg /root/grub.cfg -
Regenerate the GRUB configuration file.
Detect the root file system UUID and write it to the GRUB configuration file:
grub2-mkconfig -o /boot/grub2/grub.cfg -
Verify the root file system parameter in the GRUB configuration file.
Sample command:
grep -n -E "(kernel|linux[16]*[efi]*)[[:space:]]+(/boot)?/vmlinuz-.*root=" /boot/grub2/grub.cfg /boot/grub2/grubenvSample output:
/boot/grub2/grub.cfg:99: linux16 /boot/vmlinuz-3.10.0-514.26.2.el7.x86_64 root=UUID=48609326-10e3-40c2-93b3-3f0d9798**** ro crashkernel=auto net.ifnames=0 console=tty0 console=ttyS0,115200n8 /boot/grub2/grub.cfg:114: linux16 /boot/vmlinuz-3.10.0-327.el7.x86_64 root=UUID=48609326-10e3-40c2-93b3-3f0d9798**** ro crashkernel=auto net.ifnames=0 console=tty0 console=ttyS0,115200n8 /boot/grub2/grub.cfg:128: linux16 /boot/vmlinuz-0-rescue-36dc953195bb4389870d2591ff6bfea5 root=UUID=48609326-10e3-40c2-93b3-3f0d9798**** ro crashkernel=auto net.ifnames=0 console=tty0 console=ttyS0,115200n8 -
Exit the repair environment and verify the instance status.
-
Exit the chroot environment.
-
Unmount the virtual file systems.
Sample commands:
umount /mnt/dev umount /mnt/proc umount /mnt/sys -
Unmount the system disk.
Sample command:
umount /mnt -
Reattach the system disk to the original instance. See Step 5 in How to log on to a problematic system disk by attaching it as a data disk to an ECS instance.
-
Connect to the repaired instance and verify that it works.
-