Share VirtualBox virtual disks

Mount guest VDI partitions within the host's file system

This technical notes describes a procedure that will allow a host Linux operating system to mount a virtual partition that is contained within a VDI file. This mounting operation will allow the contents of the guest operating system's disk partitions to be accessed by the host within the host's file system.

Content revision history:
Article first written, 22nd August 2010.

Background information

An openSUSE 11.3 virtual machine had been created as a guest operating system on an OpenSuSE 11.1 host machine. Having configured and tested the guest machine it needed to be transferred to a physical disk partition so that it could be be booted as the main operating system for the real machine. To do this it was necessary to copy the virtual partition of the guest machine into a physical partion of the host machine.

The situation was as follows:

Guest operating system
OpenSUSE 11.3
Name of VDI file containing the guest operating system's disk partitions
SL11.3.vdi (This file represents a single virtual hard disk, and within the file there are several virtual partitions.)
Guest's partition to be copied
/dev/sda2 (ATTENTION! This partition designation is the one seen by the guest operating system when it is running. This is a virtual partition inside a VDI file.)
Guest partition type
Static image (i.e. of a fixed size, not a dynamic images that could change size).

Plan 'A' proved impossible

My first thought was to make the target partition available to the guest operating system as a shared directory. That plan failed because the guest couldn't acquire sufficient priviledges to create symbolic links in the target partition. Time for plan B™.

Plan 'B' described in brief

Before we go through the steps in detail, here is a brief description of what we are going to do. Please note, these instructions presume that the virtual machine has already been created and that the virtual disks were created as static rather than dynamic images.

  1. We will create a backup copy of the VDI file. A simple typing mistake in the procedure we are going to perform will render the VDI file instantly useless so a backup is a sensible precaution.
  2. We will find the starting offset of each virtual partition within the virtual disk VDI file.
  3. We will mount the virtual partitions within the host file system, using the offset information gleaned in the previous step.

Plan 'B' step-by-small-and-simple-step

Step 1: Tools required

You will need various bits of software to complete the process.

  • VirtualBox itself.
  • A file viewer than can show a file in hexadecimal with offset information

Step 2: Be clear about what is what

This might be a good moment to ensure that you have a pen and paper (electronic or carbon versions) handy so that you can write down all the key information that you will need. If you can print this table you can fill some of it out now, and some as you go along.

Information requiredWhat it is in *my* situation, and this documentWhat it is in *your* situation on the computer *you* are going to work on
Name of VDI file, as stored on the host machine.SL11.3.vdi      
Designation (on guest machine) of partition to be copied from VDI file/dev/sda2 
Obscure, memorable textTwoolySkwishedManglewort
Don't use the text I used if you have viewed this web-page from within the virtual machine, because your web-browser will have stored a copy of the 'unique-text' and therefore it won't actually be unique! Invent your own text that contains more than 10 bytes, about 20 bytes should be fine.
Command 1. (On the VM)dd count=1 if=/dev/sda2 of=remember.temp 
Command 2. (On the VM)echo TwoolySkwishedManglewort > nonsense.txt 
Command 3. (On the VM)dd bs=1 count=10 if=nonsense.txt of=/dev/sda2 
Command 4. (On the VM)dd if=remember.temp of=/dev/sda2 
Command 5. (On the host)md /mnt/VDI_with_SL11_3 
The offset within the VDI file of the guest partition. You will discover this information as we go along. Do not assume that yours is the same as mine; it is extremely unlikely and grief will likely visit you if you make such a foolish assumption. 0x3F08200 (66,093,568 decimal)  
Command 6. (On the host)
mount -t ext2 -o loop,noexec,noatime,ro,offset=66093568 SL11.3.vdi /mnt/VDI_with_SL11_3

(All of the above is one single command, that should be on one line. The offset used is referred to earlier and will be found by this process)

Step 3: Safety backup

Make a backup copy of the VDI file that you are about to work with. The process that we are about to perform contains commands that are easily mistyped and a simple typing mistake has the potential to render your VDI file instantly useless.

Step 4: Find the offset within the VDI file of the start of the source partition.

  1. Start the virtual machine, log in as root and get yourself to a shell (command) prompt. Get rid of all non-essential processes to reduce the risk of processes interfering with what you are about to do.
  2. On the VM, issue command 1 from the table above. This creates a copy of the some information we are about to destroy.
  3. On the VM, issue command 2 from the table above. This creates a file that contains some unique, memorable text.
  4. On the VM, issue command 3 from the table above. This places the unique text in the first few bytes of the virtual partition, overwriting whatever was there previously.
  5. Wait a few seconds to allow the write operation to actually be transferred to this VDI file on the physical disk. In practice I found that 15 seconds was long enough and that two seconds was invariable insufficient. Be patient, or you will just waste time later.
  6. Swap control to the host machine.
  7. On the host machine, use your file viewer to open the VDI file. Then search for the place where ONLY the first 10 characters of the unique text occurs and record its starting offset. So, in my case I was searching for “TwoolySkwi” followed by anything that was not “shedManglewort”. Write this offset value down, taking care to note whether the value is hexadecimal or decimal. If it is hexadecimal, convert it to decimal.
  8. Close the file.
  9. Swap control back to the virtual machine.
  10. On the VM, issue command 4 from the table above. This will restore the first few bytes of the partition to what they were before you started.

Step 5: Mount the virtual partition within the host file system

  1. Swap control to the host machine and log in as root.
  2. On the host, issue command 5 from the table above. This will create a mount point within the host file system.
  3. On the host, issue command 6 from the table above. This will mount point the virtual partition within the host file system. Note, these instructions assume that the virtual partition was in ext2 format. If your virtual partition was in some other format such as ext3, reiserfs, or vfat then you will need to adjust the instruction to suit.

Things to keep in mind

  • The mount command used will make the virtual partition read-only within the host file system and also disallow any attempt to execute comands from the virtual partition. These precautions were sensible in my case but might be less sensible in your case. Choose other options to suit personal circumstances.
  • Depending on exactly where you saved the 'unique' text it will possibly exist in two places in the VDI file ... once at the start of the partition and once within the file where you saved it. This is why we created a text of 16 to 20 characters but only used the first 10 of them, and why you must find the place that has only the 10 characters, not any place that contains the whole text.
  • Make sure your command #6 specifies the correct file-system type (i.e. ext2, ext3, vfat, reiserfs or whatever).
  • If you created a dynamic (expanding) virtual partition rather than a static (fixed-size) one then DO NOT attempt to mount the partition using this method. The mount operation might, superficially, appear to work, but it is likely you will corrupt your VDI file because the host operating system won't properly understand the structure and the expanding nature of the file.

Related links

Navigation: (site map) learn linux home pagetechnical articles