Virtualbox

De Admin -- TALEVAS.

Sommaire

Virtual Box Virtualbox

http://en.gentoo-wiki.com/wiki/VirtualBox


Virtualization TOC


VirtualBox is a computer emulator which allows you to create virtual computers inside your OS.

Installation

VirtualBox can be installed either from the closed source binaries or directly from the sources. The closed source version contains some features that the open source edition lacks (see Licensing), but its use is restricted to personal use and evaluation. The open source edition is published under the GPL. For more details on the features and license terms of each edition, have a look at: http://www.virtualbox.org/wiki/Editions.

OpenSource distribution (OSE)

The VirtualBox ebuild provides the following USE flags:

  • {{#ifeq: {{{extended}}}|yes|Use Flags: additions (?)|additions }} - Install Guest System Tools ISO. These speed up video, allow you to have a seamless mouse between the host and the window that the guest OS resides in, and shared folders access between the host and the guest OS.
Note: Package [{{#ifeq: yes|yes|http://gentoo-portage.com/%7Chttp://gentoo-portage.com/Search?search=}}app-emulation/virtualbox-ose-additions app-emulation/virtualbox-ose-additions] has fetch restrictions turned on, so you will need to manualy download it and put into distfiles dir
  • {{#ifeq: {{{extended}}}|yes|Use Flags: alsa (?)|alsa }} - enable ALSA support
  • {{#ifeq: {{{extended}}}|yes|Use Flags: headless (?)|headless }} - Build without any graphic frontend
  • {{#ifeq: {{{extended}}}|yes|Use Flags: python (?)|python }} - Adds support/bindings for the Python language
  • {{#ifeq: {{{extended}}}|yes|Use Flags: qt4 (?)|qt4 }} - Build qt4 frontend
  • {{#ifeq: {{{extended}}}|yes|Use Flags: pulseaudio (?)|pulseaudio }} - Adds support for PulseAudio sound server
  • {{#ifeq: {{{extended}}}|yes|Use Flags: sdk (?)|sdk }} - Enable building of SDK

After you have enabled/disabled required USE flags, install the package:

emerge -av app-emulation/virtualbox-ose
Note: You can only build VirtualBox with <sys-devel/gcc-4.3 installed and enabled via gcc-config

Binary distribution

The binary distribution provides the following additions to the OSE version:

  • Direct access to USB devices from guest operating system
  • Remote Desktop access to the virtual machine
  • Direct access / raw access to a hard disk (partition) (NOTE: You may also use the binary edition to create VMDK files that point to hard disks and partitions, and subsequently these can be used with the OSE version)

Usage of binary distribution is limited to personal or evaluation usage. A more complete list of differences between the binary edition & OSE is available at http://www.virtualbox.org/wiki/Editions

If you have gcc 3.3 installed and enabled, you can install VirtualBox just by emerging the main package with its dependencies:

emerge -av app-emulation/virtualbox-bin

If using gcc later than 4.x, you must install all the required packages individually, without dependencies. VirtualBox requires libstdc++ which requires that you emerge this manually if running gcc 4.x or later.

Note: If you don't install without dependencies, virtualbox-bin will attempt to emerge gcc 3.3 and then the version of libstdc++ which must be built from within a gcc-3.3.x environment. Compiling gcc-3.3 with gcc-4.3 may also fail. Using sys-libs/libstdc++ is quicker and can be compiled by gcc-4.x
emerge --oneshot sys-libs/libstdc++-v3
emerge --oneshot media-libs/libmng
emerge --nodeps app-emulation/virtualbox-modules
emerge --nodeps app-emulation/virtualbox-bin

Installing masked versions of VirtualBox

If you wish to install a version of VirtualBox newer than that listed as stable in the Portage tree, you must unmask it by adding the following lines to your package.keywords file. Don't forget to replace ~x86 with ~amd64 if using 64 bit.

File: /etc/portage/package.keywords
app-emulation/virtualbox-modules ~x86
app-emulation/virtualbox-bin ~x86
Update and Tip

As of today, last stable version 1.6.6 uses qt3 while latest keyworded versions (2.2.4) uses qt4. It also compiles directly with gcc-4.x (no need to emerge individually).

Run

Users that run VirtualBox must be a member of the "vboxusers" group. The user you added will not be able to access VirtualBox until they relogin.

gpasswd -a youruser vboxusers


Also the vboxdrv module should be loaded prior to starting VirtualBox.

modprobe vboxdrv

To make the module load at boot, run following commands:

For baselayout-2:

echo 'modules="${modules} vboxdrv"' >> /etc/conf.d/modules

For baselayout-1:

echo vboxdrv >> /etc/modules.autoload.d/kernel-2.6
Note: Refer to the appropriate article to load modules on boot.
Note: Keyworded versions of virtualbox-modules include a new module vboxnetflt that must also be loaded. Some may include vboxnetadp.

And to finally start VirtualBox, you can either click the menu entry (Sun xVM VirtualBox) or via the terminal using:

VirtualBox

Service

To run VirtualBox in headless mode as a service you will need to add the following scripts to your system.

File: /etc/conf.d/virtualbox.example
# Username to start vbox as, must be part of vboxusers group.
VM_USER="nobody"

# Virtual Machine Name
VM_NAME="Windows Server 2003"

# Shutdown Method: pause|resume|reset|poweroff|savestate|acpipowerbutton|acpisleepbutton
VM_SHUTDOWN="savestate"

# Nice Priority: -20 (most favorable scheduling) to 19 (least favorable)
VM_NICE=0
File: /etc/init.d/virtualbox
#!/sbin/runscript

# Not sure why but gentoo forgot to add /opt/bin to the path.
VBOXPATH="/usr/bin:/opt/bin"
VBOXNAME="${SVCNAME#*.}"

depend() {
	need net
	
	if [ "${SVCNAME}" != "virtualbox" ] ; then 
		need virtualbox
	fi
}


checkconfig() {
	if [ ! -r /etc/conf.d/$SVCNAME ] ; then
		eerror "Please create /etc/conf.d/$SVCNAME"
		eerror "Sample conf: /etc/conf.d/virtualbox.example"
		return 1
	fi

	return 0
}

checkpath() {
	local r=0

	if ! su $VM_USER -c "PATH=$VBOXPATH command -v VBoxHeadless &>/dev/null" ; then
		eerror "Could not locate VBoxHeadless"
		r=1
	fi

	if ! su $VM_USER -c "PATH=$VBOXPATH command -v VBoxManage &>/dev/null" ; then
		eerror "Could not locate VBoxManage"
		r=1
	fi

	if [ $r -gt 0 ] ; then 
		eerror "Please verify the vm users path."
	fi

	return $r
}

isloaded() {
	lsmod | grep -q "$1[^_-]"
}

isvm() {
	[ $SVCNAME != "virtualbox" ]
}

loadmodules() {
	if ! isloaded vboxdrv ; then
		if ! modprobe vboxdrv > /dev/null 2>&1 ; then
			eerror "modprobe vboxdrv failed."
			return 1
		fi
	fi 

	if ! isloaded vboxnetflt ; then
		if ! modprobe vboxnetflt > /dev/null 2>&1 ; then
			eerror "modprobe vboxnetflt failed."
			return 1
		fi
	fi
	
	return 0
}

unloadmodules() {
	if isloaded vboxnetflt ; then
		if ! rmmod vboxnetflt > /dev/null 2>&1 ; then
			eerror "rmmod vboxnetflt failed."
			return 1
		fi
	fi

	if isloaded vboxdrv ; then
		if ! rmmod vboxdrv > /dev/null 2>&1 ; then
			eerror "rmmod vboxdrv failed."
			return 1
		fi
	fi 

	return 0
}

start() {
	# If we are the original virtualbox script [ $SVCNAME = "virtualbox" ]
	if ! isvm ; then
		ebegin "Starting Virtualbox"
		loadmodules
		eend $?
	else
		checkconfig || return $?
		checkpath   || return $?

		ebegin "Starting Virtualbox: $VBOXNAME"
		su $VM_USER -c "PATH=$VBOXPATH nice -n $VM_NICE VBoxHeadless -startvm \"$VM_NAME\" &>/dev/null" &
		pid=$!
		sleep 1
		
		kill -CHLD $pid &>/dev/null
		eend $?
	fi
}

stop() {
	# If we are the original virtualbox script [ $SVCNAME = "virtualbox" ]
	if ! isvm ; then
		ebegin "Stopping Virtualbox"
		unloadmodules
		eend $?
	else
		checkconfig || return $?
		checkpath   || return $?

		ebegin "Stopping Virtualbox: $VBOXNAME"
		su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage controlvm \"$VM_NAME\" $VM_SHUTDOWN &>/dev/null"

                while [ "$(su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage showvminfo \"$VM_NAME\" | grep State | grep runn")" != "" ]
                do
                    echo -n "."
                    sleep 1
                done

		sleep 1
                echo

		eend $?
	fi
}

To add a new virtual machine to startup you will need to create a conf.d file and symbolic link for the virtual machine.

Note: You DO NOT need vboxdrv (and if needed vboxnetflt) in your /etc/modules.autoload.d/kernel-2.6 file when using this script. The modules will be automatically loaded as needed by the base script. This will make upgrading virtualbox in production simpler as it seems to freak out when the modules are updated while in use. If there is an issue with this behavior please contact me in the discussion section so a resolution can be reached.
# cp /etc/conf.d/virtualbox.example /etc/conf.d/virtualbox.NEW_VM
# cd /etc/init.d
# ln -s virtualbox virtualbox.NEW_VM

Now modify the newly created /etc/conf.d/virutalbox.NEW_VM file. Be sure the VM_USER is in the vboxusers group and that VM_NAME is the correct case.

Networking

VirtualBox supports networking via NAT and via bridges. NAT networking is very simple and works out of box.

NAT

Works perfectly when you set NAT and keep "Cable connected" checked on. VirtualBox then uses internal DHCP and NAT. Disadvantages are that ICMP doesn't work (no ping - #1247) and you can't connect to virtual machine unless you set up port-forwarding rules.

However your virtual machine will be able to connect to other machines on your LAN without any issues and LAN name resolution should work providing you have that functionality on your LAN.

Bridged

If you need your guest OS to have a different IP address in the same subnet on the same Ethernet interface, then you should use bridging.


Note: The following steps are not necessary for setting up Host Interface on VirtualBox >= 2.1.

VirtualBox Version >= 2.1

You no longer need the TUN/TAP and bridging support, as VirtualBox has built the bridging functionality directly into the software. However, for those of you have it already configured and recently upgraded from VirtualBox 2.0 to 2.1+, you may find that you you get an error when you try to start your VM. The error, ERR_HOSTIF_INIT_FAILED, will prevent you from stating the VM.

In order to fix it, you will need to modprobe the new vboxnetflt kernel module. You can find it in: /lib/modules/<kernel>/misc/vboxnetflt.ko. Simply change into that directory and run modprobe on the module, then restart your vm.


VirtualBox Version < 2.1

Do only this:

modprobe vboxnetflt echo vboxnetflt >> /etc/modules.autoload.d/kernel-2.6
Preparing

You should enable bridging support and tun/tap in kernel (or compile them as modules).

Linux Kernel Configuration: 802.1d Support
Networking --->
    Networking Options --->
    <*> 802.1d Ethernet Bridging
Linux Kernel Configuration: TUN/TAP Support
Device Drivers  --->
    Network device support  --->
    <*> Universal TUN/TAP device driver support

You can read about bridges here: Bridging

Configure network
File: /etc/conf.d/net (baselayout-2)
config_eth0="null"
# For every VM create an interface,
# set user to the one who going to use the
# interface.
tuntap_vbox0="tap"
tunctl_vbox0="-u <user>"
config_vbox0="null"
tuntap_vbox1="tap"
tunctl_vbox1="-u <user>"
config_vbox1="null"

# Add here all interfaces that you want to bridge
# eg eth0, but make sure to add config_eth0="null"
rc_need_br0="net.vbox0"
# For my baselayout 1.12.11.1 it is:
# RC_NEED_br0="..."
bridge_br0="vbox0 vbox1 eth0"

# The following will be the host IP, it can be the default router
# for the VM in routing mode.
# You can use dhcp here if you like, it makes sense if you want
# to bridge your VM into the real network.
#config_br0="dhcp"
config_br0="192.168.1.2/24"
routes_br0="default via 192.168.1.1"

brctl_br0="setfd 0
sethello 0
stp off"
File: /etc/conf.d/net (baselayout-1)
#the interface is really replaced by br0 for outgoing, so if you had dhcp here, you'll use dhcp in br0
config_eth0=( "null" )

#this specifies that interface vbox0 will be a tap interface and use the tunctl command to generate itself
tuntap_vbox0="tap"

#ALL interfaces part of a bridge should be null, otherwise oddities may occur.
config_vbox0=( "null")

# you can specify an owner of the interface if you want to run virtual box as a non root user
# [COMMENT from a reader: VirtualBox should never be run as root]
tunctl_vbox0=("-u <user>")

tuntap_vbox1="tap"
tunctl_vbox1=( "-u <user>" )
config_vbox1=( "null" )

#If you'd prefer a dynamic ip address for the machine, use
#config_br0=( "dhcp" )
config_br0=( "192.168.1.2 broadcast 192.168.1.255 netmask 255.255.255.0" )
routes_br0=( "default via 192.168.1.1" ) 
#this specifies the bridging information
bridge_br0="eth0 vbox0 vbox1"
#Make sure your baselayout is recent enough to support this
depend_br0() 
{
         need net.eth0
         need net.vbox0
         need net.vbox1
}

brctl_br0=( "setfd 0" 
"sethello 0"
"stp off" )

Now create interface symlinks:

ln -s net.lo /etc/init.d/net.vbox0

ln -s net.lo /etc/init.d/net.vbox1 ln -s net.lo /etc/init.d/net.br0 rc-update add net.br0 default

Configure virtual machine

Run virtualbox and click "Settings" for your virtual machine

  • Click Network
  • Set "Attached to" to "Host interface"
  • Set "Interface name" to "vbox0" (Don't forget this or you'll get a VERR_HOSTIF_INIT_FAILED)
Configure udev

In older versions of udev, the default permissions of /dev/net/tun do not allow all users to access the device, this is due to pre-capabilities period. Nowdays it is perfectly safe to allow all users to access the device, while only root is able to manipulate the device.

If you get other output of the following command you need to install an extra rule.

# ls -l /dev/net/tun crw-rw-rw- 1 root root 10, 200 2008-07-23 16:45 /dev/net/tun
File: /etc/udev/rules.d/60-tun.rules
KERNEL == "tun", MODE="0666"

Refresh rules

udevcontrol --reload_rules udevadm trigger

Troubleshooting

Fix me: Troubleshooting section needs cleanup

This section contains some troubleshooting guides provided by other users.

Framebuffer in Guest OS: Missing Half the Screen

If you happen to want framebuffer support for a guest OS using either vesafb or the newer uvesafb you may notice that you can't see half of your screen. I am not entirely sure why this is a problem for VirtualBox, but you can use uvesafb or vesafb without any problems. Simply remove consolefont from the boot runlevel as follows:

rc-update del consolefont boot

This seems to fix the problem and doesn't cause any other side effects.


Another user: This did not help on my machine, but setting mtrr:4 and redraw instead of ywrap in grub.conf helped. So here's the complete video option string I'm using:

video=uvesafb:800x600-16,mtrr:4,redraw

I'm not sure about mtrr:4, but at least the kernel doesn't complain about invalid mtrr settings anymore. Using "redraw" instead of "ywrap" is slower according to the (u)vesavfb docs, but in VirtualBox it's flying.

Error: interface vbox0 does not exist!

You need to ensure you have the tun / tap module compiled into your kernel and modprobed. Also, ensure you have sys-apps/usermode-utilities which now provides the tunctl app.

Virtual Windows XP Installation Crashes During Format of the Partition With 0% Progress

You might have a crash during formatting of the partition while installing your virtual Windows XP. If you get the error above, please check that the assigned amount of memory for the virtual machine is less than the real memory of your PC. The combination when you have 1GB of real memory and 2GB of memory assigned for your virtual machine will crash your Windows XP installation on the first access to the HDD, which is actually attempt to perform format during installation!

The problem also exists during the creation of Linux virtual machines. Behavior is the same: virtual machine crashes during first access to the HDD.

Guest Does Not Receive Packets In Bridge Mode

If the guest does not receive packets in bridge mode, check for bugs #150791 and #171356. The problem is because of the txqueuelen:0 setting in the assigned tap device.

If you followed this guide and still have some issues with bridged networking, then take a look at this forum thread

Stalling at "Spawning"

If your virtual machine never gets past 0% when you click "Start Machine", one way to fix it might be:

killall --verbose --signal KILL VBoxSVC

I thought I was being real smart when I put Virtual Machine Related support in my kernel. VirtualBox does not need anything in your kernel to run a machine. Once I took out that support, everything worked.

I had the same problem, however, I didn't have anything related to virtualization in my kernel. The last log messages were ALSA related, and switching the VirtualBox sound to OSS (although that's actually alsa's OSS emulation) got me to boot the machine :)

My virtual box used to work, but after accidental deletion of an image, VirtualBox refused to start a new machine and also stalled at spawning. After a simple "killall VBoxSVC" (which is a daemon process running in background), everything works well.

VirtualBox Constantly Uses CPU

When the guest is idle VirtualBox should consume very little CPU. This will vary by system but should generally be less than 10%. For example, on a quad core system the VirtualBox process consumes ~1% CPU with an idle WinXP guest. You can experience high VirtualBox CPU usage even when the guest XP session shows no activity via Task Manager. A possible remedy is to examine the WinXP applications and services. As stated in the VirtualBox built in help file, this behavior can be caused by anti-virus software and the recommendation given there is to disable anti-virus. Further examination of the services can be done to eliminate any unneeded processes. I found that a VNC server inside of the WinXP guest caused a constant load of 50% on one CPU core in a quad core system. Skype causes similar inactive cpu load issues. This issue can also arise from running the guest with an XP hardware profile copied from a native multicore ACPI system. Change the system type in the virtual profile to 'Standard PC' to reduce CPU overhead from BIOS ACPI calls.

USB Devices Grayed Out

If you can see your devices, but they are greyed out it is a permission problem:

VirtualBox Advice

The VirtualBox User FAQ specifies, "If USB doesn't work, check your usbfs permissions. See "Troubleshooting" -> "Linux hosts" in the User Manual for a solution."

The pertinent section of that section of the VirtualBox User Manual specifies:

If USB is not working on your Linux host, make sure that the current user has permission to access the USB filesystem (usbfs), which VirtualBox relies on to retrieve valid information about your host’s USB devices. As usbfs is a virtual filesystem, a chmod on /proc/bus/usb has no effect. The permissions for usbfs can therefore only be changed by editing the /etc/fstab file. For example, most Linux distributions have a user group called usb or similar, of which the current user must be a member. To give all users of that group access to usbfs, make sure the following line is present:

File: /etc/fstab
# 85 is the USB group
none /proc/bus/usb usbfs devgid=85,devmode=664 0 0

Replace 85 with the group ID that matches your system (search /etc/group for "usb" or similar).

getent group usb

How Gentoo does it specifically

Note that udev supports fstab settings. And the pertinent group to be added as the devgid is the plugdev group (ensure your user is a member of this group). Reference Chapter 11 of the Gentoo Handbook. So,

getent group plugdev

Use the number that is retrieved as the devgid=## in /etc/fstab line quoted above.

Log out and log back in.

See Also

Headline text

tr:VirtualBox