Home » How To » How to convert a laptop running Microsoft Windows 7 to a VirtualBox virtual machine

How to convert a laptop running Microsoft Windows 7 to a VirtualBox virtual machine

I was very much surprised just how easy it is to convert a Microsoft Windows 7 laptop into a virtual machine running within Sun’s VirtualBox.  This is what I did:

  • Pull the hard drive from the laptop.  On my Dell Latitude, it was simply unscrewing the two screws on the bottom of the laptop marked with the hard drive simble
  • Attach the drive to a SATA to USB connector and plug it in to your Linux box
  • If you don’t have VirtualBox installed, do so now using apt-get, rpm, or whatever you use to install software
  • Follow How to migrate existing Windows installations to VirtualBox but make sure you specify the entire drive (i.e. /dev/sdh) and not individual partitions else you will miss the hidden partitions that Vista and Windows 7 create

You should have a working Windows 7 Virtual Machine.  You’ll notice that you probably have a huge vdi file.  Let’s shrink that down:

  • Within Windows
    • download and install sdelete from Microsoft
    • download and install PowerDefragmenter
    • Run PowerDefragmenter on your c: drive (as administrator)
    • Run “sdelete -c c:” from a cmd window (as administrator)
    • shutdown windows
  • On the host
    • run “VBoxManage modifyvdi my_disk.vdi compact” replacing “my_disk.vdi” with the name of your vdi file you created earlier
    • Note that if you get a “Shrink hard disk operation is not implemented” error, then you’ve hit bug #2833 and you will want to download the latest VirtualBox to fix it.
Share Button

Comments

  1. Oh yes, you will most likely have to reactivate your windows. It is a simple thing to do – just follow Microsoft’s instructions.

  2. If you’re going to be attaching USB devices to the virtual machine, then you need to add yourself (and any other user you wish) to the vboxusers group in /etc/group. GUI way to do the same: http://news.softpedia.com/news/How-to-Fix-VirtualBox-USB-Support-111715.shtml

  3. Shawn says:

    For Windows 7, did you have to run the MergeIDE utility or deal with the agp440.sys file? The instructions on the Virtual Box website were specifically for XP, so didn’t know if these still applied for Windows 7.

    1. Nope, when I performed the physical to virtual of win7, I didn’t have to do anything outside of what I posted. Running MergeIDE by itself as a ‘rule of thumb’ probably wouldn’t be a bad idea anyways. 🙂

      As far as MergeIDE & agp440.sys combo, I’ve only had to use that when moving from virtual to virtual – specifically when moving from VMware to VirtualBox.

  4. Benedikt says:

    Will the newly made vdi-Image be the same size as the regular hard disk? I have just bought a new laptop and I would LOVE to move the running Win7 to a virtual box. I simply don’t own a hard disk (external or bound to another PC), that is big enough. Allthough I might buy one.

    As I have to install (and uninstall) new software regularly, my experiences with windows are not the best.

    1. Paul says:

      [I reply one year after, but I had the exact same problem and found this post very useful, so I try and contribute to it]

      I had a 300G disk in the laptop and managed to move it on a 34GB vdi file, without extracting the disk.
      Here is what I did.
      1. within W7 on the laptop: remove all what is big and useless (in my case, I got rid of many GB of HP multimedia stuff I don’t use). I went from 40G to about 30G.
      2. remove useless HP restore partition (using HP restore tool, and after having spent hours burning the 5 restoration DVD.
      3. shrink c: partition. I used Windows to shring its main partition from 300G to 50G, and to make a partition with the free disk space.
      4. follow Jason’s recommendation: defrag and zero the disk garbage.
      I did the linux way, but sdelete should do fine: boot linux on a liveCD, mount all partitions, fill them with a file full of zero and delete it:
      mkdir /tmp/sdaN; mount /dev/sda1 /tmp/sdaN
      dd /dev/zero > /tmp/sdaN/fillwithzeros (this will take some time …)
      sync
      rm /tmp/sdaN/fillwithzeros; umount /dev/sdaN
      5. still on the linux liveCD: copy the full disk -compressed- to a usb drive.
      dd bs=512K conv=noerror if=/dev/sda |gzip -c –fast > /media//sda.img.gz
      Nota1: I ran into a dd “input/output” error several times at the same point, after 50GB. As I could not solve or understand the problem, I added the “noerror” flag and hope the “error” would be somewhere it does not cause any problem. until now it is what happened.
      Nota2: this takes a huge time. You can do “kill -USR1 ” to get some stats, stop and restart with different parameters to find an optimal bs size. bs=512K was good for me. gzip “-1” or “–fast” made the total a few GB bigger but was twice as faster than default -6
      6. Then I could easily move the 19.6GB gziped disk image and make the vdi from that :”gunzip -c sda.img.gz |VBoxManage convertfromraw stdin W7Laptop.vdi ” and get a 34GB vdi file.

      Hope this helps someone some day

      1. Griera says:

        Very useful! Thanks for sharing it!

  5. The VDI image will be just slightly larger, only a few megs, than the used space from your win7 disk drive. This is due to overhead of the VDI format.

  6. John says:

    But there’s no following files in Windows 7 ???

    expand>NUL “%ctdir%\driver.cab” -f:Atapi.sys “%SystemRoot%\System32\Drivers”
    if errorlevel 1 goto fehler1
    expand>NUL “%ctdir%\driver.cab” -f:Intelide.sys “%SystemRoot%\System32\Drivers”
    if errorlevel 1 goto fehler1
    expand>NUL “%ctdir%\driver.cab” -f:Pciide.sys “%SystemRoot%\System32\Drivers”
    if errorlevel 1 goto fehler1
    expand>NUL “%ctdir%\driver.cab” -f:Pciidex.sys “%SystemRoot%\System32\Drivers”

    1. Eric says:

      This will not work in windows 7 or vista as the MergeIDE bat file looks to pull the drivers from the I386 folder or from the XP install cd, Athough you can still import the registry file anyway as it should not hurt your machine, but when you go to launch the virtualbox Win 7 it will most likely hang unless you happen to have those drivers already installed for boot (which is what MergeIDE attempts to do for you). As for me it did not work and I had to go through a long and length process to make sure all the virtualbox drivers were in place before i converted it to vdi and remove all the other drivers that i did not need. Maybe mine was harder because it was 64 bit?

      1. Bryn says:

        Hi Eric

        I seem to be getting a similar problem to you. My converted VM BSOD’s just after the black screen with the animated Windows Logo. I can’t see what the error is because it flashes by too quickly but I’m guessing it’s related to missing virtualbox drivers as I was unable to use the MergeIDE utility like you.

        Could you tell me what drivers I need to install on my Windows7 machine before converting it please? I’ve had a look around on Google but can’t seem to find anything telling me what Vbox drivers I need to install on my physical laptop before virtualising.

        Thanks very much in advance.
        Bryn

        1. RT says:

          By default a VirtualBox W7 VM will use SATA for the hard-drive interface.

          If, as Bryn mentioned, your converted W7 VM does start up but then either BSOD or reboots then one suggested work-around is to change your VM settings to use IDE instead of SATA for the converted .VDI connection. This has worked for me numerous times.

          1. Andreas says:

            Thank you RT, that solved my migration too!! 😀

  7. Vasu says:

    what is the equivalent command for running “cat /dev/sdg | VBoxManage convertfromraw stdin OutPutFile.vdi NUMBEROFBYTES” in windows, because my virtualbox is running in windows. I will follow the steps like connecting the drive to USB but after that how to specify the whole drive?

  8. Phil says:

    I was in a similar situation to people here, but ended up achieving it in an easier way.
    My situation was: Original laptop HDD with Windows 7 64-bit, plus SSD with Ubuntu installed.
    I wanted to take the Windows install from the original HDD, and move it to a VM in my Ubuntu install/drive.
    The advantages of my method are:
    – it only requires you to have an external HDD/flash drive slightly larger than your desired VM drive size
    – It doesn’t require you to re-activate your Windows install

    1. Create Virtual Machine, creating VDI virtual drive to desired size
    2. Set VM parameters for DMI bios to match real machine, as in this thread (section 1.2 only): https://forums.virtualbox.org/viewtopic.php?t=9697
    I also set the VMs ethernet MAC address the same as the real machine’s one – not recommended if using “bridge” over NAT in the VM, but may require you to re-activate Windows.
    3. Boot Windows on real machine, remove any unnecessary programs/files, do disk cleanup.
    4. Disable hibernation (if active), system restore, and manually set pagefile to use main partition only (the one you’re going to copy)
    5. In Windows (using Computer Manage), resize main partition down to desired size (as in VDI) MINUS 100mb.
    6. Create Windows System repair – burn to CD or DVD
    7. Reboot, then run PowerDefragmenter from Windows on main partition.
    8. Boot linux live CD with partimage (eg. http://www.sysresccd.org/SystemRescueCd_Homepage) on real machine. Connect your external HDD/flash, and use partimage to copy the first two partitions to a file/s on your external HDD – One should be labelled SYSTEM and be 100mb. The 2nd will be the main partition that you just resized down.
    9. Now boot your OS that has VirtualBox on it (for me it was my Ubuntu), start Virtualbox and mount the LiveCD to the VM, so it boots into the LiveCD
    10. Connect your external drive to the computer, and mount it in the VM.
    ALT 10. For me my external HDD wouldn’t mount in the VM, so instead I copied the partimage files to a folder in Ubuntu, and then from the VM/LiveCD I mounted the folder via Samba
    11. Use Gparted to create two partitions in the VM drive – the first 100mb, and label it SYSTEM, with filesystem as NTFS. Check the “boot” flag for this partition. Create the 2nd partition with the remaining space on the VM drive, also NTFS.
    12. Now run partimage, select the first partition (eg. sda1) and enter the image file location, and go through the restore process. Repeat for the 2nd partition – if you have the image files split, just enter the full filename of the first file, and it will sort out the rest.
    13. Cool now you have successfully copied the Windows install from the real install to the VM drive!
    14. Shutdown the VM, and mount the Windows System repair disc that you created earlier and boot into this. When I did it, “Windows installation table” didn’t show me any installations – my heart sank thinking it wasn’t going to work. However I just clicked “repair errors” (can’t remember exact wording) and it proceeded to do some checking/fixing.
    15. Reboot without the system repair disc mounted, and voila, you should have a working Windows VM
    16. Final steps for me were: install VirtualBox guest additions, use “Computer Manage” in Windows to extend the main partition to fill a few extra free MBs that were left in the VM drive.

    Hope this helps someone!

    1. thanks Phil! I’m definitely going to use that method instead 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Facebook login by WP-FB-AutoConnect