VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/installer/vboxadd.sh@ 57986

Last change on this file since 57986 was 57986, checked in by vboxsync, 9 years ago

Installers/linux: try to rebuild the kernel modules if they cannot be loaded.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1#! /bin/sh
2#
3# Linux Additions kernel module init script ($Revision: 57986 $)
4#
5
6#
7# Copyright (C) 2006-2012 Oracle Corporation
8#
9# This file is part of VirtualBox Open Source Edition (OSE), as
10# available from http://www.virtualbox.org. This file is free software;
11# you can redistribute it and/or modify it under the terms of the GNU
12# General Public License (GPL) as published by the Free Software
13# Foundation, in version 2 as it comes in the "COPYING" file of the
14# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16#
17
18
19# chkconfig: 345 30 70
20# description: VirtualBox Linux Additions kernel modules
21#
22### BEGIN INIT INFO
23# Provides: vboxadd
24# Required-Start:
25# Required-Stop:
26# Default-Start: 2 3 4 5
27# Default-Stop: 0 1 6
28# Description: VirtualBox Linux Additions kernel modules
29### END INIT INFO
30
31PATH=$PATH:/bin:/sbin:/usr/sbin
32PACKAGE=VBoxGuestAdditions
33LOG="/var/log/vboxadd-install.log"
34MODPROBE=/sbin/modprobe
35OLDMODULES="vboxguest vboxadd vboxsf vboxvfs vboxvideo"
36SCRIPTNAME=vboxadd
37
38if $MODPROBE -c 2>/dev/null | grep -q '^allow_unsupported_modules *0'; then
39 MODPROBE="$MODPROBE --allow-unsupported-modules"
40fi
41
42# Check architecture
43cpu=`uname -m`;
44case "$cpu" in
45 i[3456789]86|x86)
46 cpu="x86"
47 lib_candidates="/usr/lib/i386-linux-gnu /usr/lib /lib"
48 ;;
49 x86_64|amd64)
50 cpu="amd64"
51 lib_candidates="/usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib /lib64 /lib"
52 ;;
53esac
54for i in $lib_candidates; do
55 if test -d "$i/VBoxGuestAdditions"; then
56 lib_path=$i
57 break
58 fi
59done
60
61# Preamble for Gentoo
62if [ "`which $0`" = "/sbin/rc" ]; then
63 shift
64fi
65
66begin()
67{
68 test -n "${2}" && echo "${SCRIPTNAME}: ${1}."
69 logger "${SCRIPTNAME}: ${1}."
70}
71
72succ_msg()
73{
74 logger "${SCRIPTNAME}: done."
75}
76
77fail_msg()
78{
79 echo "${SCRIPTNAME}: failed." >&2
80 logger "${SCRIPTNAME}: failed."
81}
82
83show_error()
84{
85 echo "${SCRIPTNAME}: failed: ${1}." >&2
86 logger "${SCRIPTNAME}: ${1}."
87}
88
89fail()
90{
91 show_error "$1"
92 exit 1
93}
94
95dev=/dev/vboxguest
96userdev=/dev/vboxuser
97config=/var/lib/VBoxGuestAdditions/config
98owner=vboxadd
99group=1
100
101test_for_gcc_and_make()
102{
103 which make > /dev/null 2>&1 || printf "\nThe make utility was not found. If the following module compilation fails then\nthis could be the reason and you should try installing it.\n"
104 which gcc > /dev/null 2>&1 || printf "\nThe gcc utility was not found. If the following module compilation fails then\nthis could be the reason and you should try installing it.\n"
105}
106
107test_sane_kernel_dir()
108{
109 KERN_VER=`uname -r`
110 KERN_DIR="/lib/modules/$KERN_VER/build"
111 if [ -d "$KERN_DIR" ]; then
112 KERN_REL=`make -sC $KERN_DIR --no-print-directory kernelrelease 2>/dev/null || true`
113 if [ -z "$KERN_REL" -o "x$KERN_REL" = "x$KERN_VER" ]; then
114 return 0
115 fi
116 fi
117 printf "\nThe headers for the current running kernel were not found. If the following\nmodule compilation fails then this could be the reason.\n"
118 if which yum >/dev/null; then
119 if echo "$KERN_VER" | grep -q "uek"; then
120 printf "The missing package can be probably installed with\nyum install kernel-uek-devel-$KERN_VER\n"
121 else
122 printf "The missing package can be probably installed with\nyum install kernel-devel-$KERN_VER\n"
123 fi
124 elif which zypper >/dev/null; then
125 KERN_VER_SUSE=`echo "$KERN_VER" | sed 's/.*-\([^-]*\)/\1/g'`
126 KERN_VER_BASE=`echo "$KERN_VER" | sed 's/\(.*\)-[^-]*/\1/g'`
127 printf "The missing package can be probably installed with\nzypper install kernel-$KERN_VER_SUSE-devel-$KERN_VER_BASE\n"
128 elif which apt-get >/dev/null; then
129 printf "The missing package can be probably installed with\napt-get install linux-headers-$KERN_VER\n"
130 fi
131}
132
133running_vboxguest()
134{
135 lsmod | grep -q "vboxguest[^_-]"
136}
137
138running_vboxadd()
139{
140 lsmod | grep -q "vboxadd[^_-]"
141}
142
143running_vboxsf()
144{
145 lsmod | grep -q "vboxsf[^_-]"
146}
147
148running_vboxvideo()
149{
150 lsmod | grep -q "vboxvideo[^_-]"
151}
152
153do_vboxguest_non_udev()
154{
155 if [ ! -c $dev ]; then
156 maj=`sed -n 's;\([0-9]\+\) vboxguest;\1;p' /proc/devices`
157 if [ ! -z "$maj" ]; then
158 min=0
159 else
160 min=`sed -n 's;\([0-9]\+\) vboxguest;\1;p' /proc/misc`
161 if [ ! -z "$min" ]; then
162 maj=10
163 fi
164 fi
165 test -z "$maj" && {
166 rmmod vboxguest 2>/dev/null
167 fail "Cannot locate the VirtualBox device"
168 }
169
170 mknod -m 0664 $dev c $maj $min || {
171 rmmod vboxguest 2>/dev/null
172 fail "Cannot create device $dev with major $maj and minor $min"
173 }
174 fi
175 chown $owner:$group $dev 2>/dev/null || {
176 rm -f $dev 2>/dev/null
177 rm -f $userdev 2>/dev/null
178 rmmod vboxguest 2>/dev/null
179 fail "Cannot change owner $owner:$group for device $dev"
180 }
181
182 if [ ! -c $userdev ]; then
183 maj=10
184 min=`sed -n 's;\([0-9]\+\) vboxuser;\1;p' /proc/misc`
185 if [ ! -z "$min" ]; then
186 mknod -m 0666 $userdev c $maj $min || {
187 rm -f $dev 2>/dev/null
188 rmmod vboxguest 2>/dev/null
189 fail "Cannot create device $userdev with major $maj and minor $min"
190 }
191 chown $owner:$group $userdev 2>/dev/null || {
192 rm -f $dev 2>/dev/null
193 rm -f $userdev 2>/dev/null
194 rmmod vboxguest 2>/dev/null
195 fail "Cannot change owner $owner:$group for device $userdev"
196 }
197 fi
198 fi
199}
200
201start()
202{
203 begin "Starting the VirtualBox Guest Additions" console;
204 uname -r | grep -q -E '^2\.6|^3|^4' 2>/dev/null &&
205 ps -A -o comm | grep -q '/*udevd$' 2>/dev/null ||
206 no_udev=1
207 running_vboxguest || {
208 rm -f $dev || {
209 fail "Cannot remove $dev"
210 }
211
212 rm -f $userdev || {
213 fail "Cannot remove $userdev"
214 }
215
216 $MODPROBE vboxguest >/dev/null 2>&1 || {
217 setup
218 $MODPROBE vboxguest >/dev/null 2>&1 ||
219 fail "modprobe vboxguest failed"
220 }
221 case "$no_udev" in 1)
222 sleep .5;;
223 esac
224 }
225 case "$no_udev" in 1)
226 do_vboxguest_non_udev;;
227 esac
228
229 running_vboxsf || {
230 $MODPROBE vboxsf > /dev/null 2>&1 || {
231 if dmesg | grep "vboxConnect failed" > /dev/null 2>&1; then
232 fail_msg
233 echo "Unable to start shared folders support. Make sure that your VirtualBox build"
234 echo "supports this feature."
235 exit 1
236 fi
237 fail "modprobe vboxsf failed"
238 }
239 }
240
241 # This is needed as X.Org Server 1.13 does not auto-load the module.
242 running_vboxvideo || $MODPROBE vboxvideo > /dev/null 2>&1
243
244 # Mount all shared folders from /etc/fstab. Normally this is done by some
245 # other startup script but this requires the vboxdrv kernel module loaded.
246 # This isn't necessary anymore as the vboxsf module is autoloaded.
247 # mount -a -t vboxsf
248
249 succ_msg
250 return 0
251}
252
253stop()
254{
255 begin "Stopping VirtualBox Additions" console;
256 if ! umount -a -t vboxsf 2>/dev/null; then
257 fail "Cannot unmount vboxsf folders"
258 fi
259 if running_vboxsf; then
260 rmmod vboxsf 2>/dev/null || fail "Cannot unload module vboxsf"
261 fi
262 if running_vboxguest; then
263 rmmod vboxguest 2>/dev/null || fail "Cannot unload module vboxguest"
264 rm -f $userdev || fail "Cannot unlink $userdev"
265 rm -f $dev || fail "Cannot unlink $dev"
266 fi
267 succ_msg
268 return 0
269}
270
271restart()
272{
273 stop && start
274 return 0
275}
276
277# Remove any existing VirtualBox guest kernel modules from the disk, but not
278# from the kernel as they may still be in use
279cleanup_modules()
280{
281 begin "Removing existing VirtualBox kernel modules"
282 # We no longer support DKMS, remove any leftovers.
283 for i in vboxguest vboxadd vboxsf vboxvfs vboxvideo; do
284 rm -rf "/var/lib/dkms/${i}"*
285 done
286 for i in $OLDMODULES; do
287 find /lib/modules -name $i\* | xargs rm 2>/dev/null
288 done
289 succ_msg
290}
291
292# Build and install the VirtualBox guest kernel modules
293setup_modules()
294{
295 # don't stop the old modules here -- they might be in use
296 cleanup_modules
297 begin "Building the VirtualBox Guest Additions kernel modules"
298
299 test_for_gcc_and_make
300 test_sane_kernel_dir
301
302 begin "Building the main Guest Additions module"
303 if ! $BUILDINTMP \
304 --save-module-symvers /tmp/vboxguest-Module.symvers \
305 --module-source $MODULE_SRC/vboxguest \
306 --no-print-directory install >> $LOG 2>&1; then
307 show_error "Look at $LOG to find out what went wrong"
308 return 1
309 fi
310 succ_msg
311 begin "Building the shared folder support module"
312 if ! $BUILDINTMP \
313 --use-module-symvers /tmp/vboxguest-Module.symvers \
314 --module-source $MODULE_SRC/vboxsf \
315 --no-print-directory install >> $LOG 2>&1; then
316 show_error "Look at $LOG to find out what went wrong"
317 return 1
318 fi
319 succ_msg
320 if expr `uname -r` '<' '2.6.27' > /dev/null; then
321 echo "Not building the VirtualBox advanced graphics driver as this Linux version is"
322 echo "too old to use it."
323 else
324 begin "Building the OpenGL support module"
325 if ! $BUILDINTMP \
326 --use-module-symvers /tmp/vboxguest-Module.symvers \
327 --module-source $MODULE_SRC/vboxvideo \
328 --no-print-directory install >> $LOG 2>&1; then
329 show_error "Look at $LOG to find out what went wrong. The module is not built but the others are."
330 else
331 succ_msg
332 fi
333 fi
334 depmod
335 return 0
336}
337
338# Do non-kernel bits needed for the kernel modules to work properly (user
339# creation, udev, mount helper...)
340extra_setup()
341{
342 begin "Doing non-kernel setup of the Guest Additions"
343 echo "Creating user for the Guest Additions." >> $LOG
344 # This is the LSB version of useradd and should work on recent
345 # distributions
346 useradd -d /var/run/vboxadd -g 1 -r -s /bin/false vboxadd >/dev/null 2>&1
347 # And for the others, we choose a UID ourselves
348 useradd -d /var/run/vboxadd -g 1 -u 501 -o -s /bin/false vboxadd >/dev/null 2>&1
349
350 # Add a group "vboxsf" for Shared Folders access
351 # All users which want to access the auto-mounted Shared Folders have to
352 # be added to this group.
353 groupadd -r -f vboxsf >/dev/null 2>&1
354
355 # Create udev description file
356 if [ -d /etc/udev/rules.d ]; then
357 echo "Creating udev rule for the Guest Additions kernel module." >> $LOG
358 udev_call=""
359 udev_app=`which udevadm 2> /dev/null`
360 if [ $? -eq 0 ]; then
361 udev_call="${udev_app} version 2> /dev/null"
362 else
363 udev_app=`which udevinfo 2> /dev/null`
364 if [ $? -eq 0 ]; then
365 udev_call="${udev_app} -V 2> /dev/null"
366 fi
367 fi
368 udev_fix="="
369 if [ "${udev_call}" != "" ]; then
370 udev_out=`${udev_call}`
371 udev_ver=`expr "$udev_out" : '[^0-9]*\([0-9]*\)'`
372 if [ "$udev_ver" = "" -o "$udev_ver" -lt 55 ]; then
373 udev_fix=""
374 fi
375 fi
376 ## @todo 60-vboxadd.rules -> 60-vboxguest.rules ?
377 echo "KERNEL=${udev_fix}\"vboxguest\", NAME=\"vboxguest\", OWNER=\"vboxadd\", MODE=\"0660\"" > /etc/udev/rules.d/60-vboxadd.rules
378 echo "KERNEL=${udev_fix}\"vboxuser\", NAME=\"vboxuser\", OWNER=\"vboxadd\", MODE=\"0666\"" >> /etc/udev/rules.d/60-vboxadd.rules
379 fi
380
381 # Put mount.vboxsf in the right place
382 ln -sf "$lib_path/$PACKAGE/mount.vboxsf" /sbin
383 # And an rc file to re-build the kernel modules and re-set-up the X server.
384 ln -sf "$lib_path/$PACKAGE/vboxadd" /sbin/rcvboxadd
385 ln -sf "$lib_path/$PACKAGE/vboxadd-x11" /sbin/rcvboxadd-x11
386 # At least Fedora 11 and Fedora 12 require the correct security context when
387 # executing this command from service scripts. Shouldn't hurt for other
388 # distributions.
389 chcon -u system_u -t mount_exec_t "$lib_path/$PACKAGE/mount.vboxsf" > /dev/null 2>&1
390 # And at least Fedora 15 needs this for the acceleration support check to
391 # work
392 redhat_release=`cat /etc/redhat-release 2> /dev/null`
393 case "$redhat_release" in Fedora\ release\ 15* )
394 for i in "$lib_path"/*.so
395 do
396 restorecon "$i" >/dev/null
397 done
398 ;;
399 esac
400
401 succ_msg
402}
403
404# setup_script
405setup()
406{
407 begin "Building Guest Additions kernel modules" console
408 if test -r $config; then
409 . $config
410 else
411 fail "Configuration file $config not found"
412 fi
413 test -n "$INSTALL_DIR" -a -n "$INSTALL_VER" ||
414 fail "Configuration file $config not complete"
415 export BUILD_TYPE
416 export USERNAME
417
418 MODULE_SRC="$INSTALL_DIR/src/vboxguest-$INSTALL_VER"
419 BUILDINTMP="$MODULE_SRC/build_in_tmp"
420 chcon -t bin_t "$BUILDINTMP" > /dev/null 2>&1
421
422 setup_modules
423 mod_succ="$?"
424 extra_setup
425 if [ "$mod_succ" -eq "0" ]; then
426 if running_vboxguest || running_vboxadd; then
427 begin "You should restart your guest to make sure the new modules are actually used" console
428 else
429 start
430 fi
431 fi
432}
433
434# cleanup_script
435cleanup()
436{
437 if test -r $config; then
438 . $config
439 test -n "$INSTALL_DIR" -a -n "$INSTALL_VER" ||
440 fail "Configuration file $config not complete"
441 else
442 fail "Configuration file $config not found"
443 fi
444
445 # Delete old versions of VBox modules.
446 cleanup_modules
447 depmod
448
449 # Remove old module sources
450 for i in $OLDMODULES; do
451 rm -rf /usr/src/$i-*
452 done
453
454 # Remove other files
455 rm /sbin/mount.vboxsf 2>/dev/null
456 rm /sbin/rcvboxadd 2>/dev/null
457 rm /sbin/rcvboxadd-x11 2>/dev/null
458 rm /etc/udev/rules.d/60-vboxadd.rules 2>/dev/null
459}
460
461dmnstatus()
462{
463 if running_vboxguest; then
464 echo "The VirtualBox Additions are currently running."
465 else
466 echo "The VirtualBox Additions are not currently running."
467 fi
468}
469
470case "$1" in
471start)
472 start
473 ;;
474stop)
475 stop
476 ;;
477restart)
478 restart
479 ;;
480setup)
481 setup
482 ;;
483cleanup)
484 cleanup
485 ;;
486status)
487 dmnstatus
488 ;;
489*)
490 echo "Usage: $0 {start|stop|restart|status|setup}"
491 exit 1
492esac
493
494exit
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette