Virtual disk data recovery

The last weekend was terrible. On Friday evening I was writing some VHDL code for a small project and testing step-by-step all the stuff through a VHDL simulator running on Windows. Obviously, to do this I used a VirtualBox virtual machine, since my host OS is a Linux distro. Once I finished to work I simply saved it and closed the VM. The last backup of the whole code had been made two days before, so the Friday’s work was not safe in case of data loss (serious mistake!!!). The next morning I turned on the Windows VM and… puff! Inexplicably a critical error prevented the VM to start. It was very frustrating… Fortunately after some tries I found a way to access the virtual disk and make a copy of the files. Here below are listed all the steps I followed.

The first step is to convert your virtual disk from .vmdk to .vdi. This can be done as normal user:

VBoxManage clonehd --format VDI /path/to/vm_disk_input.vmdk /path/to/vm_disk_output.vdi

The following commands need the superuser privileges.

Install qemu. On Fedora (22) you can do it using dnf:

dnf install qemu

Load the nbd kernel module. The max_part parameter tells you the maximum number of partitions to be accessed. If you don’t specify anything, the default is 0 (no partition is mapped):

modprobe nbd max_part=16

Connect the disk image to one of the 16 devices just created (from /dev/nbd0 to /dev/nbd15):

qemu-nbd -c /dev/nbd0 /path/to/vm_disk.vdi

Check the right partition to mount (/dev/nbd0pXX). XX indicates the partition number:

fdisk -l /dev/nbd0

Mount it!

mount /dev/nbd0p2 /path/to/mount/point

Now you can take a copy of what you need ;-)

Unmount all, once you have finished:

umount /path/to/mount/point
qemu-nbd -d /dev/nbd0

And now I think you’re safe. Fiuuu! :P

Did you like this post? Share it!
Share on email
Share on twitter
Share on facebook
Share on linkedin
Share on print

Leave a Reply