VirtualBox

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

Last change on this file since 38013 was 37160, checked in by vboxsync, 14 years ago

Additions/linux/installer: fixed device node creation on mixed 2.4/2.6 kernel systems

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

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