Recompiling/Rebuilding Oracle VM VirtualBox Kernel Module

After installing Oracle VM VirtualBox in Ubuntu or CentOS you may face very first problem while
starting VirtualBox or even if started it may not function properly because It requires recompilation
of Kernel module.

==================================================================================
To recompile Kernel Module in Ubuntu or CentOS just got to terminal and type
==================================================================================

[hikmat@dhcppc4 hdp2-ansible]$ sudo /etc/init.d/vboxdrv setup -v

Stopping VirtualBox kernel modules [ OK ]
Uninstalling old VirtualBox DKMS kernel modules [ OK ]
Trying to register the VirtualBox kernel modules using DKMS
Error! Your kernel headers for kernel 2.6.32-358.el6.x86_64 cannot be found at
/lib/modules/2.6.32-358.el6.x86_64/build or /lib/modules/2.6.32-358.el6.x86_64/source.
[FAILED]
(Failed, trying without DKMS)
Recompiling VirtualBox kernel modules [FAILED]
(Look at /var/log/vbox-install.log to find out what went wrong)

==================================================================================
To resolve this issue edit following file and add highlighted lines
==================================================================================

[hikmat@dhcppc4 hdp2-ansible]$ sudo vim /etc/init.d/vboxdrv
#
# chkconfig: 35 20 80
# description: VirtualBox Linux kernel module
#
### BEGIN INIT INFO
# Provides: vboxdrv
# Required-Start: $syslog
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: VirtualBox Linux kernel module
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin:$PATH
DEVICE=/dev/vboxdrv
LOG="/var/log/vbox-install.log"
NOLSB=yes
DEBIAN=
VERSION=4.2.16
MODPROBE=/sbin/modprobe
KERN_DIR=/usr/src/kernels/2.6.32-358.23.2.el6.x86_64

if $MODPROBE -c | grep -q '^allow_unsupported_modules *0'; then
MODPROBE="$MODPROBE --allow-unsupported-modules"
fi

[ -f /lib/lsb/init-functions ] || NOLSB=yes
[ -f /etc/vbox/vbox.cfg ] && . /etc/vbox/vbox.cfg
export BUILD_TYPE
export USERNAME
export USER=$USERNAME
export KERN_DIR
"/etc/init.d/vboxdrv" 429L, 12698C


===============================================================================
Compile Kernel Module again
===============================================================================

[hikmat@dhcppc4 hdp2-ansible]$ sudo /etc/init.d/vboxdrv setup -v

Stopping VirtualBox kernel modules [ OK ]
Uninstalling old VirtualBox DKMS kernel modules [ OK ]
Trying to register the VirtualBox kernel modules using DKMS
Error! Your kernel headers for kernel 2.6.32-358.el6.x86_64 cannot be found at
/lib/modules/2.6.32-358.el6.x86_64/build or /lib/modules/2.6.32-358.el6.x86_64/source.
[FAILED]
(Failed, trying without DKMS)
Recompiling VirtualBox kernel modules [ OK ]
Starting VirtualBox kernel modules [ OK ]
[hikmat@dhcppc4 hdp2-ansible]$ virtualbox

===============================================================================
Then start virtual machine again and enjoy :) :)
==============================================================================
$ sudo virtualbox

it will start without errors


25 comments