Run Linux both from Virtual Box and natively
Today 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.
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!.
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.
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.
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.
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!
- 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