129

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.

Bruno Pereira
  • 73,643
matt wilkie
  • 2,170

11 Answers11

152

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 qemu-utils

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
Maxime R.
  • 3,820
  • 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. – firegurafiku Jun 11 '15 at 16:54
  • All answers work to some extent but this is my favorite and should be the accepted one. clonehd do the job but doubles storage requirements. virtualbox-fuse is ideal but although you can still get it, it didn't make it to the repos in modern releases of OS. – Hatoru Hansou Sep 17 '15 at 22:31
  • Wonderful! This is absolutely the best method to use a .vdi file! I was getting dumb errors cloning or resizing the VDI, but this method bypasses all problems :)

    VBoxManage: error: Could not create the clone medium '/mnt/temp6/xubuntu-14.04-64bit.img'. VBoxManage: error: Raw: disk would overflow creating image '/mnt/temp6/xubuntu-14.04-64bit.img' (VERR_DISK_FULL) VBOX_E_NOT_SUPPORTED VBoxManage: error: Resize hard disk operation for this format is not implemented yet!

    – Avio Oct 13 '16 at 15:27
  • Also think this is the better answer. I for one was not very keen on cloning my 100 GB image just to get to a couple of files ;) Mounting via qemu-nbd worked like a charm. – piit79 Sep 21 '17 at 10:45
  • 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 '18 at 21:39
  • please note that it worked for me only: sudo qemu-nbd -c /dev/nbd1 drive.vdi for a reason, otherwise i had error: mount: special device /dev/nbd0p1 does not exist – Janos Szabo Mar 08 '18 at 05:54
  • 3
    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 '18 at 14:12
  • 4
    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 (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=824553). – Kai Apr 10 '18 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 '18 at 11:17
  • If nbd0p1 doesn't appear, run sudo kpartx -av /dev/nbd0, it will add it to /dev/mapper/ – iman May 31 '18 at 10:22
  • Note that in Debian qemu-nbd is in qemu-kvm package, so you should install with apt-get install qemu-kvm. – ceremcem Aug 25 '18 at 12:18
  • Just to note that this worked for me on 19.10 with no partprobe required. – Tom Apr 27 '20 at 11:56
  • Great job, works like a charm! – Smeterlink May 07 '20 at 00:56
  • Problem with Ubuntu 20.04 LTS. I got sudo: qemu-nbd: command not found – brewmanz Nov 21 '20 at 02:42
  • Everything worked fine for me except that the mounted drive didn't show any contents... however, binwalk finds a lot of files in the VDI file. I tried the first anser as well, and again only got an empty directory on my mountpoint. Not sure why. – lucidbrot Dec 12 '20 at 13:47
  • in order to solve the sudo: qemu-nbd: command not found on ubuntu you need to install an additional package sudo apt-get install qemu-utils – megloff Jan 31 '21 at 12:47
  • 1
    The links at beginning of this answer are dead – DAB Jul 28 '22 at 12:18
  • Is this solution safe for snapshots? – zomega Nov 01 '22 at 15:52
70

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

kenorb
  • 10,347
  • 4
    Specilly useful in a situation like this. OP will be able to work on the copy keeping the original as backup. – Javier Rivera Dec 30 '10 at 15:22
  • 15
    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. – Jack O'Connor Aug 25 '13 at 22:21
  • 4
    -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 '15 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 '15 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 . – Joshua Robison Oct 14 '17 at 06:32
  • Since you cloned the vdi as RAW maybe it needs mount -t raw ? – Joshua Robison Oct 14 '17 at 06:34
  • 2
    You can put the image on loop: losetup -P /dev/loopX ubuntu.img (where X is index of the first unused loop device) then mount /dev/loopXpY /mnt (where Y is the index of the partition you want to mount). – Rufflewind Jun 24 '19 at 18:09
  • Converting the whole image is very ineffective. Also this solution does not let you edit the disk file. – zomega Nov 01 '22 at 15:52
40

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
stwissel
  • 6,211
  • 5
  • 23
  • 24
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.

Sharun
  • 117
josef
  • 273
9

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)
Benny
  • 4,920
  • Alternatively (to vboximg-mount -i <path-to-vdi>), list all of the user VDIs with vboximg-mount --list --wide and specify their UUID to vboximg-mount (i.e. vboximg-mount -i <UUID> [...]). – desseim Jul 06 '22 at 00:37
  • I found -i <UUID> only worked when not using sudo. Without sudo the second mount for the partition failed. With sudo, I needed to use a full path to the .vdi image with the -i option. – Daniel Stevens May 22 '23 at 02:22
  • I tried to mount a vdi image locally with FreeDOS 1.3 RC4: same output but I had to mount vol0 and not vol1 specifying -t vfat and the full path because the relative one didn't work... – Hastur Oct 30 '23 at 21:09
9

Moderator Notice: The vdfuse module and packages have been obsolete since 2016 as they were removed from the Ubuntu repositories. While this answer is now obsolete, due to the age of the post and the fact it has upvotes, the answer here has been retained for historical purposes.

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
Thomas Ward
  • 74,764
João Pinto
  • 17,159
8

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

user2683246
  • 193
  • 1
  • 4
  • 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 '22 at 12:58
7

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
PHZ.fi-Pharazon
  • 277
  • 2
  • 4
  • 1
    This isn't an answer to the question. You had another problem -- a corrupt image. You should have self-answered a new question. – Evan Carroll Jun 08 '18 at 04:44
  • 2
    This expands to the previous answer covering the error case. Corrupt image was one use case. +1 for many new tricks. – FractalSpace Dec 27 '18 at 21:03
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.

Flimm
  • 41,766
C.S.Cameron
  • 19,519
  • This seems to load the oldest snapshot, rather than the current state of the guest filesystem. – Flimm Jan 11 '22 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. – Francesco Galgani Apr 01 '22 at 14:29
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.
Jellicle
  • 849
  • 1
    An ugly hack, but if you're in a tight spot, it gets the job done. – Piskvor left the building Dec 14 '16 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. – Alexis Wilke Jun 23 '18 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 '22 at 12:56
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
Eliah Kagan
  • 117,780
  • 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. – ajgringo619 Nov 28 '20 at 19:17