This brief guide describes how to rescue virtual machines with Virt-rescue command line utility in Linux. Virt-rescue is like a rescue CD, but for virtual machines only. It provides a simple rescue shell and some recovery tools to inspect and correct problems in a virtual machine or disk images.
Please note that Virt-rescue is different from guestfish. Guestfish is an interactive shell for editing and making changes in virtual machines and disk images. In contrast, Virt-rescue is particularly suited to rescuing a virtual machine that has failed to start. If a VM doesn't boot, you can simply launch a rescue shell on the VM or disk image using virt-rescue and try to fix it using the standard Linux commands.
You can use virt-rescue on any disk image file or device, not just a virtual machine. For example, it can be used on USB drives, SD cards and hard disks. It doesn't require root permission unless you need root to open the disk image. Virt-rescue is part of libguestfs which provides a lot of command line utilities to access and modify virtual machine disk images.
Rescue Virtual Machines With Virt-rescue
Make sure the virtual machines are turned off. You must not run rescue shell on any live virtual machines or disk images.
To run a rescue shell on the problematic virtual machine, do:
$ virt-rescue -d centos8-uefi -i
Replace "centos8-uefi" with your virtual machine's name. You can find the list of available VMs using "virsh list --all" command.
You can also run a rescue shell directly on any disk image(s) like below:
$ virt-rescue -a CentOS_8_Server.img -i
Here, -i (--inspector) flag is used to automatically mount the filesystems of the virtual machines. If you don't use it, you will have to manually mount virtual machine's filesystems in an empty directory called /sysroot.
You will be now landed in an interactive bash shell where you can use many ordinary Linux commands to rescue the virtual machines.
[...] Welcome to virt-rescue, the libguestfs rescue shell. Note: The contents of / (root) are the rescue appliance. Use 'cd /sysroot' or 'chroot /sysroot' to see guest filesystems. groups: cannot find name for group ID 0 ><rescue> The virt-rescue escape key is ‘^]’. Type ‘^] h’ for help.
From the rescue shell, you can start using the ordinary Linux commands to fix the VM.
As stated above, the guest machine's filesystems are mounted in /sysroot directory.
You can verify it with "ls" command:
><rescue> ls /sysroot/ bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr
To chroot into the /sysroot directory first:
><rescue> chroot /sysroot
To exit from rescue shell, type exit and hit enter or press Ctrl+d.
Launch VMs in read-only mode
You should not run rescue shell on any live VMs. But if you still wanted to do it for any reason, use --ro option to use "virt-rescue" safely on a live disk image or VM.
$ virt-rescue -d centos8-uefi -i --ro
$ virt-rescue -a CentOS_8_Server.img -i --ro
The --ro option will launch the VM or disk image in read-only mode. It is ideal for experimenting VMs or disk images. No changes will be actually saved. Once you exit from the shell, all changes will be lost!
Enable network access for VMs
If you want to download file or package into the virtual machine, you can use --network option while running virt-rescue command:
$ virt-rescue -d centos8-uefi -i --network
$ virt-rescue -a CentOS_8_Server.img -i --network
This option enables network access inside the rescue shell, so you can download stuffs from internet.
Virt-rescue has many options. For more details, refer man pages:
$ man virt-rescue