VirtualBox

Ticket #14733: vboxadd

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

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