Rescue an inaccessible VM

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:

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.create on project
  • compute.disks.create on project
  • compute.instances.get on project
  • compute.disks.createSnapshot on disks
  • compute.instances.attachDisk on rescue compute instance
  • compute.disks.use on disk
  • compute.instances.start on rescue and inaccessible compute instances
  • compute.instances.stop on 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.

  1. Optional: Stop the inaccessible compute instance.
  2. 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.
  3. 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 chroot command 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.

  4. Make sure that the rescue instance is running, and verify that you can connect to the rescue instance by using SSH.

  5. 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:

    1. In the Google Cloud console, go to the VM instances page.

      Go to VM instances

    2. Click the name of the rescue instance that you created.

    3. Click Edit.

    4. Under Additional disks, click Add new disk, and then do the following:

      1. In the Name field, enter a disk name, such as my-recovery-disk.
      2. For Source type, select the Snapshot tab.
      3. In the Source snapshot list, select the snapshot of the inaccessible compute instance that you created earlier.
      4. 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.
      5. Click Done.
    5. Click Save.

  6. 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 and sdb (my-recovery-disk) is the recovery disk created from the snapshot of the inaccessible instance. Note the NAME value of the recovery disk for the next step.

  7. For each disk that you attached to the rescue instance, complete the following steps:

    1. Identify the file system of each partition:

      fdisk -l /dev/NAME -o Device,Size,Type
      

      Replace NAME with the name of the inaccessible compute instance's boot disk from the preceding step. In this example, the name is sdb.

      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 Type column 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_NAME with the name of the partition, such as sdb3.

      The output differs depending on the file system type:

      • No file system: If the output only displays the data string, 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.

    2. Create a mount point at /rescue:

      sudo mkdir /rescue
    3. Mount the partition for the Linux file system to /rescue:

      sudo mount /dev/PARTITION_NAME /rescue
      

      Replace PARTITION_NAME with the name of the partition for the Linux file system that you noted earlier, such as sdb3.

      If your disk has a separate /boot partition or an EFI System Partition, such as /dev/sdb2 in the preceding output, then mount them before entering the chroot environment. 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 /boot partition, if present, such as sdb1.
      • EFI_PARTITION_NAME: the partition name of the EFI System Partition on Unified Extensible Firmware Interface (UEFI) compute instances, such as sdb2.
    4. Optional: If the original boot disk was 100% full, then free up space or expand the file system before entering the chroot environment. Running resize commands from the rescue instance uses the rescue instance's /tmp directory instead of the full disk.

      • Free up space: Delete unnecessary files, such as large log files under the /rescue/var/log directory, and then verify available space by running the df -h /rescue command.

      • 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:

        1. Expand the partition:

          sudo growpart /dev/NAME PARTITION_NUMBER
          

          Replace the following:

          • NAME: the device name of the recovery disk, such as sdb
          • PARTITION_NUMBER: the number of the partition for the Linux file system, such as 3 for /dev/sdb3

          If the rescue instance doesn't have the growpart tool installed, then resize the partition by using the parted tool as described in Resize the file system and partitions.

        2. Extend the file system for your file system type:

          • For ext4 file systems, extend the file system:
          sudo resize2fs /dev/PARTITION_NAME
          

          Replace PARTITION_NAME with the name of the partition for the Linux file system, such as sdb3.

          • For xfs file systems, extend the file system:
          sudo xfs_growfs /rescue
          
        3. 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 chroot environment. Skip the remaining chroot substeps and continue to Revert the changes and restart the inaccessible compute instance.

    5. Mount the virtual file systems and devices so that you can modify the root directory of the file system by using the chroot command:

      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
      
    6. 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 a chroot environment 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 found error, then repair the GRUB bootloader from inside the chroot environment. The steps depend on the firmware type of your compute instance:

    • Legacy Basic Input/Output System (BIOS) compute instances: Use these steps for i386-pc compute instances, such as when the serial console shows the file '/grub2/i386-pc/increment.mod' not found error:

      • 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 NAME with the whole-disk device name of the attached recovery disk from earlier, such as sdb, not a partition name such as sdb1.

    • UEFI and Shielded VM instances: Make sure that you mounted the /rescue/boot/efi directory first. Don't run the grub2-install command 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 yum command instead of the dnf command 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
      

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:

  1. If you entered the chroot environment, then exit the chroot environment:

     exit
     

  2. 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 /boot partition, then unmount the /rescue/boot/efi directory followed by the /rescue/boot directory before you unmount the root file system by running the sudo umount /rescue command. If you skipped mounting virtual file systems for the chroot environment, then run only the cd / and sudo umount /rescue commands.

     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
     

  3. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

    1. Select the rescue instance that you created.

    2. Click Edit.

    3. Under Additional disks, click for the recovery disk that you created earlier to detach that disk from the rescue instance.

    4. Click Save.

  4. Go to the VM instances page in the Google Cloud console.

    Go to VM instances

    1. If the inaccessible compute instance is still running, then stop the compute instance.

    2. Click the name of the compute instance that you stopped, and then click Edit.

    3. Under Boot disk, click Detach boot disk to detach the existing boot disk from the inaccessible compute instance.

    4. Click CONFIGURE BOOT DISK to attach the disk that you created and fixed in Rescue a compute instance:

      1. In the Boot Disk section, click the Existing disks tab.
      2. In the list, select the disk that you created in the preceding section, such as my-recovery-disk.
      3. Click Select and then click Save.
    5. Start the compute instance.

  5. Connect to the compute instance by using SSH to verify access.

What's next