--- trunk/virtualbox/virtualbox-config.sh 2012/03/03 02:48:55 1672 +++ trunk/virtualbox/virtualbox-config.sh 2012/03/04 11:32:46 1684 @@ -11,7 +11,7 @@ VBOXNETMODULE=vboxnetflt VBOXNETADPMODULE=vboxnetadp VBOXPCIMODULE=vboxpci -KERNELMODULES="${VBOXMODULE} ${VBOXNETMODULE} ${VBOXNETADPMODULE} ${VBOXPCIMODULE}" +KERNELMODULES=( "${VBOXMODULE}" "${VBOXNETMODULE}" "${VBOXNETADPMODULE}" "${VBOXPCIMODULE}" ) COLRED="\033[1;6m\033[31m" COLGREEN="\033[1;6m\033[32m" @@ -26,6 +26,17 @@ 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!" @@ -36,6 +47,9 @@ 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 @@ -43,31 +57,36 @@ # 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} ..." # check module version and recompile if it doesen't fit else break here compile="yes" -for module in ${KERNELMODULES} +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} ]] + if [[ ${VBOXVERSION} = ${myver} ]] && [[ ${FORCED_REBUILD} = 0 ]] then compile="no" fi + fi done -for module in ${KERNELMODULES} +# 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/${module}.${VBOXMODEXT} ]] + if [[ -f /lib/modules/${KERNEL_VERSION}/misc/${KERNELMODULES[${i}]}.${VBOXMODEXT} ]] then - # try to unload the modules - if [[ -n $(grep "${module} " /proc/modules 2> /dev/null) ]] + if [[ -n $(grep "${KERNELMODULES[${i}]} " /proc/modules 2> /dev/null) ]] then - mecho "Unloading ${module} module ..." - modprobe -r ${module} + mecho "Unloading ${KERNELMODULES[${i}]} module ..." + modprobe -r ${KERNELMODULES[${i}]} fi fi done @@ -79,7 +98,7 @@ die "No kernel sources for kernel ${KERNEL_VERSION} found! Aborting." fi - for module in ${KERNELMODULES} + for module in ${KERNELMODULES[*]} do if [[ -f /lib/modules/${KERNEL_VERSION}/misc/${module}.${VBOXMODEXT} ]] then @@ -89,10 +108,11 @@ done # compile the modules - pushd ${INSTALL_DIR}/src - for module in ${KERNELMODULES} + pushd ${INSTALL_DIR}/src > /dev/null + for module in ${KERNELMODULES[*]} do - make -C ${module} KBUILD_VERBOSE=1 clean || die "mod-compile ${module}" + 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} = ${VBOXNETMODULE} ]] || @@ -102,16 +122,19 @@ [[ -f ${VBOXMODULE}/Module.symvers ]] && cp ${VBOXMODULE}/Module.symvers ${module} fi - make -C ${module} KBUILD_VERBOSE=1 || die "mod-compile ${module}" + 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 -C ${module} KBUILD_VERBOSE=1 clean || die "mod-compile ${module}" + make ${makequiet} -C ${module} KBUILD_VERBOSE=${VERBOSE} clean || die "mod-compile ${module}" done - popd + popd > /dev/null # calc module dependencies mecho "Calculating module dependencies ..." @@ -128,7 +151,7 @@ # load the module if [[ x$(uname -r) = x${KERNEL_VERSION} ]] then - for module in ${KERNELMODULES} + for module in ${KERNELMODULES[*]} do mecho "Loading kernel-module ${module} ..." modprobe ${module}