Skip to main content

Converting physical disk to Virtualbox image

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

A couple of days ago my little Linux router died after serving me fine for more than 13 years. Unfortunately, I had not a recent backup at hand so I was in need to get access to the disk. Since I had no "old enough" hardware available, I was looking for a way to convert the physical disk into a Virtualbox image which in the end turned out to be quite easy. All you need to do is to create an image of the physical disk using dd and convert that to a .vdi image file via the VBoxManage tool that is shipped with Virtualbox:

sudo dd if=/dev/sdb of=./debian.bin
VBoxManage convertfromraw debian.bin debian.vdi --format VDI

Replace /dev/sdb above with the device you want to convert. Be aware that you need to convert the whole disk, not just the partition that you might actually want to access. Otherwise, you might not be able to boot the machine afterward. In the end, all you need to do is to create a new virtual machine in Virtualbox and add the newly created .vdi disk image to the machine.

Since I could boot the image now I was able to backup all the files I needed. But as it turns out I was not able to set up the networking in the virtual machine. So I was looking for a way to access the filesystem of the virtual machine from my local Linux box. There seems to be a tool named virtualbox-fuse which can be used to mount a .vdi image file directly but the package seems to no longer live in the Ubuntu repo and I was not able to install the version I found on Launchpad. Luckily I came across this blog article which shows how to achieve the same effect using qemu. In the end, it all boils down to the following commands:

sudo apt-get install qemu
sudo rmmod nbd
sudo modprobe nbd max_part=16
sudo qemu-nbd -c /dev/nbd0 drive.vdi
# p1 is the first partition on the disk
sudo mount /dev/nbd0p1 /mnt
# unmount device when finished
sudo qemu-nbd -d /dev/nbd0

Achievement unlocked!