VirtualBox

Introduction

If you want an overview of what VirtualBox is and does, then read An Introduction to VirtualBox | Capgemini which gives a nice overview.

Installing

Start by visiting Downloads – Oracle VM VirtualBox and download the version for the Operating System that you are using, that is the Host OS, the one from which your VMs (or Guest OSes) will be launched.
Then just run the installer and off you go, nothing to it really. Personally I always go into Preferences and change the "Default Machine Folder" to something like D:\VirtualBox but that is just me, I like to be able to easily find my Virtual Machines and not have it all in my profile folder.

If you have a Linux guest OS then see Linux - VirtualBox for further information and if you are using openSUSE then definitely read openSUSE.

Hyper-V

I have recently tried to get Hyper-V working in a VM running Windows 10 or Windows Server 2016 and failed. It turns out that VirtualBox does not support this, it does not pass though the necessary CPU support into the VM. You can read about this at virtualbox.org • View topic - Hyper-V if you wish. Sadly the only solution I found was to use VMWare, see VMWare for details.

Networking

The best description of VirtualBox networking is to be found at Networking in VirtualBox (The Fat Bloke Sings) which is very comprehensive. There is also Using VirtualBox Host-only Networking to run servers in your lap (The Fat Bloke Sings) which covers the use of multiple NICs. However both of these articles were written prior to the new "NAT Network" functionality introduced in VirtualBox 4.3, this allows you to put VMs on a named NAT network and then set some basic setting on the "pseudo router", this is useful for emulating a home network for example.

If you are having networking issues then you can get VirtualBox to do packet logging, see Network_tips – Oracle VM VirtualBox for details.

There is an article on networking with VirtualBox 5.1, from May 2017 available at Oracle VM VirtualBox: Networking options and how-to manage them | Oracle Simon Coter Blog.

Browser Testing

One of the big problems we have with browser testing is the way in which Internet Explorer is very linked to the operating system. For example, Windows XP can run IE6, 7 or 8, Vista IE7, 8 or 9 and Windows 7, IE8, 9, 10 or 11. Looking at this another way, IE11 only runs on Windows 7 or later. So, testing different versions of Internet Explorer requires different operating systems and this is where Virtual Machine (VM), Windows Virtual PC & Browserstack | Modern.IE comes in. You can download different Windows/Internet Explorer combinations, for browser testing. Fortunately the world does move forward and IE6 is now largely gone and easier to ignore (with some notable exceptions!), however we seem to be stuck with IE8, for now.

Resizing VM Hard Drives

I have successfully done this with a Windows 7 based VM. However I believe you need to be careful regarding snapshots as I modified the main VDI but it was not until a removed the snapshot and rebooted that I could see the unallocated space and even they I had to refresh/rescan.

The steps to do this though, are as follows:

  • Shutdown the guest VM operating system
  • First check the HD Info with the following:
    "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" showmediuminfo "Windows 7.vdi"
  • Then resize the HD with this:
    "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifymedium "Windows 7.vdi" --resize 102400
  • Check the HD Info again:
    "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" showmediuminfo "Windows 7.vdi"
  • Start the VM
  • Load "Computer Management" and go to "Disk Management"
  • You should then be able to "Extend volume..."

See also: How to resize a virtualbox VDI disk under windows | derekmolloy.ie

Shrinking a Virtual Disk File

When creating Virtual Machines the default is to use a dynamically allocated disk. This means the file starts small and grows on demand, although the guest OS will only see its actual, maximum size. These virtual disk files do not automatically shrink, however a few simple steps can achieve significant file shrinkage, skipping steps just reduces the shrinking. The steps are as follows:

  1. Start the VM
  2. Shut down background stuff
  3. Uninstall anything not needed, delete files
  4. Run Disk Cleanup (from Drive Properties), press “Clean up system files” button if shown so you see “Windows Update Cleanup” which is often big
  5. Defragment the hard drive (from Drive Properties), takes 11 passes but exact number can vary
  6. sdelete -z c: (note that I have also used -c)
  7. Shutdown the VM
  8. Change to: C:\Program Files\Oracle\VirtualBox
  9. VBoxManage showmediuminfo "C:\Dev\VirtualHDs\Windows 7\Windows 7.vdi"
  10. VBoxManage modifymedium "C:\Dev\VirtualHDs\Windows 7\Windows 7.vdi" --compact
  11. VBoxManage showmediuminfo "C:\Dev\VirtualHDs\Windows 7\Windows 7.vdi"
The Disk Cleanup takes a few minutes but the defragment can take a while as can the actual modifymedium step.

I have seen this process reduce VDI file size by almost 8Gb, where the guest OS reported lots of free space but the actual file did not show that. However, it is worth noting that on 11-Jun-2012 the defrag process increased VDI file size to 4,405,000 first.

Operating System Date Version Original Size New Size
Windows XP 11-Jun-2012 4.1.16 3,625,004 2,680,876
Windows 8 2-Aug-2012 4.1.18 11,203,092,480 8,529,223,680
Windows 7 21-Dec-2014 4.3.18 25,458,483,200 25,380,888,576

I am not sure why the Windows 7 figures are so poor.

The last few steps of compacting can be done with a Windows Batch script, assuming your host OS in Windows. What follows is a script I have used:

@ECHO OFF
rem VBOX_DIR only needs changing if VirtualBox was not installed in the default location
SET VBOX_DIR="C:\Program Files\Oracle\VirtualBox"

rem VDI_FILE is what needs setting depending on where the VDI is
SET VDI_FILE="D:\VirtualBox\Windows 7\Windows 7.vdi"

ECHO.
ECHO About to process the following file:
ECHO %VDI_FILE%
ECHO.
PAUSE

PUSHD %VBOX_DIR%

ECHO.
ECHO Drive Info (Before)
ECHO ===================
VBoxManage.exe showmediuminfo %VDI_FILE%
DIR %VDI_FILE%

ECHO.
ECHO About to compact drive, this can take a long time
PAUSE
VBoxManage.exe modifymedium %VDI_FILE% --compact

ECHO.
ECHO Drive Info (After)
ECHO ==================
VBoxManage.exe showmediuminfo %VDI_FILE%
DIR %VDI_FILE%

POPD

Useful Information

There are lots of places to get useful information, however these have helped me: