VirtualBox

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

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

Installer/linux (guest): fix kernel module re-building looping if the build fails.

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