Tuesday, December 18, 2012

VirtualBox: Making VMs portable

You might have encountered this scenario before. Your VMs are for some reason no longer in the same location as they used to be, and when you try to open them, they fail.

Perhaps you stored them on a disk that are now on a new drive letter, or perhaps you wanted to open your VMs that was created on a Windows host, from a Linux host. The result is the same, some of your disk image files can no longer be found by VirtualBox, as they are located in a different path.

You might say you never had this problem, your VMs opens just fine from a new location. That is in fact the case for normal VMs, as the disk images are normally located in the same folder as the VM file itself, and are not referenced with a full path name anywhere. However, the minute you start using multi-attach disks, that will change. Here is a typical registration of disk images for a couple of VMs using the same multi-attached disk:

   <MediaRegistry>  
    <HardDisks>  
     <HardDisk uuid="{a28da29c-ffbb-4bec-910d-eb9604e4c85b}" location="D:/VBOX/DISKS/Win2003-32 SATA.vdi" format="VDI" type="MultiAttach">  
      <HardDisk uuid="{076b181e-6347-4d40-9ea8-adace71d70bc}" location="Snapshots/{076b181e-6347-4d40-9ea8-adace71d70bc}.vdi" format="VDI"/>  
      <HardDisk uuid="{ca35f5b3-63b0-4448-ba53-1ea4c230f449}" location="D:/vbox/VM02/Snapshots/{ca35f5b3-63b0-4448-ba53-1ea4c230f449}.vdi" format="VDI"/> 
     </HardDisk>  
    </HardDisks>  
    <DVDImages/>  
    <FloppyImages/>  
   </MediaRegistry>  

If you wonder why disks for two VMs are registered in a single .vbox file, read my post about vanishing disks.

This is from the VM01.vbox file, the first VM that was created with this particular multi-attach disk. We can see that both the master disk and the differencing disk for VM02 are registered with full path, as they do not reside in VM01's folder. To fully understand the location references in the file, you should know that I keep all my VM folders in the D:\VBOX folder, and the multi-attach disks are located in the D:\VBOX\DISKS folder. Everything on the same drive.

We can also see that VM01's differencing disk is registered with a relative path, pointing to the Snapshot folder in the VM01 folder. This led me to believe that I could use relative path names for the other files as well, and I was right. I edited the VM01.vbox file like this:

   <MediaRegistry>  
    <HardDisks>  
     <HardDisk uuid="{a28da29c-ffbb-4bec-910d-eb9604e4c85b}" location="../DISKS/Win2003-32 SATA.vdi" format="VDI" type="MultiAttach">  
      <HardDisk uuid="{076b181e-6347-4d40-9ea8-adace71d70bc}" location="Snapshots/{076b181e-6347-4d40-9ea8-adace71d70bc}.vdi" format="VDI"/>  
      <HardDisk uuid="{ca35f5b3-63b0-4448-ba53-1ea4c230f449}" location="../VM02/Snapshots/{ca35f5b3-63b0-4448-ba53-1ea4c230f449}.vdi" format="VDI"/> 
     </HardDisk>  
    </HardDisks>  
    <DVDImages/>  
    <FloppyImages/>  
   </MediaRegistry>  


As you can see, I only changed the full path names to relative path names. Relative to the VM01.vbox file itself, which resides in the D:\VBOX\VM01 folder.

The result is that I now can open the VMs from Linux without problems. I can also attach the drive to a different Windows computer, and still be able to open the VMs, even if the drive letter is different. In short, my VMs have become portable.