Click here to Skip to main content
15,881,089 members
Articles / Command line
Tip/Trick

VirtualBox Physical Disk Import Batch Script

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
31 May 2014CPOL3 min read 8.6K   55   4  
A Windows batch script to import a physical disk into a virtual machine

Introduction

In Windows, it is not so obvious how to import a physical disk into a virtual machine. Therefore, I did some research, quickly found what I needed but as I had to use it more and more often, I wrote a batch script to do the work. This script will have the following functionality:

  • Check for administrative privileges
  • Find the install path of VirtualBox from registry
  • List all physical disk drives installed
  • Ask for the disk number to import
  • Check and eventually ask if the target file shall be overwritten

Background

I am checking on many old hard disk drives of my company at the moment. I connect them using a USB-to-IDE cable and because there is a live disk encryption installed, I boot the disks with VirtualBox. Now the problem is that every time you change the disk, you have to create a new import-link file for that disk so that the correct parameters are set. So to make it nice and easy, I created this script.

The Manual Way

Usually, you would do the following to import a physical disk into Virtual Box. First, you need to find out which number your physical disk was mapped to. You can accomplish that by running 'diskmgmt.msc' and finding your disk there, It will look something like this:

Image 1

Then you start a command prompt with administrator privileges, navigate to your VirtualBox install directory ('that one where VBoxManage.exe is located) and run the following command:

VBoxManage internalcommands createrawvmdk -filename C:\sd.vmdk -rawdisk \\.\PhysicalDrive#

where # is the disk number you have found. This will (if successful) create a small file 'C:\sd.vmdk' which you can then use in VirtualBox.

Using the Script

Basically the script is a wizard - just start it and follow the instructions.

When the script starts, it checks if it has administrative privileges. If it doesn't, the command-line text turns red, a VBS script is created that starts the script again with administrative privileges (prompting if configured in Windows) and than exits the first instance.

After that (assuming administrative privileges have been granted), it reads the Registry finding the install path of VirtualBox. If it could not be found, an error is shown.

Then it lists all physical drives installed and prompts for you to input the number of the disk to create a link file from - or to press [C] to cancel.

Image 2

The scripts always chooses the file 'PhysicalDisk#.vmdk' in the same directory as the script as the target file. If the target file exists, you are asked if you want to overwrite it.

Image 3

If everything went good (the scripts if the file was created), the text turns green - if not it turns red - and a fitting message is printed.

Using the Link File

Very important: MAKE SURE YOU ARE RUNNING VIRTUALBOX AS ADMIN!

When you started VirtualBox, go into your virtual machine's settings and switch to the 'Storage' section.

Image 4

Click at 'Add Hard Disk' at the controller, click 'Choose existing disk', navigate to the created file and select it.

Image 5

If this message pops up, you most probably did not start VirtualBox with administrator privileges.

Image 6

To remove the virtual disk from VirtualBox, start the 'Virtual Media Manager' in the 'File' menu of VirtualBox, select the disk and click on 'Release' - then you can remove it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
I'm working mainly on .NET Compact Framework C# on mobile devices at work. At home it's .NET Full Framework C# and a bit JavaScript.

Comments and Discussions

 
-- There are no messages in this forum --