#!/bin/bash # $Id$ # Configures virtualbox for Magellan-Linux guests # get INSTALL_DIR location source /etc/vbox/vbox.cfg VBOXVERSION="$(< ${INSTALL_DIR}/version)" VBOXMODEXT=ko VBOXGUESTMODULE=vboxguest VBOXSFMODULE=vboxsf VBOXVIDEOMODULE=vboxsf KERNELMODULES=( "${VBOXGUESTMODULE}" "${VBOXSFMODULE}" "${VBOXVIDEOMODULE}" ) COLRED="\033[1;6m\033[31m" COLGREEN="\033[1;6m\033[32m" COLDEFAULT="\033[0m" if [[ ${NOCOLORS} = true ]] then COLRED="" COLGREEN="" COLDEFAULT="" fi die() { echo -e "${COLRED}$@${COLDEFAULT}"; exit 1; } mecho() { echo -e "${COLGREEN}$@${COLDEFAULT}"; } usage() { echo "Usage: $(basename $0 .sh) [opts]" echo "Options are:" echo " --kernel-version - build modules for given kernel version not current" echo " --kernel-sources - use kernel sources different from default path" echo " --force - force a rebuild of the kernel modules even if they exist" echo " --verbose - be verbose while building the modules" echo " --help - show this help" exit } # must be root [[ $(id -u) != 0 ]] && die "You must be r00t!" # check for given argvs for i in $* do case $1 in --kernel-version) shift; KERNEL_VERSION="$1" ;; --kernel-sources) shift; KERNEL_SOURCES="$1" ;; --force) FORCED_REBUILD="1" ;; --verbose) VERBOSE="1" ;; --help) usage ;; esac shift done # some sane defaults [[ -z ${KERNEL_VERSION} ]] && KERNEL_VERSION="$(uname -r)" [[ -z ${KERNEL_SOURCES} ]] && KERNEL_SOURCES="/lib/modules/${KERNEL_VERSION}/source" [[ -z ${FORCED_REBUILD} ]] && FORCED_REBUILD="0" [[ -z ${VERBOSE} ]] && VERBOSE="0" [[ ${VERBOSE} = 0 ]] && makequiet="-s" mecho "Running $(basename $0) for kernelversion ${KERNEL_VERSION} ..." # first stop the vboxguest service if [[ -x $(which systemctl) ]] then systemctl is-active vboxguest.service && systemctl stop vboxguest.service systemctl is-enabled vboxguest.service && systemctl disable vboxguest.service elif [[ -x /etc/rc.d/init.d/vboxguest ]] then /etc/rc.d/init.d/vboxguest stop rc-config del vboxguest fi # check module version and recompile if it doesen't fit else break here compile="yes" for module in ${KERNELMODULES[*]} do if [[ -f /lib/modules/${KERNEL_VERSION}/misc/${module}.${VBOXMODEXT} ]] then myver=$(modinfo -k ${KERNEL_VERSION} -F version ${module} | sed "s:\(.*\)_.*:\1:") if [[ ${VBOXVERSION} = ${myver} ]] && [[ ${FORCED_REBUILD} = 0 ]] then compile="no" fi fi done # try to unload the modules in reverse order to honor dependencies count=${#KERNELMODULES[*]} for (( i=count-1; i>=0; i-- )) do if [[ -f /lib/modules/${KERNEL_VERSION}/misc/${KERNELMODULES[${i}]}.${VBOXMODEXT} ]] then if [[ -n $(grep "${KERNELMODULES[${i}]} " /proc/modules 2> /dev/null) ]] then mecho "Unloading ${KERNELMODULES[${i}]} module ..." modprobe -r ${KERNELMODULES[${i}]} fi fi done if [[ ${compile} = yes ]] then if [ ! -f ${KERNEL_SOURCES}/include/linux/version.h ] then die "No kernel sources for kernel ${KERNEL_VERSION} found! Aborting." fi for module in ${KERNELMODULES[*]} do if [[ -f /lib/modules/${KERNEL_VERSION}/misc/${module}.${VBOXMODEXT} ]] then mecho "Removing old ${module} module ..." rm -f /lib/modules/${KERNEL_VERSION}/misc/${module}.${VBOXMODEXT} fi done # compile the modules pushd ${INSTALL_DIR}/additions/src > /dev/null for module in ${KERNELMODULES[*]} do mecho "Compiling ${module} module ..." make ${makequiet} -C ${module} KBUILD_VERBOSE=${VERBOSE} clean || die "mod-compile ${module}" # need some symver from vboxmodule (must be run after clean!) if [[ ${module} = ${VBOXSFMODULE} ]] then [[ -f ${VBOXGUESTMODULE}/Module.symvers ]] && cp ${VBOXGUESTMODULE}/Module.symvers ${module} fi make ${makequiet} -C ${module} KBUILD_VERBOSE=${VERBOSE} || die "mod-compile ${module}" # install the modules install -d /lib/modules/${KERNEL_VERSION}/misc install -m0644 ${module}/${module}.${VBOXMODEXT} /lib/modules/${KERNEL_VERSION}/misc || die "mod-install ${module}" done # do no clean before all modules are build, or required symbols and objects get deleted for module in ${KERNELMODULES[*]} do # clean up make ${makequiet} -C ${module} KBUILD_VERBOSE=${VERBOSE} clean || die "mod-compile ${module}" done popd > /dev/null # calc module dependencies mecho "Calculating module dependencies ..." depmod -a ${KERNEL_VERSION} # re-read udev rules to grant the right permissions if [[ -x $(which udevadm) ]] then mecho "Reloading udev configuration ..." udevadm control --reload-rules fi fi # load the module if [[ x$(uname -r) = x${KERNEL_VERSION} ]] then for module in ${KERNELMODULES[*]} do mecho "Loading kernel-module ${module} ..." modprobe ${module} done if [ $(echo ${KERNEL_VERSION%%-*} | sed 's:\.::g') -ge 2639 ] then # fixes NMI warnings with kernels >=2.6.31: # vboxdrv: Warning: 2.6.31+ kernel detected. Most likely the hardware performance # vboxdrv: counter framework which can generate NMIs is active. mecho "Disabled hardware performance counter NMIs ..." echo 2 > /proc/sys/kernel/perf_event_paranoid elif [ $(echo ${KERNEL_VERSION%%-*} | sed 's:\.::g') -ge 2631 ] then # fixes NMI warnings with kernels >=2.6.31: # vboxdrv: Warning: 2.6.31+ kernel detected. Most likely the hardware performance # vboxdrv: counter framework which can generate NMIs is active. mecho "Disabled hardware performance counter NMIs ..." echo 2 > /proc/sys/kernel/perf_counter_paranoid fi else echo -e ${COLRED} echo "Your current running kernel does not match the the module target." echo "You must boot into your target kernel and load the modules manually," echo "before you can use virtualbox." echo -en ${COLDEFAULT} fi # start the vboxguest service if [[ -x $(which systemctl) ]] then systemctl enable vboxguest.service systemctl start vboxguest.service elif [[ -x /etc/rc.d/init.d/vboxguest ]] then rc-config add vboxguest /etc/rc.d/init.d/vboxguest stop fi echo mecho "Virtualbox guest-additions are now configured for your system." echo exit 0