VirtualBox

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

最後變更 在這個檔案從57711是 57711,由 vboxsync 提交於 10 年 前

Installer/linux: refactored service script handling to external, hopefully installer-type agnostic scripts.

  • 屬性 svn:eol-style 設為 LF
  • 屬性 svn:executable 設為 *
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 12.2 KB
 
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.alldomusa.eu.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## Do we have bzip2?
112check_bzip2()
113{
114 if ! ls /bin/bzip2 /usr/bin/bzip2 /usr/local/bin/bzip2 2> /dev/null | grep bzip2 > /dev/null; then
115 echo 1>&2 "Please install the bzip2 utility."
116 log "Please install bzip2."
117 return 1
118 fi
119 return 0
120}
121
122## Do we have GNU make?
123check_gmake()
124{
125make --version 2>&1 | grep GNU > /dev/null
126 if [ ! $? = 0 ]; then
127 echo 1>&2 "Please install GNU make."
128 log "Please install GNU make."
129 return 1
130 fi
131 return 0
132}
133
134## Can we find the kernel source?
135check_ksource()
136{
137 ro_KBUILD_DIR=/lib/modules/`uname -r`/build
138 if [ ! -d $ro_KBUILD_DIR/include ]; then
139 ro_KBUILD_DIR=/usr/src/linux
140 if [ ! -d $ro_KBUILD_DIR/include ]; then
141 echo 1>&2 "Please install the build and header files for your current Linux kernel."
142 echo 1>&2 "The current kernel version is `uname -r`"
143 ro_KBUILD_DIR=""
144 log "Could not find the Linux kernel header files - the directories"
145 log " /lib/modules/`uname -r`/build/include and /usr/src/linux/include"
146 log " do not exist."
147 return 1
148 fi
149 fi
150 return 0
151}
152
153## Is GCC installed?
154check_gcc()
155{
156 ro_gcc_version=`gcc --version 2> /dev/null | head -n 1`
157 if [ "$ro_gcc_version" = "" ]; then
158 echo 1>&2 "Please install the GNU compiler."
159 log "Please install the GNU compiler."
160 return 1
161 fi # GCC installed
162 return 0
163}
164
165## Is bash installed? You never know...
166check_bash()
167{
168 if [ ! -x /bin/bash ]; then
169 echo 1>&2 "Please install GNU bash."
170 log "Please install GNU bash."
171 return 1
172 fi
173 return 0
174}
175
176## Is perl installed?
177check_perl()
178{
179 if [ ! `perl -e 'print "test"' 2> /dev/null` = "test" ]; then
180 echo 1>&2 "Please install perl."
181 echo "Please install perl."
182 return 1
183 fi
184 return 0
185}
186
187## Creates a systemd wrapper in /lib for an LSB init script
188systemd_wrap_init_script()
189{
190 self="systemd_wrap_init_script"
191 ## The init script to be installed. The file may be copied or referenced.
192 script="$(readlink -f -- "${1}")"
193 ## Name for the service.
194 name="$2"
195 test -x "$script" && test ! "$name" = "" || \
196 { echo "$self: invalid arguments" >&2 && return 1; }
197 test -d /usr/lib/systemd/system && unit_path=/usr/lib/systemd/system
198 test -d /lib/systemd/system && unit_path=/lib/systemd/system
199 test -n "${unit_path}" || \
200 { echo "$self: systemd unit path not found" >&2 && return 1; }
201 description=`sed -n 's/# *Short-Description: *\(.*\)/\1/p' "${script}"`
202 required=`sed -n 's/# *Required-Start: *\(.*\)/\1/p' "${script}"`
203 runlevels=`sed -n 's/# *Default-Start: *\(.*\)/\1/p' "${script}"`
204 before=`for i in ${runlevels}; do printf "runlevel${i}.target "; done`
205 after=`for i in ${required}; do printf "${i}.service "; done`
206 cat > "${unit_path}/${name}.service" << EOF
207[Unit]
208SourcePath=${script}
209Description=${description}
210Before=${before}shutdown.target
211After=${after}
212Conflicts=shutdown.target
213
214[Service]
215Type=forking
216Restart=no
217TimeoutSec=5min
218IgnoreSIGPIPE=no
219KillMode=process
220GuessMainPID=no
221RemainAfterExit=yes
222ExecStart=${script} start
223ExecStop=${script} stop
224
225[Install]
226WantedBy=multi-user.target
227EOF
228}
229
230## Installs a file containing a shell script as an init script
231install_init_script()
232{
233 self="install_init_script"
234 ## The init script to be installed. The file may be copied or referenced.
235 script="$1"
236 ## Name for the service.
237 name="$2"
238
239 test -x "$script" && test ! "$name" = "" ||
240 { echo "$self: invalid arguments" >&2; return 1; }
241 test -x "`which systemctl 2>/dev/null`" &&
242 { systemd_wrap_init_script "$script" "$name"; return; }
243 if test -d /etc/rc.d/init.d; then
244 cp "$script" "/etc/rc.d/init.d/$name" &&
245 chmod 755 "/etc/rc.d/init.d/$name"
246 elif test -d /etc/init.d; then
247 cp "$script" "/etc/init.d/$name" &&
248 chmod 755 "/etc/init.d/$name"
249 else
250 { echo "${self}: error: unknown init type" >&2; return 1; }
251 fi
252}
253
254## Remove the init script "name"
255remove_init_script()
256{
257 self="remove_init_script"
258 ## Name of the service to remove.
259 name="$1"
260
261 test -n "$name" ||
262 { echo "$self: missing argument"; return 1; }
263 rm -f /lib/systemd/system/"$name".service /usr/lib/systemd/system/"$name".service
264 rm -f "/etc/rc.d/init.d/$name"
265 rm -f "/etc/init.d/$name"
266}
267
268## Perform an action on a service
269do_sysvinit_action()
270{
271 self="do_sysvinit_action"
272 ## Name of service to start.
273 name="${1}"
274 ## The action to perform, normally "start", "stop" or "status".
275 action="${2}"
276
277 test ! -z "${name}" && test ! -z "${action}" ||
278 { echo "${self}: missing argument" >&2; return 1; }
279 if test -x "`which systemctl 2>/dev/null`"; then
280 systemctl -q ${action} "${name}"
281 elif test -x "`which service 2>/dev/null`"; then
282 service "${name}" ${action}
283 elif test -x "`which invoke-rc.d 2>/dev/null`"; then
284 invoke-rc.d "${name}" ${action}
285 elif test -x "/etc/rc.d/init.d/${name}"; then
286 "/etc/rc.d/init.d/${name}" "${action}"
287 elif test -x "/etc/init.d/${name}"; then
288 "/etc/init.d/${name}" "${action}"
289 fi
290}
291
292## Start a service
293start_init_script()
294{
295 do_sysvinit_action "${1}" start
296}
297
298## Stop the init script "name"
299stop_init_script()
300{
301 do_sysvinit_action "${1}" stop
302}
303
304## Extract chkconfig information from a sysvinit script.
305get_chkconfig_info()
306{
307 ## The script to extract the information from.
308 script="${1}"
309
310 set `sed -n 's/# *chkconfig: *\([0-9]*\) *\(.*\)/\1 \2/p' "${script}"`
311 ## Which runlevels should we start in?
312 runlevels="${1}"
313 ## How soon in the boot process will we start, from 00 (first) to 99
314 start_order="${2}"
315 ## How soon in the shutdown process will we stop, from 99 (first) to 00
316 stop_order="${3}"
317 test ! -z "${name}" || \
318 { echo "${self}: missing name" >&2; return 1; }
319 expr "${start_order}" + 0 > /dev/null 2>&1 && \
320 expr 0 \<= "${start_order}" > /dev/null 2>&1 && \
321 test `expr length "${start_order}"` -eq 2 > /dev/null 2>&1 || \
322 { echo "${self}: start sequence number must be between 00 and 99" >&2;
323 return 1; }
324 expr "${stop_order}" + 0 > /dev/null 2>&1 && \
325 expr 0 \<= "${stop_order}" > /dev/null 2>&1 && \
326 test `expr length "${stop_order}"` -eq 2 > /dev/null 2>&1 || \
327 { echo "${self}: stop sequence number must be between 00 and 99" >&2;
328 return 1; }
329}
330
331## Add a service to a runlevel
332addrunlevel()
333{
334 self="addrunlevel"
335 ## Service name.
336 name="${1}"
337
338 test -n "${name}" || \
339 { echo "${self}: missing argument" >&2; return 1; }
340 test -x "`which systemctl 2>/dev/null`" && \
341 { systemctl -q enable "${name}"; return; }
342 if test -x "/etc/rc.d/init.d/${name}"; then
343 init_d_path=/etc/rc.d
344 elif test -x "/etc/init.d/${name}"; then
345 init_d_path=/etc
346 else
347 { echo "${self}: error: unknown init type" >&2; return 1; }
348 fi
349 get_chkconfig_info "${init_d_path}/init.d/${name}" || return 1
350 # Redhat based sysvinit systems
351 if test -x "`which chkconfig 2>/dev/null`"; then
352 chkconfig --add "${name}"
353 # SUSE-based sysvinit systems
354 elif test -x "`which insserv 2>/dev/null`"; then
355 insserv "${name}"
356 # Debian/Ubuntu-based systems
357 elif test -x "`which update-rc.d 2>/dev/null`"; then
358 # Old Debians did not support dependencies
359 update-rc.d "${name}" defaults "${start_order}" "${stop_order}"
360 # Gentoo Linux
361 elif test -x "`which rc-update 2>/dev/null`"; then
362 rc-update add "${name}" default
363 # Generic sysvinit
364 elif test -n "${init_d_path}/rc0.d"
365 then
366 for locali in 0 1 2 3 4 5 6
367 do
368 target="${init_d_path}/rc${locali}.d/K${stop_order}${name}"
369 expr "${runlevels}" : ".*${locali}" >/dev/null && \
370 target="${init_d_path}/rc${locali}.d/S${start_order}${name}"
371 test -e "${init_d_path}/rc${locali}.d/"[KS][0-9]*"${name}" || \
372 ln -fs "${init_d_path}/init.d/${name}" "${target}"
373 done
374 else
375 { echo "${self}: error: unknown init type" >&2; return 1; }
376 fi
377}
378
379
380## Delete a service from a runlevel
381delrunlevel()
382{
383 self="delrunlevel"
384 ## Service name.
385 name="${1}"
386
387 test -n "${name}" ||
388 { echo "${self}: missing argument" >&2; return 1; }
389 systemctl -q disable "${name}" >/dev/null 2>&1
390 # Redhat-based systems
391 chkconfig --del "${name}" >/dev/null 2>&1
392 # SUSE-based sysvinit systems
393 insserv -r "${name}" >/dev/null 2>&1
394 # Debian/Ubuntu-based systems
395 update-rc.d -f "${name}" remove >/dev/null 2>&1
396 # Gentoo Linux
397 rc-update del "${name}" >/dev/null 2>&1
398 # Generic sysvinit
399 rm -f /etc/rc.d/rc?.d/[SK]??"${name}"
400 rm -f /etc/rc?.d/[SK]??"${name}"
401}
402
403
404terminate_proc() {
405 PROC_NAME="${1}"
406 SERVER_PID=`pidof $PROC_NAME 2> /dev/null`
407 if [ "$SERVER_PID" != "" ]; then
408 killall -TERM $PROC_NAME > /dev/null 2>&1
409 sleep 2
410 fi
411}
412
413
414maybe_run_python_bindings_installer() {
415 VBOX_INSTALL_PATH="${1}"
416
417 PYTHON=python
418 if [ ! `python -c 'print "test"' 2> /dev/null` = "test" ]; then
419 echo 1>&2 "Python not available, skipping bindings installation."
420 return 1
421 fi
422
423 echo 1>&2 "Python found: $PYTHON, installing bindings..."
424 # Pass install path via environment
425 export VBOX_INSTALL_PATH
426 $SHELL -c "cd $VBOX_INSTALL_PATH/sdk/installer && $PYTHON vboxapisetup.py install"
427 # remove files created during build
428 rm -rf $VBOX_INSTALL_PATH/sdk/installer/build
429
430 return 0
431}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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