VirtualBox

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

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

bugref:8062: Linux installers: drop DKMS support: on Additions installation and service start-up, build modules for all kernels found on the system; and hook into the kernel package update notification which at least Debian and Redhat-based systems provide to build new packages as soon as a new kernel is installed.

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