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.

KVM: Host CPU does not provide required features

I just exported / copied a virtual machine from one of our servers (a HP Proliant) to my laptop so that I could to mess around with it offline. I used the following commands to export the domain.

On the server:

virsh dumpxml wordpress > /tmp/wordpress.xml

On the laptop:

virsh define wordpress.xml
virsh wordpress start

However, I got this error on the laptop:

error: unsupported configuration: guest and host CPU are not compatible: Host CPU does not provide required features: rdtscp, popcnt, sse4.2, dca

The HP server has xeon processors and my laptop has a little core two duo. Obviously the CPU’s had different capabilities. You can see the cpu definitions in the wordpress.xml file were:

less wordpress.xml
<cpu match='exact'>
    <feature policy='require' name='tm2'/>
    <feature policy='require' name='est'/>
    <feature policy='require' name='monitor'/>
    <feature policy='require' name='ds'/>
    <feature policy='require' name='ss'/>
    <feature policy='require' name='vme'/>
    <feature policy='require' name='rdtscp'/>
    <feature policy='require' name='ht'/>
    <feature policy='require' name='dca'/>
    <feature policy='require' name='pbe'/>
    <feature policy='require' name='tm'/>
    <feature policy='require' name='vmx'/>
    <feature policy='require' name='ds_cpl'/>
    <feature policy='require' name='xtpr'/>
    <feature policy='require' name='acpi'/>

You can also view the same information using virsh capabilities command. It is worth piping it through less because the command produces alot of output and the host cpu information is at the top.

For the laptop:

virsh capabilities | less
    <topology sockets='1' cores='2' threads='1'/>
    <feature name='pdcm'/>
    <feature name='xtpr'/>
    <feature name='tm2'/>
    <feature name='est'/>
    <feature name='smx'/>
    <feature name='vmx'/>
    <feature name='ds_cpl'/>
    <feature name='monitor'/>
    <feature name='dtes64'/>
    <feature name='pbe'/>
    <feature name='tm'/>
    <feature name='ht'/>
    <feature name='ss'/>
    <feature name='acpi'/>
    <feature name='ds'/>
    <feature name='vme'/>

So, to fix the issue, I just edited the domains xml definition and put in the correct features.

virsh edit wordpress

Note – if you are using ubuntu on your desktop and have virt-manager installed (which is an awesome gui application which runs on Linux and Macs). You can run virt-manager, open the virtual machine, go to the information tab, select the processor and then “copy host cpu configuration”. I’ve attached a screenshot of how to do this.


I hope this helps someone.