Mounting Virtual Box Machine Images on a Host

Hello everyone!

This is a bit of a how-to for how to mount a VDI on a SuSE host machine.

REMEMBER: NEVER EVER DO THIS ON A LIVE VIRTUAL MACHINE YOU COULD CORRUPT THE GUEST!!!

On SuSE 11.4 I’ve tried the qemu-nbd way and have had mixed results. It seems to work best after a fresh reboot.

On Fedora I’ve successfully gotten libguestfs to work. I’ll put a link for those who might be looking for a way to accomplish this on Fedora…on a SuSE Forum. However, due to a compile error I’m not able to get it to work properly, and with this working I’m too lazy try and fix it.

You’ll see that I have rsync used in the script; that is because I wanted a backup of the virtual machine without having to send the whole VDI across the network every time.

How to mount a VDI file (as taken from my own documentation):

Download and install libguestfs-mount (libguestfs, library for accessing and modifying VM disk images) or from
Fedora’s package manager. From here, we are going to perform the following
command:

/usr/bin/guestmount --ro -a /path/to.vdi -m /dev/sda1 /where/to/mount/it/to

This will cause to’s partation 1 (/dev/sda1) to be mounted on the host’s
/where/to/mount/it/to/

Obviously, if we need the machine’s second partation or anything we simply
specify from that option.

This does use FUSE to mount, so to unmount you use fusermount -u /path

There are also several other tools that you can use to interact with the
guest operating system. A big one being guestfish, this will inspect the
the image and allow some manipulation.

IMPORTANT TO NOTE: NEVER do this on a live machine. This can cause disk
corruption for the guest.

On SuSE I had to do it another way, as libguestfs is not aviable:

zypper in nbd qemu
modprobe nbd max_part=16
qemu-nbd -c /dev/nbd0 ./path/to.vdi
mount /dev/nbd0p1 /media/

Let’s take this apart. First we install qemu and nbd these are the tools we
are going to use to mount the device.

We need to avtivate the kernal modues for nbd and set the maximum of
partitions to 16 (should be default)

Now, to link the vdi to a nbd node.

Finally, mount it and use just like you would with any other filesystem

SOURCES:
Tip: Open VirtualBox VDI file using libguestfs | Richard WM Jones

libguestfs, library for accessing and modifying VM disk images

How to Mount a VirtualBox VDI Hard Drive Image in Fedora 15 – RyanRhode.com

SCRIPTS USED:

#!/bin/bash
#Fedora
##guestmount
##mounts guest hard drives

##–ro read only
##-a mount this image
##-m mountpoint
/usr/bin/guestmount --ro -a /home/user/VirtualBox\ VMs/webserver-backup/webserver-backup.vdi -m /dev/sda2 /backup-webserver/

#!/bin/bash

##SuSE Script
##This “syncs” the virtual image and the local copy
##rsync
##-a archive
##-v verbose
##-z compress
##-r recursive
##-u update

/sbin/modprobe nbd max_part=16

/usr/bin/qemu-nbd -c /dev/nbd0 /backup/image/webserver-backup.vdi

##give it some time
/bin/sleep 60
/bin/mount /dev/nbd0p2 /backup/webserver-backup
/usr/bin/rsync -avzru root@re.mo.te.IP:/backup-webserver/ /backup/webserver-backup

/bin/umount /backup/webserver-backup

/usr/bin/qemu-nbd -d /dev/nbd0

Hello,

I have manage to mount a vdi disk image using vdfuse

You will first have to install the vdfuse package :

zypper -v in vdfuse

or using YaST.
Once installed, in a shell become root and :

vdfuse is called with -r to create ro disk structure without this flag it defaults to rw.

#mkdir /mnt/vdi; vdfuse -r -f yourVDIdiskImage.vdi /mnt/vdi
# ll /mnt/vdi/
total 41942016
-r-------- 1 djo users 21474836480 Dec 18 14:16 EntireDisk
-r-------- 1 djo users  1569718272 Dec 18 14:16 Partition1
-r-------- 1 djo users  8266973184 Dec 18 14:16 Partition2
-r-------- 1 djo users 11637096448 Dec 18 14:16 Partition3

As you can see fuser created 4 special devices (One for each partition on disk and one for the entire disk in a /dev/sdx, /devsdx1, /dev/sdx2 manner) that can be mounted with option loop :

Say you have a directory called vdidisk in your home directory, using the structure created previously on /mnt/vdi/, we mount it ro (if the structure is ro, otherwise, rw for rw structure)

# mount -o loop,ro /mnt/vdi/Partition2 vdidisk/
# cd vdidisk/
# ll
total 124
drwxr-xr-x   2 root root  4096 Nov 18 20:48 .config
drwxr-xr-x   2 root root  4096 Nov 18 19:40 bin
drwxr-xr-x   3 root root  4096 Nov 18 19:50 boot
drwxr-xr-x   3 root root  4096 Nov 18 19:48 dev
drwxr-xr-x 114 root root 12288 Nov 24 12:08 etc
drwxr-xr-x   2 root root  4096 Nov 18 19:35 home
drwxr-xr-x  15 root root  4096 Nov 18 19:42 lib
drwxr-xr-x  10 root root 12288 Nov 18 19:41 lib64
drwx------   2 root root 16384 Nov 18 19:35 lost+found
drwxr-xr-x   2 root root  4096 Oct 25 17:06 media
drwxr-xr-x   2 root root  4096 Oct 25 17:06 mnt
drwxr-xr-x   2 root root  4096 Oct 25 17:06 opt
drwxr-xr-x   3 root root  4096 Nov 18 19:35 proc
drwx------   9 root root  4096 Nov 24 12:08 root
drwxr-xr-x   2 root root  4096 Nov 18 19:35 run
drwxr-xr-x   3 root root 12288 Nov 24 02:48 sbin
drwxr-xr-x   2 root root  4096 Oct 25 17:06 selinux
drwxr-xr-x   4 root root  4096 Nov 10 17:12 srv
drwxr-xr-x   3 root root  4096 Nov 18 19:35 sys
drwxrwxrwt  61 root root  4096 Nov 24 12:08 tmp
drwxr-xr-x  13 root root  4096 Nov 10 18:26 usr
drwxr-xr-x  15 root root  4096 Nov 10 18:26 var

That is a listing of / of an opensuse 12.1 installation

Hope that is usefull