VirtualBox

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

Last change on this file since 62831 was 62830, checked in by vboxsync, 8 years ago

bugref:8480: Guest Additions: Cannot unload vboxguest on upgrade: changed the failure message that the guest drivers could not be unloaded when old Additions are removed to a warning that the user may need to reboot for changes to take effect.

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