Saturday, October 1, 2016

Gentoo VirtualBox

Gentoo VirtualBox



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 (seeLicensing), 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.

[edit]OpenSource distribution (OSE)

The VirtualBox ebuild provides the following USE flags:
  • 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.
  • alsa - enable ALSA support
  • headless - Build without any graphic frontend
  • python - Adds support/bindings for the Python language
  • qt4 - Build qt4 frontend
  • pulseaudio - Adds support for PulseAudio sound server
  • sdk - Enable building of SDK
Note: Gnome users should be aware that the qt4 USE flag is required to build VirtualBox with the frontend we all know and love.
After you have enabled/disabled required USE flags, install the package:
emerge -av app-emulation/virtualbox

[edit]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 athttp://www.virtualbox.org/wiki/Editions

[edit]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.
File: /etc/portage/package.keywords
app-emulation/virtualbox-modules
app-emulation/virtualbox-bin
You may need to authorise portage to accept the restrictive non-GPL licence:
File: /etc/make.conf
ACCEPT_LICENSE="PUEL"

[edit]Dont blindly authorise all PUEL packages

Since the above will automatically authorise ALL packages masked as PUEL and hence cause portage to install proprietory packages without your being aware of the license ( or even the fact that it is non GPL !) , it may be more sensible to just authorise any required licenses on a one by one basis as follows:
File: /etc/portage/package.license
echo "app-emulation/virtualbox-additions" PUEL >> /etc/portage/package.license

[edit]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
You may also need to change the group ownership of /dev/vboxdrv like so:
chgrp vboxusers /dev/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: Keyworded versions of virtualbox-modules include a new module vboxnetflt that must also be loaded. Some may include vboxnetadp. As of VirtualBox 2.2, there is another type of networking: host-only networking, for which you need to load both modules.
And to finally start VirtualBox, you can either click the menu entry (Sun xVM VirtualBox) or via the terminal using:
VirtualBox

[edit]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" -s /bin/sh ; then
eerror "Could not locate VBoxHeadless"
r=1
fi

if ! su $VM_USER -c "PATH=$VBOXPATH command -v VBoxManage &>/dev/null" -s /bin/sh ; 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

if ! isloaded vboxnetadp ; then
if ! modprobe vboxnetadp > /dev/null 2>&1 ; then
eerror "modprobe vboxnetadp 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 vboxnetadp ; then
if ! rmmod vboxnetadp > /dev/null 2>&1 ; then
eerror "rmmod vboxnetadp 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" -s /bin/sh &
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" -s /bin/sh &
c=0
while [ "$(su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage showvminfo "$VM_NAME" | grep State | grep runn|saving 2>/dev/null")" != "" ]
do
echo -n "."
sleep 1
let c=c+1

if [ "$c" = "300" ]; then
echo -n " Trying again $VM_SHUTDOWN..."
su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage controlvm "$VM_NAME" $VM_SHUTDOWN &>/dev/null" -s /bin/sh &
fi

if [ "$c" = "360" ]; then
echo ""
echo -n "$VM_SHUTDOWN not working, trying poweroff."
su ${VM_USER} -c "PATH=$VBOXPATH VBoxManage controlvm "$VM_NAME" poweroff &>/dev/null" -s /bin/sh &
fi

done

sleep 1
echo

eend $?
fi
}
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.
Note: You should consider to start VBoxHeadless with su - $VM_USER "VBoxManage startvm "$VM_NAME" --type headless. I think thats a cleaner solution, but then you are not able to give the starting process a nice level directly.
To add a new virtual machine to startup you wi


Available link for download