4

I am about to install Linux Mint 17.1 (Cinnamon) as my main OS. I also want to be able to boot into Windows 7 occasionally. I also want to run a Windows 7 virtual machine inside Linux. Ideally I would like to have a single Windows 7 installation that I can either boot into or run from a virtual machine from inside Linux. Is that possible? What virtual machine software supports that? Ideally I would like to start out by installing both Linux and Windows without involving the virtual machine and then afterwards install a virtual machine that will just work with that Windows 7 installation.

I'm sure many will want to know why I want to do this. I prefer to work in Linux as much as possible. However, occasionally I will need to run some Windows software that should run well enough in an emulated environment. At other times, I may want to play games on Windows 7 and get the full power of my graphics card and other hardware by booting directly into Windows. I don't want to deal with multiple Windows installations, I don't want to purchase another copy of Windows, and I don't want it to take up as much space as two installations. I am not sure if the Windows 7 Recovery DVDs I made would work any other way than just installing it normally without involving a virtual machine.

The more I think about this the more it seems it may not work. Windows will probably need separate driver installations depending on if it is running as a virtual machine or I am booting directly into it. Windows will probably think it is running on a separate computer and need to be registered twice for the two different computers, which is not allowed.

Update

I now know that I would prefer to use either KVM, VirtualBox or another option that is free and open source. Is it possible to do what I want with KVM or VirtualBox?

13
  • I would suggest you run Windows in a virtual VMware Player partition in Linux. Then you have access to both systems at the same time. Here is a tutorial I once made on how to deal with VMware Player that should give you a feel for what it is. That was on a Windows host but in essence it is the same if the host is Linux Mint. ==> onedrive.live.com/…
    – whs
    Commented Jun 23, 2015 at 19:09
  • I've provided this solution so many times it's getting rediculous. Not even going to bother posting an answer. There's other ways but just here: pendrivelinux.com/boot-a-usb-flash-drive-in-virtualbox I've also done the same for creating a virtual image of the windows physical partition (that's next to Linux's dualboot partition on physical dirve) but I'm not about to go searching for that answer anytime soon, especially since if I had to do it over, I'd save myself hours of tinkering and just use the suggested article.
    – user431052
    Commented Jun 24, 2015 at 1:15
  • 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... Commented Jun 24, 2015 at 1:30
  • I've provided this solution so many times it's getting ridiculous :P Not even going to bother posting an answer lol. There's this way >> geekery.amhill.net/2010/01/27/…<< but I prefer a different way since it's easier and takes much less time to setup. >> pendrivelinux.com/boot-a-usb-flash-drive-in-virtualbox
    – user431052
    Commented Jun 24, 2015 at 1:37
  • 1
    Thanks @BiTinerary! I think I am close to getting this to work thanks to a link you provided. I wanted to post the correct link here since the one you posted does not quite work: geekery.amhill.net/2010/01/27/… Since I am trying to do this from within Linux, those are the instructions I am trying to follow. If I get it to work, I will try to post a full answer. Commented Jul 2, 2015 at 0:44

1 Answer 1

3

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.

  1. 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.

  2. Boot into Linux.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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

  12. Check to see if /dev/mapper/control is present. If not, try loading the dm_mod kernel module:

    modprobe dm_mod

  13. sudo dmsetup create virtual_windows_disk ~/vm/virtual_windows_disk_def

  14. Make sure /dev/mapper/virtual_windows_disk is the correct size.

  15. 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
    
  16. sudo chown your_username:group /dev/mapper/virtual_windows_disk

  17. From the main Menu, start Virtual Machine Manager (this is virt-manager).
  18. Inside virt-manager select File->Add Connection.
  19. Make sure Hypervisor is set to QEMU/KVM.
  20. Make sure the check box for Connect to remote host is unchecked.
  21. Make sure Autoconnect is checked.
  22. Click "Connect" to create the connection.
  23. Right click on the connection (localhost (QEMU)) and select New.
  24. Enter a name.
  25. For "Choose how you would like to install the operation system" select "Import existing disk image".
  26. Click the "Forward" button.
  27. 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)
  28. 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.
  29. Force the VM to shut down.
  30. 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.
  31. 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.
  32. Start the VM and press F12 to access the boot menu.
  33. Boot from the DVD/CDROM drive. This should start your Windows install/repair disk.
  34. 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.
  35. 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.
  36. 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..."
  37. Select Open a command prompt.
  38. Enter this command into the prompt:

    bootrec /FixMbr

    Now your VM should be bootable, but don't do that yet.

  39. 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.

  40. 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

  41. 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.
  42. Without closing the Regsitry Editor, activate the command prompt window. Enter the following commands:

    diskpart

    DISKPART> select disk 0

  43. 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

  44. Enter this command: exit. The diskpart program should exit, leaving you inside the normal command prompt.
  45. Close the registry editor.
  46. Enter this into the command prompt: reg unload HKLM\Computer_System
  47. Close the command prompt.
  48. 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.
  49. 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.
  50. Start the VM. It should boot into Windows normally with no issues or repairs needed. Shut down the VM.
  51. 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.
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .