VirtualBox

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

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

Additions/linux/installer: detect compressed kernel modules too in rebuild test.
bugref:9387: Linux Additions: recommend up-to-date distribution-built Additions
Recent Linux distributions compress kernel modules and store them with a .ko.xz
file name. When testing whether to build new modules we only looked for
vboxguest.ko. To fix that and future-proof it somewhat we now check for any
file with the name vboxguest.*o* in the right folder, which will also catch
Linux 2.4 .o modules.
Thank you Cyrax for the fix.

  • 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 77328 2019-02-15 10:18:16Z vboxsync $
3## @file
4# Linux Additions kernel module init script ($Revision: 77328 $)
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 # Match (at least): vboxguest.o; vboxguest.ko; vboxguest.ko.xz
280 set /lib/modules/"$KERN_VER"/misc/vboxguest.*o*
281 test ! -f "$1" || return 0
282 test -d /lib/modules/"$KERN_VER"/build || return 0
283 export KERN_VER
284 info "Building the modules for kernel $KERN_VER."
285
286 log "Building the main Guest Additions module for kernel $KERN_VER."
287 if ! myerr=`$BUILDINTMP \
288 --save-module-symvers /tmp/vboxguest-Module.symvers \
289 --module-source $MODULE_SRC/vboxguest \
290 --no-print-directory install 2>&1`; then
291 # If check_module_dependencies.sh fails it prints a message itself.
292 module_build_log "$myerr"
293 "${INSTALL_DIR}"/other/check_module_dependencies.sh 2>&1 &&
294 info "Look at $LOG to find out what went wrong"
295 return 0
296 fi
297 log "Building the shared folder support module."
298 if ! myerr=`$BUILDINTMP \
299 --use-module-symvers /tmp/vboxguest-Module.symvers \
300 --module-source $MODULE_SRC/vboxsf \
301 --no-print-directory install 2>&1`; then
302 module_build_log "$myerr"
303 info "Look at $LOG to find out what went wrong"
304 return 0
305 fi
306 log "Building the graphics driver module."
307 if ! myerr=`$BUILDINTMP \
308 --use-module-symvers /tmp/vboxguest-Module.symvers \
309 --module-source $MODULE_SRC/vboxvideo \
310 --no-print-directory install 2>&1`; then
311 module_build_log "$myerr"
312 info "Look at $LOG to find out what went wrong"
313 fi
314 [ -d /etc/depmod.d ] || mkdir /etc/depmod.d
315 echo "override vboxguest * misc" > /etc/depmod.d/vboxvideo-upstream.conf
316 echo "override vboxsf * misc" >> /etc/depmod.d/vboxvideo-upstream.conf
317 echo "override vboxvideo * misc" >> /etc/depmod.d/vboxvideo-upstream.conf
318 update_initramfs "${KERN_VER}"
319 return 0
320}
321
322create_vbox_user()
323{
324 # This is the LSB version of useradd and should work on recent
325 # distributions
326 useradd -d /var/run/vboxadd -g 1 -r -s /bin/false vboxadd >/dev/null 2>&1 || true
327 # And for the others, we choose a UID ourselves
328 useradd -d /var/run/vboxadd -g 1 -u 501 -o -s /bin/false vboxadd >/dev/null 2>&1 || true
329
330}
331
332create_udev_rule()
333{
334 # Create udev description file
335 if [ -d /etc/udev/rules.d ]; then
336 udev_call=""
337 udev_app=`which udevadm 2> /dev/null`
338 if [ $? -eq 0 ]; then
339 udev_call="${udev_app} version 2> /dev/null"
340 else
341 udev_app=`which udevinfo 2> /dev/null`
342 if [ $? -eq 0 ]; then
343 udev_call="${udev_app} -V 2> /dev/null"
344 fi
345 fi
346 udev_fix="="
347 if [ "${udev_call}" != "" ]; then
348 udev_out=`${udev_call}`
349 udev_ver=`expr "$udev_out" : '[^0-9]*\([0-9]*\)'`
350 if [ "$udev_ver" = "" -o "$udev_ver" -lt 55 ]; then
351 udev_fix=""
352 fi
353 fi
354 ## @todo 60-vboxadd.rules -> 60-vboxguest.rules ?
355 echo "KERNEL=${udev_fix}\"vboxguest\", NAME=\"vboxguest\", OWNER=\"vboxadd\", MODE=\"0660\"" > /etc/udev/rules.d/60-vboxadd.rules
356 echo "KERNEL=${udev_fix}\"vboxuser\", NAME=\"vboxuser\", OWNER=\"vboxadd\", MODE=\"0666\"" >> /etc/udev/rules.d/60-vboxadd.rules
357 fi
358}
359
360create_module_rebuild_script()
361{
362 # And a post-installation script for rebuilding modules when a new kernel
363 # is installed.
364 mkdir -p /etc/kernel/postinst.d /etc/kernel/prerm.d
365 cat << EOF > /etc/kernel/postinst.d/vboxadd
366#!/bin/sh
367# This only works correctly on Debian derivatives - Red Hat calls it before
368# installing the right header files.
369/sbin/rcvboxadd quicksetup "\${1}"
370exit 0
371EOF
372 cat << EOF > /etc/kernel/prerm.d/vboxadd
373#!/bin/sh
374for i in ${OLDMODULES}; do rm -f /lib/modules/"\${1}"/misc/"\${i}".ko; done
375rmdir -p /lib/modules/"\$1"/misc 2>/dev/null || true
376exit 0
377EOF
378 chmod 0755 /etc/kernel/postinst.d/vboxadd /etc/kernel/prerm.d/vboxadd
379}
380
381shared_folder_setup()
382{
383 # Add a group "vboxsf" for Shared Folders access
384 # All users which want to access the auto-mounted Shared Folders have to
385 # be added to this group.
386 groupadd -r -f vboxsf >/dev/null 2>&1
387
388 # Put the mount.vboxsf mount helper in the right place.
389 ## @todo It would be nicer if the kernel module just parsed parameters
390 # itself instead of needing a separate binary to do that.
391 ln -sf "${INSTALL_DIR}/other/mount.vboxsf" /sbin
392 # SELinux security context for the mount helper.
393 if test -e /etc/selinux/config; then
394 # This is correct. semanage maps this to the real path, and it aborts
395 # with an error, telling you what you should have typed, if you specify
396 # the real path. The "chcon" is there as a back-up for old guests.
397 command -v semanage > /dev/null &&
398 semanage fcontext -a -t mount_exec_t "${INSTALL_DIR}/other/mount.vboxsf"
399 chcon -t mount_exec_t "${INSTALL_DIR}/other/mount.vboxsf" 2>/dev/null
400 fi
401}
402
403# setup_script
404setup()
405{
406 # chcon is needed on old Fedora/Redhat systems. No one remembers which.
407 test ! -e /etc/selinux/config ||
408 chcon -t bin_t "$BUILDINTMP" 2>/dev/null
409
410 if test -z "$INSTALL_NO_MODULE_BUILDS"; then
411 info "Building the VirtualBox Guest Additions kernel modules. This may take a while."
412 info "To build modules for other installed kernels, run"
413 info " /sbin/rcvboxadd quicksetup <version>"
414 info "or"
415 info " /sbin/rcvboxadd quicksetup all"
416 if test -d /lib/modules/"$TARGET_VER"/build; then
417 setup_modules "$TARGET_VER"
418 depmod
419 else
420 info "Kernel headers not found for target kernel $TARGET_VER. \
421Please install them and execute
422 /sbin/rcvboxadd setup"
423 fi
424 fi
425 create_vbox_user
426 create_udev_rule
427 test -n "${INSTALL_NO_MODULE_BUILDS}" || create_module_rebuild_script
428 shared_folder_setup
429 if running_vboxguest || running_vboxadd; then
430 info "Running kernel modules will not be replaced until the system is restarted"
431 fi
432
433 # Put the X.Org driver in place. This is harmless if it is not needed.
434 # Also set up the OpenGL library.
435 myerr=`"${INSTALL_DIR}/init/vboxadd-x11" setup 2>&1`
436 test -z "${myerr}" || log "${myerr}"
437
438 return 0
439}
440
441# cleanup_script
442cleanup()
443{
444 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
445 # Delete old versions of VBox modules.
446 cleanup_modules
447 depmod
448
449 # Remove old module sources
450 for i in $OLDMODULES; do
451 rm -rf /usr/src/$i-*
452 done
453 fi
454
455 # Clean-up X11-related bits
456 "${INSTALL_DIR}/init/vboxadd-x11" cleanup
457
458 # Remove other files
459 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
460 rm -f /etc/kernel/postinst.d/vboxadd /etc/kernel/prerm.d/vboxadd
461 rmdir -p /etc/kernel/postinst.d /etc/kernel/prerm.d 2>/dev/null || true
462 fi
463 rm -f /sbin/mount.vboxsf 2>/dev/null
464 rm -f /etc/udev/rules.d/60-vboxadd.rules 2>/dev/null
465}
466
467start()
468{
469 begin "Starting."
470 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
471 # We want to build modules for newly installed kernels on shutdown, so
472 # mark the ones already present. These will be ignored on shutdown.
473 rm -f "$SKIPFILE_BASE"-*
474 for setupi in /lib/modules/*; do
475 KERN_VER="${setupi##*/}"
476 # For a full setup, mark kernels we do not want to build.
477 touch "$SKIPFILE_BASE"-"$KERN_VER"
478 done
479 fi
480 setup
481 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
482 test -d /sys &&
483 ps -A -o comm | grep -q '/*udevd$' 2>/dev/null ||
484 no_udev=1
485 running_vboxguest || {
486 rm -f $dev || {
487 fail "Cannot remove $dev"
488 }
489 rm -f $userdev || {
490 fail "Cannot remove $userdev"
491 }
492 $MODPROBE vboxguest >/dev/null 2>&1 ||
493 fail "modprobe vboxguest failed"
494 case "$no_udev" in 1)
495 sleep .5;;
496 esac
497 $MODPROBE vboxsf > /dev/null 2>&1 ||
498 info "modprobe vboxsf failed"
499 }
500 case "$no_udev" in 1)
501 do_vboxguest_non_udev;;
502 esac
503 fi # INSTALL_NO_MODULE_BUILDS
504
505 return 0
506}
507
508stop()
509{
510 begin "Stopping."
511 if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
512 # We want to build modules for newly installed kernels on shutdown, so
513 # check which we marked at start-up.
514 for setupi in /lib/modules/*; do
515 KERN_VER="${setupi##*/}"
516 # For a full setup, mark kernels we do not want to build.
517 test -f "$SKIPFILE_BASE"-"$KERN_VER" || setup_modules "$KERN_VER"
518 done
519 fi
520 if test -r /etc/ld.so.conf.d/00vboxvideo.conf; then
521 rm /etc/ld.so.conf.d/00vboxvideo.conf
522 ldconfig
523 fi
524 if ! umount -a -t vboxsf 2>/dev/null; then
525 fail "Cannot unmount vboxsf folders"
526 fi
527 test -n "${INSTALL_NO_MODULE_BUILDS}" ||
528 info "You may need to restart your guest system to finish removing guest drivers."
529 return 0
530}
531
532dmnstatus()
533{
534 if running_vboxguest; then
535 echo "The VirtualBox Additions are currently running."
536 else
537 echo "The VirtualBox Additions are not currently running."
538 fi
539}
540
541for i; do
542 case "$i" in quiet) QUIET=yes;; esac
543done
544case "$1" in
545# Does setup without clean-up first and marks all kernels currently found on the
546# system so that we can see later if any were added.
547start)
548 start
549 ;;
550# Tries to build kernel modules for kernels added since start. Tries to unmount
551# shared folders. Uninstalls our Chromium 3D libraries since we can't always do
552# this fast enough at start time if we discover we do not want to use them.
553stop)
554 stop
555 ;;
556restart)
557 restart
558 ;;
559# Setup does a clean-up (see below) and re-does all Additions-specific
560# configuration of the guest system, including building kernel modules for the
561# current kernel.
562setup)
563 cleanup && start
564 ;;
565# Builds kernel modules for the specified kernels if they are not already built.
566quicksetup)
567 if test x"$2" = xall; then
568 for topi in /lib/modules/*; do
569 KERN_VER="${topi%/misc}"
570 KERN_VER="${KERN_VER#/lib/modules/}"
571 setup_modules "$KERN_VER"
572 done
573 elif test -n "$2"; then
574 setup_modules "$2"
575 else
576 setup_modules "$TARGET_VER"
577 fi
578 ;;
579# Clean-up removes all Additions-specific configuration of the guest system,
580# including all kernel modules.
581cleanup)
582 cleanup
583 ;;
584status)
585 dmnstatus
586 ;;
587*)
588 echo "Usage: $0 {start|stop|restart|status|setup|quicksetup|cleanup} [quiet]"
589 exit 1
590esac
591
592exit
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use