bootstrap.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/bash
  2. # Based on: http://www.richud.com/wiki/Ubuntu_Fluxbox_GUI_with_x11vnc_and_Xvfb
  3. main() {
  4. log_i "Starting xvfb virtual display..."
  5. launch_xvfb
  6. log_i "Starting window manager..."
  7. launch_window_manager
  8. log_i "Starting VNC server..."
  9. run_vnc_server
  10. }
  11. launch_xvfb() {
  12. local xvfbLockFilePath="/tmp/.X1-lock"
  13. if [ -f "${xvfbLockFilePath}" ]
  14. then
  15. log_i "Removing xvfb lock file '${xvfbLockFilePath}'..."
  16. if ! rm -v "${xvfbLockFilePath}"
  17. then
  18. log_e "Failed to remove xvfb lock file"
  19. exit 1
  20. fi
  21. fi
  22. # Set defaults if the user did not specify envs.
  23. export DISPLAY=${XVFB_DISPLAY:-:1}
  24. local screen=${XVFB_SCREEN:-0}
  25. local resolution=${XVFB_RESOLUTION:-1280x960x24}
  26. local timeout=${XVFB_TIMEOUT:-5}
  27. # Start and wait for either Xvfb to be fully up or we hit the timeout.
  28. Xvfb ${DISPLAY} -screen ${screen} ${resolution} &
  29. local loopCount=0
  30. until xdpyinfo -display ${DISPLAY} > /dev/null 2>&1
  31. do
  32. loopCount=$((loopCount+1))
  33. sleep 1
  34. if [ ${loopCount} -gt ${timeout} ]
  35. then
  36. log_e "xvfb failed to start"
  37. exit 1
  38. fi
  39. done
  40. }
  41. launch_window_manager() {
  42. local timeout=${XVFB_TIMEOUT:-5}
  43. # Start and wait for either fluxbox to be fully up or we hit the timeout.
  44. fluxbox &
  45. local loopCount=0
  46. until wmctrl -m > /dev/null 2>&1
  47. do
  48. loopCount=$((loopCount+1))
  49. sleep 1
  50. if [ ${loopCount} -gt ${timeout} ]
  51. then
  52. log_e "fluxbox failed to start"
  53. exit 1
  54. fi
  55. done
  56. }
  57. run_vnc_server() {
  58. local passwordArgument='-nopw'
  59. if [ -n "${VNC_SERVER_PASSWORD}" ]
  60. then
  61. local passwordFilePath="${HOME}/.x11vnc.pass"
  62. if ! x11vnc -storepasswd "${VNC_SERVER_PASSWORD}" "${passwordFilePath}"
  63. then
  64. log_e "Failed to store x11vnc password"
  65. exit 1
  66. fi
  67. passwordArgument=-"-rfbauth ${passwordFilePath}"
  68. log_i "The VNC server will ask for a password"
  69. else
  70. log_w "The VNC server will NOT ask for a password"
  71. fi
  72. x11vnc -ncache 10 -ncache_cr -display ${DISPLAY} -forever ${passwordArgument} &
  73. wait $!
  74. }
  75. log_i() {
  76. log "[INFO] ${@}"
  77. }
  78. log_w() {
  79. log "[WARN] ${@}"
  80. }
  81. log_e() {
  82. log "[ERROR] ${@}"
  83. }
  84. log() {
  85. echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${@}"
  86. }
  87. control_c() {
  88. echo ""
  89. exit
  90. }
  91. trap control_c SIGINT SIGTERM SIGHUP
  92. main
  93. exit