Friday 21 July 2017

Booting Windows as either dual-boot physical machine or as a Virtual machine

How to boot Windows as either dual-boot physical machine or as a Virtual machine

Here is a scenario.

You have a Linux PC that you want to also dual boot to Windows. That's not hard to do right?

Unfortunately though, we all know dual booting is a bit of a pain when you just want to do something quick in Windows. You need to close everything you are doing in Linux just to boot into Windows.

One solution to that is to run Windows in a VM, that's been possible for years now right? Well the problem with that is that sometimes you need to run native Windows to say, play a 3D game.

You could maintain 2 Windows systems, one via dual-booting and the other as a VM but who wants to maintain two copies of Windows?

This is what I do.

Our starting point is a Linux system on one disk (/dev/sda). We will be installing Windows on to a separate disk that is currently empty (/dev/sdb)

WARNING: If you muck this up you can destroy all the data on your Linux system. Make sure you have backups of everything and you are abso-fricking-lutely sure you have identified the correct drive devices

You can check your disks using this command:
sudo fdisk -l
For the remainder of this tutorial I will be using /dev/sdb for the drive that will host Windows 7.

Let's get started!

In your Linux installation you want to add your user to the "disk" group:
sudo usermod -a -G disk brettg
This allows your user to access the physical drive that Windows 7 is installed on.

Note: You will need to logoff and log back in again for this change to take effect.

Install Virtualbox:
sudo apt install virtualbox
Create a place for your Virtualbox disk images:
mkdir -p $HOME/VirtualboxImages/
Create a new virtual disk that references the physical drive that Windows 7 will be installed on:
VBoxManage internalcommands createrawvmdk -filename $HOME/VirtualboxImages/Windows7.vmdk" -rawdisk /dev/sdb
Open up Virtualbox and create a new virtual machine selecting "Use an existing virtual harddisk file" when you are setting it up.

Insert your Windows CD into the virtual machine (either as a physical CD or an ISO image).

Start the VM and go through the normal Windows installation process, then log into Windows.

Open the registry editor. Edit the following keys and set them all to 0:
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\atapi\Start
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\intelide\Start
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\pciide\Start
This forces Windows to install and load three types of disk drivers at boot. (I don't know why you need to set these to zero). By default it just installs the driver that was needed at install time. This is requireed because when we try and boot Windows later it will not be able to read the disk because it doesn't have the proper driver for it.

Shutdown the VM

You will need to add your Windows disk to grub:
sudo update-grub
Now, hopefully if everything went according to plan you should be able to reboot and find Windows listed in your grub menu. Select that and boot into Windows.

Once you are in Windows, you will have to do all the usual driver installs etc but you already knew that right?

Have fun!

Note: Tested with Ubuntu 16.04.1 host and Windows 7/10 guests