VirtualBox

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

Last change on this file since 58097 was 58085, checked in by vboxsync, 9 years ago

Installers/linux: routines.sh: remove existing /sbin/rc symlinks before creating new ones.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 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}"`
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 test -x "`which systemctl 2>/dev/null`" &&
169 { systemd_wrap_init_script "$script" "$name"; return; }
170 if test -d /etc/rc.d/init.d; then
171 cp "${script}" "/etc/rc.d/init.d/${name}" &&
172 chmod 755 "/etc/rc.d/init.d/${name}"
173 elif test -d /etc/init.d; then
174 cp "${script}" "/etc/init.d/${name}" &&
175 chmod 755 "/etc/init.d/${name}"
176 else
177 { echo "${self}: error: unknown init type" >&2; return 1; }
178 fi
179}
180
181## Remove the init script "name"
182remove_init_script()
183{
184 self="remove_init_script"
185 ## Name of the service to remove.
186 name="$1"
187
188 test -n "${name}" ||
189 { echo "$self: missing argument"; return 1; }
190 rm -f "/sbin/rc${name}"
191 rm -f /lib/systemd/system/"$name".service /usr/lib/systemd/system/"$name".service
192 rm -f "/etc/rc.d/init.d/$name"
193 rm -f "/etc/init.d/$name"
194}
195
196## Perform an action on a service
197do_sysvinit_action()
198{
199 self="do_sysvinit_action"
200 ## Name of service to start.
201 name="${1}"
202 ## The action to perform, normally "start", "stop" or "status".
203 action="${2}"
204
205 test ! -z "${name}" && test ! -z "${action}" ||
206 { echo "${self}: missing argument" >&2; return 1; }
207 if test -x "`which systemctl 2>/dev/null`"; then
208 systemctl -q ${action} "${name}"
209 elif test -x "`which service 2>/dev/null`"; then
210 service "${name}" ${action}
211 elif test -x "`which invoke-rc.d 2>/dev/null`"; then
212 invoke-rc.d "${name}" ${action}
213 elif test -x "/etc/rc.d/init.d/${name}"; then
214 "/etc/rc.d/init.d/${name}" "${action}"
215 elif test -x "/etc/init.d/${name}"; then
216 "/etc/init.d/${name}" "${action}"
217 fi
218}
219
220## Start a service
221start_init_script()
222{
223 do_sysvinit_action "${1}" start
224}
225
226## Stop the init script "name"
227stop_init_script()
228{
229 do_sysvinit_action "${1}" stop
230}
231
232## Extract chkconfig information from a sysvinit script.
233get_chkconfig_info()
234{
235 ## The script to extract the information from.
236 script="${1}"
237
238 set `sed -n 's/# *chkconfig: *\([0-9]*\) *\(.*\)/\1 \2/p' "${script}"`
239 ## Which runlevels should we start in?
240 runlevels="${1}"
241 ## How soon in the boot process will we start, from 00 (first) to 99
242 start_order="${2}"
243 ## How soon in the shutdown process will we stop, from 99 (first) to 00
244 stop_order="${3}"
245 test ! -z "${name}" || \
246 { echo "${self}: missing name" >&2; return 1; }
247 expr "${start_order}" + 0 > /dev/null 2>&1 && \
248 expr 0 \<= "${start_order}" > /dev/null 2>&1 && \
249 test `expr length "${start_order}"` -eq 2 > /dev/null 2>&1 || \
250 { echo "${self}: start sequence number must be between 00 and 99" >&2;
251 return 1; }
252 expr "${stop_order}" + 0 > /dev/null 2>&1 && \
253 expr 0 \<= "${stop_order}" > /dev/null 2>&1 && \
254 test `expr length "${stop_order}"` -eq 2 > /dev/null 2>&1 || \
255 { echo "${self}: stop sequence number must be between 00 and 99" >&2;
256 return 1; }
257}
258
259## Add a service to a runlevel
260addrunlevel()
261{
262 self="addrunlevel"
263 ## Service name.
264 name="${1}"
265
266 test -n "${name}" || \
267 { echo "${self}: missing argument" >&2; return 1; }
268 test -x "`which systemctl 2>/dev/null`" && \
269 { systemctl -q enable "${name}"; return; }
270 if test -x "/etc/rc.d/init.d/${name}"; then
271 init_d_path=/etc/rc.d
272 elif test -x "/etc/init.d/${name}"; then
273 init_d_path=/etc
274 else
275 { echo "${self}: error: unknown init type" >&2; return 1; }
276 fi
277 get_chkconfig_info "${init_d_path}/init.d/${name}" || return 1
278 # Redhat based sysvinit systems
279 if test -x "`which chkconfig 2>/dev/null`"; then
280 chkconfig --add "${name}"
281 # SUSE-based sysvinit systems
282 elif test -x "`which insserv 2>/dev/null`"; then
283 insserv "${name}"
284 # Debian/Ubuntu-based systems
285 elif test -x "`which update-rc.d 2>/dev/null`"; then
286 # Old Debians did not support dependencies
287 update-rc.d "${name}" defaults "${start_order}" "${stop_order}"
288 # Gentoo Linux
289 elif test -x "`which rc-update 2>/dev/null`"; then
290 rc-update add "${name}" default
291 # Generic sysvinit
292 elif test -n "${init_d_path}/rc0.d"
293 then
294 for locali in 0 1 2 3 4 5 6
295 do
296 target="${init_d_path}/rc${locali}.d/K${stop_order}${name}"
297 expr "${runlevels}" : ".*${locali}" >/dev/null && \
298 target="${init_d_path}/rc${locali}.d/S${start_order}${name}"
299 test -e "${init_d_path}/rc${locali}.d/"[KS][0-9]*"${name}" || \
300 ln -fs "${init_d_path}/init.d/${name}" "${target}"
301 done
302 else
303 { echo "${self}: error: unknown init type" >&2; return 1; }
304 fi
305}
306
307
308## Delete a service from a runlevel
309delrunlevel()
310{
311 self="delrunlevel"
312 ## Service name.
313 name="${1}"
314
315 test -n "${name}" ||
316 { echo "${self}: missing argument" >&2; return 1; }
317 systemctl -q disable "${name}" >/dev/null 2>&1
318 # Redhat-based systems
319 chkconfig --del "${name}" >/dev/null 2>&1
320 # SUSE-based sysvinit systems
321 insserv -r "${name}" >/dev/null 2>&1
322 # Debian/Ubuntu-based systems
323 update-rc.d -f "${name}" remove >/dev/null 2>&1
324 # Gentoo Linux
325 rc-update del "${name}" >/dev/null 2>&1
326 # Generic sysvinit
327 rm -f /etc/rc.d/rc?.d/[SK]??"${name}"
328 rm -f /etc/rc?.d/[SK]??"${name}"
329}
330
331
332terminate_proc() {
333 PROC_NAME="${1}"
334 SERVER_PID=`pidof $PROC_NAME 2> /dev/null`
335 if [ "$SERVER_PID" != "" ]; then
336 killall -TERM $PROC_NAME > /dev/null 2>&1
337 sleep 2
338 fi
339}
340
341
342maybe_run_python_bindings_installer() {
343 VBOX_INSTALL_PATH="${1}"
344
345 PYTHON=python
346 if [ ! `python -c 'print "test"' 2> /dev/null` = "test" ]; then
347 echo 1>&2 "Python not available, skipping bindings installation."
348 return 1
349 fi
350
351 echo 1>&2 "Python found: $PYTHON, installing bindings..."
352 # Pass install path via environment
353 export VBOX_INSTALL_PATH
354 $SHELL -c "cd $VBOX_INSTALL_PATH/sdk/installer && $PYTHON vboxapisetup.py install"
355 # remove files created during build
356 rm -rf $VBOX_INSTALL_PATH/sdk/installer/build
357
358 return 0
359}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use