How to Use Windows Drive as a Hard Disk On VirtualBox VM

VirtualBox allows you to add new Hard Disks from the UI, you can just goto Virtual machine settings and add new hard disks, but its a bit tricky if you want to use as one of your hard disk partitions as the new hard disk, specially if your host is Windows OS. We are going to use VBoxManage utility which was installed with VirtualBox.

To be able to use any selected hard disk partition on your VirtualBox VM follow the steps below, I am going to give all the details and explanations but my assumption is you at least have an understanding of virtual machines, command line and environment variables.

What is VBoxManage?

VBoxManage utility allows you to configure and control almost all the aspects of VirtualBox which includes creating virtual hard disks, network interfaces configuring your virtual machines and much more.

For the purpose of this howto we are going to use VBoxManage to list the partitions, and then create a virtual disk file which will be pointing to one of the hard drive partitions.

Set up Environment Variable

First we need to set up the “PATH” environment variable, when ever you run a command on command line all the folders specified in “PATH” environment variable are search to find the respective executable.

Click on Start menu and type “system”, click on “System” Control Panel menu item.

System Control Panel App in Start Menu

Click “Environment Variables” in System properties window.

System Properties Window

Under “System Variables” select “Path” variable and click “Edit”.

Select Path Environment variable

Click “New”

Add-Path-VirtualBox

 

 

Be default VirtualBox is installed in “C:\Program Files\Oracle\VirtualBox”, if you haven’t installed in custom folder location just add this path in the new field.

Add-Path-VirtualBox

Find Out The Hard Drive Number

Now we need to find out the Hard Drive number, so we can properly construct the PhsyicalDrive (more on this later) name for VBoxManage utility.

Click on Start Menu and type “hard disk partitions” and click on “Create and format hard disk partitions”.

create-and-format-hard-disk-partitions

In the “Disk Management” window note the Disk number from the left side of the disk.

disk-management

Create Virtual Drive using VBoxManage

Now we are going to create a virtual disk which we will map to one of the partitions of our physical drive. As we have already noted the physical disk number which is “1” so our Physical Drive construct would look something like: “\\.\PhysicalDrive1”

Click on Start Menu and type “cmd”, right-click on “Command Prompt” menu item and click “Run as administrator”.

cmd-run-as-administrator

Run the following command to get the list of partitions, so we can use the partition number in next command:

# VBoxManage internalcommands listpartitions -rawdisk \\.\PhysicalDrive1

You should see an output like following

vboxmanage-listpartitions-output

Now that we know the partition numbers we can map them to virtual disk files and then add those virtual disks to our Virtual Machine.

Run the following command to map partition number 5 from above output to HomeDrive.vmdk

# VBoxManage internalcommands createrawvmdk  -filename "C:\Users\hamekhan\VirtualBox VMs\Hawk\HomeDrive.vmdk" -rawdisk \\.\PhysicalDrive1 -partitions 5

If the command will execute successfully you should see something like

virtual-disk-created

Now goto your virtual machine settings and select navigate to “Storage” from left side then click on the plus sign with hard drive icon under your desired controller.

click-plus-sign-with-hd-icon

Click on “Choose Existing” and navigate to the folder where you created the “.vmdk” file and then select the file.

click-choose-existing-disk

That’s it, now boot your VM and start using the new hard disk in your virtual machine.

Common Errors

VERR_ACCESS_DENIED

If you get the “VERR_ACCESS_DENIED”, make sure you have started the “Command Prompt” with administrator privileges, see first step in “Create Virtual Drive using VBoxManage”.

 

Vagrant 101: Getting Started

Although late to party, but finally I joined. Until now I had been manually managing my VMs in Virtualbox without Vagrant, but that process was very tedious and slow. Find the image, download it, import it, boot the machine, configure networking, assign the IP addresses, setup the machine. But with Vagrant its as easy as:

Note: Dont’ worry about what it means, I will explain in later chapters.

vagrant init <url to box>
vagrant up
vagrant ssh

As you can see how easy it is to get your new sandbox environment up and running within few minutes. Its not just the basic setup, but Vagrant allows you to completely setup your new VM with everything you need in your dev environment.

The great part is you don’t have to leave your local machine to write code. Confused? Vagrant actually mounts your local folder (where you ran vagrant init) in your VM, so what ever changes you make on your local machine will be available in your vagrant box.

Let’s Dive into Vagrant

Enough of the praising, let’s dive in and experience the power of vagrant. First you need to find Vagrant boxes (VM images), so you can use them for your development. The following instructions assumes that your are using some flavor of linux (the concept is same for other OS, you just have to convert the commands as per your OS).

The best place to find the Vagrant boxes are Vagrant Cloud. Once you find the suitable box for your development. Create your development directory and do the vagrant init (remember git or hg init?) with the the name of the box from Vagrant Cloud. See the commands below:

mkdir web-app-dev
cd web-app-dev
vagrant init hashicorp/precise32

This will create the Vagrantfile and add the box name to the Vagrantfile. You are ready to launch your first vagrant controlled sandboxed dev environment. Just run the vagrant up to get the VM up and running. See the example below (run the command in same folder where you ran vagrant init):

vagrant up

This will download the box (VM image) from the Vagrant cloud, after downloading it will boot the box, do the provisioning and then it will mount your development folder on VM at /vagrant. After few minutes your new VM will be up and running.

Once the VM is up, you can login to the VM by just doing a vagrant ssh. Yes it is this simple, see the example below:

vagrant ssh

Now that you are logged into the VM, you can configure it for your development manually (for now). Later on I will write about how can you automate the setup part too, based on your development needs.

Checkout the folder share, by creating files in /vagrant folder on your new VM and then go back to your local folder and see them appear there. Similarly you can create files on local and they will immediately appear in your VM.

Where is the Version Control?

You can add Vagrantfile to your version control so you can replicate your Vagrant setup to other devs or on other locations too. Don’t add other Vagrant files to version control, for now it is just Vagrantfile. Simply just run the following commands (from same folder where you ran vagrant init):

git init
git add Vagrantfile
git commit -m "Add Vagrantfile"

What’s next?

I tried my best to make this getting started guide as simple as I could, and not be confusing for the new developers who are still trying to figure out how linux works. Later on I will write about provisioning and advanced networking setup in Vagrant.

If you have any questions, confusions feel free to ask them in comments and I will try to help you as much as I can.