If your Linux VM is inaccessible due to any reason, you can try rescue the VM using the following steps.
Required roles
To get the permissions that you need to rescue a VM, ask your administrator to grant you the following IAM roles on the project:
-
Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1
) -
VMs 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 VM. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to rescue a VM:
-
compute.instances.create
on project -
compute.disks.create
on project -
compute.instances.get
on project -
compute.disks.createSnapshot
on disks -
compute.instances.attachDisk
on new VM -
compute.disks.use
on disk -
compute.instances.start
on new and inaccessible VM -
compute.instances.stop
on new and inaccessible VM
You might also be able to get these permissions with custom roles or other predefined roles.
Rescue a VM
If you can't connect to your VM, or your boot disk is full, you must create a temporary VM to rescue the inaccessible VM.
- (Optional) Stop the inaccessible VM.
- Create a snapshot from the boot disk of the inaccessible VM.
- Create a temporary VM using a public image closest to inaccessible VM's OS.
- Check if you're able to connect to the temporary VM using SSH.
Add an additional disk to the temporary VM by following these steps:
In the Google Cloud console, go to the VM instances page.
Click the name of the temporary VM that you created.
Click
Edit.Under Additional disks, click
Add new disk, and then do the following:- Add the disk name, like my-recovery-disk
- For Source type, select the Snapshot tab.
- In the Source snapshot drop-down menu, select the snapshot of the source VM that you created earlier in these steps.
- Click Done.
Click Save.
Connect to the temporary VM using SSH.
Execute the following command:
ls -l /dev/disk/by-id/google-*
The output will be similar to the following:
/dev/disk/by-id/google-my-vm -> ../../sda /dev/disk/by-id/google-my-vm-part1 -> ../../sda1 /dev/disk/by-id/google-my-vm-part14 -> ../../sda14 /dev/disk/by-id/google-my-vm-part15 -> ../../sda15 /dev/disk/by-id/google-my-recovery-disk -> ../../sdb /dev/disk/by-id/google-my-recovery-disk-part1 -> ../../sdb1 /dev/disk/by-id/google-my-recovery-disk-part2 -> ../../sdb2 /dev/disk/by-id/google-my-recovery-disk-part5 -> ../../sdb5
Use the symlinks (
/dev/disk/by-id/google-my-recovery-disk-partN
) to locate the underlying device and partitions for the newly added disk, for example,/dev/sdb1
.The symlink for the disk is either google-DISK_NAME or, if you specified a custom device name for the disk, google-DEVICE_NAME. Make note of the device name that the new disk symlink points to.
Create a mount point at
/mnt/newdisk
:sudo mkdir /mnt/newdisk
Mount the additional disk partition to the mount point
/mnt/newdisk
:sudo mount -o discard,defaults DISK_NAME /mnt/newdisk
Replace DISK_NAME with the device name that you noted earlier in these steps — for example
/dev/sdb1
.If you see the error
Filesystem has duplicate UUID XXXXXX - can't mount
,mount: /mnt/newdisk: wrong fs type, bad option
orbad superblock on /dev/sdb
, use the following command:sudo mount -o nouuid DISK_NAME /mnt/newdisk
The inaccessible VM's file system is now mounted at
/mnt/newdisk
. You can navigate the file system, change config files, fix issues or retrieve the data.
Revert the changes and boot the inaccessible VM back
After the issue is fixed or data is retrieved, you need to bring back the actual VM. Use the following steps to restore the original VM:
Unmount the additional disk which is mounted at
/mnt/newdisk
in the temporary VM:cd ~ sudo umount /mnt/newdisk
In the Google Cloud console, go to the VM instances page.
Select the temporary VM that you created.
Click
Edit.Under Additional disks, click
for the disk created in earlier steps to detach the additional disk from the temporary VM.Click Save.
Go to the VM instances page in the Google Cloud console.
If the inaccessible VM is still running, stop the VM.
Click the name of the VM you just stopped, and then click
Edit.Under Boot disk, click
Detach book disk to detach the exiting boot disk from the inaccessible VM.Next, click Rescue a VM on this page.
CONFIGURE BOOT DISK to attach the disk you created and fixed previously in- In the Boot Disk section, click the Existing disks tab.
- In the drop-down list, select the disk that you created in the previous
section, for example
my-recovery-disk
. - Click Select and then click Save.
You should now be able to connect to the VM using SSH.