Home > Tips, Windows > Run Linux both from Virtual Box and natively

Run Linux both from Virtual Box and natively

December 26, 2014 Leave a comment Go to comments

VirtualBoxToday I had a need to run linux natively. Don’t ask why – it’s a sad story about multiplatform programming in GCC which should work on any platform. Unfortunatelly it’s not compatible with Windows. To have all necessary tools I need any kind of linux machine. It’s possible to run linux in a virtual machine (like Virtual Box) but when you need to use the full power of your machine, then running linux on Virtual Machine on Windows eats some resources unnecessarily. So the question is – how to install linux and usie it at Windows and natively without Windows? – I’m a Windows fan and also too lazy to make some big experiments or make 1001 workarounds but finally I found one interesting solution.

The Background

Well if you are not interested in why I had to install linux then just skip this section. I would be gratefull if you know the answer for my question. I had to compile two simple files of code using gcc. Visual Studio by default is not gcc compilant but there are many tools which allow to compile gcc also in Visual Studio – but everything failed for me.

I have installed MinGW and compiled some libraries from source code (I do not want to install CygWin) but still some issues were present: there was a need to use a linux specific function like qsort_r. Yes… there are some workarounds for this one, but well… really? Sorry I am too lazy to experiment with it, next time I will probably need some other unix-specific function – I do not want to focus on the workarounds and hacks!.

Available solutions

It popped in my head that there is a possibility to run Windows installed on a VHD (Virtual Hard Drive) from boot – like it is presented on Hanselman blog or MSDN. But unfortunatelly this functionality is available for Windows only (I have found some workarounds and answers how to run linux from VHD, but I hate hacks and what is more some solutions seemed to be outdated).

A good one, but still I hate it

There is also possibility to install natively linux on your USB stick or external drive and then boot it from the external drive, but do I really need to take my external HDD or USB everywhere?, I have enough space on my laptop, so…

The final soulution

I have used Virtual Box tool which is not available through the UI but from command line. Finally I used one empty partiion on my physical HDD and installed Ubuntu LTS on it – this allows me to run Ubuntu on Virtual Box when Windows is being used or I’m able to to start Ubuntu during boot. Below I’m presenting all necessary steps and commands which I had to use.

Create partition for linux

I have two HDDs on my laptop – originally I had a CD-rom, but who uses it today? As presented on the screenshot I created a partition on my HDD for linux using diskmgmt.msc command. The original drive where Windows is installed is a SDD disk and I do not want to touch it.

DiskMgmt

Map your physical Disk using Virtual Box

Now I will map my Disk 0 (presented on the screenshot above as Dysk 0) to Virtual Box using VBoxManage.exe command. You can find detailed description of this command in Advanced Storage Configuration chapter, here I’m presenting only steps which were required for me.

  • Open command prompt as an administrator (I used Powershell)
  • Navigate to Virtual Box folder where the VBoxManage.exe command is placed
  • Run the command below (everything is in one line)
.\VBoxManage.exe internalcommands createrawvmdk 
    -filename 'F:\VHD\hdd0.vmdk' 
    -rawdisk \\.\PhysicalDrive0

Explanation of this command. We are going to create a file hdd0.vmdk which is a mapping of our physical Disk 0 found in the diskmanager. There is also a possibility to map only a specified partition (good for some testing purposes), but I granted whole access to Virtual Box for this machine. The created vmdk file is very small, it’s size is just few kilobytes. Please note that the PhysicalDrive0 is Windows specific – Linux and OSX use different names.

Create Virtual Machine on Virtual Box

Next create virtual machine in VirtualBox. The procedure is the same as for any virtual machine except of one thing – do not create disk – leave it empty for this time. I called this instance just Ubuntu. You can also point Ubuntu ISO image with the installation and set any necessary configurations. Save it and close Virtual Box.

VirtualBox

Attach Disk to Virtual Machine

Now after you created the virtual machine called Ubuntu – now it’s possible to attach vmdk file to it. From the same command prompt (which is run as an administrator) execute another command (also in one line).

.\VBoxManage.exe storageattach Ubuntu 
  --storagectl "SATA" 
  --port 0 
  --device 0 
  --type hdd 
  --medium 'F:\VHD\hdd0.vmdk'

Start your Virtual Machine

If you want to start your Virtual Machine with the physical disk, you need to run Virtual Box as an administrator. When you will navigate to the disk manager of your instance you will see, that the vmdk file is attached and ready to use.

VirtualBoxStorage

You can now start your instance and install Ubuntu on the prepared partition. After the installation is complete you are able to boot Ubuntu directly from the disk!

Few things

  • Remember to run Virtual Box as an administrator
  • Unfortunatelly those commands may be removed or may change in future releases of VirtualBox
  • I have no access to other Windows partitions from linux (when ran from Virtual Box) – I see those partitions on Linux and I can attach them only in read only mode. A workaround is to use a shared folder for VIrtual Box
  1. December 27, 2014 at 21:05

    “I have no access to other Windows partitions from linux” – I assume, this is only when you use VM mode? It seems logical – virtual machine is supposed to be as isolated from the host as possible. Aside from Virtual Box shared folders, you can also simply share your Windows hard drive using SMB/CIFS and mount it in Linux. It’s not a dirty hack… you can probably also write a simple init script that can check if the “windows” drive is accessible natively or if we need to mount it using SMB.

    On the other hand… you might also be interested in checking alternative approaches like coLinux (http://www.colinux.org) / andLinux (http://www.andlinux.org). Seems a bit outdated, but might work well for you. As far as I can tell coLinux is being developed on 64bit systems, and SVN doesn’t look as dead as the web site. So maybe a fresh checkout from SVN does the job well…

    Other than that – I understand your unhappiness with the non-portability of things between Linux and Windows, but keep in mind that the same story goes between every two different platforms and native code. The function you mentioned – qsort_r is a great example. It exists on both OS X and Linux, but … the ordering of arguments is different on each platform! And it’s not gcc fault – it’s much more general problem. Basically different platforms are different :)

    • December 27, 2014 at 21:14

      Yes – I’m not able to attach those Windows partitions when linux is executed in Virtual Box (and as you mentioned, it’s normal) – I’m able to mount those partitions only in read only mode. I have already modified the cited text you mentioned in my post, hope it’s more clear.

      Thanks for your great links!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: