VirtualBox

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

Last change on this file since 77307 was 77307, checked in by vboxsync, 5 years ago

Additions/linux/installer: clean up kernel module rebuilding again.
bugref:9387: Linux Additions: recommend up-to-date distribution-built Additions
Fix trailing whitespace and rename a top-level shell variable to prevent
future clashes.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 18.5 KB
Line 
1#! /bin/sh
2# $Id: vboxadd.sh 77307 2019-02-14 10:32:59Z vboxsync $
3## @file
4# Linux Additions kernel module init script ($Revision: 77307 $)
5#
6
7#
8# Copyright (C) 2006-2019 Oracle Corporation
9#
10# This file is part of VirtualBox Open Source Edition (OSE), as
11# available from http://www.virtualbox.org. This file is free software;
12# you can redistribute it and/or modify it under the terms of the GNU
13# General Public License (GPL) as published by the Free Software
14# Foundation, in version 2 as it comes in the "COPYING" file of the
15# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17#
18
19# X-Start-Before is a Debian Addition which we use when converting to
20# a systemd unit. X-Service-Type is our own invention, also for systemd.
21
22# chkconfig: 345 10 90
23# description: VirtualBox Linux Additions kernel modules
24#
25### BEGIN INIT INFO
26# Provides: vboxadd
27# Required-Start:
28# Required-Stop:
29# Default-Start: 2 3 4 5
30# Default-Stop: 0 1 6
31# X-Start-Before: display-manager
32# X-Service-Type: oneshot
33# Description: VirtualBox Linux Additions kernel modules
34### END INIT INFO
35
36## @todo This file duplicates a lot of script with vboxdrv.sh. When making
37# changes please try to reduce differences between the two wherever possible.
38
39# Testing:
40# * Should fail if the configuration file is missing or missing INSTALL_DIR or
41# INSTALL_VER entries.
42# * vboxadd user and vboxsf groups should be created if they do not exist - test
43# by removing them before installing.
44# * Shared folders can be mounted and auto-mounts accessible to vboxsf group,
45# including on recent Fedoras with SELinux.
46# * Setting INSTALL_NO_MODULE_BUILDS inhibits modules and module automatic
47# rebuild script creation; otherwise modules, user, group, rebuild script,
48# udev rule and shared folder mount helper should be created/set up.
49# * Setting INSTALL_NO_MODULE_BUILDS inhibits module load and unload on start
50# and stop.
51# * Uninstalling the Additions and re-installing them does not trigger warnings.
52
53export LC_ALL=C
54PATH=$PATH:/bin:/sbin:/usr/sbin
55PACKAGE=VBoxGuestAdditions
56MODPROBE=/sbin/modprobe
57OLDMODULES="vboxguest vboxadd vboxsf vboxvfs vboxvideo"
58SERVICE="VirtualBox Guest Additions"
59## systemd logs information about service status, otherwise do that ourselves.
60QUIET=
61test -z "${TARGET_VER}" && TARGET_VER=`uname -r`
62# Marker to ignore a particular kernel version which was already installed.
63SKIPFILE_BASE=/var/lib/VBoxGuestAdditions/skip
64export BUILD_TYPE
65export USERNAME
66
67setup_log()
68{
69 test -z "${LOG}" || return 0
70 # Rotate log files
71 LOG="/var/log/vboxadd-setup.log"
72 mv "${LOG}.3" "${LOG}.4" 2>/dev/null
73 mv "${LOG}.2" "${LOG}.3" 2>/dev/null
74 mv "${LOG}.1" "${LOG}.2" 2>/dev/null
75 mv "${LOG}" "${LOG}.1" 2>/dev/null
76}
77
78if $MODPROBE -c 2>/dev/null | grep -q '^allow_unsupported_modules *0'; then
79 MODPROBE="$MODPROBE --allow-unsupported-modules"
80fi
81
82# Preamble for Gentoo
83if [ "`which $0`" = "/sbin/rc" ]; then
84 shift
85fi
86
87begin()
88{
89 test -n "${QUIET}" || echo "${SERVICE}: ${1}"
90}
91
92info()
93{
94 if test -z "${QUIET}"; then
95 echo "${SERVICE}: $1" | fold -s
96 else
97 echo "$1" | fold -s
98 fi
99}
100
101fail()
102{
103 log "${1}"
104 echo "$1" >&2
105 echo "The log file $LOG may contain further information." >&2
106 exit 1
107}
108
109log()
110{
111 setup_log
112 echo "${1}" >> "${LOG}"
113}
114
115module_build_log()
116{
117 log "Error building the module. Build output follows."
118 echo ""
119 echo "${1}" >> "${LOG}"
120}
121
122dev=/dev/vboxguest
123userdev=/dev/vboxuser
124config=/var/lib/VBoxGuestAdditions/config
125owner=vboxadd
126group=1
127
128if test -r $config; then
129 . $config
130else
131 fail "Configuration file $config not found"
132fi
133test -n "$INSTALL_DIR" -a -n "$INSTALL_VER" ||
134 fail "Configuration file $config not complete"
135MODULE_SRC="$INSTALL_DIR/src/vboxguest-$INSTALL_VER"
136BUILDINTMP="$MODULE_SRC/build_in_tmp"
137
138running_vboxguest()
139{
140 lsmod | grep -q "vboxguest[^_-]"
141}
142
143running_vboxadd()
144{
145 lsmod | grep -q "vboxadd[^_-]"
146}
147
148running_vboxsf()
149{
150 lsmod | grep -q "vboxsf[^_-]"
151}
152
153running_vboxvideo()
154{
155 lsmod | grep -q "vboxvideo[^_-]"
156}
157
158do_vboxguest_non_udev()
159{
160 if [ ! -c $dev ]; then
161 maj=`sed -n 's;\([0-9]\+\) vboxguest;\1;p' /proc/devices`
162 if [ ! -z "$maj" ]; then
163 min=0
164 else
165 min=`sed -n 's;\([0-9]\+\) vboxguest;\1;p' /proc/misc`
166 if [ ! -z "$min" ]; then
167 maj=10
168 fi
169 fi
170 test -n "$maj" || {
171 rmmod vboxguest 2>/dev/null
172 fail "Cannot locate the VirtualBox device"
173 }
174
175 mknod -m 0664 $dev c $maj $min || {
176 rmmod vboxguest 2>/dev/null
177 fail "Cannot create device $dev with major $maj and minor $min"
178 }
179 fi
180 chown $owner:$group $dev 2>/dev/null || {
181 rm -f $dev 2>/dev/null
182 rm -f $userdev 2>/dev/null
183 rmmod vboxguest 2>/dev/null
184 fail "Cannot change owner $owner:$group for device $dev"
185 }
186
187 if [ ! -c $userdev ]; then
188 maj=10
189 min=`sed -n 's;\([0-9]\+\) vboxuser;\1;p' /proc/misc`
190 if [ ! -z "$min" ]; then
191 mknod -m 0666 $userdev c $maj $min || {
192 rm -f $dev 2>/dev/null
193 rmmod vboxguest 2>/dev/null
194 fail "Cannot create device $userdev with major $maj and minor $min"
195 }
196 chown $owner:$group $userdev 2>/dev/null || {
197 rm -f $dev 2>/dev/null
198 rm -f $userdev 2>/dev/null
199 rmmod vboxguest 2>/dev/null
200 fail "Cannot change owner $owner:$group for device $userdev"
201 }
202 fi
203 fi
204}
205
206restart()
207{
208 stop && start
209 return 0
210}
211
212## Update the initramfs. Debian and Ubuntu put the graphics driver in, and
213# need the touch(1) command below. Everyone else that I checked just need
214# the right module alias file from depmod(1) and only use the initramfs to
215# load the root filesystem, not the boot splash. update-initramfs works
216# for the first two and dracut for every one else I checked. We are only
217# interested in distributions recent enough to use the KMS vboxvideo driver.
218update_initramfs()
219{
220 ## kernel version to update for.
221 version="${1}"
222 depmod "${version}"
223 rm -f "/lib/modules/${version}/initrd/vboxvideo"
224 test ! -d "/lib/modules/${version}/initrd" ||
225 test ! -f "/lib/modules/${version}/misc/vboxvideo.ko" ||
226 touch "/lib/modules/${version}/initrd/vboxvideo"
227
228 # Systems without systemd-inhibit probably don't need their initramfs
229 # rebuild here anyway.
230 type systemd-inhibit >/dev/null 2>&1 || return
231 if type dracut >/dev/null 2>&1; then
232 systemd-inhibit --why="Installing VirtualBox Guest Additions" \
233 dracut -f --kver "${version}"
234 elif type update-initramfs >/dev/null 2>&1; then
235 systemd-inhibit --why="Installing VirtualBox Guest Additions" \
236 update-initramfs -u -k "${version}"
237 fi
238}
239
240# Remove any existing VirtualBox guest kernel modules from the disk, but not
241# from the kernel as they may still be in use
242cleanup_modules()
243{
244 # Needed for Ubuntu and Debian, see update_initramfs
245 rm -f /lib/modules/*/initrd/vboxvideo
246 for i in /lib/modules/*/misc; do
247 KERN_VER="${i%/misc}"
248 KERN_VER="${KERN_VER#/lib/modules/}"
249 unset do_update
250 for j in ${OLDMODULES}; do
251 test -f "${i}/${j}.ko" && do_update=1 && rm -f "${i}/${j}.ko"
252 done
253 test -z "$do_update" || update_initramfs "$KERN_VER"
254 # Remove empty /lib/modules folders which may have been kept around
255 rmdir -p "${i}" 2>/dev/null || true
256 unset keep
257 for j in /lib/modules/"${KERN_VER}"/*; do
258 name="${j##*/}"
259 test -d "${name}" || test "${name%%.*}" != modules && keep=1
260 done
261 if test -z "${keep}"; then
262 rm -rf /lib/modules/"${KERN_VER}"
263 rm -f /boot/initrd.img-"${KERN_VER}"
264 fi
265 done
266 for i in ${OLDMODULES}; do
267 # We no longer support DKMS, remove any leftovers.
268 rm -rf "/var/lib/dkms/${i}"*
269 done
270 rm -f /etc/depmod.d/vboxvideo-upstream.conf
271 rm -f "$SKIPFILE_BASE"-*
272}
273
274# Build and install the VirtualBox guest kernel modules
275setup_modules()
276{
277 KERN_VER="$1"
278 test -n "$KERN_VER" || return 1
279 test ! -f /lib/modules/"$KERN_VER"/misc/vboxguest.ko || return 0
280 test ! -f /lib/modules/"$KERN_VER"/misc/vboxguest.o || return 0
281 test -d /lib/modules/"$KERN_VER"/build || return 0
282 export KERN_VER
283 info "Building the modules for kernel $KERN_VER."
284
285 log "Building the main Guest Additions module for kernel $KERN_VER."
286 if ! myerr=`$BUILDINTMP \
287 --save-module-symvers /tmp/vboxguest-Module.symvers \
288 --module-source $MODULE_SRC/vboxguest \
289 --no-print-directory install 2>&1`; then
290 # If check_module_dependencies.sh fails it prints a message itself.
291 module_build_log "$myerr"
292 "${INSTALL_DIR}"/other/check_module_dependencies.sh 2>&1 &&
293 info "Look at $LOG to find out what went wrong"
294 return 0
295 fi
296 log "Building the shared folder support module."
297 if ! myerr=`$BUILDINTMP \
298 --use-module-symvers /tmp/vboxguest-Module.symvers \
299 --module-source $MODULE_SRC/vboxsf \
300 --no-print-directory install 2>&1`; then
301 module_build_log "$myerr"
302 info "Look at $LOG to find out what went wrong"
303 return 0
304 fi
305 log "Building the graphics driver module."
306 if ! myerr=`$BUILDINTMP \
307 --use-module-symvers /tmp/vboxguest-Module.symvers \
308 --module-source $MODULE_SRC/vboxvideo \
309 --no-print-directory install 2>&1`; then
310 module_build_log "$myerr"
311 info "Look at $LOG to find out what went wrong"
312 fi
313 [ -d /etc/depmod.d ] || mkdir /etc/depmod.d
314 echo "override vboxguest * misc" > /etc/depmod.d/vboxvideo-upstream.conf
315 echo "override vboxsf * misc" >> /etc/depmod.d/vboxvideo-upstream.conf
316 echo "override vboxvideo * misc" >> /etc/depmod.d/vboxvideo-upstream.conf
317 update_initramfs "${KERN_VER}"
318 return 0
319}
320
321create_vbox_user()
322{
323 # This is the LSB version of useradd and should work on recent
324 # distributions
325 useradd -d /var/run/vboxadd -g 1 -r -s /bin/false vboxadd >/dev/null 2>&1 || true
326 # And for the others, we choose a UID ourselves
327 useradd -d /var/run/vboxadd -g 1 -u 501 -o -s /bin/false vboxadd >/dev/null 2>&1 || true
328
329}
330
331create_udev_rule()
332{
333 # Create udev description file
334 if [ -d /etc/udev/rules.d ]; then
335 udev_call=""
336 udev_app=`which udevadm 2> /dev/null`
337 if [ $? -eq 0 ]; then
338 udev_call="${udev_app} version 2> /dev/null"
339 else
340 udev_app=`which udevinfo 2> /dev/null`
341 if [ $? -eq 0 ]; then
342 udev_call="${udev_app} -V 2> /dev/null"
343 fi
344 fi
345 udev_fix="="
346 if [ "${udev_call}" != "" ]; then
347 udev_out=`${udev_call}`
348 udev_ver=`expr "$udev_out" : '[^0-9]*\([0-9]*\)'`
349 if [ "$udev_ver" = "" -o "$udev_ver" -lt 55 ]; then
350 udev_fix=""
351 fi
352 fi
353 ## @todo 60-vboxadd.rules -> 60-vboxguest.rules ?
354 echo "KERNEL=${udev_fix}\"vboxguest\", NAME=\"vboxguest\", OWNER=\"vboxadd\", MODE=\"0660\"" > /etc/udev/rules.d/60-vboxadd.rules
355 echo "KERNEL=${udev_fix}\"vboxuser\", NAME=\"vboxuser\", OWNER=\"vboxadd\", MODE=\"0666\"" >> /etc/udev/rules.d/60-vboxadd.rules
356 fi
357}
358
359create_module_rebuild_script()
360{
361 # And a post-installation script for rebuilding modules when a new kernel
362 # is installed.
363 mkdir -p /etc/kernel/postinst.d /etc/kernel/prerm.d
364 cat << EOF > /etc/kernel/postinst.d/vboxadd
365#!/bin/sh
366# This only works correctly on Debian derivatives - Red Hat calls it before
367# installing the right header files.
368/sbin/rcvboxadd quicksetup "\${1}"
369exit 0
370EOF
371 cat << EOF > /etc/kernel/prerm.d/vboxadd
372#!/bin/sh
373for i in ${OLDMODULES}; do rm -f /lib/modules/"\${1}"/misc/"\${i}".ko; done
374rmdir -p /lib/modules/"\$1"/misc 2>/dev/null || true
375exit 0
376EOF
377 chmod 0755 /etc/kernel/postinst.d/vboxadd /etc/kernel/prerm.d/vboxadd
378}
379
380shared_folder_setup()
381{
382 # Add a group "vboxsf" for Shared Folders access
383 # All users which want to access the auto-mounted Shared Folders have to
384 # be added to this group.
385 groupadd -r -f vboxsf >/dev/null 2>&1
386
387 # Put the mount.vboxsf mount helper in the right place.
388 ## @todo It would be nicer if the kernel module just parsed parameters
389 # itself instead of needing a separate binary to do that.
390 ln -sf "${INSTALL_DIR}/other/mount.vboxsf" /sbin
391 # SELinux security context for the mount helper.
392 if test -e /etc/selinux/config; then
393 # This is correct. semanage maps this to the real path, and it aborts
394 # with an error, telling you what you should have typed, if you specify
395 # the real path. The "chcon" is there as a back-up for old guests.
396 command -v semanage > /dev/null &&
397 semanage fcontext -a -t mount_exec_t "${INSTALL_DIR}/other/mount.vboxsf"
398 chcon -t mount_exec_t "${INSTALL_DIR}/other/mount.vboxsf" 2>/dev/null
399 fi
400}
401
402# setup_script
403setup()
404{
405 # chcon is needed on old Fedora/Redhat systems. No one remembers which.
406 test ! -e /etc/selinux/config ||
407 chcon -t bin_t "$BUILDINTMP" 2>/dev/null
408
409 if test -z "$INSTALL_NO_MODULE_BUILDS"; then
410 info "Building the VirtualBox Guest Additions kernel modules. This may take a while."
411 info "To build modules for other installed kernels, run"
412 info " /sbin/rcvboxadd quicksetup <version>"
413 info "or"
414 info " /sbin/rcvboxadd quicksetup all"
415 if test -d /lib/modules/"$TARGET_VER"/build; then
416 setup_modules "$TARGET_VER"
417 depmod
418 else
419 info "Kernel headers not found for target kernel $TARGET_VER. \
420Please install them and execute
421 /sbin/rcvboxadd setup"
422 fi
423 fi
424 create_vbox_user
425 create_udev_rule
426 test -n "${INSTALL_NO_MODULE_BUILDS}" || create_module_rebuild_script
427 shared_folder_setup
428 if running_vboxguest || running_vboxadd; then
429 info "Running kernel modules will not be replaced until the system is restarted"
430 fi
431
432 # Put the X.Org driver in place. This is harmless if it is not needed.
433 # Also set up the OpenGL library.
434 myerr=`"${INSTALL_DIR}/init/vboxadd-x11" setup 2>&1`
435 test -z "${myerr}" || log "${myerr}"
436
437 return 0
438}
439
440# cleanup_script
441cleanup()
442{
443 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
444 # Delete old versions of VBox modules.
445 cleanup_modules
446 depmod
447
448 # Remove old module sources
449 for i in $OLDMODULES; do
450 rm -rf /usr/src/$i-*
451 done
452 fi
453
454 # Clean-up X11-related bits
455 "${INSTALL_DIR}/init/vboxadd-x11" cleanup
456
457 # Remove other files
458 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
459 rm -f /etc/kernel/postinst.d/vboxadd /etc/kernel/prerm.d/vboxadd
460 rmdir -p /etc/kernel/postinst.d /etc/kernel/prerm.d 2>/dev/null || true
461 fi
462 rm -f /sbin/mount.vboxsf 2>/dev/null
463 rm -f /etc/udev/rules.d/60-vboxadd.rules 2>/dev/null
464}
465
466start()
467{
468 begin "Starting."
469 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
470 # We want to build modules for newly installed kernels on shutdown, so
471 # mark the ones already present. These will be ignored on shutdown.
472 rm -f "$SKIPFILE_BASE"-*
473 for setupi in /lib/modules/*; do
474 KERN_VER="${setupi##*/}"
475 # For a full setup, mark kernels we do not want to build.
476 touch "$SKIPFILE_BASE"-"$KERN_VER"
477 done
478 fi
479 setup
480 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
481 test -d /sys &&
482 ps -A -o comm | grep -q '/*udevd$' 2>/dev/null ||
483 no_udev=1
484 running_vboxguest || {
485 rm -f $dev || {
486 fail "Cannot remove $dev"
487 }
488 rm -f $userdev || {
489 fail "Cannot remove $userdev"
490 }
491 $MODPROBE vboxguest >/dev/null 2>&1 ||
492 fail "modprobe vboxguest failed"
493 case "$no_udev" in 1)
494 sleep .5;;
495 esac
496 $MODPROBE vboxsf > /dev/null 2>&1 ||
497 info "modprobe vboxsf failed"
498 }
499 case "$no_udev" in 1)
500 do_vboxguest_non_udev;;
501 esac
502 fi # INSTALL_NO_MODULE_BUILDS
503
504 return 0
505}
506
507stop()
508{
509 begin "Stopping."
510 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
511 # We want to build modules for newly installed kernels on shutdown, so
512 # check which we marked at start-up.
513 for setupi in /lib/modules/*; do
514 KERN_VER="${setupi##*/}"
515 # For a full setup, mark kernels we do not want to build.
516 test -f "$SKIPFILE_BASE"-"$KERN_VER" || setup_modules "$KERN_VER"
517 done
518 fi
519 if test -r /etc/ld.so.conf.d/00vboxvideo.conf; then
520 rm /etc/ld.so.conf.d/00vboxvideo.conf
521 ldconfig
522 fi
523 if ! umount -a -t vboxsf 2>/dev/null; then
524 fail "Cannot unmount vboxsf folders"
525 fi
526 test -n "${INSTALL_NO_MODULE_BUILDS}" ||
527 info "You may need to restart your guest system to finish removing guest drivers."
528 return 0
529}
530
531dmnstatus()
532{
533 if running_vboxguest; then
534 echo "The VirtualBox Additions are currently running."
535 else
536 echo "The VirtualBox Additions are not currently running."
537 fi
538}
539
540for i; do
541 case "$i" in quiet) QUIET=yes;; esac
542done
543case "$1" in
544# Does setup without clean-up first and marks all kernels currently found on the
545# system so that we can see later if any were added.
546start)
547 start
548 ;;
549# Tries to build kernel modules for kernels added since start. Tries to unmount
550# shared folders. Uninstalls our Chromium 3D libraries since we can't always do
551# this fast enough at start time if we discover we do not want to use them.
552stop)
553 stop
554 ;;
555restart)
556 restart
557 ;;
558# Setup does a clean-up (see below) and re-does all Additions-specific
559# configuration of the guest system, including building kernel modules for the
560# current kernel.
561setup)
562 cleanup && start
563 ;;
564# Builds kernel modules for the specified kernels if they are not already built.
565quicksetup)
566 if test x"$2" = xall; then
567 for topi in /lib/modules/*; do
568 KERN_VER="${topi%/misc}"
569 KERN_VER="${KERN_VER#/lib/modules/}"
570 setup_modules "$KERN_VER"
571 done
572 elif test -n "$2"; then
573 setup_modules "$2"
574 else
575 setup_modules "$TARGET_VER"
576 fi
577 ;;
578# Clean-up removes all Additions-specific configuration of the guest system,
579# including all kernel modules.
580cleanup)
581 cleanup
582 ;;
583status)
584 dmnstatus
585 ;;
586*)
587 echo "Usage: $0 {start|stop|restart|status|setup|quicksetup|cleanup} [quiet]"
588 exit 1
589esac
590
591exit
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use