Warning: This is a highly sensitive operation. You can very easily damage your system and lose data. What worked for me may not work for you. You would be wise to make any needed backups before proceeding.
My experiences and this website are the source of this information: http://fds-team.de/cms/articles/2013-12/use-a-real-windows-7-partition-in-virtualbox-kvm-vmware-player-u.html That website also contains other details that may be helpful.
These instructions are for Linux Mint 17.1, but the steps should be similar for other Linux distributions especially ones based on Ubuntu. I recommend using KVM instead of VirtualBox or VMware Player or other VMware products. For instructions on getting this to work in something other than KVM, see the link.
Why KVM? From the research I did, there seems to be three main players in this category at this time. KVM, VirutalBox, and a few different VMware products. The VMware products are not open source and none of them are free for commercial use. VirtualBox is free and open source, but if you need to add USB 2.0 support, you need to install an expansion pack that you are not supposed to use for business/commercial use. Even though USB 3.0 is supported without that expansion pack, the USB support in VirtualBox is more problematic than in either VMware products or KVM. KVM is open source and free to use for both personal and business/commercial use. KVM avoids overhead by using a kernel module added to Linux, thus improving performance and compatibility: http://blog.scalecomputing.com/kvm-or-vmware-why-kvm-is-right-for-the-times-part-1-of-2/
In the following instructions, I explain how to use virt-manager which will use KVM under the hood, so you can use a GUI instead of setting up everything in KVM through a terminal.
Install both Linux and Windows 7 on the same hard drive on separate partitions if you don't already have them installed. If you install them on separate drives refer to the instructions in the link. Set them up to where you can dual boot between them. Get your partitions set up how you want before proceeding. Note: Some Windows installations create a separate partition for hibernation. If you want to remove this partition now is the time to do it. You will have to look up instructions for this, but I know you can do it from inside Windows. If you don't do it correctly/fully, the partition can recreate itself. Therefore, if you choose to delete this partition, restart your computer and boot into Windows again, to see if it recreates the partition.
Boot into Linux.
Figure out the path to the hard drive that contains both Linux and Windows. Usually this is /dev/sda
On Linux Mint 17.1 you can start Disks from the main Menu to see the path to your hard drives. Select your hard drive from the list of Disk Drives. Now information about your hard drive will take up the rest of the window. At the top of this information, it should show the size of your hard drive in bold with the path you need in small text just underneath that.
NOTE: From now on I will use /dev/sda
to refer to the path to the hard drive containing partitions for both Windows 7 and Linux. You need to substitute this with the correct path.
Run sudo fdisk -l /dev/sda
to get some needed information about the partitions on your hard drive with Windows 7. You will likely see two NTFS partitions used by Windows 7. The first one is fairly small. It contains the bootloader and Recovery System. The other NTFS partition should be much larger since it contains the actual Windows 7 installation. Both NTFS partitions will be needed for the VM.
Goal: Now your goal is to create a virtual hard disk. This will allow you to setup the MBR to be and stay correct regardless of whether you are booting Windows 7 from a VM or directly. This will prevent you from having to run a repair utility every time you switch between booting into Windows 7 directly and from a VM. I use device-mapper to create a linearly mapped virtual hard disk mapped to a loop back device and my two Windows 7 partitions. The website I got most of these steps from has you use RAID, however, I had problems getting that to work right. With RAID the virtual disk was too small due to chunk size rounding. https://unix.stackexchange.com/questions/217078/raid-mdadm-virtual-disk-too-small Setting up a device-mapper linear mapping seems like a better fit, and it avoids the chunk size rounding issue. I have had success with device-mapper.
Examine your fdisk output from the previous step. Determine how many bytes a single sector takes up. If your sectors are 512 bytes like mine, this will make things easier.
Examine your fdisk output again. Determine how many sectors are not used before the start of the first partition. In my case, the first partition starts at sector 2048, which means the first 2048 sectors are not being used by any partition (0-2047). This contains the GRUB bootloader.
Create an image of the bytes from the previous step using the following command. You will need to replace 2048 with the correct number of sectors. If your sectors are not 512 bytes you will also need to specify the bs=bytes_per_sector parameter:
dd if=/dev/zero of=boot.mbr count=2048
You should now have an image file named boot.mbr in your current directory.
Examine your fdisk output again. Determine the path to your Windows 7 partitions. For me they are the first two listed partitions and they both use NTFS. My two paths are /dev/sda1
and /dev/sda2
NOTE: From now on, I will use /dev/sda1
and /dev/sda2
to refer to the Windows 7 partitions. You will need to change these to the correct path on your system.
Create a loop back device from the image file:
sudo losetup /dev/loop0 boot.mbr
This sets you up to be able to map the loop0 device to a virtual disk.
Create a file somewhere that will define what gets mapped to the virtual disk. This file will be used by device-mapper. I put mine at ~/vm/virtual_windows_disk_def.
Inside ~/vm/virtual_windows_disk_def, create a line for each mapping. Each line should take the form "logical_start_sector num_sectors linear destination_device 0". linear just means we are doing a linear map. destination_device should be the device path. The 0 means the mapping will start at the first sector of the destination device. The first line needs to map boot.mbr to the start of the virtual disk. You will need an additional line for each Windows 7 partition. These are the contents of my file:
0 2048 linear /dev/loop0 0
2048 3072000 linear /dev/sda1 0
3074048 447886719 linear /dev/sda2 0
This is how I got the correct number of sectors for each line:
blockdev --getsize /dev/loop0
blockdev --getsize /dev/sda1
blockdev --getsize /dev/sda2
Check to see if /dev/mapper/control is present. If not, try loading the dm_mod kernel module:
modprobe dm_mod
sudo dmsetup create virtual_windows_disk ~/vm/virtual_windows_disk_def
Make sure /dev/mapper/virtual_windows_disk is the correct size.
Start fdisk in interactive mode to setup two partitions on your /dev/mapper/virtual_windows_disk disk to perfectly and exactly match your two Windows partitions on your /dev/sda disk. You can use the on screen help and instructions to complete this process. If you cannot get the partitions setup perfectly, quit instead of writing/saving these partitions. As far as I know, these are the things that need to match for each partition: Starting sector, ending sector, whether or not it is bootable, and the file system type. Replace 512 with the number of bytes per sector for your /dev/sda disk (this will start fdisk in interactive mode):
sudo fdisk -b 512 /dev/mapper/virtual_windows_disk
sudo chown your_username:group /dev/mapper/virtual_windows_disk
- From the main Menu, start Virtual Machine Manager (this is virt-manager).
- Inside virt-manager select File->Add Connection.
- Make sure Hypervisor is set to QEMU/KVM.
- Make sure the check box for Connect to remote host is unchecked.
- Make sure Autoconnect is checked.
- Click "Connect" to create the connection.
- Right click on the connection (localhost (QEMU)) and select New.
- Enter a name.
- For "Choose how you would like to install the operation system" select "Import existing disk image".
- Click the "Forward" button.
- Under "Provide the existing storage path" click Browse and select /dev/md-0 (on my system this was automatically setup as an alias to /dev/mapper/virtual_windows_disk)
- Keep going forward until the VM starts. It should indicate a problem that needs to be repaired. Windows should not be able to boot because of this problem, but at least if you get those results, you are on the right track.
- Force the VM to shut down.
- Attach a Windows Install/Repair DVD/image to the VM. My computer came with Windows installed already, but Toshiba provided a way to create install/repair DVDs. For me, I need to use disk 4 since that is the disk with Windows Recovery Environment on it. I was able to create an ISO of this disk from inside Linux. I prefer to just attach the ISO to the VM as a CD Drive rather than placing the disk in the drive, but either way works.
- In the VM details area, select "Boot Options". Make sure "Enable boot menu" is checked. Make sure Hard Disk and CDROM are checked indicating they will be boot options. Move the Hard Disk to the top with the CDROM just below that using the arrow buttons. Click the "Apply" button to save your changes. Now when the VM is booting, it should wait a little while to give you a change to press F12 to select which device to boot from.
- Start the VM and press F12 to access the boot menu.
- Boot from the DVD/CDROM drive. This should start your Windows install/repair disk.
- The instructions from other website say to press shift + F10 to access the command prompt, but this does nothing for me. I had to select System Recovery options (instead of Toshiba recovery options). Next.
- On my system a diagnostic is automatically run and the computer offers to repair a problem it found. Do not let it do the repair, choose the other option.
- Make sure "Use recovery tools..." is selected instead of "Restore your computer using a system image...". You should see Windows 7 in the list of operating systems. Make sure that is selected and press "Next". Click the link that says "View advanced options..."
- Select Open a command prompt.
Enter this command into the prompt:
bootrec /FixMbr
Now your VM should be bootable, but don't do that yet.
Determine which drive letter was assigned to your Windows installation by entering the following commands into the prompt:
dir C:
dir D:
One of those should indicate that drive letter is not empty and contains your files. That is the correct drive letter you will need for the next step. For me it was drive C.
You will now need to locate your drive signature in the registry. Enter these commands into the prompt:
reg load HKLM\Computer_System D:\Windows\system32\config\system
regedit
- Navigate to HKEY_LOCAL_MACHINE\Computer_System\MountedDevices. Locate the \DosDevices\X: entry where X matches the drive letter from two steps ago. You will need to reference the data from that entry for the next step.
Without closing the Regsitry Editor, activate the command prompt window. Enter the following commands:
diskpart
DISKPART> select disk 0
For the next step, you will need to replace the letters at the end with the first four sets of characters currently displayed in the value for that entry in the registry editor. However, you will need to reverse the order of the sets. For example, if you see ab cd ef gh, you will need have ghefcdab at the end of the command:
DISKPART>uniqueid disk id=ghefcdab
- Enter this command:
exit
. The diskpart program should exit, leaving you inside the normal command prompt.
- Close the registry editor.
- Enter this into the command prompt:
reg unload HKLM\Computer_System
- Close the command prompt.
- If you see an option to restart or shut down, do this now. Otherwise instruct the VM to shut down. If that does not work, force the VM to shut down. If your VM is shut down, leave it that way. If it restarted, either shut it down manually, or if that is not possible, force it to shut down.
- In your VM details, check the "configuration" arrow to show more options. Click the "Copy host CPU configuration" button. Click the "Apply" button. You are finished! The next steps will help you confirm everything worked correctly.
- Start the VM. It should boot into Windows normally with no issues or repairs needed. Shut down the VM.
- Do not take your eyes off the screen during this next step. Shut down your system by choosing to Hibernate (otherwise you will have to repeat some steps when you boot Linux again). Start the computer and then boot into Windows directly. Watch the screen carefully to see if any repairs happen or are required. If everything is correct, it should boot into Windows without doing or needing any repairs. On my system, when things are not setup correctly, it automatically repairs Windows and then boots into Windows as if nothing bad happened, which is why you should watch the screen carefully. If a repair is needed, something went wrong with the signature change you did in diskpart. Boot into Linux and repeat the relevant steps being very careful to get the signature correct.
Suggestion
The steps involving creating the loop back device, and setting up the /dev/dm-0 virtual drive using device-mapper will have to be repeated each time you boot Linux. You should script this so you can just run the script before starting virt-manager. For now, that script is beyond the scope of this answer, but I may add it later after I create one for my system.
Tips
- As you are trying to get things working, if you need to restart your computer while Linux is running for the purpose of booting into Windows directly, I suggest Hibernating and then starting the computer. This way when you eventually start Linux again you will not lose the work you did. This will help you until you get a script setup, but it is not a permanent solution as you will eventually need to fully restart Linux for real.
- If you get stuck somewhere due to problems with virt-manager or KVM, you can try other options like VirtualBox or VMware Player. Since this solution creates a virtual drive you can switch between different virtual machine programs without losing your progress. Even after everything is working, this is part of the beauty of this solution. Any time you want, you can run the same Windows installation from a normal Windows boot or any virtual machine program of your choice. Just don't run more than one virtual machine at a time.
- The website most of this info came from suggests that you will probably get much better KVM performance if you install VirtIO drivers on your Windows installation. I have not tried this yet.
occasionally I will need to run some Windows software that should run well enough in an emulated environment
- If that's the case, than you would probably be better off trying to use Wine...