VirtualBox

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

Last change on this file since 56881 was 56881, checked in by vboxsync, 10 years ago

Installers/linux: /etc/init.d/service may not exist, use /sbin/rcservice instead.

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