How to mount a VirtualBox VDI image

Don’t believe the hype! It is entirely possible to mount a VirtualBox VDI image, just like a loopback filesystem… all you need are the right tools and know-how. Allow me to illustrate.

My apologies, that was the wrong illustration. Onward!

Before we start, it should be noted that you don’t want to do this while your disk image is already in use. That is to say, if you’re running a virtualised host using this image, GTFO.

First, install the QEMU tools. In Ubuntu, you’ll find them in the qemu-kvm package. Whatever package your distribution ships which contains the qemu-nbd binary should be fine.

Load the nbd kernel module. Yes, I’m serious, the network block device module! (Note: All of the following commands require superuser permissions, so escalate your privileges in whatever way makes you most comfortable.)

modprobe nbd

Then run qemu-nbd, which is a user space loopback block device server for QEMU-supported disk images. Basically, it knows all about weird disk image formats, and presents them to the kernel via nbd, and ultimately to the rest of the system as if they were a normal disk.

qemu-nbd -c /dev/nbd0 <vdi-file>

That command will expose the entire image as a block device named /dev/nbd0, and the partitions within it as subdevices. For example, the first partition in the image will appear as /dev/nbd0p1.

Now you could, for instance, run cfdisk on the block device, but you will most likely want to mount an individual partition.

mount /dev/nbd0p1 /mnt

Gadzooks! Now you can muck around inside the filesystem to your heart’s content. Go ahead and copy stuff in or out, or if you’re feeling fruity, have some chrooty: chroot /mnt.

When you’re done, unmount the filesystem and shut down the qemu-nbd service.

umount /mnt
qemu-nbd -d /dev/nbd0
This entry was posted in General and tagged , , , , , . Bookmark the permalink.

71 Responses to How to mount a VirtualBox VDI image

  1. Chinmay K says:

    Works like a charm! Thanks, it saved me a painful trip to the office just to get one file off a VM!

  2. unity fan says:

    hehe ;) lol

  3. dremon says:

    Thanks a lot. I think there must be a “-c” parameter specified to qemu-nbd (at least on my machine).

    • Jeff Waugh says:

      Oops, crucial missing parameter! Thanks for pointing that out, I’ll fix the post.

      • Hubert Kario says:

        I’ve noticed that “max_part” parameter for nbd kernel module in my distribution is 0, so for this to work, one has to do “modprobe nbd max_part=16″ or similar

        • zzeroo says:

          Yeah, my debian wheezy is one of them.
          Without Your modprobe nbd max_part=16 I’m not able to mount some partitions other the first one.

  4. Paul Goller says:

    I also have to run partprobe (from parted package) before I’m able to access the partitions (tested under Debian Squeeze RC). After that the partitions will accessible through /dev/disk/by-id/*nbd*part[0-9]*.

    Sadly the nbd module seems to be very unstable for this kind of operation. After copying ~1.5GB of data from a VDI-File the module dies and the copying process (cp and rsync tested) will produce I/O Errors.

    • Jeff Waugh says:

      The versions of qemu and nbd worked well for me on Ubuntu 10.10.

      In fact, I was meant to be fetching packaging stuff off the disk image (because the new VirtualBox was wedging the machine) when I found myself doing the packaging and building while chrooted in it! Not a terrible stress test. ;-)

    • shabeer says:

      You are right Paul!

      Me too getting same IO errors are using rsync over 1.5GB

  5. aperson says:

    I believe this to only be applicable to fixed-size images. Dynamically-sized images cannot be mounted, IIRC.

  6. Ulli Hochholdinger says:

    Hi,
    Many thanks for the Tip.

    I use debian squeeze. To get this to work there you have to insert the nbd module with the paramter max_part with appropriate number of partitions to use:

    rmmod nbd
    modprobe nbd max_part=16

    without this Parameter you won’t get the partitions. partprobe gives a weird Error:
    “Error: Error informing the kernel about modifications to partition /dev/nbd0p1 — Invalid argument. This means Linux won’t know about any changes you made to /dev/nbd0p1 until you reboot — so you shouldn’t mount it or use it in any way before rebooting.”

    Cheers Ulli

  7. Erik N says:

    Works like a charm on Ubuntu 10.10

  8. Simon W. says:

    Thanks YOU!!!

    you R my life saver! :)
    Yours tutorial 100% works :)
    Ubuntu 11.04

  9. jorge says:

    so, this is if you are running virtualbox on an ubuntu system, how would I go about it if I’m running virtualbox on a winxp machine?
    thanx.

  10. antiplex says:

    finally a solution, many thanks jeff!
    i thought i might want to bring this to ubuntuforums as well, so i created a posting there mentioning your blog-entry here: http://ubuntuforums.org/showpost.php?p=10972071&postcount=6

  11. gmart says:

    Sadly the nbd module is very unstable for this kind of operation. It only mostly works in Ubuntu 9:10, 10:04, 10:10 and now 11:04. Unfortunately, ‘mostly’ is not good enough for production work. Sooner or later the copy process (also cp and rsync tested) will produce I/O Errors. This has caused me no end of headaches. I have investigated many fixes and work arounds. The I/O error proble is easy to duplicate:

    root@somehost:~# qemu-nbd --connect=/dev/nbd0 ./gmos/tmp4UatFh.qcow2
    root@somehost:~# dd if=/dev/nbd0p1 of=/dev/null bs=1M
    dd: reading `/dev/nbd0p1': Input/output error
    1175+0 records in
    1175+0 records out
    1232076800 bytes (1.2 GB) copied, 16.2789 s, 75.7 MB/s

  12. gmart says:

    My latest suspect for the I/O errors was double disk caching. I have found that adding the -n (–nocache) option to the qemu-nbd connect command seems to solve the problem (more testing needed). Thanks to http://superuser.com/users/79064/exot for that tip.

  13. Tom H says:

    had to use the max_part=16 trick, but got it working on Linux Mint 9. Very cool info, thanks for sharing!

  14. The pabster says:

    Thank you for this. :)

    But it doesn’t work for me. :(

    qemu-nbd dies without doing nothing. Neither passing the max_part=16 to the module, nor running partprobe afterwards.

    If I run qemu-nbd with the -v (verbose) option, I always get an error: “Failed setting NBD block size”. I’ve googled that, but didn’t find anything understandable. Any suggestion?

  15. someone in the world [of linux ;)] says:

    (Y)(Y)(Y)(Y)(Y)
    (Y)(Y)(Y)(Y)
    (Y)(Y)(Y)
    (Y)(Y)
    (Y)

    Thank you man,
    thank you many times
    there should be more people and blogs like you/rs
    makes linux better than already is

  16. Erwin Kooi says:

    In case you get
    qemu-nbd: Could not access ‘/dev/nbd0′: No such file or directory

    Do this first:
    apt-get install nbd-server

  17. jlettvin says:

    Nice access of vdi from host.
    How about the other way, guest accesses passive partition in host?
    Host system has 2 partitions Windows 7 and ubuntu 11.10 with Windows hosting.
    Normal ubuntu guest running in vbox needs access to host ubuntu partition.
    Any suggestions?

  18. Anyeos says:

    Mine worked one time but next it say:

    fdisk /dev/nbd0

    Unable to read /dev/nbd0

    Wtf? nbd0 is unreadable? why?

    I just tried with nbd1 and it worked but again it freezes last when I try to -d it.
    Now I have two qemu-nbd freezed in my terminals. So? What I must wait for?
    killalled it all (it hangs with a message).

    SOoo!!! I cannot use this method guys!!

    I need something to read my VDI image and for now I cannot get nothing to work.

    Thanks but no thanks… :S

  19. Anyeos says:

    Sorry guys my qemu-nbd is using 100% cpu when I give to it the -d parameter. It does not disconnect anything just use 100% CPU.

    What happen with that? How to update to the lastest version? I’m using Ubuntu 10.10 and don’t want to update to 11.

    I searched in google but there are no ppa related to backports of qemu for Ubuntu 10.10. I’m using custom kernel manually built (2.6.38-rc3).

    I see a possible solution there are some bug report about qemu-nbd hangued. It is related with a kernel 2.6.38 update. I guess that what I’m using (rc3) have the bug. I will recompile a kernel but 2.6.39.

    Bye

  20. henri says:

    On my slackware box (Q6600, kernel 3.2), partprobe -s /dev/nbd0 could not notify the creation of the new device /dev/nbd0p1 (ERROR in modification transmitting to the kernel)!! although fdisk /dev/nbd0 shows correctly partition 1

    • henri says:

      I just not read the Ulli Hochholdinger message… SORRY

      In my Slackware 13.37, the max_part parameter is also mandatory!

  21. Olaf L says:

    Works flawlessly on Mint-11 (Ubuntu-11.04) as well – when the correction (max_part=16 on modprobe; and qemu-nbd -c as stated above) are applied. Thank you for a great tip.

  22. roberto says:

    On suse 11.4 the tip:

    rmmod nbd
    modprobe nbd max_part=16

    make the difference
    It works like a charm thanks ;)) very useful

  23. Rabin says:

    This is the only solution for Dynamic virtual disks!!!. You better emphasize this. All other solution are deploying loop back concept which is lacking the awareness to partition table and dynamic drives.
    R.

  24. Antonio says:

    Thanks a lot for the useful post. I test with two vdi images: ext4 and ntfs, in read and write. My VDI disks are dynamically-sized images.
    It Works ^_^

    Bye,
    Antonio

  25. Dast says:

    Awesome! Worked beautifully on Ubuntu 11.04. Thanks!

  26. Pat says:

    Woohoo!!! I searched google all night to figure out how to mount a dynamic vdi image, and this worked like a charm. Most other methods required finding the data offset number, or didnt work right with dynamic images (for example, after I finally figured out the correct offset number, I kept getting a “Failed to read last sector” error for an ntfs filesystem trying to mount it with a /dev/loop0).
    I did have to use the “max_part” option as listed in the above post.

    For clarity and to help others, the following worked for me on openSuse 11.4 – 64bit host, mounting an ntfs filesystem from a dynamic vdi created from a virtualbox windows7 guest:

    1) unload network block devicei kernel module, in case it is loaded without the max_part option (if you have already linked any nbd devices to vdi files, remove them with “qemu-nbd -d /dev/nbd0″ or similar)

    rmmod nbd

    2) reload nbd with max_part option

    modprobe nbd max_part=16

    3) link your nbd to your vdi file

    qemu-nbd -c /dev/nbd0 VDIFILE.vdi

    4) scan for partitions, this should create the new device files in dev

    partprobe

    5) list your device files to make sure they were created

    ls -l /dev/nbd0*

    6) to view the sizes of any partions found in the vdi, you can run fdisk and print the partition table

    fdisk /dev/nbd0
    p

    7) finaly, mount the desired partition (I used ‘-r’ for readonly to be safe)

    mount -t ntfs -r /dev/nbd0p2 /MNTPOINT

  27. js200300953 says:

    Thanks a lot.

  28. alby1729 says:

    YOU ARE GREAT!

  29. Marcus Downing says:

    Ulli, that hint with rmmod saved my skin. Thank you!

  30. M. Belbut says:

    Hi,

    I tried your technique and it works for me. But I wanted to save the pain of having to go to the terminal and eventually create mountpoins for my partitions, so I was hoping ‘partprobe’ to allow me to handle the nbdXpY volumes through nautilus…
    Should it work?
    When I run partprobe, I get this error:
    miguel@cdrsp-laptop-miguel:~$ sudo rmmod nbd
    miguel@cdrsp-laptop-miguel:~$ sudo modprobe nbd max_part=16
    miguel@cdrsp-laptop-miguel:~$ sudo qemu-nbd -c /dev/nbd0 '/home/miguel/VirtualBox VMs/Tests/NewHardDisk1.vdi'
    miguel@cdrsp-laptop-miguel:~$ sudo partprobe -s/dev/sda: msdos partitions 1 2 3
    Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
    Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
    Error: Can't have a partition outside the disk!
    /dev/nbd0: msdos partitions 1
    miguel@cdrsp-laptop-miguel:~$

    Any hint?

    • Jeff Waugh says:

      Firstly, I don’t understand why you’re using -s or specifying /dev/sda… but I’m not entirely sure that partprobe is going to do what you want anyway. :-)

  31. romain dartigues says:

    My Ubuntu didn’t want to create the /dev/nbd0p1 device, and partprobe failed with Error: Error informing the kernel about modifications to partition /dev/nbd0p1 — Invalid argument. This means Linux won’t know about any changes you made to /dev/nbd0p1 until you reboot — so you shouldn’t mount it or use it in any way before rebooting..

    So i used another way:
    $ fdisk -lu /dev/nbd0
    Device Boot Start End Blocks Id System
    /dev/nbd0p1 * 2048 16775167 8386560 83 Linux

    $ mount -o loop,offset=1048576 /dev/nbd0 /mnt # sector * size (2048*512)

    That was about it. Thank’s.

  32. Manuel H.P. says:

    Thanks a lot Jeff,

    Your excellent blog has allowed the recovery of a full VDI disk containing critical working documents of one colleague of mine.

    All the best from the Pyrenees,

    Manuel

  33. dobbs says:

    Sweet, just used qemu-nbd and gdisks to move the backup table of a resized VDI with GUID/GPT partition table.

  34. Irregular Joe says:

    Thanks for a great tutorial, just something to add for those that use LVM within their virtual machines: Once you mount the virtual drive, you can issue a ‘pvscan’ to check for any LVM physical volumes, then you may need to ‘vgchange -ay vgname’ to activate any LVM volumes. Once that has been done, you can do a normal mount of the logical volume.
    The only potential problem may come if the volume group name on the virtual disk matches your host machine’s group name. I resolved this by mounting a live-cd with my virtual machine and renaming the group with ‘vgrename oldvgname newvgname’. You will also have to change the /etc/fstab and /boot/grub/grub.cfg to reflect the newvgname.
    Hope this helps someone! Great info!

  35. Derek says:

    Couldn’t get this working at first – the partitions didn’t show up as devices. Before I read all of the comments and saw the ‘partprobe’ method, I’d discovered a Partition option to qemu-nbd, so I simply mounted the one partition that I wanted as nbd0. Thanks for the pointers!

  36. Menner says:

    Thank you very much for this post – it took only a few minutes to resize the root partition of the virtual machine!
    First, I increased the VDI image size via
    $ VBoxManage modifyhd archlinux.vdi –resize 2500
    There’s no VirtualBox GUI for that.
    After that I used the nbd trick from this post to mount the VDI in the host.
    I only needed to install qemu-utils; the kernel module was already there.
    Then I could resize the partition via gparted GUI:
    $ sudo gparted /dev/nbd0

  37. Calmarius says:

    I get I/O errors too even if -nocache is on.

  38. kendall says:

    works like a charm everytime on nearly any distro!

  39. calamari says:

    Your method works great for VMDK images too (even the expandable ones). Thanks!

  40. arashium says:

    Failed setting NBD block size

    make sure you run qemu-nbd as root

  41. pek says:

    qemu-nbd -P 1 -c /dev/nbd0 /data/virtual-box/android.vdi

    Work for me.

  42. molecular says:

    I had some problems with the partition (“/dev/nbd0p1″) not showing up.

    using: qemu-nbd -P 1 -c /dev/nbd0

    worked. The partition #1 was then mapped directly to the existing node /dev/nbd0

    thanks!

  43. Roel de Cock says:

    Damn, why didn’t I come up with this solution myself! It even mounts a Windows-7 partition.

    I was afraid for a moment that, after the untimely demise of vdfuse, I’d have to use libguestfs (which is less of a ‘lib’ and more of a ‘massive dependency takeover of your PC’).

    But I still have to figure out how to make it work with an LVM volume.

  44. Parad says:

    OK, edge case here.
    It’s possibly a bad habit.
    I sometimes create virtualHD’s with no_partitions.
    The entire HD is the file system.
    So far I’ve not found a way to mount such a vdi.
    fdisk see the disk once looped back to /mnt/nbd0
    Though mounting cannot see a valid file system (type).
    Any clues?

    Thanks for a well simplified page.

    M

  45. David Strauss says:

    > First, install the QEMU tools. In Ubuntu, you’ll find them in the qemu-kvm package. Whatever package your distribution ships which contains the qemu-nbd binary should be fine.

    For Fedora systems, the package is qemu-img.

    • WJCarpenter says:

      On Ubuntu 12.04, I found it in qemu-utils. You get that if you install qemu-kvm. If you don’t need the other stuff, then the qemu-utils has a smaller footprint.

  46. wyatt says:

    thanks! this is great for moving files to and from my freeDOS VM!

  47. LS says:

    This guide works like a charm except for one specific issue that I ran into.

    In my particular scenario, I want to mount a vdi disk used by my Win Server 2008 R2 guest to access some program files. After I mount the vdi and access the filesystem according to this guide, I can see *most* files on the disk. I can, however, not see the files that belong to some programs that I installed on the guest myself. I can not even see the directories used by these programs, and yet they are not hidden or anything of that sort.

    Am I missing something obvious? I will be very grateful for your help.

  48. Chathura says:

    Thank you for infinite times and once more!!! This article was a LIFE SAVER!!!! Saved my time and Work!!!

  49. Nelson Zamith says:

    It saved me like a few people here, the time for a trip in the office :)
    Really, thank you :)

  50. ernemir says:

    Works great for me. Thanks you!!!

  51. Stuart Rothrock says:

    Thanks so much. Works on Fedora as written. Appreciated!

  52. Stuart Rothrock says:

    Followup – Had to use kpartx -a /dev/nbd0 before mounting partitions. kpartx makes partitions visible via /dev/mapper directory.

    * moderator may combine this and my previous post if desired.

  53. Sven says:

    It seems that to see the partitions in the image, one must load the nbd module with the max_part option, i.e., modprobe nbd max_part=16

  54. Paul says:

    Hi,
    thanks a lot, work perfectly for me even with dynamically allocated VDI disk (thin provisioning).

    Just a note:
    on my debian squeeze x86 I have to install qemu-utils package which contains qemu-nbd not found in the qemu-kvm package.

    Thanks again
    paul

  55. Gannet says:

    I am an XP refugee running Mint 17 with Cinnamon. Been trying to find out how to get into the .vdi files for ages. That worked a dream for me. Thank you. To get qemu-nbd I had to install qemu-utils using the software manager.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comments will be sent to the moderation queue.