VirtualBox

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

Last change on this file since 58591 was 58399, checked in by vboxsync, 9 years ago

#8051: Installers: unify Linux host installers as far as possible: since we stopped inserting the common script code containing set_selinux_permissions() into VirtualBox.tmpl that function was no longer called. Move it to postinst-common.sh instead, and remove it from routines.sh and the .run installer.

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