116

Is it possible to mount a VirtualBox drive image (.vdi) so the contents can be viewed in Nautilus etc.?

I have a windows 2000 .vdi which won't boot ("inaccessible boot device") after upgrading from VirtualBox 2.x to 3.1.6. I believe the IDE drive details have changed and that all I need to do is access the internal drive image and edit the Windows boot.ini to point to the new location.

1

11 Answers 11

134

Use qemu-nbd, the process is explained on serverfault and in this blog.

Basically, you'll have to install qemu if needed:

sudo apt-get install qemu

Then you'll need to load the network block device module:

sudo rmmod nbd
sudo modprobe nbd max_part=16

Attach the .vdi image to one of the nbd you just created:

sudo qemu-nbd -c /dev/nbd0 drive.vdi

Now you will get a /dev/nbd0 block device, along with several /dev/nbd0p* partition device nodes.

sudo mount /dev/nbd0p1 /mnt

Once you are done, unmount everything and disconnect the device:

sudo qemu-nbd -d /dev/nbd0
16
  • 2
    I'd like to note that max_part=16 seems to be a critical part. Without it you won't see any /dev/nbd0pX files, despite the main device will work properly. Check the following discussion on Gentoo forums to see people met this trouble. Jun 11, 2015 at 16:54
  • 3
    This worked great. Note that installing qemu-kvm instead of qemu may work just as well. It did for me on my Ububtu 16.04, and installed only 23MB and 13 dependencies instead of 230MB and many more dependencies for the full qemu install.
    – mivk
    Jan 21, 2018 at 21:39
  • 2
    On Debian buster amd64 I installed only ~qemu-utils~ and its dependencies, for a total of just a few MB of disk usage, and it worked: I successfully mounted the .vdi and copied one file.
    – Jorge
    Mar 20, 2018 at 14:12
  • 3
    I'd like to add that apparently sudo partprobe needs to be executed with newer kernels so that /dev/nbd0p* are created in Debian/Ubuntu (bugs.debian.org/cgi-bin/bugreport.cgi?bug=824553).
    – Kai
    Apr 10, 2018 at 20:56
  • 4
    This should be the accepted answer!! My vdi is too big to convert to an .iso on my hard drive. After struggling for a while, I scrolled down to see if any other methods used the vdi directly. This one is simple. (Note: qemu takes about 300MB for those of us who care about our hard drive filling up.)
    – psitae
    May 26, 2018 at 11:17
64

You can convert into standard image and then mount it.

VBoxManage clonehd --format RAW ubuntu.vdi ubuntu.img

Then

mount -t ext3 -o loop,rw ./ubuntu.img /mnt

You will need to KNOW the type of file system, ext3 in this case. After it is mounted, go in and edit away with the editor of your choice. Don't confuse files inside the /mnt location with the running host, or it will be bad.

To check details about your VDI file, run: VBoxManage showhdinfo ubuntu.vdi.

Source: [SOLVED] Open .vdi with archive manager? - ubuntuforums.org

7
  • 4
    Specilly useful in a situation like this. OP will be able to work on the copy keeping the original as backup. Dec 30, 2010 at 15:22
  • 13
    Careful doing this with dynamically sized .vdi files. Converting to .img will force the image file to be as big as the logical size of the disk, which is why this approach works, but it might be much larger than .vdi file was. Take a look at the output of VBoxManage showhdinfo [your vdi file] if you're not sure. Aug 25, 2013 at 22:21
  • 3
    -1, stwissel and Maxime R.'s answers provide solutions--this is a fair workaround, but not the best answer to the question
    – STW
    Feb 17, 2015 at 18:39
  • 4
    If the image has a partition table (like most drives), then you will need to combine this answer with this one to mount the individual partitions.
    – bain
    Mar 22, 2015 at 19:49
  • 2
    Doesn't work. Keep getting mounting errors wrong fs type. I tried ext3 and ext2 and ext4 . Virtual box says the hd fs type is ext4 . Oct 14, 2017 at 6:32
39

It looks like vdfuse is the tool you are looking for. It is in the Ubuntu repositories. If you want to compile yourself (2010 source) you can do that too.

However for most of us it boils down to a simple:

sudo apt-get install virtualbox-fuse

It seems it now supports dynamic vdi as well.

And to mount the .vdi file in /mnt dir use the command:

sudo vdfuse -a -f /path-to-vdi-file /mnt

The entire disk will be mounted with partitions Partition1 , Partition2 naming format. Then those files can be loop mounted. For example,

sudo mount -o loop /mnt/Parition1 /mountpoint
9
  • 1
    @mattwilkie: The difference now: no manual compile required anymore
    – stwissel
    May 9, 2012 at 2:20
  • 2
    You are welcome. Sitting at a propper screen/keyboard instead of pecking on glas made all the difference :-)
    – stwissel
    May 10, 2012 at 3:15
  • 6
    It's not available in 14.04.
    – int_ua
    Aug 9, 2014 at 20:14
  • 2
    One can download it from here: sourceforge.net/projects/vdfuse/?source=typ_redirect and it works (on 15.04)
    – sup
    May 13, 2015 at 14:17
  • 7
    No longer valid for 16.10, package no longer exists. Dec 14, 2016 at 13:13
16

Set the disk as secondary master for another virtual OS, then boot into this (virtual) OS and you can mount it.

Step 1: Assuming you have a virtual os(say Ubuntu 16.04) already installed in Virtual box, add a new storage from settings of that virtual os. enter image description here

Step 2: Browse the Vdi file you want to access and select it.

Step 3: Logon to the virtual OS.

Step 4: The virtual disk will probably be available inside the OS. If not, follow step 5

Step 5: Inside the virtual OS, use Gparted and check the disks. and ensure the disk is of type which is accessible by the virtual OS. If it is not, you may have to edit the disk, but then you will lose the existing contents.

4
  • 1
    For the record, in 2016 it won't work Feb 25, 2016 at 10:16
  • 1
    It works in ubuntu 16.04.
    – Sharun
    Dec 12, 2016 at 7:56
  • this is really the only solution when you need to make changes and then go back to using it, couldn't get rw working with vdfuse Mar 28, 2018 at 9:07
  • you can even use a live ubuntu and add the .vdi file as extra strorage. Mar 30, 2020 at 16:53
8

I haven't tested myself but there is a fuse module to mount them (vdfuse), check the following page:

Please note that using it for write access is risky.


Usage:

$ ./vdfuse-v<version> -h
USAGE: ./vdfuse [options] -f image-file mountpoint
   -h   help
   -r   readonly
   -t   specify type (VDI, VMDK, VHD, or raw; default: auto)
   -f   VDimage file
   -a   allow all users to read disk
   -w   allow all users to read and write to disk
   -g   run in foreground
   -v   verbose
   -d   debug

To mount an image directly using its filename:

$ ./vdfuse-v<version> -f image.vdi /mnt/vdi
$ mount -o loop /mnt/vdi/Partition1 /mnt/WindowsXP

To mount an machine disk (including snapshots):

$ ./vdautomount-<version> -p /path/to/vdfuse WinXP /mnt/vdi
$ mount -o loop /mnt/vdi/Partition1 /mnt/WindowsXP
1
  • 1
    No longer relevant as this utility isn't supported. Jun 8, 2018 at 4:42
6

As Vojtech Trefny mentioned above,

1) first convert your VDI -> IMG

VBoxManage clonehd --format RAW ubuntu.vdi ubuntu.img

2) Then mount the IMG

mount -t ext3 -o loop,rw ./ubuntu.img /mnt

3) However, as I got this error message:

mount: wrong fs type, bad option, bad superblock on /dev/loop0,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail  or so

and dmesg said:

[3105578.013671] EXT4-fs (loop0): VFS: Can't find ext3 filesystem

you need to check out the partition structure of the .img:

fdisk -l ubuntu.img

Disk ubuntu.img: 21.0 GB, 20971520000 bytes
255 heads, 63 sectors/track, 2549 cylinders, total 40960000 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
Disk identifier: 0xf45bc910

   Device Boot      Start         End      Blocks   Id  System
ubuntu.img1   *        2048    12582911     6290432   83  Linux
ubuntu.img2        12584958    16775167     2095105    5  Extended
ubuntu.img3        16775168    40959999    12092416   83  Linux
ubuntu.img5        12584960    16775167     2095104   82  Linux swap / Solaris

4) As you can see, the main partition is starting at bytes 16775168. However, note that the sector size is 512 bytes, so you need to multiply the results 16775168 x 512 = 8588886016 so you need to offset the mount like this:

mount -t ext3 -o loop,rw,offset=8588886016 ./ubuntu.img /mnt

5) Actually this didn't work for me in case where the filesystem was dirty after resize. In this case I further did this:

dd if=ubuntu.img of=ubuntu.disk bs=512 skip=16775168 count=12092416
e2fsck ubuntu.disk
mount ubuntu.disk /mnt
2
  • 1
    This isn't an answer to the question. You had another problem -- a corrupt image. You should have self-answered a new question. Jun 8, 2018 at 4:44
  • 2
    This expands to the previous answer covering the error case. Corrupt image was one use case. +1 for many new tricks. Dec 27, 2018 at 21:03
5

You can use vboximg-mount program which is a part of the VirtualBox's command line tools.

1
  • Interesting! It crashed with a segmentation fault for me, but this is the first answer that mentions this tool, so I upvoted.
    – Flimm
    Jan 11 at 12:58
3

Not what you requested, but if it's just a matter of getting files off of the vdi, and you want a very quick solution:

  1. Run an ssh server on your host (apt-get install openssh-server && service ssh restart)
  2. Use VirtualBox to build a virtual machine from the existing vdi file, then boot up that virtual machine. (I just kept the default Network Adapter (NAT) when building the VM.)
  3. Within the virtual machine, sftp to your host. (sftp hostuser@hostip)
  4. In the sftp session, put as many files to the host as you need.
3
  • 1
    An ugly hack, but if you're in a tight spot, it gets the job done. Dec 14, 2016 at 13:13
  • One problem may be that the machine doesn't currently boot correctly so you'd need to fix that problem first and that means you can't do it from within. Also using SSH with your virtualbox OSes is the way to go to copy files around. Way faster than trying to share a disk. Jun 23, 2018 at 23:21
  • The guest system needs to have SSH installed for this to work. Easy on a Linux guest, less straightforward on a Windows guest.
    – Flimm
    Jan 11 at 12:56
3

P7Zip will open VDI files

  • Install P7Zip - Desktop from Ubuntu Software from this link or by running:

    sudo snap install p7zip-desktop
    
  • Launch P7Zip Desktop and navigate to the folder containing the .vdi file (probably under VirtualBox VMs in your home directory)

  • Double click the VDI file in the P7Zip window.

  • View or extract files as you like.

2
  • This seems to load the oldest snapshot, rather than the current state of the guest filesystem.
    – Flimm
    Jan 11 at 13:08
  • P7zip is the simplest approach if we just need to extract a single file from a valid VDI. However, it seems bugged, becase it can open VDIs only in the current file system, while it doesn't open files in other mounted file system in /media. Apr 1 at 14:29
1

The qemu approach from this answer needed to be modified a bit to work in Ubuntu 20.04.1. First I had to load the nbd module, by:

lsmod | grep nbd
modprobe nbd

This did not work:

sudo mount /dev/nbd0p1 /mnt

I needed to do this instead:

sudo mount /dev/nbd0p2 /mnt
1
  • What does sudo fdisk -l /dev/nbd0 show? After the modprobe command, you need to run sudo qemu-nbd --connect=/dev/nbd0 /path/to/your/image-file. Nov 28, 2020 at 19:17
1

Make VirtualBox disk images available to the host by using vboximg-mount utility:

Open VDI

sudo mkdir -p /mnt/vdi/expanded
sudo vboximg-mount -i <path-to-vdi> -o allow_other /mnt/vdi/expanded
Output Example
ls /mnt/vdi/expanded
<filename.vdi>  vhdd  vol0  vol1

Mount Disk

mkdir /mnt/vdi/vol1
sudo mount /mnt/vdi/expanded/vol1 /mnt/vdi/vol1

Unmount

cd /mnt                       # Optional (avoid mount: ... target is busy)
sudo umount /mnt/vdi/vol1     # Order importent
sudo umount /mnt/vdi/expanded
rm -rf /mnt/vdi               # Optional (clean-up resources)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.