This document explains how to rescue an inaccessible Linux Compute Engine instance by using a temporary rescue instance.
To identify why a compute instance fails to boot before you rescue it, see Troubleshoot Linux Compute Engine instance boot issues.
Required roles
To get the permissions that you need to rescue a compute instance, ask your administrator to grant you the following IAM roles on the project:
- Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1) -
Compute instances that use a service account:
Service Account User (
roles/iam.serviceAccountUser)
For more information about granting roles, see Manage access to projects, folders, and organizations.
These predefined roles contain the permissions required to rescue a compute instance. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to rescue a compute instance:
-
compute.instances.createon project -
compute.disks.createon project -
compute.instances.geton project -
compute.disks.createSnapshoton disks -
compute.instances.attachDiskon rescue compute instance -
compute.disks.useon disk -
compute.instances.starton rescue and inaccessible compute instances -
compute.instances.stopon rescue and inaccessible compute instances
You might also be able to get these permissions with custom roles or other predefined roles.
Rescue a compute instance
If you can't connect to your compute instance, or if its boot disk is full, then you can create a temporary rescue instance to inspect, repair, or recover data from the inaccessible boot disk.
- Optional: Stop the inaccessible compute instance.
- Create a snapshot from the boot disk of the inaccessible compute instance, and note the size of the original boot disk. If the root file system is split across multiple disks, then you must snapshot each disk.
Create a rescue compute instance by using any compatible Linux public image. Google recommends creating the rescue instance by using a different operating system (OS) image than the inaccessible instance. Using a different image helps prevent universally unique identifier (UUID) conflicts that can cause the rescue instance to boot from the inaccessible disk. If you plan to use the
chrootcommand or OS-specific repair tools for the file system, then select a different image from the same OS family.If the original boot disk is 100% full, then create the rescue instance from a public image rather than from the snapshot. Provision sufficient free space on the rescue instance's boot disk so that the rescue instance doesn't fail with a full-disk error.
In some cases, a trusted image policy might restrict you from creating boot disks from public images. In such cases, you must ask an administrator to temporarily lift this restriction before you can create a rescue instance. For more information, see Set image access constraints.
Make sure that the rescue instance is running, and verify that you can connect to the rescue instance by using SSH.
While the rescue instance is running, create a recovery disk from each snapshot that you created earlier. Attach each recovery disk to the running rescue instance without stopping the rescue instance:
In the Google Cloud console, go to the VM instances page.
Click the name of the rescue instance that you created.
Click Edit.
Under Additional disks, click Add new disk, and then do the following:
- In the Name field, enter a disk name, such as
my-recovery-disk. - For Source type, select the Snapshot tab.
- In the Source snapshot list, select the snapshot of the inaccessible compute instance that you created earlier.
- In the Size field, specify the disk size. If the original compute instance became inaccessible because its boot disk is 100% full, then enter a size larger than the original boot disk. A larger recovery disk gives you room to expand the partition and file system, preventing the full-disk failure from recurring after recovery.
- Click Done.
- In the Name field, enter a disk name, such as
Click Save.
Identify the name of each disk that you attached to the rescue instance by running the following command:
lsblk -d -o NAME,SERIAL
The output is similar to the following:
NAME SERIAL sda rescue-vm sdb my-recovery-disk
In this example,
sda(rescue-vm) is the boot disk of the rescue instance andsdb(my-recovery-disk) is the recovery disk created from the snapshot of the inaccessible instance. Note theNAMEvalue of the recovery disk for the next step.For each disk that you attached to the rescue instance, complete the following steps:
Identify the file system of each partition:
fdisk -l /dev/NAME -o Device,Size,Type
Replace
NAMEwith the name of the inaccessible compute instance's boot disk from the preceding step. In this example, the name issdb.The output is similar to the following:
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors Disk model: PersistentDisk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disklabel type: gpt Disk identifier: B31430F1-F041-4555-96B9-B2F43DC057AD Device Size Type /dev/sdb1 2M BIOS boot /dev/sdb2 20M EFI System /dev/sdb3 10G Linux filesystem
The
Typecolumn lists the file system of each partition. If the file system type is missing for any partitions, then run the following command:file -sL /dev/PARTITION_NAME
Replace
PARTITION_NAMEwith the name of the partition, such assdb3.The output differs depending on the file system type:
No file system: If the output only displays the
datastring, then the partition doesn't contain a file system. The output is similar to the following:/dev/sdb1: data
Extensible Firmware Interface (EFI) file system: If the output describes a DOS/MBR boot sector, then the partition has an EFI file system. The output is similar to the following:
dev/sdb2: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 4, reserved sectors 4, root entries 512, sectors 40960 (volumes <=32 MB), Media descriptor 0xf8, sectors/FAT 40, sectors/ track 32, heads 64, serial number 0xf2af2664, label: "EFI ", FAT (16 bit)
Linux file system: If the output describes file system data, then the partition is a Linux file system. The output is similar to the following:
/dev/sdb3: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs)
Note the partition name for the Linux file system.
Create a mount point at
/rescue:sudo mkdir /rescue
Mount the partition for the Linux file system to
/rescue:sudo mount /dev/PARTITION_NAME /rescue
Replace
PARTITION_NAMEwith the name of the partition for the Linux file system that you noted earlier, such assdb3.If your disk has a separate
/bootpartition or an EFI System Partition, such as/dev/sdb2in the preceding output, then mount them before entering thechrootenvironment. Mounting these partitions makes the bootloader and kernel files accessible:# Run only if your disk has a separate /boot partition: sudo mount /dev/BOOT_PARTITION_NAME /rescue/boot # Run only if your disk has an EFI System Partition on UEFI instances: sudo mount /dev/EFI_PARTITION_NAME /rescue/boot/efi
Replace the following:
BOOT_PARTITION_NAME: the partition name of the separate/bootpartition, if present, such assdb1.EFI_PARTITION_NAME: the partition name of the EFI System Partition on Unified Extensible Firmware Interface (UEFI) compute instances, such assdb2.
Optional: If the original boot disk was 100% full, then free up space or expand the file system before entering the
chrootenvironment. Running resize commands from the rescue instance uses the rescue instance's/tmpdirectory instead of the full disk.Free up space: Delete unnecessary files, such as large log files under the
/rescue/var/logdirectory, and then verify available space by running thedf -h /rescuecommand.Expand the partition and file system: If you created a larger recovery disk, then complete the following steps on the rescue instance to use the added space:
Expand the partition:
sudo growpart /dev/NAME PARTITION_NUMBER
Replace the following:
NAME: the device name of the recovery disk, such assdbPARTITION_NUMBER: the number of the partition for the Linux file system, such as3for/dev/sdb3
If the rescue instance doesn't have the
growparttool installed, then resize the partition by using thepartedtool as described in Resize the file system and partitions.Extend the file system for your file system type:
- For
ext4file systems, extend the file system:
sudo resize2fs /dev/PARTITION_NAME
Replace
PARTITION_NAMEwith the name of the partition for the Linux file system, such assdb3.- For
xfsfile systems, extend the file system:
sudo xfs_growfs /rescue
- For
Verify that free space is available on the recovery disk:
df -h /rescue
If freeing space or expanding the file system resolves the issue, then you don't need the
chrootenvironment. Skip the remainingchrootsubsteps and continue to Revert the changes and restart the inaccessible compute instance.Mount the virtual file systems and devices so that you can modify the root directory of the file system by using the
chrootcommand:sudo mount -t proc /proc /rescue/proc sudo mount -t sysfs /sys /rescue/sys sudo mount -o bind /dev /rescue/dev sudo mount -o bind /dev/pts /rescue/dev/pts sudo mount -o bind /run /rescue/run
Change the root directory to the mounted file system:
sudo chroot /rescue
The file system of the inaccessible boot disk is mounted at
/rescue, and your shell runs in achrootenvironment on that file system. You can navigate the file system, change configuration files, fix issues, or retrieve data.If your compute instance fails to boot with a GRUB
module not founderror, then repair the GRUB bootloader from inside thechrootenvironment. The steps depend on the firmware type of your compute instance:Legacy Basic Input/Output System (BIOS) compute instances: Use these steps for
i386-pccompute instances, such as when the serial console shows thefile '/grub2/i386-pc/increment.mod' not founderror:- RHEL, CentOS, Rocky Linux, and SUSE:
grub2-install --target=i386-pc /dev/NAME grub2-mkconfig -o /boot/grub2/grub.cfg
- Debian and Ubuntu:
grub-install --target=i386-pc /dev/NAME update-grub
Replace
NAMEwith the whole-disk device name of the attached recovery disk from earlier, such assdb, not a partition name such assdb1.UEFI and Shielded VM instances: Make sure that you mounted the
/rescue/boot/efidirectory first. Don't run thegrub2-installcommand on UEFI RHEL, CentOS, or Rocky Linux compute instances because that command overwrites the signed Secure Boot shim:- RHEL, CentOS, and Rocky Linux: Use the
yumcommand instead of thednfcommand on CentOS 7 and RHEL 7:
dnf reinstall -y shim-* grub2-efi-* grub2-common grub2-mkconfig -o /boot/grub2/grub.cfg
- SUSE:
shim-install --config-file=/boot/grub2/grub.cfg grub2-mkconfig -o /boot/grub2/grub.cfg
- Debian and Ubuntu:
grub-install --efi-directory=/boot/efi --no-nvram update-grub
- RHEL, CentOS, and Rocky Linux: Use the
Revert the changes and restart the inaccessible compute instance
After you fix the issue or retrieve your data, use the following steps to restore the original compute instance:
If you entered the
chrootenvironment, then exit thechrootenvironment:exit
Unmount the virtual file systems, devices, and root file system of the inaccessible compute instance from the rescue instance:
If you mounted an EFI or a separate
/bootpartition, then unmount the/rescue/boot/efidirectory followed by the/rescue/bootdirectory before you unmount the root file system by running thesudo umount /rescuecommand. If you skipped mounting virtual file systems for thechrootenvironment, then run only thecd /andsudo umount /rescuecommands.cd / sudo umount /rescue/run sudo umount /rescue/dev/pts sudo umount /rescue/dev sudo umount /rescue/sys sudo umount /rescue/proc sudo umount /rescue
In the Google Cloud console, go to the VM instances page.
Select the rescue instance that you created.
Click Edit.
Under Additional disks, click for the recovery disk that you created earlier to detach that disk from the rescue instance.
Click Save.
Go to the VM instances page in the Google Cloud console.
If the inaccessible compute instance is still running, then stop the compute instance.
Click the name of the compute instance that you stopped, and then click Edit.
Under Boot disk, click Detach boot disk to detach the existing boot disk from the inaccessible compute instance.
Click CONFIGURE BOOT DISK to attach the disk that you created and fixed in Rescue a compute instance:
- In the Boot Disk section, click the Existing disks tab.
- In the list, select the disk that you created in the preceding
section, such as
my-recovery-disk. - Click Select and then click Save.
Connect to the compute instance by using SSH to verify access.