1 | #!/bin/sh
|
---|
2 | # $Id: VBoxHeadlessXOrg.sh 43797 2012-11-02 12:08:57Z vboxsync $
|
---|
3 | #
|
---|
4 | # VirtualBox X Server auto-start service.
|
---|
5 | #
|
---|
6 | # Copyright (C) 2012 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 |
|
---|
17 | PATH=$PATH:/bin:/sbin:/usr/sbin
|
---|
18 |
|
---|
19 | ## Start one or several X servers in the background for use with headless
|
---|
20 | # rendering. For details, options and configuration see the usage() function
|
---|
21 | # further down.
|
---|
22 | #
|
---|
23 | # I have tried to follow the best practices I could find for writing a Linux
|
---|
24 | # service (and doing it in shell script) which should work well with
|
---|
25 | # traditional and modern service systems using minimal init or service files.
|
---|
26 | # In our case this boils down to:
|
---|
27 | # * Start with a single command line, stop using one of ${EXIT_SIGNALS} below.
|
---|
28 | # * Stopping with a signal can be done safely using the pid stored in the
|
---|
29 | # pid-file and our (presumably unique) command name. For this reason we
|
---|
30 | # only support running one instance of the service though.
|
---|
31 | # * Start in the foreground. Systems without proper service control can take
|
---|
32 | # care of the backgrounding in the init script.
|
---|
33 | # * Clean up all sub-processes (X servers) ourselves when we are stopped
|
---|
34 | # cleanly and don't provide any other way to clean them up automatically (in
|
---|
35 | # case we are stopped uncleanly) as we don't know of a generic safe way to
|
---|
36 | # do so, though some service management systems (i.e. systemd) can do so.
|
---|
37 | # (A more thorough automatic clean-up would be possible if Xorg didn't
|
---|
38 | # potentially have to be run as root, so that we could run all processes
|
---|
39 | # using a service-specific user account and just terminate all processes
|
---|
40 | # run by that user to clean up.)
|
---|
41 |
|
---|
42 | ## Default configuration file name.
|
---|
43 | # @note This is not very nice - /etc/default is actually Debian-specific.
|
---|
44 | CONFIGURATION_FILE=/etc/default/virtualbox
|
---|
45 | ## The name of this script.
|
---|
46 | SCRIPT_NAME="$0"
|
---|
47 | ## Command line we were called with.
|
---|
48 | SCRIPT_COMMAND_LINE="$0 $@"
|
---|
49 | ## The service name. Should match the init script name.
|
---|
50 | SERVICE_NAME="vboxheadlessxorg"
|
---|
51 | ## The descriptive service name.
|
---|
52 | SERVICE_LONG_NAME="VBoxHeadless X Server service"
|
---|
53 | ## Signals and conditions which may be used to terminate the service.
|
---|
54 | EXIT_SIGNALS="EXIT HUP INT QUIT ABRT TERM"
|
---|
55 | ## The default X server configuration directory.
|
---|
56 | DEFAULT_CONFIGURATION_FOLDER="/etc/vbox/headlessxorg.conf.d"
|
---|
57 |
|
---|
58 | ## Print usage information for the service script.
|
---|
59 | ## @todo Perhaps we should support some of the configuration file options from
|
---|
60 | # the command line. Opinions welcome.
|
---|
61 | ## @todo Possibly extract this information for the user manual.
|
---|
62 | usage() {
|
---|
63 | cat << EOF
|
---|
64 | Usage:
|
---|
65 |
|
---|
66 | $(basename "${SCRIPT_NAME}") [<options>]
|
---|
67 |
|
---|
68 | Start one or several X servers in the background for use with headless
|
---|
69 | rendering. We only support X.Org Server at the moment. Starting the X servers
|
---|
70 | is managed by dropping one or more files xorg.conf.<n> into a configuration
|
---|
71 | directory (by default ${DEFAULT_CONFIGURATION_FOLDER} - this can be overridden
|
---|
72 | in the configuration file (see below). We will attempt to start an X server
|
---|
73 | process for each configuration file using display number <n>.
|
---|
74 |
|
---|
75 | Options:
|
---|
76 |
|
---|
77 | -c|--conf-file) Specify an alternative locations for the configuration
|
---|
78 | file. The default location is \"${CONFIGURATION_FILE}\".
|
---|
79 |
|
---|
80 | --help|--usage Print this text.
|
---|
81 |
|
---|
82 | The optional configuration file should contain a series of lines of the form
|
---|
83 | "KEY=value". It will be read in as a command shell sub-script. Here is the
|
---|
84 | current list of possible key settings with a short explanation. Usually it
|
---|
85 | should be sufficient to change the value of \${HEADLESS_X_ORG_USERS} and to
|
---|
86 | leave all other settings unchanged.
|
---|
87 |
|
---|
88 | HEADLESS_X_ORG_CONFIGURATION_FOLDER
|
---|
89 | The folder where the configuration files for the X servers are to be found.
|
---|
90 |
|
---|
91 | HEADLESS_X_ORG_LOG_FOLDER
|
---|
92 | The folder where log files will be created.
|
---|
93 |
|
---|
94 | HEADLESS_X_ORG_LOG_FILE
|
---|
95 | The main log file name.
|
---|
96 |
|
---|
97 | HEADLESS_X_ORG_RUN_FOLDER
|
---|
98 | The folder to store run-time data in.
|
---|
99 |
|
---|
100 | HEADLESS_X_ORG_CHECK_PREREQUISITES
|
---|
101 | Shell command to execute to check whether all dependencies for the X
|
---|
102 | servers are available - usually a test for a device node. This will be
|
---|
103 | repeated at regular intervals until it returns successfully, so a command
|
---|
104 | which can be executed internally be the shell (like "[") is preferable.
|
---|
105 | The default command waits until the udev event queue has settled.
|
---|
106 |
|
---|
107 | HEADLESS_X_ORG_USERS
|
---|
108 | List of users who will have access to the X servers started and for whom we
|
---|
109 | will provide the configuration details via VirtualBox extra data. This
|
---|
110 | variable is only used by the commands in the default configuration
|
---|
111 | (\${HEADLESS_X_ORG_SERVER_PRE_COMMAND} and
|
---|
112 | \${HEADLESS_X_ORG_SERVER_POST_COMMAND}), and not by the service itself.
|
---|
113 |
|
---|
114 | HEADLESS_X_ORG_SERVER_PRE_COMMAND
|
---|
115 | Command to execute once to perform any set-up needed before starting the
|
---|
116 | X servers, such as setting up the X server authentication. The default
|
---|
117 | command creates an authority file for each of the users in the list
|
---|
118 | \${HEADLESS_X_ORG_USERS}.
|
---|
119 |
|
---|
120 | HEADLESS_X_ORG_SERVER_COMMAND
|
---|
121 | The default X server start-up command, containing the variables "${screen}"
|
---|
122 | for the screen number, "${conf_file}" for the configuration file path and
|
---|
123 | ${log_file} for the log file. The variables must be quoted for expansion
|
---|
124 | at evaluation time, e.g.
|
---|
125 | HEADLESS_X_ORG_SERVER_COMMAND="Xorg :\${screen} [...] > \"\${log_file}\"
|
---|
126 |
|
---|
127 | HEADLESS_X_ORG_SERVER_POST_COMMAND
|
---|
128 | Command to execute once the X servers have been successfully started. By
|
---|
129 | default this stores the service configuration information to VirtualBox
|
---|
130 | extra data for each of the users in the list \${HEADLESS_X_ORG_USERS}.
|
---|
131 |
|
---|
132 | HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE
|
---|
133 | The default X server log file name, containing the variable "\${screen}" for
|
---|
134 | the screen number. The variable must be quoted for expansion at evaluation
|
---|
135 | time, e.g.
|
---|
136 | HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE="Xorg.\${screen}.log"
|
---|
137 | EOF
|
---|
138 | }
|
---|
139 |
|
---|
140 | # Default configuration.
|
---|
141 | HEADLESS_X_ORG_CONFIGURATION_FOLDER="${DEFAULT_CONFIGURATION_FOLDER}"
|
---|
142 | HEADLESS_X_ORG_LOG_FOLDER="/var/log/${SERVICE_NAME}"
|
---|
143 | HEADLESS_X_ORG_LOG_FILE="${SERVICE_NAME}.log"
|
---|
144 | HEADLESS_X_ORG_RUN_FOLDER="/var/run/${SERVICE_NAME}"
|
---|
145 | HEADLESS_X_ORG_CHECK_PREREQUISITES="udevadm settle || ! udevadm -V"
|
---|
146 | X_AUTH_FILE="${HEADLESS_X_ORG_RUN_FOLDER}/xauth"
|
---|
147 | HEADLESS_X_ORG_SERVER_PRE_COMMAND="echo > \"${X_AUTH_FILE}\"; xauth -f \"${X_AUTH_FILE}\" add :0 . \"\$(dd if=/dev/urandom count=1 bs=16 2>/dev/null | od -An -x)\"; for i in ${HEADLESS_X_ORG_USERS}; do cp \"${X_AUTH_FILE}\" \"${X_AUTH_FILE}.${i}\"; chown \"${i}\" \"${X_AUTH_FILE}.${i}\"; done"
|
---|
148 | HEADLESS_X_ORG_SERVER_COMMAND="Xorg :\${screen} -auth \"${HEADLESS_X_ORG_RUN_FOLDER}/xauth\" -config \"\${conf_file}\" -logverbose 0 -logfile /dev/null -verbose 7 > \"\${log_file}\" 2>&1"
|
---|
149 | HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE="Xorg.\${screen}.log"
|
---|
150 |
|
---|
151 | ## The function definition at the start of every non-trivial shell script!
|
---|
152 | abort() {
|
---|
153 | ## $@, ... Error text to output to standard error in printf format.
|
---|
154 | printf "$@" >&2
|
---|
155 | exit 1
|
---|
156 | }
|
---|
157 |
|
---|
158 | ## Milder version of abort, when we can't continue because of a valid condition.
|
---|
159 | abandon() {
|
---|
160 | ## $@, ... Text to output to standard error in printf format.
|
---|
161 | printf "$@" >&2
|
---|
162 | exit 0
|
---|
163 | }
|
---|
164 |
|
---|
165 | abort_usage() {
|
---|
166 | usage >&2
|
---|
167 | abort "$@"
|
---|
168 | }
|
---|
169 |
|
---|
170 | # Print a banner message
|
---|
171 | banner() {
|
---|
172 | cat << EOF
|
---|
173 | ${VBOX_PRODUCT} VBoxHeadless X Server start-up service Version ${VBOX_VERSION_STRING}
|
---|
174 | (C) 2005-${VBOX_C_YEAR} ${VBOX_VENDOR}
|
---|
175 | All rights reserved.
|
---|
176 |
|
---|
177 | EOF
|
---|
178 | }
|
---|
179 |
|
---|
180 | # Get the directory where the script is located.
|
---|
181 | VBOX_FOLDER="$(dirname "${SCRIPT_NAME}")"
|
---|
182 | VBOX_FOLDER=$(cd "${VBOX_FOLDER}" && pwd)
|
---|
183 | [ -d "${VBOX_FOLDER}" ] ||
|
---|
184 | abort "Failed to change to directory ${VBOX_FOLDER}.\n"
|
---|
185 | # And change to the root directory so we don't hold any other open.
|
---|
186 | cd /
|
---|
187 |
|
---|
188 | [ -r "${VBOX_FOLDER}/scripts/generated.sh" ] ||
|
---|
189 | abort "${LOG_FILE}" "Failed to find installation information in ${VBOX_FOLDER}.\n"
|
---|
190 | . "${VBOX_FOLDER}/scripts/generated.sh"
|
---|
191 |
|
---|
192 | # Parse our arguments.
|
---|
193 | while [ "$#" -gt 0 ]; do
|
---|
194 | case $1 in
|
---|
195 | -c|--conf-file)
|
---|
196 | [ "$#" -gt 1 ] ||
|
---|
197 | {
|
---|
198 | banner
|
---|
199 | abort "%s requires at least one argument.\n" "$1"
|
---|
200 | }
|
---|
201 | CONFIGURATION_FILE="$2"
|
---|
202 | shift
|
---|
203 | ;;
|
---|
204 | --help|--usage)
|
---|
205 | banner
|
---|
206 | usage
|
---|
207 | exit 0
|
---|
208 | ;;
|
---|
209 | *)
|
---|
210 | banner
|
---|
211 | abort_usage "Unknown argument $1.\n"
|
---|
212 | ;;
|
---|
213 | esac
|
---|
214 | shift
|
---|
215 | done
|
---|
216 |
|
---|
217 | [ -r "${CONFIGURATION_FILE}" ] && . "${CONFIGURATION_FILE}"
|
---|
218 |
|
---|
219 | # If something fails here we will catch it when we create the directory.
|
---|
220 | [ -e "${HEADLESS_X_ORG_LOG_FOLDER}" ] &&
|
---|
221 | [ -d "${HEADLESS_X_ORG_LOG_FOLDER}" ] &&
|
---|
222 | rm -rf "${HEADLESS_X_ORG_LOG_FOLDER}.old" 2> /dev/null &&
|
---|
223 | mv "${HEADLESS_X_ORG_LOG_FOLDER}" "${HEADLESS_X_ORG_LOG_FOLDER}.old" 2> /dev/null
|
---|
224 | mkdir -p "${HEADLESS_X_ORG_LOG_FOLDER}" 2>/dev/null ||
|
---|
225 | {
|
---|
226 | banner
|
---|
227 | abort "Failed to create log folder \"${HEADLESS_X_ORG_LOG_FOLDER}\".\n"
|
---|
228 | }
|
---|
229 | mkdir -p "${HEADLESS_X_ORG_RUN_FOLDER}" 2>/dev/null ||
|
---|
230 | {
|
---|
231 | banner
|
---|
232 | abort "Failed to create run folder \"${HEADLESS_X_ORG_RUN_FOLDER}\".\n"
|
---|
233 | }
|
---|
234 | exec > "${HEADLESS_X_ORG_LOG_FOLDER}/${HEADLESS_X_ORG_LOG_FILE}" 2>&1
|
---|
235 |
|
---|
236 | banner
|
---|
237 |
|
---|
238 | # Wait for our dependencies to become available. The increasing delay is
|
---|
239 | # probably not the cleverest way to do this.
|
---|
240 | DELAY=1
|
---|
241 | while ! eval ${HEADLESS_X_ORG_CHECK_PREREQUISITES}; do
|
---|
242 | sleep $((${DELAY} / 10 + 1))
|
---|
243 | DELAY=$((${DELAY} + 1))
|
---|
244 | done
|
---|
245 |
|
---|
246 | # Do any pre-start setup.
|
---|
247 | eval "${HEADLESS_X_ORG_SERVER_PRE_COMMAND}"
|
---|
248 |
|
---|
249 | X_SERVER_PIDS=""
|
---|
250 | trap "kill \${X_SERVER_PIDS} 2>/dev/null" ${EXIT_SIGNALS}
|
---|
251 | space="" # Hack to put spaces between the pids but not before or after.
|
---|
252 | for conf_file in "${HEADLESS_X_ORG_CONFIGURATION_FOLDER}"/*; do
|
---|
253 | [ x"${conf_file}" = x"${HEADLESS_X_ORG_CONFIGURATION_FOLDER}/*" ] &&
|
---|
254 | ! [ -e "${conf_file}" ] &&
|
---|
255 | abort "No configuration files found.\n"
|
---|
256 | filename="$(basename "${conf_file}")"
|
---|
257 | screen="$(expr "${filename}" : "xorg\.conf\.\(.*\)")"
|
---|
258 | [ 0 -le "${screen}" ] 2>/dev/null ||
|
---|
259 | abort "Badly formed file name \"${conf_file}\".\n"
|
---|
260 | log_file="${HEADLESS_X_ORG_LOG_FOLDER}/$(eval echo "${HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE}")"
|
---|
261 | eval "${HEADLESS_X_ORG_SERVER_COMMAND}" "&"
|
---|
262 | X_SERVER_PIDS="${X_SERVER_PIDS}${space}$!"
|
---|
263 | space=" "
|
---|
264 | done
|
---|
265 |
|
---|
266 | # Do any post-start work.
|
---|
267 | eval "${HEADLESS_X_ORG_SERVER_POST_COMMAND}"
|
---|
268 |
|
---|
269 | wait
|
---|