HOWTO: Windows 7: In both VM and native -- VBox3.x

Post Reply
pedrogfrancisco
Posts: 10
Joined: 6. Jun 2010, 12:23
Primary OS: Fedora other
VBox Version: OSE Fedora
Guest OSses: Windows

HOWTO: Windows 7: In both VM and native -- VBox3.x

Post by pedrogfrancisco »

Windows 7: In both VM and native (using Linux as host OS) -- VirtualBox 3.x

Some scripts were updated for VirtualBox 4.x at Discuss: HOWTO: Windows 7: In both VM and native by PDP8User.

Feel free to create another guide based on this one.

Note: for some reason quotes are disabled, "You cannot use certain BBCodes: [ quote ].", so Italic will have to replace quotations.

Also, this tutorial is shamelessly based on vkov_tinsky's tutorial, without it I wouldn't have been able to boot Windows XP, let alone 7, both in VM and Native.

Please post any comments/suggestions etc in this thread: Discuss: HOWTO: Windows 7: In both VM and native

Requirements
  • one hour of your time (probably less if you don't get it wrong along the way but it's a long text)
  • Windows 7 installation DVD (not recovery)
  • willingness to create another user on your Linux install
  • capability of following instructions
  • backup(s), lots of backup(s)
Terminology
  • Native - Native; in our case, the Windows 7 running by itself on the 'regular' disk, .
  • VM - Virtual Machine; in our case, the Windows 7 running inside Virtual Box, through the Virtual Machine we will create in Virtual Box; that VM will have as disk a 'raw' partition. This means that instead of creating an empty disk in Virtual Box, we will instead tell Virtual Box to use the partition - the segment of the disk - where Windows 7 is already installed as the disk for the Virtual Machine.
  • MBR - first 512 bytes of the disk. When you boot any OS like Windows or Linux the steps are: load BIOS -> BIOS loads MBR -> MBR loads BCD/GRUB/NTLDR -> the appropriate OS is then loaded; MBR includes disk partition information.
  • BCD - Boot Configuration Data, Windows 7/Vista & others boot loader
  • GRUB - GRand Unified Bootloader, Linux & others boot loader
  • NTLDR - NT loader, for Windows XP & others, old Windows boot loader
References which should be read to feel comfortable and to know about potential issues but not really necessary unless things go wrong. However, unlike [1] I'm not installing Windows on the VM first and only afterwards booting the native; I'm booting in VirtualBox an already installed Windows 7 version

I can't say this enough, so I'll quote vkov_tinsky:
WARNING: Only follow these instructions if you understand what you're doing, I'm not responsible for you losing your data!

No warranty is given that this tutorial doesn't blow up your computer :mrgreen:
  1. Virtual Machine Configuration
    1. General Standard settings (as chosen by the wizard) will be adequate, taking the following into account:
      • [I/O APIC] This needs AFAIK to be turned on as well as ACPI.
      • [SATA Controller] Enable this since it will offer much better I/O performance. Yes, finding SATA option isn't checking a box as it was, but the option is there.
      • [Network] Enable one adapter and set its MAC address to that of your real network card.
      • The rest of the devices can be left as it is
    2. DMI BIOS settings (information taken from this thread which in turn had taken it from this thread)

      Now, [1] says:
      If you have an OEM version of Windows XP you should set these additional parameters as follows so your copy of Windows does not have to be reactivated. In total there are 13 such parameters (which you can set, see section 9.13 in the manual). To determine them use the below commands.

      Supposedly the following things are just for OEM versions. Personally, I haven't and won't try creating the VM without them, as I'm afraid doing it will unleash the Wrath of Windows Activation upon me.

      First use dmidecode -t0:

      Code: Select all

      vt@vt:~$ sudo dmidecode -t0
      # dmidecode 2.9
      SMBIOS 2.3 present.
      
      Handle 0x0000, DMI type 0, 20 bytes
      BIOS Information
              Vendor: Dell Computer Corporation
              Version: A12
              Release Date: 08/26/2004
              Address: 0xF0000
              Runtime Size: 64 kB
              ROM Size: 512 kB
              Characteristics:
                      PCI is supported
                      PNP is supported
                      [...]
              Bios Revision: 2.3
              Firmware Revision: 2.3
      
      This determines: DmiBIOSVendor (Dell Computer Corporation), DmiBIOSVersion (A12), DmiBIOSReleaseDate (08/26/2004), DmiBIOSReleaseMajor (2), DmiBIOSReleaseMinor (3), DmiBIOSFirmwareMajor (2) and DmiBIOSFirmwareMinor (3).


      • If Bios and Firmware Revisions are not listed, set them both to the version of SMBIOS.



      Subsequently run dmidecode -t1:

      Code: Select all

      vt@vt:~$ sudo dmidecode -t1
      # dmidecode 2.9
      SMBIOS 2.3 present.
      
      Handle 0x0100, DMI type 1, 25 bytes
      System Information
              Manufacturer: Dell Computer Corporation
              Product Name: Dimension 4600i             
              Version: Not Specified
              Serial Number: JTGL999
              UUID: 99999C9C-9999-9999-999C-CAC99F999999
              Wake-up Type: Power Switch
              Family: X86-based PC
      
      This determines: DmiSystemVendor (Dell Computer Corporation), DmiSystemProduct (Dimension 4600i), DmiSystemVersion (Not Specified), DmiSystemSerial (JTGL999), DmiSystemUuid (99999C9C-9999-9999-999C-CAC99F999999) and DmiSystemFamily (X86-based PC).
      • If a field is not shown or indicates "Not Specified" fill it in as "<EMPTY>" (see below).
      Now you can use the VBoxManage setextradata command to set these fields. The easiest way is to do this with a script:

      Code: Select all

      #! /bin/bash
      VM_NAME="raw7" # Name of your Virtual Machine
      VSETED="VBoxManage setextradata $VM_NAME"
      CFG_PATH="VBoxInternal/Devices/pcbios/0/Config"
      $VSETED $CFG_PATH/DmiBIOSVendor       "Dell Computer Corporation"
      $VSETED $CFG_PATH/DmiBIOSVersion      "A12"
      $VSETED $CFG_PATH/DmiBIOSReleaseDate  "08/26/2004"
      $VSETED $CFG_PATH/DmiBIOSReleaseMajor  2
      $VSETED $CFG_PATH/DmiBIOSReleaseMinor  3
      $VSETED $CFG_PATH/DmiBIOSFirmwareMajor 2
      $VSETED $CFG_PATH/DmiBIOSFirmwareMinor 3
      $VSETED $CFG_PATH/DmiSystemVendor     "Dell Computer Corporation"
      $VSETED $CFG_PATH/DmiSystemProduct    "Dimension 4600i"
      $VSETED $CFG_PATH/DmiSystemVersion    "<EMPTY>"
      $VSETED $CFG_PATH/DmiSystemSerial     "JTGL999"
      $VSETED $CFG_PATH/DmiSystemUuid       "99999C9C-9999-9999-999C-CAC99F999999"
      $VSETED $CFG_PATH/DmiSystemFamily     "X86-based PC"
  2. Partitioning
    I'm writing for the case Windows 7 has already been installed.
  3. VMDK creation and usage (information partially taken from this thread)
    WARNING: You do not want applications on the Host OS to write to the partitions you're using in the guest as this can lead to data corruption. If you're using e.g. Ubuntu which automounts certain partitions it is advisable that you add the Windows partition(s) to fstab with the read-only flag.

    Notes
    • For in-depth information about virtual disk images please see Tutorial: All about VDIs by TerryE.
    • Normally you won't want not use the -register parameter under Linux since it will register the vmdk with the root account instead of your own.
    • After you've created the vmdk you might have to change its permissions, owner and group so VirtualBox can access it.


    1. Check which partition you're currently using; I'll use sda1 .
    2. We'll need a similar line to vkov_tisnky's tutorial but a bit different, otherwise Windows may need to be repaired everytime we switch between VM and native.

      We will get our current MBR (and thus our current partition info) copied into a file and from there into the VM; afterwards (at the end of the tutorial) we will repair it using the Windows CD to remove GRUB from the VM's MBR, thus getting the VM to boot directly into Windows.

      Warning: dd is a tool capable of creating disaster (i.e., deleting all your data) if used incorrectly; assuming sda1 for the Windows partition:

      Code: Select all

      sudo dd if=/dev/sda of=myMBR.mbr bs=512 count=1

      Code: Select all

      sudo VBoxManage internalcommands createrawvmdk -filename raw7.vmdk -rawdisk /dev/sda -partitions 1 -relative -mbr myMBR.mbr
    3. Set access to raw partitions from host

      There are three solutions in [1] but I'll choose the safest one:

      Use a separate user, just for raw disk access - (suggested by TerryE)
      To set up such a user and give it the right privileges and permissions you can do the following:

      Code: Select all

      #!/bin/sh
      VBUSER=virtualbox
      sudo adduser $VBUSER --gid $USER --no-create-home
      #sudo adduser $VBUSER
      #sudo usermod -a -G $USER $VBUSER
      sudo usermod -a -G disk $VBUSER
      sudo usermod -a -G vboxusers $VBUSER
      To then start VirtualBox as this user use the following script:

      Windows 7 VM boot script -- you'll have to always use it instead of running VirtualBox

      Code: Select all

      #! /bin/bash
      VBUSER=virtualbox            # name of custom account created (which is a member of disk group)
      VM_NAME=raw7                # name of virtual machine
      
      
      sudo chown -R $USER:$USER /home/$USER/.VirtualBox    # make sure $VBUSER will be able to access VirtualBox settings etc.
                                  # we use sudo because $VBUSER creates files with its ownership on previous runs
      chmod -R g=u /home/$USER/.VirtualBox            # $VBOXUSER permissions should be the same as ours
      
      xauth extract /home/$USER/cookieTmp $DISPLAY
      
      chmod g+r /home/$USER/cookieTmp
      sudo chmod g+x /home/$USER/
      
      sudo -u $VBUSER XAUTHORITY=/tmp/.Xauthority_$VBUSER xauth merge /home/$USER/cookieTmp
      rm /home/$USER/cookieTmp
      
      sudo -u $VBUSER XAUTHORITY=/tmp/.Xauthority_$VBUSER VBOX_USER_HOME=/home/$USER/.VirtualBox VBoxManage startvm $VM_NAME
      By default you will be prompted for your password when you run this script since you're trying to run xauth and VBoxManage as a different user. You can get around this by adding a rule to (the end of) /etc/sudoers.
      Personally, I didn't edit sudoers. I prefer being asked for password before accessing the raw disk. If you want to, follow [1], otherwise, you're done with this section.
  4. Add the newly created vmdk to VirtualBox using the Virtual Disk Manager
    Add the newly created vmdk to VirtualBox using the Virtual Disk Manager

    You, the user, can't access the raw partitions, and therefore you can't add the vmdk to VirtualBox since VirtualBox's permissions are the same as yours. So you'll need to do

    Code: Select all

    sudo chmod 666 /dev/sda*
    before adding the vmdk to VirtualBox. Be aware those permissions are resetted to their safe defaults now and then, so you may need to repeat the command immediately before adding the vmdk.
  5. Quick n' Dirty Final Steps
    1. Now, add the disk to the VM. Permissions error will probably ensue, do again:

      Code: Select all

      sudo chmod 666 /dev/sda*
    2. Boot the VM using the script above (the one which said "Windows 7 VM boot script -- you'll have to always use it instead of running VirtualBox"). You can't run the VM from inside a 'regular' VirtualBox window, since running it that way runs with your permissions instead of that new user you created which is the only one who can access the raw disk. Also note that Windows won't load. See next step.
    3. I don't remember what happened here but probably GRUB failed horribly due to not finding it's partition. It's all right, go to next step, you'll need Windows 7 install (not recovery) DVD.
    4. Follow [3] on how to restore Windows MBR.
    5. Reboot and see Windows booting!
  6. VirtualBox Additions
    Please read the whole section before proceeding.

    Install them normally while in the VM and reboot; check they're installed. Afterwards, reboot natively. Be prepared for the following: mouse pointer stuck in the lower left corner and inability to use the keyboard.

    If that happens, turn off the computer (probably you'll need to press the power button 5 seconds) and return to Windows while running in the VM and uninstall the VBox Guest Additions. If not, then it's cool.

    It happened with me at first and then stopped, I don't know why. Maybe because I uninstalled Synaptics Touchpad Driver, but I may be wrong.

    NOTE: Hardware profiles don't work with Windows 7, not even as they worked in Windows Vista, since the first hardware profile is always the one used.
The End

Thanks a lot to vkov_tinsky for his excellent and complete tutorial for Windows XP (and Vista) and Rajat Arya for showing me it was possible to do it as well with Windows 7.

And thanks to everyone of you who will try to get the VBoxAdditions working and telling me afterwards how to do it ;)

This tutorial is published here along the lines of the WTFPL which allows you to do what you want ;) (warning, license terms may be considered offensive).
No warranty is given that this tutorial doesn't blow up your computer :mrgreen:

Changelog
  • 6. Jun 2010, 13:33 -- first version; VBox Additions recommendation is a screwup, since the recommendation, in case the user decides to install, practically causes everything to fail, minus VBoxSF (probably).
  • 6. Jun 2010, 14:09 -- VBox Additions recommendation probably causes everything to work minus mouse integration.
  • 6. Jun 2010, 14:29 -- minor fixes, clarified document.
  • 30. Jun 2010, 00:27 -- changed recommendation to install VBox Guest Additions with theory about what may go wrong.
  • 12. Jul 2010, 22:28 -- rephrased VM and Native definition.
  • 28. Set 2010, 10:06 -- filled missing parts prior to submitting to LXF Answers.
  • 30. Set 2010, 10:24 -- better scripts, removed redundant text.
Next
  • ???
  • Profit!
Last edited by pedrogfrancisco on 12. Jul 2010, 23:28, edited 1 time in total.
Post Reply