VirtualBox

Changeset 43634 in vbox


Ignore:
Timestamp:
Oct 12, 2012 12:42:47 PM (12 years ago)
Author:
vboxsync
Message:

Installer/linux: Headless X.Org starter script, now somewhat tested.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Installer/linux/VBoxHeadlessXOrg.sh

    r43580 r43634  
    2222# below can be overridden in the configuration file.  We will attempt to start
    2323# 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#
    2526# I have tried to follow the best practices I could find for writing a Linux
    2627# daemon process (and doing it in shell script) which should work well with
     
    4546#    Debian-conform progress information to standard output (what processes we
    4647#    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.
     53usage() {
     54  cat << EOF
     55Usage:
     56
     57  $(basename "${SCRIPT_NAME}") [<options>]
     58
     59Do any system-wide set-up required to properly run the copy of VirtualBox
     60residing in the current directory.  If no options are specified, everything
     61except --no-udev-and --log-file is assumed.
     62
     63Options:
     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.
     83EOF
     84}
    5385
    5486## Configuration file name.
     
    5991CONFIGURATION_FILE=/etc/vbox/vbox.conf
    6092
     93## The name of this script.
     94SCRIPT_NAME="$0"
     95## Command line we were called with.
     96SCRIPT_COMMAND_LINE="$0 $@"
    6197## The service name.  Should match the init script name.
    6298SERVICE_NAME="vboxheadlessxorg"
     
    72108HEADLESS_X_ORG_CONFIGURATION_DIRECTORY=/etc/vbox/headlessxorg.conf.d
    73109## The path to the pidfile for the service.
    74 HEADLESS_X_ORG_PIDFILE="/var/run/${SERVICE_NAME}.pid"
     110HEADLESS_X_ORG_PID_FILE="/var/run/${SERVICE_NAME}.pid"
     111## The default log folder
     112HEADLESS_X_ORG_LOG_FOLDER="/var/log/${SERVICE_NAME}"
    75113
    76114## The function definition at the start of every non-trivial shell script!
     
    90128}
    91129
    92 usage() {
     130banner() {
    93131  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}
     134All rights reserved.
     135
    112136EOF
     137[ -n "${QUICK}" ] &&
     138  printf "Internal command line: ${SCRIPT_COMMAND_LINE}\n\n"
    113139}
    114140
    115141abort_usage() {
    116   usage > &2
     142  usage >&2
    117143  abort "$@"
    118144}
    119145
    120146# Change to the directory where the script is located.
    121 MY_DIR="$(dirname "$0")"
     147MY_DIR="$(dirname "${SCRIPT_NAME}")"
    122148MY_DIR=`cd "${MY_DIR}" && pwd`
    123149[ -d "${MY_DIR}" ] ||
     
    134160[ -r /etc/vbox/vbox.conf ] && . /etc/vbox/vbox.conf
    135161
    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 
    143162# Parse our arguments.
    144 while true; do
     163while [ "$#" -gt 0 ]; do
    145164  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      ;;
    146177    -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"
    154184      shift
     185      ;;
     186    -q|--quiet)
     187      QUIET=true
     188      ;;
     189    --quick)
     190      QUICK=true
    155191      ;;
    156192    --help|--usage)
     
    159195      ;;
    160196    *)
    161       abort_usage "Unknown argument $1.\n"
     197      {
     198        banner
     199        abort_usage "Unknown argument $1.\n"
     200      }
    162201      ;;
    163202  esac
     203  shift
    164204done
    165205
    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
     208if [ -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"
     225fi # -z "${QUICK}"
    182226
    183227# Double background from shell script, disconnecting all standard streams, to
    184228# 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).
     229# descriptor.  This is intended (see e.g. fghack in the daemontools package).
    186230if [ -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.
    189233  exit 0
    190234fi
    191235
    192236X_SERVER_PIDS=""
    193 trap "kill \"\${X_SERVER_PIDS}\"; exit 0" ${EXIT_SIGNALS}
     237trap "kill \${X_SERVER_PIDS} 2>/dev/null; exit 0" ${EXIT_SIGNALS}
    194238space=""  # Hack to put spaces between the pids but not before or after.
    195239for file in "${HEADLESS_X_ORG_CONFIGURATION_DIRECTORY}"/xorg.conf.*; do
    196240  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"; }
    199243  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 &
    202245  X_SERVER_PIDS="${X_SERVER_PIDS}${space}$!"
    203246  space=" "
    204247done
    205 [ -z "${HEADLESS_X_ORG_PIDFILE}" ] &&
    206   echo "$$" > "${HEADLESS_X_ORG_PIDFILE}"
     248[ -n "${HEADLESS_X_ORG_PID_FILE}" ] &&
     249  echo "$$" > "${HEADLESS_X_ORG_PID_FILE}"
    207250wait
Note: See TracChangeset for help on using the changeset viewer.

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