1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # VirtualBox X Server auto-start script.
|
---|
4 | #
|
---|
5 | # Copyright (C) 2012 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 |
|
---|
16 | PATH=$PATH:/bin:/sbin:/usr/sbin
|
---|
17 |
|
---|
18 | ## Start one or several X servers in the background for use with headless
|
---|
19 | # rendering. We only support X.Org Server at the moment. Starting the X
|
---|
20 | # servers is managed by dropping one or more files xorg.conf.<n> into the
|
---|
21 | # directory ${HEADLESS_X_ORG_CONFIGURATION_DIRECTORY} - the default value
|
---|
22 | # below can be overridden in the configuration file. We will attempt to start
|
---|
23 | # an X server process for each configuration file using display number <n>.
|
---|
24 |
|
---|
25 | # I have tried to follow the best practices I could find for writing a Linux
|
---|
26 | # daemon process (and doing it in shell script) which should work well with
|
---|
27 | # traditional and modern service systems using minimal init or service files.
|
---|
28 | # In our case this boils down to:
|
---|
29 | # * Start with a single command line, stop using one of ${EXIT_SIGNALS} below.
|
---|
30 | # * Stopping with a signal can be done safely using the pid stored in the
|
---|
31 | # pid-file and our (presumably unique) command name. For this reason we only
|
---|
32 | # support running one instance of the service though.
|
---|
33 | # * Support starting in the foreground for systems with proper service control.
|
---|
34 | # * Support backgrounding with a pid-file for other systems.
|
---|
35 | # * Clean up all sub-processes (X servers) ourselves when we are stopped
|
---|
36 | # cleanly and don't provide any other way to clean them up automatically (in
|
---|
37 | # case we are stopped uncleanly) as we don't know of a generic safe way to
|
---|
38 | # do so, though some service management systems (i.e. systemd) can do so.
|
---|
39 | # (A more thorough automatic clean-up would be possible if Xorg didn't
|
---|
40 | # potentially have to be run as root, so that we could run all processes
|
---|
41 | # using a service-specific user account and just terminate all processes
|
---|
42 | # run by that user to clean up.)
|
---|
43 | # * For this reason, our pid-file only contains the pid of the master process.
|
---|
44 | # * To simplify system service and start-up message handling, we write
|
---|
45 | # Debian-conform progress information to standard output (what processes we
|
---|
46 | # are starting, information if we do something slow) and errors to standard
|
---|
47 | # error. The init script or service file can log a single success or
|
---|
48 | # failure line when we are ready in whatever way the system expects and deal
|
---|
49 | # with our output in the most appropriate way for the system. Some systems
|
---|
50 | # require output, others don't mind, and Debian-conform information should
|
---|
51 | # actually fit everyone's requirements (others are more lax about the
|
---|
52 | # format).
|
---|
53 |
|
---|
54 | ## Configuration file name.
|
---|
55 | # Don't use vbox.cfg as that is currently automatically created and deleted.
|
---|
56 | # Don't use /etc/default/virtualbox as that is Debian policy only and not very
|
---|
57 | # nice. Let's try to use this in future and phase out the other two.
|
---|
58 | ## @todo Should we be using /etc/virtualbox instead of /etc/vbox?
|
---|
59 | CONFIGURATION_FILE=/etc/vbox/vbox.conf
|
---|
60 |
|
---|
61 | ## The service name. Should match the init script name.
|
---|
62 | SERVICE_NAME="vboxheadlessxorg"
|
---|
63 | ## The descriptive service name.
|
---|
64 | SERVICE_LONG_NAME="VBoxHeadless X Server service"
|
---|
65 | ## Timeout in seconds when shutting down the service.
|
---|
66 | SERVICE_SHUTDOWN_TIMEOUT=5
|
---|
67 | ## Signals and conditions which may be used to terminate the service.
|
---|
68 | EXIT_SIGNALS="EXIT HUP INT QUIT ABRT TERM"
|
---|
69 |
|
---|
70 | # Default settings values, override in the configuration file.
|
---|
71 | ## The directory where the configuration files for the X servers are dropped.
|
---|
72 | HEADLESS_X_ORG_CONFIGURATION_DIRECTORY=/etc/vbox/headlessxorg.conf.d
|
---|
73 | ## The path to the pidfile for the service.
|
---|
74 | HEADLESS_X_ORG_PIDFILE="/var/run/${SERVICE_NAME}.pid"
|
---|
75 |
|
---|
76 | ## The function definition at the start of every non-trivial shell script!
|
---|
77 | abort() {
|
---|
78 | stop 2>/dev/null
|
---|
79 | ## $@, ... Error text to output to standard error in printf format.
|
---|
80 | printf "$@" >&2
|
---|
81 | exit 1
|
---|
82 | }
|
---|
83 |
|
---|
84 | ## Milder version of abort, when we can't continue because of a valid condition.
|
---|
85 | abandon() {
|
---|
86 | stop 2>/dev/null
|
---|
87 | ## $@, ... Text to output to standard error in printf format.
|
---|
88 | printf "$@" >&2
|
---|
89 | exit 0
|
---|
90 | }
|
---|
91 |
|
---|
92 | usage() {
|
---|
93 | cat << EOF
|
---|
94 | Usage:
|
---|
95 |
|
---|
96 | $(basename $0) [<options>]
|
---|
97 |
|
---|
98 | Do any system-wide set-up required to properly run the copy of VirtualBox
|
---|
99 | residing in the current directory. If no options are specified, everything
|
---|
100 | except --no-udev-and --log-file is assumed.
|
---|
101 |
|
---|
102 | Options:
|
---|
103 |
|
---|
104 | -p|--pidfile <name> Specify the name of the file to save the pids of child
|
---|
105 | processes in. Pass in an empty string to disable
|
---|
106 | pid-file creation.
|
---|
107 |
|
---|
108 | -d|-daemonize) Detach fron the terminal and continue running in the
|
---|
109 | background.
|
---|
110 |
|
---|
111 | --help|--usage Print this text.
|
---|
112 | EOF
|
---|
113 | }
|
---|
114 |
|
---|
115 | abort_usage() {
|
---|
116 | usage > &2
|
---|
117 | abort "$@"
|
---|
118 | }
|
---|
119 |
|
---|
120 | # Change to the directory where the script is located.
|
---|
121 | MY_DIR="$(dirname "$0")"
|
---|
122 | MY_DIR=`cd "${MY_DIR}" && pwd`
|
---|
123 | [ -d "${MY_DIR}" ] ||
|
---|
124 | abort "Failed to change to directory ${MY_DIR}.\n"
|
---|
125 |
|
---|
126 | # Get installation configuration
|
---|
127 | [ -r /etc/vbox/vbox.cfg ] || abort "/etc/vbox/vbox.cfg not found.\n"
|
---|
128 | . /etc/vbox/vbox.cfg
|
---|
129 |
|
---|
130 | [ -r scripts/generated.sh ] ||
|
---|
131 | abort "${LOG_FILE}" "Failed to find installation information in ${MY_DIR}.\n"
|
---|
132 | . scripts/generated.sh
|
---|
133 |
|
---|
134 | [ -r /etc/vbox/vbox.conf ] && . /etc/vbox/vbox.conf
|
---|
135 |
|
---|
136 | cat << EOF
|
---|
137 | ${VBOX_PRODUCT} VBoxHeadless X Server start-up service Version ${VBOX_VERSION_STRING}
|
---|
138 | (C) 2005-${VBOX_C_YEAR} ${VBOX_VENDOR}
|
---|
139 | All rights reserved.
|
---|
140 |
|
---|
141 | EOF
|
---|
142 |
|
---|
143 | # Parse our arguments.
|
---|
144 | while true; do
|
---|
145 | case $1 in
|
---|
146 | -p|--pidfile)
|
---|
147 | [ -n "$2" ] || abort_usage "Missing argument for $1.\n"
|
---|
148 | ARGUMENT_LIST="$ARGUMENT_LIST $1 $2"
|
---|
149 | HEADLESS_X_ORG_PIDFILE="$2"
|
---|
150 | shift 2
|
---|
151 | ;;HEADLESS_X_ORG_PIDFILE
|
---|
152 | -d|-daemonize)
|
---|
153 | DAEMONIZE=true
|
---|
154 | shift
|
---|
155 | ;;
|
---|
156 | --help|--usage)
|
---|
157 | usage
|
---|
158 | exit 0
|
---|
159 | ;;
|
---|
160 | *)
|
---|
161 | abort_usage "Unknown argument $1.\n"
|
---|
162 | ;;
|
---|
163 | esac
|
---|
164 | done
|
---|
165 |
|
---|
166 | [ -f "${HEADLESS_X_ORG_PIDFILE}" ] &&
|
---|
167 | ps -p "$(cat "${HEADLESS_X_ORG_PIDFILE}")" -o comm | grep -q "${SERVICE_NAME}" &&
|
---|
168 | abort "The service appears to be already running.\n"
|
---|
169 |
|
---|
170 | PIDFILE_DIRECTORY="$(dirname "${HEADLESS_X_ORG_PIDFILE}")"
|
---|
171 | [ -n "${HEADLESS_X_ORG_PIDFILE}" ] &&
|
---|
172 | { [ ! -d "${PIDFILE_DIRECTORY}" ] && [ ! -w "${PIDFILE_DIRECTORY}" ] } &&
|
---|
173 | abort "Can't write to pid-file directory ${PIDFILE_DIRECTORY}.\n"
|
---|
174 |
|
---|
175 | { [ -d "/var/log" ] && [ -w "/var/log" ] } ||
|
---|
176 | abort "Can't write to /var/log.\n"
|
---|
177 |
|
---|
178 | # Refuse to daemonize without creating a pid-file. Change this if we think of
|
---|
179 | # a good reason.
|
---|
180 | { [ -n "${DAEMONIZE}" ] && [ -z "${HEADLESS_X_ORG_PIDFILE}" ] } &&
|
---|
181 | abort "Daemonizing without creating a pid-file is not supported.\n"
|
---|
182 |
|
---|
183 | # Double background from shell script, disconnecting all standard streams, to
|
---|
184 | # daemonise. This may fail if someone has connected something to another file
|
---|
185 | # descriptor. This is indented (see e.g. fghack in the daemontools package).
|
---|
186 | if [ -n "${DAEMONIZE}" ]; then
|
---|
187 | ("$0" -p "${HEADLESS_X_ORG_PIDFILE}" < /dev/null > /dev/null 2>&1 &
|
---|
188 | echo "Created daemon process with PID $!.") &
|
---|
189 | exit 0
|
---|
190 | fi
|
---|
191 |
|
---|
192 | X_SERVER_PIDS=""
|
---|
193 | trap "kill \"\${X_SERVER_PIDS}\"; exit 0" ${EXIT_SIGNALS}
|
---|
194 | space="" # Hack to put spaces between the pids but not before or after.
|
---|
195 | for file in "${HEADLESS_X_ORG_CONFIGURATION_DIRECTORY}"/xorg.conf.*; do
|
---|
196 | filename="$(basename "${file}")"
|
---|
197 | expr "${filename}" : "xorg.conf.[0-9]*" ||
|
---|
198 | { stop; abort "Badly formed file name \"${file}\".\n" }
|
---|
199 | screen="${filename##*.}"
|
---|
200 | Xorg ":${screen}" -config "${file}" > /dev/null 2>&1 & ||
|
---|
201 | abort "Failed to create screen ${screen}.\n"
|
---|
202 | X_SERVER_PIDS="${X_SERVER_PIDS}${space}$!"
|
---|
203 | space=" "
|
---|
204 | done
|
---|
205 | [ -z "${HEADLESS_X_ORG_PIDFILE}" ] &&
|
---|
206 | echo "$$" > "${HEADLESS_X_ORG_PIDFILE}"
|
---|
207 | wait
|
---|