How To: Fix (Workaround, really) VirtualBox High CPU Load with Ubuntu 9.10 Host

Updated to Karmic the other day, and all is well and good with the world, but VirtualBox is determined to use a heap of CPU time even when the guest OS is idling. This sucks, and from what I’ve read is related to CPU affinity.

I tried changing the affinity from 3 down to 1 with sudo taskset -p 1 31540 from the terminal, where 31540 was the PID (Process ID) of VirtualBox at the time – and did it work? Um, no…

After some further reading I found a quirky, but totally working fix workaround:
– Create a new virtual machine, call it “Dummy” or something
– Assign it as little resources as you can, like 4MB RAM, use a 10MB HD image etc
– Start this dummy virtual machine, skipping any virtual hard drive formatting
– That’s it! Just leave the dummy machine running at the screen saying “FATAL: No bootable medium found! System halted”

With the dummy machine running, VirtualBox sorts out its own processor affinity properly, and the CPU usage of your actual VM guest drops waaaaaaay down and becomes snappy & responsive again.

The joyz…

Update: This problem’s been recurring for me (as you can see in the comments below), but I think I’ve cracked it: With an XP guest running on a single core of a Core 2 Duo, if I run the following command in bash I can get the CPU usage down to negligible levels:

sudo su
echo 2 > /proc/sys/kernel/perf_counter_paranoid

If this works for you and you want it to be applied at every boot, just edit the file /etc/sysctl.conf and add the line kernel.perf_counter_paranoid = 2 at the bottom.

Additional Information: Although perf_counter_paranoid 2 seems to work reasonably well – we can still do a bit better. With an XP guest, once you’ve set XP to use multiple processors, it’s going to keep trying to use multiple processors, even if you’ve told VB to only allow it access to a single CPU. To fix this you can grab yourself a copy of of HALu and change your XP CPU setup from ACPI Multiprocessor PC to ACPI Uniprocessor PC. With that in place, CPU usage for your linux host drops down a little further, and doesn’t ramp back up too far under minimal guest load. Or you could just use the dummy VM… FFFFFFUUUUUUUUUUUUUUUUUUU…. =P

How To: Make VirtualBox Use Your Router’s DHCP to get an IP Address in Linux

Update: The method below for getting a virtualbox IP from your DHCP works (in linux) – but it turns out there’s an easier way (kindly pointed out by Mike in the comments below). You can just change your VirtualBox network settings from NAT to Bridged Adapter and point it at eth0/wlan0 or whichever connection is being used for networking. Then, optionally, you can configure the MAC address of the bridged adapter and set your router to assign a specific IP to a specific bridged adapter. Also, the built-in Bridged Adapter method works to deploy solutions from XNA Game Studio to my Xbox 360, so I’m rapt! Thanks, Mike!

VirtualBox Bridged Adapter Settings

Note: The below bit is for linux only, the above method should work on any host OS!

VirtualBox is an awesome bit of kit and I <3 it long time ten-dorrah. But by default when your virtual copy of Windows/Linux/Solaris/Whatever grabs an IP address, it does so through NAT, and at version 3.0.4, this means it gives us a default Category A network address (i.e. 10.x.y.z).

It’s a working cat-A address, as in it’s fully functional and can talk to the Internet and all that, but sometimes life is a lot easier if you have an IP in the same range as the DHCP pool your router is dishing out. For example, my lappy is internally, my Wii might be, the NAS .103 etc, so I want my virtualboxen to take addresses like .104, .105 and such.

I’m doing this to bridge my wireless connection on wlan0, if you’re bridging an ethernet connection substitute eth0 or whatever connection as necessary.

Also, to perform the bridging using this method, you’ll need some tools (feel free to sudo apt-get install NAME-OF-TOOL as necessary):
– 1.) uml-utilities
– 2.) parprouted
– 3.) bcrelay

Now, with that lot installed, run the following commands (provided here in bash script form):

# To use this script you will need the following utilities installed:
# 1.) uml-utilities 2.) parprouted  3.) bcrelay
# sudo apt-get install 'em
# Enable IP forwarding
sudo sysctl net.ipv4.ip_forward=1
# Create a TAP as your current logged in user (replace with -u YOUR-USER-NAME if you want...)
sudo tunctl -u $USER
# Tell tap0 to respond to ARP (Address Resolution Protocal) packets 
sudo sysctl net.ipv4.conf.tap0.proxy_arp=1
# Give your TAP (tap0) an IP address and enable it (make sure the IP address is OUTSIDE the DHCP range on your router)
sudo ip addr add dev tap0
sudo ip link set tap0 up
# Run parprouted and make it add routes automatically for wlan0 and tap0 as needed
# Make the command "parprouted -d wlan0 tap0" to display routes added and additional info. No sudo necc. for this one.
parprouted wlan0 tap0
# Use bcrelay on your net connection and TAP to "rewrite the layer-2 header and forward broadcast messages between network interfaces"
# The "-d" in this instance MAKES bcrelay work as a daemon. Need to sudo this or it doesn't work.
sudo bcrelay -d -i tap0 -o wlan0

Now, fire up VirtualBox and for your machine of choice change the network selection from NAT to tap0 as shown:


Then boot up your virtual machine and check the IP:


Great. Super. Smashing. =D

Note: The entire reason I wanted to grab an IP from the router was so my virtual copy of XP could be on the same network as my XBox 360, so I could deploy games to it through XNA Studio 3.1, however XNA Studio is very fussy about timing when it comes to registering the 360, and although it can see the 360 using the bridge, and it tries to connect, it times out before it can fully establish a connection. I guess I’ll have to go with an IP routes method of bridging if I want it to work for that purpose, but as yet I haven’t quite figured it all out. Will keep trying when I have time, or if you know a way, feel free to call me technically incompetent and sling a solution in the comments! Cheers!

How To: Get Sound Working in Windows 7 64-Bit running in VirtualBox

I’m running Ubuntu 9.04 64-bit, and messing around with Windows 7 RC 64-bit as a guest OS in virtual box – but, alas, no sound in Windows…

The fix? Set the virtual machine to use the ICH AC97 virtual soundcard then google for and install it inside the virtual box running Windows 7. Annoying start-up sounds are now yours to disable treasure.

Next up will be hardware accelerated graphics – works fine in a 32-bit XP guest OS, no joy in 64-bit Win7 so far…