1 | #! /bin/sh
|
---|
2 | # $Id: vboxadd.sh 107963 2025-01-28 12:55:41Z vboxsync $
|
---|
3 | ## @file
|
---|
4 | # Linux Additions kernel module init script ($Revision: 107963 $)
|
---|
5 | #
|
---|
6 |
|
---|
7 | #
|
---|
8 | # Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
9 | #
|
---|
10 | # This file is part of VirtualBox base platform packages, as
|
---|
11 | # available from https://www.virtualbox.org.
|
---|
12 | #
|
---|
13 | # This program is free software; you can redistribute it and/or
|
---|
14 | # modify it under the terms of the GNU General Public License
|
---|
15 | # as published by the Free Software Foundation, in version 3 of the
|
---|
16 | # License.
|
---|
17 | #
|
---|
18 | # This program is distributed in the hope that it will be useful, but
|
---|
19 | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
21 | # General Public License for more details.
|
---|
22 | #
|
---|
23 | # You should have received a copy of the GNU General Public License
|
---|
24 | # along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
25 | #
|
---|
26 | # SPDX-License-Identifier: GPL-3.0-only
|
---|
27 | #
|
---|
28 |
|
---|
29 | # X-Start-Before is a Debian Addition which we use when converting to
|
---|
30 | # a systemd unit. X-Service-Type is our own invention, also for systemd.
|
---|
31 |
|
---|
32 | # chkconfig: 345 10 90
|
---|
33 | # description: VirtualBox Linux Additions kernel modules
|
---|
34 | #
|
---|
35 | ### BEGIN INIT INFO
|
---|
36 | # Provides: vboxadd
|
---|
37 | # Required-Start:
|
---|
38 | # Required-Stop:
|
---|
39 | # Default-Start: 2 3 4 5
|
---|
40 | # Default-Stop: 0 1 6
|
---|
41 | # X-Start-Before: display-manager
|
---|
42 | # X-Service-Type: oneshot
|
---|
43 | # Description: VirtualBox Linux Additions kernel modules
|
---|
44 | ### END INIT INFO
|
---|
45 |
|
---|
46 | ## @todo This file duplicates a lot of script with vboxdrv.sh. When making
|
---|
47 | # changes please try to reduce differences between the two wherever possible.
|
---|
48 |
|
---|
49 | # Testing:
|
---|
50 | # * Should fail if the configuration file is missing or missing INSTALL_DIR or
|
---|
51 | # INSTALL_VER entries.
|
---|
52 | # * vboxadd, vboxsf and vboxdrmipc user groups should be created if they do not exist - test
|
---|
53 | # by removing them before installing.
|
---|
54 | # * Shared folders can be mounted and auto-mounts accessible to vboxsf group,
|
---|
55 | # including on recent Fedoras with SELinux.
|
---|
56 | # * Setting INSTALL_NO_MODULE_BUILDS inhibits modules and module automatic
|
---|
57 | # rebuild script creation; otherwise modules, user, group, rebuild script,
|
---|
58 | # udev rule and shared folder mount helper should be created/set up.
|
---|
59 | # * Setting INSTALL_NO_MODULE_BUILDS inhibits module load and unload on start
|
---|
60 | # and stop.
|
---|
61 | # * Uninstalling the Additions and re-installing them does not trigger warnings.
|
---|
62 |
|
---|
63 | export LC_ALL=C
|
---|
64 | PATH=$PATH:/bin:/sbin:/usr/sbin
|
---|
65 | PACKAGE=VBoxGuestAdditions
|
---|
66 | MODPROBE=/sbin/modprobe
|
---|
67 | OLDMODULES="vboxguest vboxadd vboxsf vboxvfs vboxvideo"
|
---|
68 | SERVICE="VirtualBox Guest Additions"
|
---|
69 | VBOXSERVICE_PIDFILE="/var/run/vboxadd-service.sh"
|
---|
70 | ## systemd logs information about service status, otherwise do that ourselves.
|
---|
71 | QUIET=
|
---|
72 | test -z "${TARGET_VER}" && TARGET_VER=`uname -r`
|
---|
73 |
|
---|
74 | export VBOX_KBUILD_TYPE
|
---|
75 | export USERNAME
|
---|
76 |
|
---|
77 | setup_log()
|
---|
78 | {
|
---|
79 | test -z "${LOG}" || return 0
|
---|
80 | # Rotate log files
|
---|
81 | LOG="/var/log/vboxadd-setup.log"
|
---|
82 | mv -f "${LOG}.3" "${LOG}.4" 2>/dev/null
|
---|
83 | mv -f "${LOG}.2" "${LOG}.3" 2>/dev/null
|
---|
84 | mv -f "${LOG}.1" "${LOG}.2" 2>/dev/null
|
---|
85 | mv -f "${LOG}" "${LOG}.1" 2>/dev/null
|
---|
86 | }
|
---|
87 |
|
---|
88 | if $MODPROBE -c 2>/dev/null | grep -q '^allow_unsupported_modules *0'; then
|
---|
89 | MODPROBE="$MODPROBE --allow-unsupported-modules"
|
---|
90 | fi
|
---|
91 |
|
---|
92 | # Preamble for Gentoo
|
---|
93 | if [ "`which $0`" = "/sbin/rc" ]; then
|
---|
94 | shift
|
---|
95 | fi
|
---|
96 |
|
---|
97 | begin()
|
---|
98 | {
|
---|
99 | test -n "${QUIET}" || echo "${SERVICE}: ${1}"
|
---|
100 | }
|
---|
101 |
|
---|
102 | info()
|
---|
103 | {
|
---|
104 | if test -z "${QUIET}"; then
|
---|
105 | echo "${SERVICE}: $1" | fold -s
|
---|
106 | else
|
---|
107 | echo "$1" | fold -s
|
---|
108 | fi
|
---|
109 | }
|
---|
110 |
|
---|
111 | # When script is running as non-root, it does not have access to log
|
---|
112 | # files in /var. In this case, lets print error message to stdout and
|
---|
113 | # exit with bad status.
|
---|
114 | early_fail()
|
---|
115 | {
|
---|
116 | echo "$1" >&2
|
---|
117 | exit 1
|
---|
118 | }
|
---|
119 |
|
---|
120 | fail()
|
---|
121 | {
|
---|
122 | log "${1}"
|
---|
123 | echo "${SERVICE}: $1" >&2
|
---|
124 | echo "The log file $LOG may contain further information." >&2
|
---|
125 | exit 1
|
---|
126 | }
|
---|
127 |
|
---|
128 | log()
|
---|
129 | {
|
---|
130 | setup_log
|
---|
131 | echo "${1}" >> "${LOG}"
|
---|
132 | }
|
---|
133 |
|
---|
134 | module_build_log()
|
---|
135 | {
|
---|
136 | log "Error building the module. Build output follows."
|
---|
137 | echo ""
|
---|
138 | echo "${1}" >> "${LOG}"
|
---|
139 | }
|
---|
140 |
|
---|
141 | dev=/dev/vboxguest
|
---|
142 | userdev=/dev/vboxuser
|
---|
143 | config=/var/lib/VBoxGuestAdditions/config
|
---|
144 | user_config=/etc/virtualbox-guest-additions.conf
|
---|
145 | owner=vboxadd
|
---|
146 | group=1
|
---|
147 |
|
---|
148 | # Include custom user configuration file.
|
---|
149 | [ -r "$user_config" ] && . "$user_config"
|
---|
150 |
|
---|
151 | if test -r $config; then
|
---|
152 | . $config
|
---|
153 | else
|
---|
154 | fail "Configuration file $config not found"
|
---|
155 | fi
|
---|
156 | test -n "$INSTALL_DIR" -a -n "$INSTALL_VER" ||
|
---|
157 | fail "Configuration file $config not complete"
|
---|
158 | MODULE_SRC="$INSTALL_DIR/src/vboxguest-$INSTALL_VER"
|
---|
159 | BUILDINTMP="$MODULE_SRC/build_in_tmp"
|
---|
160 |
|
---|
161 | # Path to VBoxService control script.
|
---|
162 | VBOX_SERVICE_SCRIPT="/sbin/rcvboxadd-service"
|
---|
163 |
|
---|
164 | # Attempt to detect VirtualBox Guest Additions version and revision information.
|
---|
165 | VBOXCONTROL="${INSTALL_DIR}/bin/VBoxControl"
|
---|
166 | VBOX_VERSION="`"$VBOXCONTROL" --version | cut -d r -f1`"
|
---|
167 | [ -n "$VBOX_VERSION" ] || VBOX_VERSION='unknown'
|
---|
168 | VBOX_REVISION="r`"$VBOXCONTROL" --version | cut -d r -f2`"
|
---|
169 | [ "$VBOX_REVISION" != "r" ] || VBOX_REVISION='unknown'
|
---|
170 |
|
---|
171 | # Returns if a specific module is running or not.
|
---|
172 | #
|
---|
173 | # Input $1: Module name to check running status for.
|
---|
174 | #
|
---|
175 | # Returns true if the module is running, false if not.
|
---|
176 | running_module()
|
---|
177 | {
|
---|
178 | mod="$1"
|
---|
179 | [ -d "/sys/module/$mod" ]
|
---|
180 | }
|
---|
181 |
|
---|
182 | # Returns the version string of a currently running kernel module.
|
---|
183 | #
|
---|
184 | # Input $1: Module name to check.
|
---|
185 | #
|
---|
186 | # Returns the module version string if found, or none if not found.
|
---|
187 | running_module_version()
|
---|
188 | {
|
---|
189 | mod="$1"
|
---|
190 | version_string_path="/sys/module/$mod/version"
|
---|
191 |
|
---|
192 | [ -n "$mod" ] || return
|
---|
193 | if [ -r "$version_string_path" ]; then
|
---|
194 | cat "$version_string_path"
|
---|
195 | else
|
---|
196 | echo "unknown"
|
---|
197 | fi
|
---|
198 | }
|
---|
199 |
|
---|
200 | # Checks if a loaded kernel module version matches to the currently installed Guest Additions version and revision.
|
---|
201 | #
|
---|
202 | # Input $1: Module name to check.
|
---|
203 | #
|
---|
204 | # Returns "1" if the module matches the installed Guest Additions, or none if not.
|
---|
205 | check_running_module_version()
|
---|
206 | {
|
---|
207 | mod=$1
|
---|
208 | expected="$VBOX_VERSION $VBOX_REVISION"
|
---|
209 |
|
---|
210 | [ -n "$mod" ] || return
|
---|
211 | [ -n "$expected" ] || return
|
---|
212 |
|
---|
213 | [ "$expected" = "$(running_module_version "$mod")" ] || return
|
---|
214 | }
|
---|
215 |
|
---|
216 | do_vboxguest_non_udev()
|
---|
217 | {
|
---|
218 | if [ ! -c $dev ]; then
|
---|
219 | maj=`sed -n 's;\([0-9]\+\) vboxguest;\1;p' /proc/devices`
|
---|
220 | if [ ! -z "$maj" ]; then
|
---|
221 | min=0
|
---|
222 | else
|
---|
223 | min=`sed -n 's;\([0-9]\+\) vboxguest;\1;p' /proc/misc`
|
---|
224 | if [ ! -z "$min" ]; then
|
---|
225 | maj=10
|
---|
226 | fi
|
---|
227 | fi
|
---|
228 | test -n "$maj" || {
|
---|
229 | rmmod vboxguest 2>/dev/null
|
---|
230 | fail "Cannot locate the VirtualBox device"
|
---|
231 | }
|
---|
232 |
|
---|
233 | mknod -m 0664 $dev c $maj $min || {
|
---|
234 | rmmod vboxguest 2>/dev/null
|
---|
235 | fail "Cannot create device $dev with major $maj and minor $min"
|
---|
236 | }
|
---|
237 | fi
|
---|
238 | chown $owner:$group $dev 2>/dev/null || {
|
---|
239 | rm -f $dev 2>/dev/null
|
---|
240 | rm -f $userdev 2>/dev/null
|
---|
241 | rmmod vboxguest 2>/dev/null
|
---|
242 | fail "Cannot change owner $owner:$group for device $dev"
|
---|
243 | }
|
---|
244 |
|
---|
245 | if [ ! -c $userdev ]; then
|
---|
246 | maj=10
|
---|
247 | min=`sed -n 's;\([0-9]\+\) vboxuser;\1;p' /proc/misc`
|
---|
248 | if [ ! -z "$min" ]; then
|
---|
249 | mknod -m 0666 $userdev c $maj $min || {
|
---|
250 | rm -f $dev 2>/dev/null
|
---|
251 | rmmod vboxguest 2>/dev/null
|
---|
252 | fail "Cannot create device $userdev with major $maj and minor $min"
|
---|
253 | }
|
---|
254 | chown $owner:$group $userdev 2>/dev/null || {
|
---|
255 | rm -f $dev 2>/dev/null
|
---|
256 | rm -f $userdev 2>/dev/null
|
---|
257 | rmmod vboxguest 2>/dev/null
|
---|
258 | fail "Cannot change owner $owner:$group for device $userdev"
|
---|
259 | }
|
---|
260 | fi
|
---|
261 | fi
|
---|
262 | }
|
---|
263 |
|
---|
264 | restart()
|
---|
265 | {
|
---|
266 | stop && start
|
---|
267 | return 0
|
---|
268 | }
|
---|
269 |
|
---|
270 | ## Updates the initramfs. Debian and Ubuntu put the graphics driver in, and
|
---|
271 | # need the touch(1) command below. Everyone else that I checked just need
|
---|
272 | # the right module alias file from depmod(1) and only use the initramfs to
|
---|
273 | # load the root filesystem, not the boot splash. update-initramfs works
|
---|
274 | # for the first two and dracut for every one else I checked. We are only
|
---|
275 | # interested in distributions recent enough to use the KMS vboxvideo driver.
|
---|
276 | update_initramfs()
|
---|
277 | {
|
---|
278 | ## kernel version to update for.
|
---|
279 | version="${1}"
|
---|
280 | depmod "${version}"
|
---|
281 | rm -f "/lib/modules/${version}/initrd/vboxvideo"
|
---|
282 | test ! -d "/lib/modules/${version}/initrd" ||
|
---|
283 | test ! -f "/lib/modules/${version}/misc/vboxvideo.ko" ||
|
---|
284 | touch "/lib/modules/${version}/initrd/vboxvideo"
|
---|
285 |
|
---|
286 | # Systems without systemd-inhibit probably don't need their initramfs
|
---|
287 | # rebuild here anyway.
|
---|
288 | type systemd-inhibit >/dev/null 2>&1 || return
|
---|
289 | if type dracut >/dev/null 2>&1; then
|
---|
290 | systemd-inhibit --why="Installing VirtualBox Guest Additions" \
|
---|
291 | dracut -f --kver "${version}"
|
---|
292 | elif type update-initramfs >/dev/null 2>&1; then
|
---|
293 | systemd-inhibit --why="Installing VirtualBox Guest Additions" \
|
---|
294 | update-initramfs -u -k "${version}"
|
---|
295 | fi
|
---|
296 | }
|
---|
297 |
|
---|
298 | # Removes any existing VirtualBox guest kernel modules from the disk, but not
|
---|
299 | # from the kernel as they may still be in use
|
---|
300 | cleanup_modules()
|
---|
301 | {
|
---|
302 | # Needed for Ubuntu and Debian, see update_initramfs
|
---|
303 | rm -f /lib/modules/*/initrd/vboxvideo
|
---|
304 | for i in /lib/modules/*/misc; do
|
---|
305 | KERN_VER="${i%/misc}"
|
---|
306 | KERN_VER="${KERN_VER#/lib/modules/}"
|
---|
307 | unset do_update
|
---|
308 | for j in ${OLDMODULES}; do
|
---|
309 | for mod_ext in ko ko.gz ko.xz ko.zst; do
|
---|
310 | test -f "${i}/${j}.${mod_ext}" && do_update=1 && rm -f "${i}/${j}.${mod_ext}"
|
---|
311 | done
|
---|
312 | done
|
---|
313 | test -z "$do_update" || update_initramfs "$KERN_VER"
|
---|
314 | # Remove empty /lib/modules folders which may have been kept around
|
---|
315 | rmdir -p "${i}" 2>/dev/null || true
|
---|
316 | unset keep
|
---|
317 | for j in /lib/modules/"${KERN_VER}"/*; do
|
---|
318 | name="${j##*/}"
|
---|
319 | test -d "${name}" || test "${name%%.*}" != modules && keep=1
|
---|
320 | done
|
---|
321 | if test -z "${keep}"; then
|
---|
322 | rm -rf /lib/modules/"${KERN_VER}"
|
---|
323 | rm -f /boot/initrd.img-"${KERN_VER}"
|
---|
324 | fi
|
---|
325 | done
|
---|
326 | for i in ${OLDMODULES}; do
|
---|
327 | # We no longer support DKMS, remove any leftovers.
|
---|
328 | rm -rf "/var/lib/dkms/${i}"*
|
---|
329 | done
|
---|
330 | rm -f /etc/depmod.d/vboxvideo-upstream.conf
|
---|
331 | }
|
---|
332 |
|
---|
333 | # Secure boot state.
|
---|
334 | case "`mokutil --sb-state 2>/dev/null`" in
|
---|
335 | *"disabled in shim"*) unset HAVE_SEC_BOOT;;
|
---|
336 | *"SecureBoot enabled"*) HAVE_SEC_BOOT=true;;
|
---|
337 | *) unset HAVE_SEC_BOOT;;
|
---|
338 | esac
|
---|
339 | # So far we can only sign modules on Ubuntu and on Debian 10 and later.
|
---|
340 | DEB_PUB_KEY=/var/lib/shim-signed/mok/MOK.der
|
---|
341 | DEB_PRIV_KEY=/var/lib/shim-signed/mok/MOK.priv
|
---|
342 | # Check if key already enrolled.
|
---|
343 | unset HAVE_DEB_KEY
|
---|
344 | case "`mokutil --test-key "$DEB_PUB_KEY" 2>/dev/null`" in
|
---|
345 | *"is already"*) DEB_KEY_ENROLLED=true;;
|
---|
346 | *) unset DEB_KEY_ENROLLED;;
|
---|
347 | esac
|
---|
348 |
|
---|
349 | # Checks if update-secureboot-policy tool supports required commandline options.
|
---|
350 | update_secureboot_policy_supports()
|
---|
351 | {
|
---|
352 | opt_name="$1"
|
---|
353 | [ -n "$opt_name" ] || return
|
---|
354 |
|
---|
355 | [ -z "$(update-secureboot-policy --help 2>&1 | grep "$opt_name")" ] && return
|
---|
356 | echo "1"
|
---|
357 | }
|
---|
358 |
|
---|
359 | HAVE_UPDATE_SECUREBOOT_POLICY_TOOL=
|
---|
360 | if type update-secureboot-policy >/dev/null 2>&1; then
|
---|
361 | [ "$(update_secureboot_policy_supports new-key)" = "1" -a "$(update_secureboot_policy_supports enroll-key)" = "1" ] && \
|
---|
362 | HAVE_UPDATE_SECUREBOOT_POLICY_TOOL=true
|
---|
363 | fi
|
---|
364 |
|
---|
365 | # Reads kernel configuration option.
|
---|
366 | kernel_get_config_opt()
|
---|
367 | {
|
---|
368 | kern_ver="$1"
|
---|
369 | opt_name="$2"
|
---|
370 |
|
---|
371 | [ -n "$kern_ver" ] || return
|
---|
372 | [ -n "$opt_name" ] || return
|
---|
373 |
|
---|
374 | # Check if there is a kernel tool which can extract config option.
|
---|
375 | if test -x /lib/modules/"$kern_ver"/build/scripts/config; then
|
---|
376 | /lib/modules/"$kern_ver"/build/scripts/config \
|
---|
377 | --file /lib/modules/"$kern_ver"/build/.config \
|
---|
378 | --state "$opt_name" 2>/dev/null
|
---|
379 | elif test -f /lib/modules/"$kern_ver"/build/.config; then
|
---|
380 | # Extract config option manually.
|
---|
381 | grep "$opt_name=" /lib/modules/"$kern_ver"/build/.config | sed -e "s/^$opt_name=//" -e "s/\"//g"
|
---|
382 | fi
|
---|
383 | }
|
---|
384 |
|
---|
385 | # Reads CONFIG_MODULE_SIG_HASH from kernel config.
|
---|
386 | kernel_module_sig_hash()
|
---|
387 | {
|
---|
388 | kern_ver="$1"
|
---|
389 | [ -n "$kern_ver" ] || return
|
---|
390 |
|
---|
391 | kernel_get_config_opt "$kern_ver" "CONFIG_MODULE_SIG_HASH"
|
---|
392 | }
|
---|
393 |
|
---|
394 | # Returns "1" if kernel module signature hash algorithm
|
---|
395 | # is supported by us. Or empty string otherwise.
|
---|
396 | module_sig_hash_supported()
|
---|
397 | {
|
---|
398 | sig_hashalgo="$1"
|
---|
399 | [ -n "$sig_hashalgo" ] || return
|
---|
400 |
|
---|
401 | # Go through supported list.
|
---|
402 | [ "$sig_hashalgo" = "sha1" \
|
---|
403 | -o "$sig_hashalgo" = "sha224" \
|
---|
404 | -o "$sig_hashalgo" = "sha256" \
|
---|
405 | -o "$sig_hashalgo" = "sha384" \
|
---|
406 | -o "$sig_hashalgo" = "sha512" ] || return
|
---|
407 |
|
---|
408 | echo "1"
|
---|
409 | }
|
---|
410 |
|
---|
411 | # Check if kernel configuration requires modules signature.
|
---|
412 | kernel_requires_module_signature()
|
---|
413 | {
|
---|
414 | kern_ver="$1"
|
---|
415 | vbox_sys_lockdown_path="/sys/kernel/security/lockdown"
|
---|
416 |
|
---|
417 | [ -n "$kern_ver" ] || return
|
---|
418 |
|
---|
419 | requires=""
|
---|
420 | # We consider that if kernel is running in the following configurations,
|
---|
421 | # it will require modules to be signed.
|
---|
422 | if [ "$(kernel_get_config_opt "$kern_ver" "CONFIG_MODULE_SIG")" = "y" ]; then
|
---|
423 |
|
---|
424 | # Modules signature verification is hardcoded in kernel config.
|
---|
425 | [ "$(kernel_get_config_opt "$kern_ver" "CONFIG_MODULE_SIG_FORCE")" = "y" ] && requires="1"
|
---|
426 |
|
---|
427 | # Unsigned modules loading is restricted by "lockdown" feature in runtime.
|
---|
428 | if [ "$(kernel_get_config_opt "$kern_ver" "CONFIG_LOCK_DOWN_KERNEL")" = "y" \
|
---|
429 | -o "$(kernel_get_config_opt "$kern_ver" "CONFIG_SECURITY_LOCKDOWN_LSM")" = "y" \
|
---|
430 | -o "$(kernel_get_config_opt "$kern_ver" "CONFIG_SECURITY_LOCKDOWN_LSM_EARLY")" = "y" ]; then
|
---|
431 |
|
---|
432 | # Once lockdown level is set to something different from "none" (e.g., "integrity"
|
---|
433 | # or "confidentiality"), kernel will reject unsigned modules loading.
|
---|
434 | if [ -r "$vbox_sys_lockdown_path" ]; then
|
---|
435 | [ -n "$(cat "$vbox_sys_lockdown_path" | grep "\[integrity\]")" ] && requires="1"
|
---|
436 | [ -n "$(cat "$vbox_sys_lockdown_path" | grep "\[confidentiality\]")" ] && requires="1"
|
---|
437 | fi
|
---|
438 |
|
---|
439 | # This configuration is used by a number of modern Linux distributions and restricts
|
---|
440 | # unsigned modules loading when Secure Boot mode is enabled.
|
---|
441 | [ "$(kernel_get_config_opt "$kern_ver" "CONFIG_LOCK_DOWN_IN_EFI_SECURE_BOOT")" = "y" -a -n "$HAVE_SEC_BOOT" ] && requires="1"
|
---|
442 | fi
|
---|
443 | fi
|
---|
444 |
|
---|
445 | [ -n "$requires" ] && echo "1"
|
---|
446 | }
|
---|
447 |
|
---|
448 | sign_modules()
|
---|
449 | {
|
---|
450 | KERN_VER="$1"
|
---|
451 | test -n "$KERN_VER" || return 1
|
---|
452 |
|
---|
453 | # Make list of mudules to sign.
|
---|
454 | MODULE_LIST="vboxguest vboxsf"
|
---|
455 | # vboxvideo might not present on for older kernels.
|
---|
456 | [ -f "/lib/modules/"$KERN_VER"/misc/vboxvideo.ko" ] && MODULE_LIST="$MODULE_LIST vboxvideo"
|
---|
457 |
|
---|
458 | # Sign kernel modules if kernel configuration requires it.
|
---|
459 | if test "$(kernel_requires_module_signature $KERN_VER)" = "1"; then
|
---|
460 | begin "Signing VirtualBox Guest Additions kernel modules"
|
---|
461 |
|
---|
462 | # Generate new signing key if needed.
|
---|
463 | [ -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL" ] && SHIM_NOTRIGGER=y update-secureboot-policy --new-key
|
---|
464 |
|
---|
465 | # Check if signing keys are in place.
|
---|
466 | if test ! -f "$DEB_PUB_KEY" || ! test -f "$DEB_PRIV_KEY"; then
|
---|
467 | # update-secureboot-policy tool present in the system, but keys were not generated.
|
---|
468 | [ -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL" ] && info "
|
---|
469 |
|
---|
470 | update-secureboot-policy tool does not generate signing keys
|
---|
471 | in your distribution, see below on how to generate them manually."
|
---|
472 | # update-secureboot-policy not present in the system, recommend generate keys manually.
|
---|
473 | fail "
|
---|
474 |
|
---|
475 | System is running in Secure Boot mode, however your distribution
|
---|
476 | does not provide tools for automatic generation of keys needed for
|
---|
477 | modules signing. Please consider to generate and enroll them manually:
|
---|
478 |
|
---|
479 | sudo mkdir -p /var/lib/shim-signed/mok
|
---|
480 | sudo openssl req -nodes -new -x509 -newkey rsa:2048 -outform DER -addext \"extendedKeyUsage=codeSigning\" -keyout $DEB_PRIV_KEY -out $DEB_PUB_KEY
|
---|
481 | sudo mokutil --import $DEB_PUB_KEY
|
---|
482 | sudo reboot
|
---|
483 |
|
---|
484 | Restart \"rcvboxadd setup\" after system is rebooted.
|
---|
485 | "
|
---|
486 | fi
|
---|
487 |
|
---|
488 | # Get kernel signature hash algorithm from kernel config and validate it.
|
---|
489 | sig_hashalgo=$(kernel_module_sig_hash "$KERN_VER")
|
---|
490 | [ "$(module_sig_hash_supported $sig_hashalgo)" = "1" ] \
|
---|
491 | || fail "Unsupported kernel signature hash algorithm $sig_hashalgo"
|
---|
492 |
|
---|
493 | # Sign modules.
|
---|
494 | for i in $MODULE_LIST; do
|
---|
495 |
|
---|
496 | # Try to find a tool for modules signing.
|
---|
497 | SIGN_TOOL=$(which kmodsign 2>/dev/null)
|
---|
498 | # Attempt to use in-kernel signing tool if kmodsign not found.
|
---|
499 | if test -z "$SIGN_TOOL"; then
|
---|
500 | if test -x "/lib/modules/$KERN_VER/build/scripts/sign-file"; then
|
---|
501 | SIGN_TOOL="/lib/modules/$KERN_VER/build/scripts/sign-file"
|
---|
502 | fi
|
---|
503 | fi
|
---|
504 |
|
---|
505 | # Check if signing tool is available.
|
---|
506 | [ -n "$SIGN_TOOL" ] || fail "Unable to find signing tool"
|
---|
507 |
|
---|
508 | "$SIGN_TOOL" "$sig_hashalgo" "$DEB_PRIV_KEY" "$DEB_PUB_KEY" \
|
---|
509 | /lib/modules/"$KERN_VER"/misc/"$i".ko || fail "Unable to sign $i.ko"
|
---|
510 | done
|
---|
511 | # Enroll signing key if needed.
|
---|
512 | if test -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL"; then
|
---|
513 | # update-secureboot-policy "expects" DKMS modules.
|
---|
514 | # Work around this and talk to the authors as soon
|
---|
515 | # as possible to fix it.
|
---|
516 | mkdir -p /var/lib/dkms/vbox-temp
|
---|
517 | update-secureboot-policy --enroll-key 2>/dev/null ||
|
---|
518 | fail "Failed to enroll secure boot key."
|
---|
519 | rmdir -p /var/lib/dkms/vbox-temp 2>/dev/null
|
---|
520 |
|
---|
521 | # Indicate that key has been enrolled and reboot is needed.
|
---|
522 | HAVE_DEB_KEY=true
|
---|
523 | fi
|
---|
524 | fi
|
---|
525 | }
|
---|
526 |
|
---|
527 | # Build and install the VirtualBox guest kernel modules
|
---|
528 | setup_modules()
|
---|
529 | {
|
---|
530 | KERN_VER="$1"
|
---|
531 | test -n "$KERN_VER" || return 1
|
---|
532 | # Match (at least): vboxguest.o; vboxguest.ko; vboxguest.ko.xz
|
---|
533 | set /lib/modules/"$KERN_VER"/misc/vboxguest.*o*
|
---|
534 | #test ! -f "$1" || return 0
|
---|
535 | test -d /lib/modules/"$KERN_VER"/build || return 0
|
---|
536 | export KERN_VER
|
---|
537 | info "Building the modules for kernel $KERN_VER."
|
---|
538 |
|
---|
539 | # Prepend PATH for building UEK7 on OL8 distribution.
|
---|
540 | case "$KERN_VER" in
|
---|
541 | 5.15.0-*.el8uek*) PATH="/opt/rh/gcc-toolset-11/root/usr/bin:$PATH";;
|
---|
542 | esac
|
---|
543 |
|
---|
544 | # Detect if kernel was built with clang.
|
---|
545 | unset LLVM
|
---|
546 | vbox_cc_is_clang=$(kernel_get_config_opt "$KERN_VER" "CONFIG_CC_IS_CLANG")
|
---|
547 | if test "${vbox_cc_is_clang}" = "y"; then
|
---|
548 | info "Using clang compiler."
|
---|
549 | export LLVM=1
|
---|
550 | fi
|
---|
551 |
|
---|
552 | log "Building the main Guest Additions $INSTALL_VER module for kernel $KERN_VER."
|
---|
553 | if ! myerr=`$BUILDINTMP \
|
---|
554 | --save-module-symvers /tmp/vboxguest-Module.symvers \
|
---|
555 | --module-source $MODULE_SRC/vboxguest \
|
---|
556 | --no-print-directory install 2>&1`; then
|
---|
557 | # If check_module_dependencies.sh fails it prints a message itself.
|
---|
558 | module_build_log "$myerr"
|
---|
559 | "${INSTALL_DIR}"/other/check_module_dependencies.sh 2>&1 &&
|
---|
560 | info "Look at $LOG to find out what went wrong"
|
---|
561 | return 0
|
---|
562 | fi
|
---|
563 | log "Building the shared folder support module."
|
---|
564 | if ! myerr=`$BUILDINTMP \
|
---|
565 | --use-module-symvers /tmp/vboxguest-Module.symvers \
|
---|
566 | --module-source $MODULE_SRC/vboxsf \
|
---|
567 | --no-print-directory install 2>&1`; then
|
---|
568 | module_build_log "$myerr"
|
---|
569 | info "Look at $LOG to find out what went wrong"
|
---|
570 | return 0
|
---|
571 | fi
|
---|
572 | log "Building the graphics driver module."
|
---|
573 | if ! myerr=`$BUILDINTMP \
|
---|
574 | --use-module-symvers /tmp/vboxguest-Module.symvers \
|
---|
575 | --module-source $MODULE_SRC/vboxvideo \
|
---|
576 | --no-print-directory install 2>&1`; then
|
---|
577 | module_build_log "$myerr"
|
---|
578 | info "Look at $LOG to find out what went wrong"
|
---|
579 | fi
|
---|
580 | [ -d /etc/depmod.d ] || mkdir /etc/depmod.d
|
---|
581 | echo "override vboxguest * misc" > /etc/depmod.d/vboxvideo-upstream.conf
|
---|
582 | echo "override vboxsf * misc" >> /etc/depmod.d/vboxvideo-upstream.conf
|
---|
583 | echo "override vboxvideo * misc" >> /etc/depmod.d/vboxvideo-upstream.conf
|
---|
584 |
|
---|
585 | sign_modules "${KERN_VER}"
|
---|
586 |
|
---|
587 | update_initramfs "${KERN_VER}"
|
---|
588 |
|
---|
589 | return 0
|
---|
590 | }
|
---|
591 |
|
---|
592 | create_vbox_user()
|
---|
593 | {
|
---|
594 | # This is the LSB version of useradd and should work on recent
|
---|
595 | # distributions
|
---|
596 | useradd -d /var/run/vboxadd -g 1 -r -s /bin/false vboxadd >/dev/null 2>&1 || true
|
---|
597 | # And for the others, we choose a UID ourselves
|
---|
598 | useradd -d /var/run/vboxadd -g 1 -u 501 -o -s /bin/false vboxadd >/dev/null 2>&1 || true
|
---|
599 |
|
---|
600 | }
|
---|
601 |
|
---|
602 | create_udev_rule()
|
---|
603 | {
|
---|
604 | # Create udev description file
|
---|
605 | if [ -d /etc/udev/rules.d ]; then
|
---|
606 | udev_call=""
|
---|
607 | udev_app=`which udevadm 2> /dev/null`
|
---|
608 | if [ $? -eq 0 ]; then
|
---|
609 | udev_call="${udev_app} version 2> /dev/null"
|
---|
610 | else
|
---|
611 | udev_app=`which udevinfo 2> /dev/null`
|
---|
612 | if [ $? -eq 0 ]; then
|
---|
613 | udev_call="${udev_app} -V 2> /dev/null"
|
---|
614 | fi
|
---|
615 | fi
|
---|
616 | udev_fix="="
|
---|
617 | if [ "${udev_call}" != "" ]; then
|
---|
618 | udev_out=`${udev_call}`
|
---|
619 | udev_ver=`expr "$udev_out" : '[^0-9]*\([0-9]*\)'`
|
---|
620 | if [ "$udev_ver" = "" -o "$udev_ver" -lt 55 ]; then
|
---|
621 | udev_fix=""
|
---|
622 | fi
|
---|
623 | fi
|
---|
624 | ## @todo 60-vboxadd.rules -> 60-vboxguest.rules ?
|
---|
625 | echo "KERNEL=${udev_fix}\"vboxguest\", NAME=\"vboxguest\", OWNER=\"vboxadd\", MODE=\"0660\"" > /etc/udev/rules.d/60-vboxadd.rules
|
---|
626 | echo "KERNEL=${udev_fix}\"vboxuser\", NAME=\"vboxuser\", OWNER=\"vboxadd\", MODE=\"0666\"" >> /etc/udev/rules.d/60-vboxadd.rules
|
---|
627 | # Make sure the new rule is noticed.
|
---|
628 | udevadm control --reload >/dev/null 2>&1 || true
|
---|
629 | udevcontrol reload_rules >/dev/null 2>&1 || true
|
---|
630 | fi
|
---|
631 | }
|
---|
632 |
|
---|
633 | create_module_rebuild_script()
|
---|
634 | {
|
---|
635 | # And a post-installation script for rebuilding modules when a new kernel
|
---|
636 | # is installed.
|
---|
637 | mkdir -p /etc/kernel/postinst.d /etc/kernel/prerm.d
|
---|
638 | cat << EOF > /etc/kernel/postinst.d/vboxadd
|
---|
639 | #!/bin/sh
|
---|
640 | # This only works correctly on Debian derivatives - Red Hat calls it before
|
---|
641 | # installing the right header files.
|
---|
642 | /sbin/rcvboxadd quicksetup "\${1}"
|
---|
643 | exit 0
|
---|
644 | EOF
|
---|
645 | cat << EOF > /etc/kernel/prerm.d/vboxadd
|
---|
646 | #!/bin/sh
|
---|
647 | for i in ${OLDMODULES}; do rm -f /lib/modules/"\${1}"/misc/"\${i}".ko; done
|
---|
648 | rmdir -p /lib/modules/"\$1"/misc 2>/dev/null || true
|
---|
649 | exit 0
|
---|
650 | EOF
|
---|
651 | chmod 0755 /etc/kernel/postinst.d/vboxadd /etc/kernel/prerm.d/vboxadd
|
---|
652 | }
|
---|
653 |
|
---|
654 | shared_folder_setup()
|
---|
655 | {
|
---|
656 | # Add a group "vboxsf" for Shared Folders access
|
---|
657 | # All users which want to access the auto-mounted Shared Folders have to
|
---|
658 | # be added to this group.
|
---|
659 | groupadd -r -f vboxsf >/dev/null 2>&1
|
---|
660 |
|
---|
661 | # Put the mount.vboxsf mount helper in the right place.
|
---|
662 | ## @todo It would be nicer if the kernel module just parsed parameters
|
---|
663 | # itself instead of needing a separate binary to do that.
|
---|
664 | ln -sf "${INSTALL_DIR}/other/mount.vboxsf" /sbin
|
---|
665 | # SELinux security context for the mount helper.
|
---|
666 | if test -e /etc/selinux/config; then
|
---|
667 | # This is correct. semanage maps this to the real path, and it aborts
|
---|
668 | # with an error, telling you what you should have typed, if you specify
|
---|
669 | # the real path. The "chcon" is there as a back-up for old guests.
|
---|
670 | command -v semanage > /dev/null &&
|
---|
671 | semanage fcontext -a -t mount_exec_t "${INSTALL_DIR}/other/mount.vboxsf"
|
---|
672 | chcon -t mount_exec_t "${INSTALL_DIR}/other/mount.vboxsf" 2>/dev/null
|
---|
673 | fi
|
---|
674 | }
|
---|
675 |
|
---|
676 | # Returns path to a module file as seen by modinfo(8), or none if not found.
|
---|
677 | #
|
---|
678 | # Input $1: Module name to get path for.
|
---|
679 | #
|
---|
680 | # Returns the module path as a string.
|
---|
681 | module_path()
|
---|
682 | {
|
---|
683 | mod="$1"
|
---|
684 | [ -n "$mod" ] || return
|
---|
685 |
|
---|
686 | modinfo "$mod" 2>/dev/null | grep -e "^filename:" | tr -s ' ' | cut -d " " -f2
|
---|
687 | }
|
---|
688 |
|
---|
689 | # Returns module version if module is available, or none if not found.
|
---|
690 | #
|
---|
691 | # Input $1: Module name to get version for.
|
---|
692 | #
|
---|
693 | # Returns the module version as a string.
|
---|
694 | module_version()
|
---|
695 | {
|
---|
696 | mod="$1"
|
---|
697 | [ -n "$mod" ] || return
|
---|
698 |
|
---|
699 | modinfo "$mod" 2>/dev/null | grep -e "^version:" | tr -s ' ' | cut -d " " -f2
|
---|
700 | }
|
---|
701 |
|
---|
702 | # Returns the module revision if module is available in the system, or none if not found.
|
---|
703 | #
|
---|
704 | # Input $1: Module name to get revision for.
|
---|
705 | #
|
---|
706 | # Returns the module revision as a string.
|
---|
707 | module_revision()
|
---|
708 | {
|
---|
709 | mod="$1"
|
---|
710 | [ -n "$mod" ] || return
|
---|
711 |
|
---|
712 | modinfo "$mod" 2>/dev/null | grep -e "^version:" | tr -s ' ' | cut -d " " -f3
|
---|
713 | }
|
---|
714 |
|
---|
715 | # Checks if a given kernel module is properly signed or not.
|
---|
716 | #
|
---|
717 | # Input $1: Module name to check.
|
---|
718 | #
|
---|
719 | # Returns "1" if module is signed and signature can be verified
|
---|
720 | # with public key provided in DEB_PUB_KEY, or none otherwise.
|
---|
721 | module_signed()
|
---|
722 | {
|
---|
723 | mod="$1"
|
---|
724 | [ -n "$mod" ] || return
|
---|
725 |
|
---|
726 | # Be nice with distributions which do not provide tools which we
|
---|
727 | # use in order to verify module signature. This variable needs to
|
---|
728 | # be explicitly set by administrator. This script will look for it
|
---|
729 | # in /etc/virtualbox-guest-additions.conf. Make sure that you know
|
---|
730 | # what you do!
|
---|
731 | if [ "$VBOX_BYPASS_MODULES_SIGNATURE_CHECK" = "1" ]; then
|
---|
732 | echo "1"
|
---|
733 | return
|
---|
734 | fi
|
---|
735 |
|
---|
736 | extraction_tool=/lib/modules/"$(uname -r)"/build/scripts/extract-module-sig.pl
|
---|
737 | mod_path=$(module_path "$mod" 2>/dev/null)
|
---|
738 | openssl_tool=$(which openssl 2>/dev/null)
|
---|
739 | # Do not use built-in printf!
|
---|
740 | printf_tool=$(which printf 2>/dev/null)
|
---|
741 |
|
---|
742 | # Make sure all the tools required for signature validation are available.
|
---|
743 | [ -x "$extraction_tool" ] || return
|
---|
744 | [ -n "$mod_path" ] || return
|
---|
745 | [ -n "$openssl_tool" ] || return
|
---|
746 | [ -n "$printf_tool" ] || return
|
---|
747 |
|
---|
748 | # Make sure openssl can handle hash algorithm.
|
---|
749 | sig_hashalgo=$(modinfo -F sig_hashalgo "$mod" 2>/dev/null)
|
---|
750 | [ "$(module_sig_hash_supported $sig_hashalgo)" = "1" ] || return
|
---|
751 |
|
---|
752 | # Generate file names for temporary stuff.
|
---|
753 | mod_pub_key=$(mktemp -u)
|
---|
754 | mod_signature=$(mktemp -u)
|
---|
755 | mod_unsigned=$(mktemp -u)
|
---|
756 |
|
---|
757 | # Convert public key in DER format into X509 certificate form.
|
---|
758 | "$openssl_tool" x509 -pubkey -inform DER -in "$DEB_PUB_KEY" -out "$mod_pub_key" 2>/dev/null
|
---|
759 | # Extract raw module signature and convert it into binary format.
|
---|
760 | "$printf_tool" \\x$(modinfo -F signature "$mod" | sed -z 's/[ \t\n]//g' | sed -e "s/:/\\\x/g") 2>/dev/null > "$mod_signature"
|
---|
761 | # Extract unsigned module for further digest calculation.
|
---|
762 | "$extraction_tool" -0 "$mod_path" 2>/dev/null > "$mod_unsigned"
|
---|
763 |
|
---|
764 | # Verify signature.
|
---|
765 | rc=""
|
---|
766 | "$openssl_tool" dgst "-$sig_hashalgo" -binary -verify "$mod_pub_key" -signature "$mod_signature" "$mod_unsigned" 2>&1 >/dev/null && rc="1"
|
---|
767 | # Clean up.
|
---|
768 | rm -f $mod_pub_key $mod_signature $mod_unsigned
|
---|
769 |
|
---|
770 | # Check result.
|
---|
771 | [ "$rc" = "1" ] || return
|
---|
772 |
|
---|
773 | echo "1"
|
---|
774 | }
|
---|
775 |
|
---|
776 | # Checks if a given kernel module matches the installed VirtualBox Guest Additions version.
|
---|
777 | #
|
---|
778 | # Input $1: Module name to check.
|
---|
779 | #
|
---|
780 | # Returns "1" if externally built module is available in the system and its
|
---|
781 | # version and revision number do match to current VirtualBox installation.
|
---|
782 | # None otherwise.
|
---|
783 | module_available()
|
---|
784 | {
|
---|
785 | mod="$1"
|
---|
786 | [ -n "$mod" ] || return
|
---|
787 |
|
---|
788 | [ "$VBOX_VERSION" = "$(module_version "$mod")" ] || return
|
---|
789 | [ "$VBOX_REVISION" = "$(module_revision "$mod")" ] || return
|
---|
790 |
|
---|
791 | # Check if module belongs to VirtualBox installation.
|
---|
792 | #
|
---|
793 | # We have a convention that only modules from /lib/modules/*/misc
|
---|
794 | # belong to us. Modules from other locations are treated as
|
---|
795 | # externally built.
|
---|
796 | mod_path="$(module_path "$mod")"
|
---|
797 |
|
---|
798 | # If module path points to a symbolic link, resolve actual file location.
|
---|
799 | [ -L "$mod_path" ] && mod_path="$(readlink -e -- "$mod_path")"
|
---|
800 |
|
---|
801 | # File exists?
|
---|
802 | [ -f "$mod_path" ] || return
|
---|
803 |
|
---|
804 | # Extract last component of module path and check whether it is located
|
---|
805 | # outside of /lib/modules/*/misc.
|
---|
806 | mod_dir="$(dirname "$mod_path" | sed 's;^.*/;;')"
|
---|
807 | [ "$mod_dir" = "misc" ] || return
|
---|
808 |
|
---|
809 | # In case if kernel configuration (for currently loaded kernel) requires
|
---|
810 | # module signature, check if module is signed.
|
---|
811 | if test "$(kernel_requires_module_signature $(uname -r))" = "1"; then
|
---|
812 | [ "$(module_signed "$mod")" = "1" ] || return
|
---|
813 | fi
|
---|
814 |
|
---|
815 | echo "1"
|
---|
816 | }
|
---|
817 |
|
---|
818 | # Check if required modules are installed in the system and versions match.
|
---|
819 | #
|
---|
820 | # Returns "1" on success, none otherwise.
|
---|
821 | setup_complete()
|
---|
822 | {
|
---|
823 | [ "$(module_available vboxguest)" = "1" ] || return
|
---|
824 | [ "$(module_available vboxsf)" = "1" ] || return
|
---|
825 |
|
---|
826 | # All modules are in place.
|
---|
827 | echo "1"
|
---|
828 | }
|
---|
829 |
|
---|
830 | # setup_script
|
---|
831 | setup()
|
---|
832 | {
|
---|
833 | info "Setting up modules"
|
---|
834 |
|
---|
835 | # chcon is needed on old Fedora/Redhat systems. No one remembers which.
|
---|
836 | test ! -e /etc/selinux/config ||
|
---|
837 | chcon -t bin_t "$BUILDINTMP" 2>/dev/null
|
---|
838 |
|
---|
839 | if test -z "$INSTALL_NO_MODULE_BUILDS"; then
|
---|
840 | # Check whether modules setup is already complete for currently running kernel.
|
---|
841 | # Prevent unnecessary rebuilding in order to speed up booting process.
|
---|
842 | if test "$(setup_complete)" = "1"; then
|
---|
843 | info "VirtualBox Guest Additions kernel modules $VBOX_VERSION $VBOX_REVISION are \
|
---|
844 | already available for kernel $TARGET_VER and do not require to be rebuilt."
|
---|
845 | else
|
---|
846 | info "Building the VirtualBox Guest Additions kernel modules. This may take a while."
|
---|
847 | info "To build modules for other installed kernels, run"
|
---|
848 | info " /sbin/rcvboxadd quicksetup <version>"
|
---|
849 | info "or"
|
---|
850 | info " /sbin/rcvboxadd quicksetup all"
|
---|
851 | if test -d /lib/modules/"$TARGET_VER"/build; then
|
---|
852 | setup_modules "$TARGET_VER"
|
---|
853 | depmod
|
---|
854 | else
|
---|
855 | info "Kernel headers not found for target kernel $TARGET_VER. \
|
---|
856 | Please install them and execute
|
---|
857 | /sbin/rcvboxadd setup"
|
---|
858 | fi
|
---|
859 | fi
|
---|
860 | fi
|
---|
861 | create_vbox_user
|
---|
862 | create_udev_rule
|
---|
863 | test -n "${INSTALL_NO_MODULE_BUILDS}" || create_module_rebuild_script
|
---|
864 | shared_folder_setup
|
---|
865 | # Create user group which will have permissive access to DRP IPC server socket.
|
---|
866 | groupadd -r -f vboxdrmipc >/dev/null 2>&1
|
---|
867 |
|
---|
868 | if running_module "vboxguest"; then
|
---|
869 | # Only warn user if currently loaded modules version do not match Guest Additions Installation.
|
---|
870 | check_running_module_version "vboxguest" || info "Running kernel modules will not be replaced until the system is restarted or 'rcvboxadd reload' triggered"
|
---|
871 | fi
|
---|
872 |
|
---|
873 | # Put the X.Org driver in place. This is harmless if it is not needed.
|
---|
874 | # Also set up the OpenGL library.
|
---|
875 | myerr=`"${INSTALL_DIR}/init/vboxadd-x11" setup 2>&1`
|
---|
876 | test -z "${myerr}" || log "${myerr}"
|
---|
877 |
|
---|
878 | return 0
|
---|
879 | }
|
---|
880 |
|
---|
881 | # cleanup_script
|
---|
882 | cleanup()
|
---|
883 | {
|
---|
884 | if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
|
---|
885 | # Delete old versions of VBox modules.
|
---|
886 | cleanup_modules
|
---|
887 | depmod
|
---|
888 |
|
---|
889 | # Remove old module sources
|
---|
890 | for i in $OLDMODULES; do
|
---|
891 | rm -rf /usr/src/$i-*
|
---|
892 | done
|
---|
893 | fi
|
---|
894 |
|
---|
895 | # Clean-up X11-related bits
|
---|
896 | "${INSTALL_DIR}/init/vboxadd-x11" cleanup
|
---|
897 |
|
---|
898 | # Remove other files
|
---|
899 | if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
|
---|
900 | rm -f /etc/kernel/postinst.d/vboxadd /etc/kernel/prerm.d/vboxadd
|
---|
901 | rmdir -p /etc/kernel/postinst.d /etc/kernel/prerm.d 2>/dev/null || true
|
---|
902 | fi
|
---|
903 | rm -f /sbin/mount.vboxsf 2>/dev/null
|
---|
904 | rm -f /etc/udev/rules.d/60-vboxadd.rules 2>/dev/null
|
---|
905 | udevadm control --reload >/dev/null 2>&1 || true
|
---|
906 | udevcontrol reload_rules >/dev/null 2>&1 || true
|
---|
907 | }
|
---|
908 |
|
---|
909 | start()
|
---|
910 | {
|
---|
911 | begin "Starting."
|
---|
912 |
|
---|
913 | # Check if kernel modules for currently running kernel are ready
|
---|
914 | # and rebuild them if needed.
|
---|
915 | test "$(setup_complete)" = "1" || setup
|
---|
916 |
|
---|
917 | # Warn if Secure Boot setup not yet complete.
|
---|
918 | if test "$(kernel_requires_module_signature)" = "1" && test -z "$DEB_KEY_ENROLLED"; then
|
---|
919 | if test -n "$HAVE_DEB_KEY"; then
|
---|
920 | info "You must re-start your system to finish secure boot set-up."
|
---|
921 | else
|
---|
922 | info "You must sign vboxguest, vboxsf and
|
---|
923 | vboxvideo (if present) kernel modules before using
|
---|
924 | VirtualBox Guest Additions. See the documentation
|
---|
925 | for your Linux distribution."
|
---|
926 | fi
|
---|
927 | fi
|
---|
928 |
|
---|
929 | if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
|
---|
930 | test -d /sys &&
|
---|
931 | ps -A -o comm | grep -q '/*udevd$' 2>/dev/null ||
|
---|
932 | no_udev=1
|
---|
933 | check_running_module_version "vboxguest" || {
|
---|
934 | rm -f $dev || {
|
---|
935 | fail "Cannot remove $dev"
|
---|
936 | }
|
---|
937 | rm -f $userdev || {
|
---|
938 | fail "Cannot remove $userdev"
|
---|
939 | }
|
---|
940 | # Assuming modules were just (re-)built, try to reload everything.
|
---|
941 | reload
|
---|
942 | }
|
---|
943 | case "$no_udev" in 1)
|
---|
944 | do_vboxguest_non_udev;;
|
---|
945 | esac
|
---|
946 | fi # INSTALL_NO_MODULE_BUILDS
|
---|
947 |
|
---|
948 | return 0
|
---|
949 | }
|
---|
950 |
|
---|
951 | stop()
|
---|
952 | {
|
---|
953 | begin "Stopping."
|
---|
954 |
|
---|
955 | if test -r /etc/ld.so.conf.d/00vboxvideo.conf; then
|
---|
956 | rm /etc/ld.so.conf.d/00vboxvideo.conf
|
---|
957 | ldconfig
|
---|
958 | fi
|
---|
959 | if ! umount -a -t vboxsf 2>/dev/null; then
|
---|
960 | # Make sure we only fail, if there are truly no more vboxsf
|
---|
961 | # mounts in the system.
|
---|
962 | [ -n "$(findmnt -t vboxsf)" ] && fail "Cannot unmount vboxsf folders"
|
---|
963 | fi
|
---|
964 | test -n "${INSTALL_NO_MODULE_BUILDS}" ||
|
---|
965 | info "You may need to restart your guest system to finish removing guest drivers or consider running 'rcvboxadd reload'."
|
---|
966 | return 0
|
---|
967 | }
|
---|
968 |
|
---|
969 | check_root()
|
---|
970 | {
|
---|
971 | # Check if script is running with root privileges and exit if it does not.
|
---|
972 | [ `id -u` -eq 0 ] || early_fail "root privileges are required"
|
---|
973 | }
|
---|
974 |
|
---|
975 | # Check if process with this PID is running.
|
---|
976 | check_pid()
|
---|
977 | {
|
---|
978 | pid="$1"
|
---|
979 |
|
---|
980 | test -n "$pid" -a -d "/proc/$pid"
|
---|
981 | }
|
---|
982 |
|
---|
983 | # A wrapper for check_running_module_version.
|
---|
984 | # Verify if module is loaded and its version matches
|
---|
985 | # to current Additions installation version.
|
---|
986 | check_running_module()
|
---|
987 | {
|
---|
988 | mod="$1"
|
---|
989 |
|
---|
990 | # Check args.
|
---|
991 | [ -n "$mod" ] || return
|
---|
992 |
|
---|
993 | # During reload action it may take some time for module
|
---|
994 | # to be fully loaded, so make a few attempts while checking this.
|
---|
995 | for attempt in 1 2 3 4 5; do
|
---|
996 |
|
---|
997 | # Wait before the next attempt.
|
---|
998 | [ -n "$vbox_add_wait" -a $? -ne 0 ] && sleep 1
|
---|
999 |
|
---|
1000 | running_module "$mod"
|
---|
1001 | if [ $? -eq 0 ]; then
|
---|
1002 | mod_is_running="1"
|
---|
1003 | check_running_module_version "$mod"
|
---|
1004 | [ $? -eq 0 ] && break
|
---|
1005 | else
|
---|
1006 | mod_is_running=""
|
---|
1007 | false
|
---|
1008 | fi
|
---|
1009 |
|
---|
1010 | done
|
---|
1011 |
|
---|
1012 | # In case of error, try to print out proper reason of failure.
|
---|
1013 | if [ $? -ne 0 ]; then
|
---|
1014 | # Was module loaded?
|
---|
1015 | if [ -z "$mod_is_running" ]; then
|
---|
1016 | info "module $mod is not loaded"
|
---|
1017 | else
|
---|
1018 | # If module was loaded it means that it has incorrect version.
|
---|
1019 | info "currently loaded module $mod version ($(running_module_version "$mod")) does not match to VirtualBox Guest Additions installation version ($VBOX_VERSION $VBOX_REVISION)"
|
---|
1020 | fi
|
---|
1021 |
|
---|
1022 | # Set "bad" rc.
|
---|
1023 | false
|
---|
1024 | fi
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | # Go through list of Additions modules and check
|
---|
1028 | # if they were properly loaded.
|
---|
1029 | check_status_kernel()
|
---|
1030 | {
|
---|
1031 | # Module vboxguest should be loaded unconditionally once Guest Additions were installed.
|
---|
1032 | check_running_module "vboxguest"
|
---|
1033 |
|
---|
1034 | # Module vboxsf module might not be loaded if VM has no Shared Folder mappings.
|
---|
1035 | # Check that first and then verify the module.
|
---|
1036 | if [ $? -eq 0 ]; then
|
---|
1037 | VBoxControl sharedfolder list >/dev/null 2>&1
|
---|
1038 | if [ $? -eq 0 ]; then
|
---|
1039 | check_running_module "vboxsf"
|
---|
1040 | else
|
---|
1041 | # Do not spoil $?.
|
---|
1042 | true
|
---|
1043 | fi
|
---|
1044 | fi
|
---|
1045 |
|
---|
1046 | # Module vboxvideo is optional and expected to be loaded only when VM is
|
---|
1047 | # running VBoxVGA or VBoxSVGA graphics.
|
---|
1048 | if [ $? -eq 0 ]; then
|
---|
1049 | gpu_vendor=$(lspci | grep 'VGA compatible controller' | cut -d ' ' -f 5 2>/dev/null)
|
---|
1050 | if [ "gpu_vendor" = "InnoTek" ]; then
|
---|
1051 | check_running_module "vboxvideo"
|
---|
1052 | else
|
---|
1053 | # Do not spoil $?.
|
---|
1054 | true
|
---|
1055 | fi
|
---|
1056 | fi
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | # Check whether user-land processes are running.
|
---|
1060 | # Currently only check for VBoxService.
|
---|
1061 | check_status_user()
|
---|
1062 | {
|
---|
1063 | [ -r "$VBOXSERVICE_PIDFILE" ] && check_pid "$(cat $VBOXSERVICE_PIDFILE)" >/dev/null 2>&1
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | send_signal_by_pidfile()
|
---|
1067 | {
|
---|
1068 | sig="$1"
|
---|
1069 | pidfile="$2"
|
---|
1070 |
|
---|
1071 | if [ -f "$pidfile" ]; then
|
---|
1072 | check_pid $(cat "$pidfile")
|
---|
1073 | if [ $? -eq 0 ]; then
|
---|
1074 | kill "$sig" $(cat "$pidfile") >/dev/null 2>&1
|
---|
1075 | else
|
---|
1076 | # Do not spoil $?.
|
---|
1077 | true
|
---|
1078 | fi
|
---|
1079 | else
|
---|
1080 | # Do not spoil $?.
|
---|
1081 | true
|
---|
1082 | fi
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | # SIGUSR1 is used in order to notify VBoxClient processes that system
|
---|
1086 | # update is started or kernel modules are going to be reloaded,
|
---|
1087 | # so VBoxClient can release vboxguest.ko resources and then restart itself.
|
---|
1088 | send_signal()
|
---|
1089 | {
|
---|
1090 | sig="$1"
|
---|
1091 | # Specify whether we sending signal to VBoxClient parent (control)
|
---|
1092 | # process or a child (actual service) process.
|
---|
1093 | process_type="$2"
|
---|
1094 |
|
---|
1095 | pidfile_postfix=""
|
---|
1096 | [ -z "$process_type" ] || pidfile_postfix="-$process_type"
|
---|
1097 |
|
---|
1098 | for user_name in $(getent passwd | cut -d ':' -f 1); do
|
---|
1099 |
|
---|
1100 | # Filter out empty login names (paranoia).
|
---|
1101 | [ -n "$user_name" ] || continue
|
---|
1102 |
|
---|
1103 | user_shell=$(getent passwd "$user_name" | cut -d ':' -f 7)
|
---|
1104 |
|
---|
1105 | # Filter out login names with not specified shells (paranoia).
|
---|
1106 | [ -n "$user_shell" ] || continue
|
---|
1107 |
|
---|
1108 | # Filter out know non-login account names.
|
---|
1109 | case "$user_shell" in
|
---|
1110 | *nologin) skip_user_home="1";;
|
---|
1111 | *sync) skip_user_home="1";;
|
---|
1112 | *shutdown) skip_user_home="1";;
|
---|
1113 | *halt) skip_user_home="1";;
|
---|
1114 | *) skip_user_home=""
|
---|
1115 | esac
|
---|
1116 | [ -z "$skip_user_home" ] || continue;
|
---|
1117 |
|
---|
1118 | user_home=$(getent passwd "$user_name" | cut -d ':' -f 6)
|
---|
1119 |
|
---|
1120 | for pid_file in "$user_home"/.vboxclient-*"$pidfile_postfix".pid; do
|
---|
1121 |
|
---|
1122 | [ -r "$pid_file" ] || continue
|
---|
1123 |
|
---|
1124 | # If process type was not specified, we assume that signal supposed
|
---|
1125 | # to be sent to legacy VBoxClient processes which have different
|
---|
1126 | # pidfile name pattern (it does not contain "control" or "service").
|
---|
1127 | # Skip those pidfiles who has.
|
---|
1128 | [ -z "$process_type" -a -n "$(echo "$pid_file" | grep "control")" ] && continue
|
---|
1129 | [ -z "$process_type" -a -n "$(echo "$pid_file" | grep "service")" ] && continue
|
---|
1130 |
|
---|
1131 | send_signal_by_pidfile -USR1 "$pid_file"
|
---|
1132 |
|
---|
1133 | done
|
---|
1134 | done
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | # Helper function which executes a command, prints error message if command fails,
|
---|
1138 | # and preserves command execution status for further processing.
|
---|
1139 | try_load_preserve_rc()
|
---|
1140 | {
|
---|
1141 | cmd="$1"
|
---|
1142 | msg="$2"
|
---|
1143 |
|
---|
1144 | $cmd >/dev/null 2>&1
|
---|
1145 |
|
---|
1146 | rc=$?
|
---|
1147 | [ $rc -eq 0 ] || info "$msg"
|
---|
1148 |
|
---|
1149 | return $rc
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | reload()
|
---|
1153 | {
|
---|
1154 | begin "reloading kernel modules and services"
|
---|
1155 |
|
---|
1156 | # Stop VBoxService if running.
|
---|
1157 | $VBOX_SERVICE_SCRIPT status >/dev/null 2>&1
|
---|
1158 | if [ $? -eq 0 ]; then
|
---|
1159 | $VBOX_SERVICE_SCRIPT stop >/dev/null 2>&1 || fail "unable to stop VBoxService"
|
---|
1160 | fi
|
---|
1161 |
|
---|
1162 | # Unmount Shared Folders.
|
---|
1163 | umount -a -t vboxsf >/dev/null 2>&1 || fail "unable to unmount shared folders, mount point(s) might be still in use"
|
---|
1164 |
|
---|
1165 | # Stop VBoxDRMClient.
|
---|
1166 | send_signal_by_pidfile "-USR1" "/var/run/VBoxDRMClient" || fail "unable to stop VBoxDRMClient"
|
---|
1167 |
|
---|
1168 | if [ $? -eq 0 ]; then
|
---|
1169 | # Tell legacy VBoxClient processes to release vboxguest.ko references.
|
---|
1170 | send_signal "-USR1" ""
|
---|
1171 |
|
---|
1172 | # Tell compatible VBoxClient processes to release vboxguest.ko references.
|
---|
1173 | send_signal "-USR1" "service"
|
---|
1174 |
|
---|
1175 | # Try unload.
|
---|
1176 | for attempt in 1 2 3 4 5; do
|
---|
1177 |
|
---|
1178 | # Give VBoxClient processes some time to close reference to vboxguest module.
|
---|
1179 | [ $? -ne 0 ] && sleep 1
|
---|
1180 |
|
---|
1181 | # Try unload drivers unconditionally (ignore previous command exit code).
|
---|
1182 | # If final goal of unloading vboxguest.ko won't be met, we will fail on
|
---|
1183 | # the next step anyway.
|
---|
1184 | running_module "vboxsf" && modprobe -r vboxsf >/dev/null 2>&1
|
---|
1185 | running_module "vboxguest"
|
---|
1186 | if [ $? -eq 0 ]; then
|
---|
1187 | modprobe -r vboxguest >/dev/null 2>&1
|
---|
1188 | [ $? -eq 0 ] && break
|
---|
1189 | else
|
---|
1190 | # Do not spoil $?.
|
---|
1191 | true
|
---|
1192 | fi
|
---|
1193 | done
|
---|
1194 |
|
---|
1195 | # Check if we succeeded with unloading vboxguest after several attempts.
|
---|
1196 | running_module "vboxguest"
|
---|
1197 | if [ $? -eq 0 ]; then
|
---|
1198 | info "cannot reload kernel modules: one or more module(s) is still in use"
|
---|
1199 | false
|
---|
1200 | else
|
---|
1201 | # Do not spoil $?.
|
---|
1202 | true
|
---|
1203 | fi
|
---|
1204 |
|
---|
1205 | # Load drivers (skip vboxvideo since it is not loaded for very old guests).
|
---|
1206 | [ $? -eq 0 ] && try_load_preserve_rc "modprobe vboxguest" "unable to load vboxguest kernel module, see dmesg"
|
---|
1207 | [ $? -eq 0 ] && try_load_preserve_rc "modprobe vboxsf" "unable to load vboxsf kernel module, see dmesg"
|
---|
1208 |
|
---|
1209 | # Start VBoxService and VBoxDRMClient.
|
---|
1210 | [ $? -eq 0 ] && try_load_preserve_rc "$VBOX_SERVICE_SCRIPT start" "unable to start VBoxService"
|
---|
1211 |
|
---|
1212 | # Reload VBoxClient processes.
|
---|
1213 | [ $? -eq 0 ] && try_load_preserve_rc "send_signal -USR1 control" "unable to reload user session services"
|
---|
1214 |
|
---|
1215 | # Check if we just loaded modules of correct version.
|
---|
1216 | [ $? -eq 0 ] && try_load_preserve_rc "check_status_kernel" "kernel modules were not reloaded"
|
---|
1217 |
|
---|
1218 | # Check if user-land processes were restarted as well.
|
---|
1219 | [ $? -eq 0 ] && try_load_preserve_rc "check_status_user" "user-land services were not started"
|
---|
1220 |
|
---|
1221 | if [ $? -eq 0 ]; then
|
---|
1222 | # Take reported version of running Guest Additions from running vboxguest module (as a paranoia check).
|
---|
1223 | info "kernel modules and services $(running_module_version "vboxguest") reloaded"
|
---|
1224 | info "NOTE: you may still consider to re-login if some user session specific services (Shared Clipboard, Drag and Drop, Seamless or Guest Screen Resize) were not restarted automatically"
|
---|
1225 | else
|
---|
1226 | # In case of failure, sent SIGTERM to abandoned control processes to remove leftovers from failed reloading.
|
---|
1227 | send_signal "-TERM" "control"
|
---|
1228 |
|
---|
1229 | fail "kernel modules and services were not reloaded"
|
---|
1230 | fi
|
---|
1231 | else
|
---|
1232 | fail "cannot stop user services"
|
---|
1233 | fi
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | dmnstatus()
|
---|
1237 | {
|
---|
1238 | if running_module "vboxguest"; then
|
---|
1239 | echo "The VirtualBox Additions are currently running."
|
---|
1240 | else
|
---|
1241 | echo "The VirtualBox Additions are not currently running."
|
---|
1242 | fi
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | for i; do
|
---|
1246 | case "$i" in quiet) QUIET=yes;; esac
|
---|
1247 | done
|
---|
1248 | case "$1" in
|
---|
1249 | # Does setup without clean-up first and marks all kernels currently found on the
|
---|
1250 | # system so that we can see later if any were added.
|
---|
1251 | start)
|
---|
1252 | check_root
|
---|
1253 | start
|
---|
1254 | ;;
|
---|
1255 | # Tries to build kernel modules for kernels added since start. Tries to unmount
|
---|
1256 | # shared folders. Uninstalls our Chromium 3D libraries since we can't always do
|
---|
1257 | # this fast enough at start time if we discover we do not want to use them.
|
---|
1258 | stop)
|
---|
1259 | check_root
|
---|
1260 | stop
|
---|
1261 | ;;
|
---|
1262 | restart)
|
---|
1263 | check_root
|
---|
1264 | restart
|
---|
1265 | ;;
|
---|
1266 | # Tries to reload kernel modules and restart user processes.
|
---|
1267 | reload)
|
---|
1268 | check_root
|
---|
1269 | # reload() we will call modprobe(8) in order to reload kernel
|
---|
1270 | # modules. This operation is asynchronous and requires some time for
|
---|
1271 | # modules to be loaded in most of the cases. By setting this variable, we
|
---|
1272 | # ask check_running_module() to wait a bit before making a decision
|
---|
1273 | # whether modules were loaded or not.
|
---|
1274 | vbox_add_wait=1
|
---|
1275 | reload
|
---|
1276 | ;;
|
---|
1277 | # Setup does a clean-up (see below) and re-does all Additions-specific
|
---|
1278 | # configuration of the guest system, including building kernel modules for the
|
---|
1279 | # current kernel.
|
---|
1280 | setup)
|
---|
1281 | check_root
|
---|
1282 | cleanup && start
|
---|
1283 | ;;
|
---|
1284 | # Builds kernel modules for the specified kernels if they are not already built.
|
---|
1285 | quicksetup)
|
---|
1286 | check_root
|
---|
1287 | if test x"$2" = xall; then
|
---|
1288 | for topi in /lib/modules/*; do
|
---|
1289 | KERN_VER="${topi%/misc}"
|
---|
1290 | KERN_VER="${KERN_VER#/lib/modules/}"
|
---|
1291 | setup_modules "$KERN_VER"
|
---|
1292 | done
|
---|
1293 | elif test -n "$2"; then
|
---|
1294 | setup_modules "$2"
|
---|
1295 | else
|
---|
1296 | setup_modules "$TARGET_VER"
|
---|
1297 | fi
|
---|
1298 | ;;
|
---|
1299 | # Clean-up removes all Additions-specific configuration of the guest system,
|
---|
1300 | # including all kernel modules.
|
---|
1301 | cleanup)
|
---|
1302 | check_root
|
---|
1303 | cleanup
|
---|
1304 | ;;
|
---|
1305 | status)
|
---|
1306 | dmnstatus
|
---|
1307 | ;;
|
---|
1308 | status-kernel)
|
---|
1309 | check_root
|
---|
1310 | check_status_kernel
|
---|
1311 | if [ $? -eq 0 ]; then
|
---|
1312 | info "kernel modules $VBOX_VERSION $VBOX_REVISION are loaded"
|
---|
1313 | else
|
---|
1314 | info "kernel modules $VBOX_VERSION $VBOX_REVISION were not loaded"
|
---|
1315 | false
|
---|
1316 | fi
|
---|
1317 | ;;
|
---|
1318 | status-user)
|
---|
1319 | check_root
|
---|
1320 | check_status_user
|
---|
1321 | if [ $? -eq 0 ]; then
|
---|
1322 | info "user-land services $VBOX_VERSION $VBOX_REVISION are running"
|
---|
1323 | else
|
---|
1324 | info "user-land services $VBOX_VERSION $VBOX_REVISION are not running"
|
---|
1325 | false
|
---|
1326 | fi
|
---|
1327 | ;;
|
---|
1328 | *)
|
---|
1329 | echo "Usage: $0 {start|stop|restart|reload|status|status-kernel|status-user|setup|quicksetup|cleanup} [quiet]"
|
---|
1330 | exit 1
|
---|
1331 | esac
|
---|
1332 |
|
---|
1333 | exit
|
---|