Mount and repair a KVM / Xen virtual guest image file

Once upon a time there was a very old and grumpy server with ancient virtual guests. So it came to be that a major power failure occurred rendering one of the guests useless. Logs told me that it might due to problems with file systems so here is how to mount and fsck them.

Firstly check the first available loopback device

losetup -f

Use the device shown by this command to set up the device

losetup /dev/loop0 <image>
kpartx -av /dev/loop0

Observe with fdisk

fdisk -l /dev/loop0

Identify the file system on each partition

file -Ls /dev/mapper/loop0p1

Repair with the tool appropriate to the file system type

fsck.ext4 /dev/mapper/loop0p1