Magellan Linux

Annotation of /trunk/virtualbox/virtualbox-guest-config.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1686 - (hide annotations) (download) (as text)
Sun Mar 4 12:32:49 2012 UTC (12 years, 8 months ago) by niro
File MIME type: application/x-sh
File size: 5276 byte(s)
-always reload udev rules, not only after module compilation
1 niro 1685 #!/bin/bash
2     # $Id$
3     # Configures virtualbox for Magellan-Linux guests
4    
5     # get INSTALL_DIR location
6     source /etc/vbox/vbox.cfg
7    
8     VBOXVERSION="$(< ${INSTALL_DIR}/version)"
9     VBOXMODEXT=ko
10     VBOXGUESTMODULE=vboxguest
11     VBOXSFMODULE=vboxsf
12     VBOXVIDEOMODULE=vboxsf
13     KERNELMODULES=( "${VBOXGUESTMODULE}" "${VBOXSFMODULE}" "${VBOXVIDEOMODULE}" )
14    
15     COLRED="\033[1;6m\033[31m"
16     COLGREEN="\033[1;6m\033[32m"
17     COLDEFAULT="\033[0m"
18    
19     if [[ ${NOCOLORS} = true ]]
20     then
21     COLRED=""
22     COLGREEN=""
23     COLDEFAULT=""
24     fi
25    
26     die() { echo -e "${COLRED}$@${COLDEFAULT}"; exit 1; }
27     mecho() { echo -e "${COLGREEN}$@${COLDEFAULT}"; }
28     usage()
29     {
30     echo "Usage: $(basename $0 .sh) [opts]"
31     echo "Options are:"
32     echo " --kernel-version - build modules for given kernel version not current"
33     echo " --kernel-sources - use kernel sources different from default path"
34     echo " --force - force a rebuild of the kernel modules even if they exist"
35     echo " --verbose - be verbose while building the modules"
36     echo " --help - show this help"
37     exit
38     }
39    
40     # must be root
41     [[ $(id -u) != 0 ]] && die "You must be r00t!"
42    
43     # check for given argvs
44     for i in $*
45     do
46     case $1 in
47     --kernel-version) shift; KERNEL_VERSION="$1" ;;
48     --kernel-sources) shift; KERNEL_SOURCES="$1" ;;
49     --force) FORCED_REBUILD="1" ;;
50     --verbose) VERBOSE="1" ;;
51     --help) usage ;;
52     esac
53     shift
54     done
55    
56     # some sane defaults
57     [[ -z ${KERNEL_VERSION} ]] && KERNEL_VERSION="$(uname -r)"
58     [[ -z ${KERNEL_SOURCES} ]] && KERNEL_SOURCES="/lib/modules/${KERNEL_VERSION}/source"
59     [[ -z ${FORCED_REBUILD} ]] && FORCED_REBUILD="0"
60     [[ -z ${VERBOSE} ]] && VERBOSE="0"
61     [[ ${VERBOSE} = 0 ]] && makequiet="-s"
62    
63     mecho "Running $(basename $0) for kernelversion ${KERNEL_VERSION} ..."
64    
65     # first stop the vboxguest service
66     if [[ -x $(which systemctl) ]]
67     then
68     systemctl is-active vboxguest.service && systemctl stop vboxguest.service
69     systemctl is-enabled vboxguest.service && systemctl disable vboxguest.service
70     elif [[ -x /etc/rc.d/init.d/vboxguest ]]
71     then
72     /etc/rc.d/init.d/vboxguest stop
73     rc-config del vboxguest
74     fi
75    
76     # check module version and recompile if it doesen't fit else break here
77     compile="yes"
78     for module in ${KERNELMODULES[*]}
79     do
80     if [[ -f /lib/modules/${KERNEL_VERSION}/misc/${module}.${VBOXMODEXT} ]]
81     then
82     myver=$(modinfo -k ${KERNEL_VERSION} -F version ${module} | sed "s:\(.*\)_.*:\1:")
83     if [[ ${VBOXVERSION} = ${myver} ]] && [[ ${FORCED_REBUILD} = 0 ]]
84     then
85     compile="no"
86     fi
87     fi
88     done
89    
90     # try to unload the modules in reverse order to honor dependencies
91     count=${#KERNELMODULES[*]}
92     for (( i=count-1; i>=0; i-- ))
93     do
94     if [[ -f /lib/modules/${KERNEL_VERSION}/misc/${KERNELMODULES[${i}]}.${VBOXMODEXT} ]]
95     then
96     if [[ -n $(grep "${KERNELMODULES[${i}]} " /proc/modules 2> /dev/null) ]]
97     then
98     mecho "Unloading ${KERNELMODULES[${i}]} module ..."
99     modprobe -r ${KERNELMODULES[${i}]}
100     fi
101     fi
102     done
103    
104     if [[ ${compile} = yes ]]
105     then
106     if [ ! -f ${KERNEL_SOURCES}/include/linux/version.h ]
107     then
108     die "No kernel sources for kernel ${KERNEL_VERSION} found! Aborting."
109     fi
110    
111     for module in ${KERNELMODULES[*]}
112     do
113     if [[ -f /lib/modules/${KERNEL_VERSION}/misc/${module}.${VBOXMODEXT} ]]
114     then
115     mecho "Removing old ${module} module ..."
116     rm -f /lib/modules/${KERNEL_VERSION}/misc/${module}.${VBOXMODEXT}
117     fi
118     done
119    
120     # compile the modules
121     pushd ${INSTALL_DIR}/additions/src > /dev/null
122     for module in ${KERNELMODULES[*]}
123     do
124     mecho "Compiling ${module} module ..."
125     make ${makequiet} -C ${module} KBUILD_VERBOSE=${VERBOSE} clean || die "mod-compile ${module}"
126    
127     # need some symver from vboxmodule (must be run after clean!)
128     if [[ ${module} = ${VBOXSFMODULE} ]]
129     then
130     [[ -f ${VBOXGUESTMODULE}/Module.symvers ]] && cp ${VBOXGUESTMODULE}/Module.symvers ${module}
131     fi
132    
133     make ${makequiet} -C ${module} KBUILD_VERBOSE=${VERBOSE} || die "mod-compile ${module}"
134    
135     # install the modules
136     install -d /lib/modules/${KERNEL_VERSION}/misc
137     install -m0644 ${module}/${module}.${VBOXMODEXT} /lib/modules/${KERNEL_VERSION}/misc || die "mod-install ${module}"
138     done
139     # do no clean before all modules are build, or required symbols and objects get deleted
140     for module in ${KERNELMODULES[*]}
141     do
142     # clean up
143     make ${makequiet} -C ${module} KBUILD_VERBOSE=${VERBOSE} clean || die "mod-compile ${module}"
144     done
145     popd > /dev/null
146    
147     # calc module dependencies
148     mecho "Calculating module dependencies ..."
149     depmod -a ${KERNEL_VERSION}
150 niro 1686 fi
151 niro 1685
152 niro 1686 # re-read udev rules to grant the right permissions
153     if [[ -x $(which udevadm) ]]
154     then
155     mecho "Reloading udev configuration ..."
156     udevadm control --reload-rules
157 niro 1685 fi
158    
159     # load the module
160     if [[ x$(uname -r) = x${KERNEL_VERSION} ]]
161     then
162     for module in ${KERNELMODULES[*]}
163     do
164     mecho "Loading kernel-module ${module} ..."
165     modprobe ${module}
166     done
167     else
168     echo -e ${COLRED}
169     echo "Your current running kernel does not match the the module target."
170     echo "You must boot into your target kernel and load the modules manually,"
171     echo "before you can use virtualbox."
172     echo -en ${COLDEFAULT}
173     fi
174    
175     # start the vboxguest service
176     if [[ -x $(which systemctl) ]]
177     then
178     systemctl enable vboxguest.service
179     systemctl start vboxguest.service
180     elif [[ -x /etc/rc.d/init.d/vboxguest ]]
181     then
182     rc-config add vboxguest
183     /etc/rc.d/init.d/vboxguest stop
184     fi
185    
186     echo
187     mecho "Virtualbox guest-additions are now configured for your system."
188     echo
189    
190     exit 0