VirtualBox

source: vbox/trunk/src/VBox/Installer/linux/routines.sh@ 73009

Last change on this file since 73009 was 72196, checked in by vboxsync, 7 years ago

Installers/Linux: clean up logging.

  • Remove strange kernel output from kernel module build log.
  • Remove most non-error output, in particular, only print module build output in case of error.
  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1# Oracle VM VirtualBox
2# VirtualBox installer shell routines
3#
4
5#
6# Copyright (C) 2007-2017 Oracle Corporation
7#
8# This file is part of VirtualBox Open Source Edition (OSE), as
9# available from http://www.virtualbox.org. This file is free software;
10# you can redistribute it and/or modify it under the terms of the GNU
11# General Public License (GPL) as published by the Free Software
12# Foundation, in version 2 as it comes in the "COPYING" file of the
13# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15#
16
17ro_LOG_FILE=""
18ro_X11_AUTOSTART="/etc/xdg/autostart"
19ro_KDE_AUTOSTART="/usr/share/autostart"
20
21## Aborts the script and prints an error message to stderr.
22#
23# syntax: abort message
24
25abort()
26{
27 echo 1>&2 "$1"
28 exit 1
29}
30
31## Creates an empty log file and remembers the name for future logging
32# operations
33create_log()
34{
35 ## The path of the file to create.
36 ro_LOG_FILE="$1"
37 if [ "$ro_LOG_FILE" = "" ]; then
38 abort "create_log called without an argument! Aborting..."
39 fi
40 # Create an empty file
41 echo > "$ro_LOG_FILE" 2> /dev/null
42 if [ ! -f "$ro_LOG_FILE" -o "`cat "$ro_LOG_FILE"`" != "" ]; then
43 abort "Error creating log file! Aborting..."
44 fi
45}
46
47## Writes text to standard error, as standard output is masked.
48#
49# Syntax: info text
50info()
51{
52 echo 1>&2 "$1"
53}
54
55## Copies standard input to standard error, as standard output is masked.
56#
57# Syntax: info text
58catinfo()
59{
60 cat 1>&2
61}
62
63## Writes text to the log file
64#
65# Syntax: log text
66log()
67{
68 if [ "$ro_LOG_FILE" = "" ]; then
69 abort "Error! Logging has not been set up yet! Aborting..."
70 fi
71 echo "$1" >> $ro_LOG_FILE
72 return 0
73}
74
75## Writes test to standard output and to the log file
76#
77# Syntax: infolog text
78infolog()
79{
80 info "$1"
81 log "$1"
82}
83
84## Checks whether a module is loaded with a given string in its name.
85#
86# syntax: module_loaded string
87module_loaded()
88{
89 if [ "$1" = "" ]; then
90 log "module_loaded called without an argument. Aborting..."
91 abort "Error in installer. Aborting..."
92 fi
93 lsmod | grep -q $1
94}
95
96## Abort if we are not running as root
97check_root()
98{
99 if [ `id -u` -ne 0 ]; then
100 abort "This program must be run with administrator privileges. Aborting"
101 fi
102}
103
104## Abort if dependencies are not found
105check_deps()
106{
107 for i in ${@}; do
108 type "${i}" >/dev/null 2>&1 ||
109 abort "${i} not found. Please install: ${*}; and try again."
110 done
111}
112
113## Abort if a copy of VirtualBox is already running
114check_running()
115{
116 VBOXSVC_PID=`pidof VBoxSVC 2> /dev/null`
117 if [ -n "$VBOXSVC_PID" ]; then
118 if [ -f /etc/init.d/vboxweb-service ]; then
119 kill -USR1 $VBOXSVC_PID
120 fi
121 sleep 1
122 if pidof VBoxSVC > /dev/null 2>&1; then
123 echo 1>&2 "A copy of VirtualBox is currently running. Please close it and try again."
124 abort "Please note that it can take up to ten seconds for VirtualBox to finish running."
125 fi
126 fi
127}
128
129## Creates a systemd wrapper in /lib for an LSB init script
130systemd_wrap_init_script()
131{
132 self="systemd_wrap_init_script"
133 ## The init script to be installed. The file may be copied or referenced.
134 script="$(readlink -f -- "${1}")"
135 ## Name for the service.
136 name="$2"
137 test -x "$script" && test ! "$name" = "" || \
138 { echo "$self: invalid arguments" >&2 && return 1; }
139 test -d /usr/lib/systemd/system && unit_path=/usr/lib/systemd/system
140 test -d /lib/systemd/system && unit_path=/lib/systemd/system
141 test -n "${unit_path}" || \
142 { echo "$self: systemd unit path not found" >&2 && return 1; }
143 conflicts=`sed -n 's/# *X-Conflicts-With: *\(.*\)/\1/p' "${script}" | sed 's/\$[a-z]*//'`
144 description=`sed -n 's/# *Short-Description: *\(.*\)/\1/p' "${script}"`
145 required=`sed -n 's/# *Required-Start: *\(.*\)/\1/p' "${script}" | sed 's/\$[a-z]*//'`
146 startbefore=`sed -n 's/# *X-Start-Before: *\(.*\)/\1/p' "${script}" | sed 's/\$[a-z]*//'`
147 runlevels=`sed -n 's/# *Default-Start: *\(.*\)/\1/p' "${script}"`
148 servicetype=`sed -n 's/# *X-Service-Type: *\(.*\)/\1/p' "${script}"`
149 test -z "${servicetype}" && servicetype="forking"
150 targets=`for i in ${runlevels}; do printf "runlevel${i}.target "; done`
151 before=`for i in ${startbefore}; do printf "${i}.service "; done`
152 after=`for i in ${required}; do printf "${i}.service "; done`
153 cat > "${unit_path}/${name}.service" << EOF
154[Unit]
155SourcePath=${script}
156Description=${description}
157Before=${targets}shutdown.target ${before}
158After=${after}
159Conflicts=shutdown.target ${conflicts}
160
161[Service]
162Type=${servicetype}
163Restart=no
164TimeoutSec=5min
165IgnoreSIGPIPE=no
166KillMode=process
167GuessMainPID=no
168RemainAfterExit=yes
169ExecStart=${script} start
170ExecStop=${script} stop
171
172[Install]
173WantedBy=multi-user.target
174EOF
175 systemctl daemon-reexec
176}
177
178## Installs a file containing a shell script as an init script
179install_init_script()
180{
181 self="install_init_script"
182 ## The init script to be installed. The file may be copied or referenced.
183 script="$1"
184 ## Name for the service.
185 name="$2"
186
187 test -x "${script}" && test ! "${name}" = "" ||
188 { echo "${self}: invalid arguments" >&2; return 1; }
189 # Do not unconditionally silence the following "ln".
190 test -L "/sbin/rc${name}" && rm "/sbin/rc${name}"
191 ln -s "${script}" "/sbin/rc${name}"
192 if test -x "`which systemctl 2>/dev/null`"; then
193 if ! test -f /sbin/init || ls -l /sbin/init | grep -q ">.*systemd"; then
194 { systemd_wrap_init_script "$script" "$name"; return; }
195 fi
196 fi
197 if test -d /etc/rc.d/init.d; then
198 cp "${script}" "/etc/rc.d/init.d/${name}" &&
199 chmod 755 "/etc/rc.d/init.d/${name}"
200 elif test -d /etc/init.d; then
201 cp "${script}" "/etc/init.d/${name}" &&
202 chmod 755 "/etc/init.d/${name}"
203 else
204 { echo "${self}: error: unknown init type" >&2; return 1; }
205 fi
206}
207
208## Remove the init script "name"
209remove_init_script()
210{
211 self="remove_init_script"
212 ## Name of the service to remove.
213 name="$1"
214
215 test -n "${name}" ||
216 { echo "$self: missing argument"; return 1; }
217 rm -f "/sbin/rc${name}"
218 rm -f /lib/systemd/system/"$name".service /usr/lib/systemd/system/"$name".service
219 rm -f "/etc/rc.d/init.d/$name"
220 rm -f "/etc/init.d/$name"
221}
222
223## Did we install a systemd service?
224systemd_service_installed()
225{
226 ## Name of service to test.
227 name="${1}"
228
229 test -f /lib/systemd/system/"${name}".service ||
230 test -f /usr/lib/systemd/system/"${name}".service
231}
232
233## Perform an action on a service
234do_sysvinit_action()
235{
236 self="do_sysvinit_action"
237 ## Name of service to start.
238 name="${1}"
239 ## The action to perform, normally "start", "stop" or "status".
240 action="${2}"
241
242 test ! -z "${name}" && test ! -z "${action}" ||
243 { echo "${self}: missing argument" >&2; return 1; }
244 if systemd_service_installed "${name}"; then
245 systemctl -q ${action} "${name}"
246 elif test -x "/etc/rc.d/init.d/${name}"; then
247 "/etc/rc.d/init.d/${name}" "${action}" quiet
248 elif test -x "/etc/init.d/${name}"; then
249 "/etc/init.d/${name}" "${action}" quiet
250 fi
251}
252
253## Start a service
254start_init_script()
255{
256 do_sysvinit_action "${1}" start
257}
258
259## Stop the init script "name"
260stop_init_script()
261{
262 do_sysvinit_action "${1}" stop
263}
264
265## Extract chkconfig information from a sysvinit script.
266get_chkconfig_info()
267{
268 ## The script to extract the information from.
269 script="${1}"
270
271 set `sed -n 's/# *chkconfig: *\([0-9]*\) *\(.*\)/\1 \2/p' "${script}"`
272 ## Which runlevels should we start in?
273 runlevels="${1}"
274 ## How soon in the boot process will we start, from 00 (first) to 99
275 start_order="${2}"
276 ## How soon in the shutdown process will we stop, from 99 (first) to 00
277 stop_order="${3}"
278 test ! -z "${name}" || \
279 { echo "${self}: missing name" >&2; return 1; }
280 expr "${start_order}" + 0 > /dev/null 2>&1 && \
281 expr 0 \<= "${start_order}" > /dev/null 2>&1 && \
282 test `expr length "${start_order}"` -eq 2 > /dev/null 2>&1 || \
283 { echo "${self}: start sequence number must be between 00 and 99" >&2;
284 return 1; }
285 expr "${stop_order}" + 0 > /dev/null 2>&1 && \
286 expr 0 \<= "${stop_order}" > /dev/null 2>&1 && \
287 test `expr length "${stop_order}"` -eq 2 > /dev/null 2>&1 || \
288 { echo "${self}: stop sequence number must be between 00 and 99" >&2;
289 return 1; }
290}
291
292## Add a service to its default runlevels (annotated inside the script, see get_chkconfig_info).
293addrunlevel()
294{
295 self="addrunlevel"
296 ## Service name.
297 name="${1}"
298
299 test -n "${name}" || \
300 { echo "${self}: missing argument" >&2; return 1; }
301 systemd_service_installed "${name}" && \
302 { systemctl -q enable "${name}"; return; }
303 if test -x "/etc/rc.d/init.d/${name}"; then
304 init_d_path=/etc/rc.d
305 elif test -x "/etc/init.d/${name}"; then
306 init_d_path=/etc
307 else
308 { echo "${self}: error: unknown init type" >&2; return 1; }
309 fi
310 get_chkconfig_info "${init_d_path}/init.d/${name}" || return 1
311 # Redhat based sysvinit systems
312 if test -x "`which chkconfig 2>/dev/null`"; then
313 chkconfig --add "${name}"
314 # SUSE-based sysvinit systems
315 elif test -x "`which insserv 2>/dev/null`"; then
316 insserv "${name}"
317 # Debian/Ubuntu-based systems
318 elif test -x "`which update-rc.d 2>/dev/null`"; then
319 # Old Debians did not support dependencies
320 update-rc.d "${name}" defaults "${start_order}" "${stop_order}"
321 # Gentoo Linux
322 elif test -x "`which rc-update 2>/dev/null`"; then
323 rc-update add "${name}" default
324 # Generic sysvinit
325 elif test -n "${init_d_path}/rc0.d"
326 then
327 for locali in 0 1 2 3 4 5 6
328 do
329 target="${init_d_path}/rc${locali}.d/K${stop_order}${name}"
330 expr "${runlevels}" : ".*${locali}" >/dev/null && \
331 target="${init_d_path}/rc${locali}.d/S${start_order}${name}"
332 test -e "${init_d_path}/rc${locali}.d/"[KS][0-9]*"${name}" || \
333 ln -fs "${init_d_path}/init.d/${name}" "${target}"
334 done
335 else
336 { echo "${self}: error: unknown init type" >&2; return 1; }
337 fi
338}
339
340
341## Delete a service from a runlevel
342delrunlevel()
343{
344 self="delrunlevel"
345 ## Service name.
346 name="${1}"
347
348 test -n "${name}" ||
349 { echo "${self}: missing argument" >&2; return 1; }
350 systemctl -q disable "${name}" >/dev/null 2>&1
351 # Redhat-based systems
352 chkconfig --del "${name}" >/dev/null 2>&1
353 # SUSE-based sysvinit systems
354 insserv -r "${name}" >/dev/null 2>&1
355 # Debian/Ubuntu-based systems
356 update-rc.d -f "${name}" remove >/dev/null 2>&1
357 # Gentoo Linux
358 rc-update del "${name}" >/dev/null 2>&1
359 # Generic sysvinit
360 rm -f /etc/rc.d/rc?.d/[SK]??"${name}"
361 rm -f /etc/rc?.d/[SK]??"${name}"
362}
363
364
365terminate_proc() {
366 PROC_NAME="${1}"
367 SERVER_PID=`pidof $PROC_NAME 2> /dev/null`
368 if [ "$SERVER_PID" != "" ]; then
369 killall -TERM $PROC_NAME > /dev/null 2>&1
370 sleep 2
371 fi
372}
373
374
375maybe_run_python_bindings_installer() {
376 VBOX_INSTALL_PATH="${1}"
377
378 PYTHON=python
379 if [ "`python -c 'import sys
380if sys.version_info >= (2, 6):
381 print \"test\"' 2> /dev/null`" != "test" ]; then
382 echo 1>&2 "Python 2.6 or later not available, skipping bindings installation."
383 return 1
384 fi
385
386 echo 1>&2 "Python found: $PYTHON, installing bindings..."
387 # Pass install path via environment
388 export VBOX_INSTALL_PATH
389 $SHELL -c "cd $VBOX_INSTALL_PATH/sdk/installer && $PYTHON vboxapisetup.py install \
390 --record $CONFIG_DIR/python-$CONFIG_FILES"
391 cat $CONFIG_DIR/python-$CONFIG_FILES >> $CONFIG_DIR/$CONFIG_FILES
392 rm $CONFIG_DIR/python-$CONFIG_FILES
393 # remove files created during build
394 rm -rf $VBOX_INSTALL_PATH/sdk/installer/build
395
396 return 0
397}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette