Mount VirtualBox image drive on Ubuntu (VDI)

In this small tutorial, I will explain how to simply mount a VDI image to Ubuntu. I guess it should work on most Debian based distribution.

I had to do this trick because I used for a long time a Kali VM on VirtualBox and I stored a lot of files on it. But one day, after an update, I was impossible to reboot my Kali VM. So to avoid loss all my documents on this VM, I mounted the virtualBox Image on my Ubuntu.

In my case, Ubuntu version was 18.04.3 LTS but I tested the solution with 16.10 too.

First we need to install QEMU tools using the following command:

sudo apt install qemu-kvm

To be able to mount the VDI disk, we need to load the network block device module (nbd) using the following command:

sudo modprobe nbd

Now, we can run the tool qemu-nbd on the VDI file. This tool is a user space loopback block device server for QEMU disk images.

qemu-nbd -c /dev/nbd0 [path-to-vdi-file]

Replace [path-to-vdi-file] by the path towards your VDI image. After that, you “linked” your VDI to this virtual block device (/dev/nbd0).

You can check the partitions in the image using the following command:

sudo fdisk -l /dev/nbd0

This command will check the entire image as a block device named /dev/nbd0, and the partition it as subdevices. In my case, I can see 3 different partitions:

sudo fdisk -l /dev/nbd0
Disk /dev/nbd0: 40.4 GiB, 43365695488 bytes, 84698624 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xeadb4be2

Device      Boot    Start      End  Sectors Size Id Type
/dev/nbd0p1 *        2048 37748735 37746688  18G 83 Linux
/dev/nbd0p2      37750782 41940991  4190210   2G  5 Extended
/dev/nbd0p5      37750784 41940991  4190208   2G 82 Linux swap / Solaris

Now, you just have to mount the desired partition on your system. In my case, I only need to mount /dev/nbd0p1 to retrieve my files. I mounted it to /mnt directly.

sudo mount /dev/nbd0p1 /mnt

Now you can check the content of /mnt and you should see the content of your partition.

ls /mnt
bin   home            lib32       media  root  sys  vmlinuz
boot  initrd.img      lib64       mnt    run   tmp  vmlinuz.old
dev   initrd.img.old  libx32      opt    sbin  usr
etc   lib             lost+found  proc   srv   var

After completing your work, you can unmount the partition using the following command:

sudo umount /mnt

Don’t forget to shutdown nbd service (the server) and remove the ndb module from the kernel

sudo qemu-nbd -d /dev/nbd0
sudo modprobe -r nbd

Hope it can help 🙂

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.