How to recover a qcow2 file using fsck

I just upgrade Ubuntu on one of my virtual machines and for some reason the file system was corrupted after I rebooted the server. I got errors like:

ext4-fs error ext4_lookup deleted inode referenced

Great. Here is how I fixed the problem.

I found the solution was to attach the qcow2 file as a device on the host and then use fsck. Obviously, you will need to shutdown the virtual machine before doing this.

On the host, enable the nbd (network block device) kernel module. This should be available on all Ubuntu servers after Intrepid.

sudo modprobe nbd max_part=8

You can this use qemu-nbd to connect your qcow2 file as a network block device.

sudo qemu-nbd --connect=/dev/nbd0 /mnt/kvm/wordpress-sites.qcow2

You can then find the partitions using

sudo fdisk /dev/nbd0

On the virtual machine, partition 1 was corrupted, so here is the command I ran:

sudo fsck /dev/nbd0p1

Then disconnect the disk:

sudo qemu-nbd --disconnect /dev/nbd0

Everything seemed to be fine and so I started the domain up again.

One thought on “How to recover a qcow2 file using fsck

Leave a Reply