Changeset 43634 in vbox
- Timestamp:
- Oct 12, 2012 12:42:47 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Installer/linux/VBoxHeadlessXOrg.sh
r43580 r43634 22 22 # below can be overridden in the configuration file. We will attempt to start 23 23 # an X server process for each configuration file using display number <n>. 24 24 # For usage and options see the usage() function below this comment block. 25 # 25 26 # I have tried to follow the best practices I could find for writing a Linux 26 27 # daemon process (and doing it in shell script) which should work well with … … 45 46 # Debian-conform progress information to standard output (what processes we 46 47 # 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). 48 # error. This should allow us to write pretty generic init/startup scripts 49 # for different distributions which produce more-or-less correct output for 50 # the system they run on. 51 52 ## Print usage information for the service script. 53 usage() { 54 cat << EOF 55 Usage: 56 57 $(basename "${SCRIPT_NAME}") [<options>] 58 59 Do any system-wide set-up required to properly run the copy of VirtualBox 60 residing in the current directory. If no options are specified, everything 61 except --no-udev-and --log-file is assumed. 62 63 Options: 64 65 -d|--daemonize) Detach fron the terminal and continue running in the 66 background. 67 68 -l|--log-folder) Create log files in this folder. 69 70 -p|--pidfile <name> Specify the name of the file to save the pids of child 71 processes in. Pass in an empty string to disable 72 pid-file creation. 73 74 -q|--quiet) Do not produce unnecessary console output. We still 75 show a banner if the command line arguments are 76 invalid. 77 78 --quick) Intended for internal use. Skip certain checks and 79 actions at start-up and print the command line 80 arguments to standard output. 81 82 --help|--usage Print this text. 83 EOF 84 } 53 85 54 86 ## Configuration file name. … … 59 91 CONFIGURATION_FILE=/etc/vbox/vbox.conf 60 92 93 ## The name of this script. 94 SCRIPT_NAME="$0" 95 ## Command line we were called with. 96 SCRIPT_COMMAND_LINE="$0 $@" 61 97 ## The service name. Should match the init script name. 62 98 SERVICE_NAME="vboxheadlessxorg" … … 72 108 HEADLESS_X_ORG_CONFIGURATION_DIRECTORY=/etc/vbox/headlessxorg.conf.d 73 109 ## The path to the pidfile for the service. 74 HEADLESS_X_ORG_PIDFILE="/var/run/${SERVICE_NAME}.pid" 110 HEADLESS_X_ORG_PID_FILE="/var/run/${SERVICE_NAME}.pid" 111 ## The default log folder 112 HEADLESS_X_ORG_LOG_FOLDER="/var/log/${SERVICE_NAME}" 75 113 76 114 ## The function definition at the start of every non-trivial shell script! … … 90 128 } 91 129 92 usage() {130 banner() { 93 131 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. 132 ${VBOX_PRODUCT} VBoxHeadless X Server start-up service Version ${VBOX_VERSION_STRING} 133 (C) 2005-${VBOX_C_YEAR} ${VBOX_VENDOR} 134 All rights reserved. 135 112 136 EOF 137 [ -n "${QUICK}" ] && 138 printf "Internal command line: ${SCRIPT_COMMAND_LINE}\n\n" 113 139 } 114 140 115 141 abort_usage() { 116 usage > 142 usage >&2 117 143 abort "$@" 118 144 } 119 145 120 146 # Change to the directory where the script is located. 121 MY_DIR="$(dirname "$ 0")"147 MY_DIR="$(dirname "${SCRIPT_NAME}")" 122 148 MY_DIR=`cd "${MY_DIR}" && pwd` 123 149 [ -d "${MY_DIR}" ] || … … 134 160 [ -r /etc/vbox/vbox.conf ] && . /etc/vbox/vbox.conf 135 161 136 cat << EOF137 ${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 EOF142 143 162 # Parse our arguments. 144 while true; do163 while [ "$#" -gt 0 ]; do 145 164 case $1 in 165 -d|--daemonize) 166 DAEMONIZE=true 167 ;; 168 -l|--log-folder) 169 [ "$#" -gt 1 ] || 170 { 171 banner 172 abort "%s requires at least one argument.\n" "$1" 173 } 174 HEADLESS_X_ORG_LOG_FOLDER="$2" 175 shift 176 ;; 146 177 -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 178 [ "$#" -gt 1 ] || 179 { 180 banner 181 abort "%s requires at least one argument.\n" "$1" 182 } 183 HEADLESS_X_ORG_PID_FILE="$2" 154 184 shift 185 ;; 186 -q|--quiet) 187 QUIET=true 188 ;; 189 --quick) 190 QUICK=true 155 191 ;; 156 192 --help|--usage) … … 159 195 ;; 160 196 *) 161 abort_usage "Unknown argument $1.\n" 197 { 198 banner 199 abort_usage "Unknown argument $1.\n" 200 } 162 201 ;; 163 202 esac 203 shift 164 204 done 165 205 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" 206 [ -z "${QUIET}" ] && banner 207 208 if [ -z "${QUICK}" ]; then 209 [ -f "${HEADLESS_X_ORG_PID_FILE}" ] && 210 ps -p "$(cat "${HEADLESS_X_ORG_PID_FILE}")" -o comm | 211 grep -q "${SERVICE_NAME}" && 212 abort "The service appears to be already running.\n" 213 214 PIDFILE_DIRECTORY="$(dirname "${HEADLESS_X_ORG_PID_FILE}")" 215 { ! [ -d "${PIDFILE_DIRECTORY}" ] || ! [ -w "${PIDFILE_DIRECTORY}" ]; } && 216 abort "Can't write to pid-file directory \"${PIDFILE_DIRECTORY}\".\n" 217 218 # If something fails here we will catch it when we create the directory. 219 [ -e "${HEADLESS_X_ORG_LOG_FOLDER}" ] && 220 [ -d "${HEADLESS_X_ORG_LOG_FOLDER}" ] && 221 rm -rf "${HEADLESS_X_ORG_LOG_FOLDER}.old" 2> /dev/null && 222 mv "${HEADLESS_X_ORG_LOG_FOLDER}" "${HEADLESS_X_ORG_LOG_FOLDER}.old" 2> /dev/null 223 mkdir -p "${HEADLESS_X_ORG_LOG_FOLDER}" 2>/dev/null || 224 abort "Failed to create log folder \"${HEADLESS_X_ORG_LOG_FOLDER}\".\n" 225 fi # -z "${QUICK}" 182 226 183 227 # Double background from shell script, disconnecting all standard streams, to 184 228 # daemonise. This may fail if someone has connected something to another file 185 # descriptor. This is in dented (see e.g. fghack in the daemontools package).229 # descriptor. This is intended (see e.g. fghack in the daemontools package). 186 230 if [ -n "${DAEMONIZE}" ]; then 187 ("$ 0" -p "${HEADLESS_X_ORG_PIDFILE}" < /dev/null > /dev/null 2>&1&188 echo "Created daemon process with PID $!.") &231 ("${SCRIPT_NAME}" --quick -p "${HEADLESS_X_ORG_PID_FILE}" < /dev/null > "${HEADLESS_X_ORG_LOG_FOLDER}/${SERVICE_NAME}.log" 2>&1 &) & 232 ## @todo wait for the servers to start accepting connections before exiting. 189 233 exit 0 190 234 fi 191 235 192 236 X_SERVER_PIDS="" 193 trap "kill \ "\${X_SERVER_PIDS}\"; exit 0" ${EXIT_SIGNALS}237 trap "kill \${X_SERVER_PIDS} 2>/dev/null; exit 0" ${EXIT_SIGNALS} 194 238 space="" # Hack to put spaces between the pids but not before or after. 195 239 for file in "${HEADLESS_X_ORG_CONFIGURATION_DIRECTORY}"/xorg.conf.*; do 196 240 filename="$(basename "${file}")" 197 expr "${filename}" : "xorg.conf.[0-9]*" ||198 { stop; abort "Badly formed file name \"${file}\".\n" }241 expr "${filename}" : "xorg.conf.[0-9]*" > /dev/null || 242 { stop; abort "Badly formed file name \"${file}\".\n"; } 199 243 screen="${filename##*.}" 200 Xorg ":${screen}" -config "${file}" > /dev/null 2>&1 & || 201 abort "Failed to create screen ${screen}.\n" 244 Xorg ":${screen}" -config "${file}" -logverbose 0 -logfile /dev/null -verbose 7 > "${HEADLESS_X_ORG_LOG_FOLDER}/Xorg.${screen}.log" 2>&1 & 202 245 X_SERVER_PIDS="${X_SERVER_PIDS}${space}$!" 203 246 space=" " 204 247 done 205 [ - z "${HEADLESS_X_ORG_PIDFILE}" ] &&206 echo "$$" > "${HEADLESS_X_ORG_PID FILE}"248 [ -n "${HEADLESS_X_ORG_PID_FILE}" ] && 249 echo "$$" > "${HEADLESS_X_ORG_PID_FILE}" 207 250 wait
Note:
See TracChangeset
for help on using the changeset viewer.