You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
8.3 KiB

  1. #!/bin/sh
  2. #
  3. # xrdp: A Remote Desktop Protocol server.
  4. #
  5. # Copyright (C) 2021 Matt Burt, all xrdp contributors
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. # Wrapper to call install_pulseaudio_sources.sh and tidy up afterwards
  20. #
  21. # The following command line switches are supported, for systems based on
  22. # Debian or Ubuntu:-
  23. #
  24. # 1) --mirror= Specify an alternative mirror for debootstrap
  25. # 2) --keyring= Specify an alternative keyring for debootstrap
  26. # 3) --suite= Specify an alternative suite for debootstrap
  27. #
  28. # The first two of these are are needed for systems with their own
  29. # mirrors and signing keys (i.e. Raspberry PI OS).
  30. #
  31. # --suite is useful for systems which report their own codename for
  32. # `lsb_release -c`, but are otherwise based on a standard distro. For
  33. # example Linux Mint 20.04 reports 'una', but is largely based on
  34. # Ubuntu 'focal'
  35. # ---------------------------------------------------------------------------
  36. # G L O B A L S
  37. # ---------------------------------------------------------------------------
  38. # Where the output files are going
  39. PULSE_DIRNAME=pulseaudio.src
  40. PULSE_DIR=$HOME/$PULSE_DIRNAME
  41. # Absolute path to the script we're wrapping. This picks it up from
  42. # the same directory this file is in
  43. WRAPPED_SCRIPT=$(cd $(dirname $0) && pwd)/install_pulseaudio_sources_apt.sh
  44. # The buildroot directory. Choose fast, temporary storage if available
  45. BUILDROOT=/var/lib/pa-build/$USER
  46. # Extra packages to install in the build root which the wrapped script
  47. # may be using. These are packages available by default when using
  48. # GitHub actions
  49. WRAPPED_SCRIPT_DEPS="sudo lsb-release doxygen"
  50. # -----------------------------------------------------------------------------
  51. # S U I T E E X I S T S
  52. #
  53. # Does the specified debootstrap suite exist?
  54. # -----------------------------------------------------------------------------
  55. SuiteExists()
  56. {
  57. [ -f "/usr/share/debootstrap/scripts/$1" ]
  58. }
  59. # -----------------------------------------------------------------------------
  60. # I N S T A L L R E Q U I R E D P A C K A G E S
  61. #
  62. # Installs packages required for the build on the host machine
  63. # -----------------------------------------------------------------------------
  64. InstallRequiredPackages()
  65. {
  66. set -- \
  67. /usr/sbin/debootstrap debootstrap \
  68. /usr/bin/schroot schroot \
  69. /usr/bin/lsb_release lsb-release
  70. pkgs=
  71. while [ $# -ge 2 ]; do
  72. if [ ! -x $1 ]; then
  73. pkgs="$pkgs $2"
  74. fi
  75. shift 2
  76. done
  77. if [ -n "$pkgs" ]; then
  78. echo "- Need to install packages :$pkgs"
  79. echo
  80. echo " These can be removed when this script completes with:-"
  81. echo " sudo apt-get purge$pkgs && apt-get autoremove"
  82. echo
  83. sudo apt-get install -y $pkgs
  84. fi
  85. }
  86. # -----------------------------------------------------------------------------
  87. # R U N W R A P P E D S C R I P T
  88. #
  89. # Runs the wrapped build script using schroot
  90. #
  91. # This function definition uses () rather than {} to create an extra
  92. # sub-process where we can run 'set -e' without affecting the parent
  93. #
  94. # Parameters : <script> [<script params>...]
  95. # -----------------------------------------------------------------------------
  96. RunWrappedScript()
  97. (
  98. # In this sub-process, fail on error
  99. set -e
  100. # Define default args for running program
  101. # -c : Define the schroot config to use
  102. # -d : Directory to switch to before running command
  103. schroot="schroot -c pa-build-$USER -d /build"
  104. # Install extra dependencies
  105. $schroot -u root -- apt-get install -y $WRAPPED_SCRIPT_DEPS
  106. # Allow normal user to sudo without a password
  107. $schroot -u root -- \
  108. /bin/sh -c "echo '$USER ALL=(ALL) NOPASSWD:ALL'>/etc/sudoers.d/nopasswd-$USER"
  109. $schroot -u root -- chmod 400 /etc/sudoers.d/nopasswd-$USER
  110. # Call the wrapped script
  111. $schroot -- "$@"
  112. )
  113. # -----------------------------------------------------------------------------
  114. # M A I N
  115. # -----------------------------------------------------------------------------
  116. debootstrap_mirror=""
  117. debootstrap_switches=""
  118. debootstrap_suite=""
  119. # Parse command line switches
  120. while [ -n "$1" ]; do
  121. case "$1" in
  122. --mirror=*)
  123. debootstrap_mirror="${1#--mirror=}"
  124. ;;
  125. --keyring=*)
  126. file="${1#--keyring=}"
  127. if [ -f "$file" ]; then
  128. debootstrap_switches="$debootstrap_switches $1"
  129. else
  130. echo "** Ignoring missing keyring $1" >&2
  131. fi
  132. ;;
  133. --suite=*)
  134. debootstrap_suite="${1#--suite=}"
  135. if ! SuiteExists "$debootstrap_suite"; then
  136. echo "** Unsupported suite '$debootstrap_suite'" >&2
  137. exit 1
  138. fi
  139. ;;
  140. *) echo "** Unrecognised parameter '$1'" >&2
  141. exit 1
  142. esac
  143. shift
  144. done
  145. # Start with a few sanity checks
  146. if [ -d $PULSE_DIR ]; then
  147. echo "** Target directory $PULSE_DIR already exists" >&2
  148. exit 0
  149. fi
  150. if [ ! -x $WRAPPED_SCRIPT ]; then
  151. echo "** Can't find wrapped script $WRAPPED_SCRIPT" >&2
  152. exit 1
  153. fi
  154. if [ -e $BUILDROOT ]; then
  155. echo "** Remove old build root $BUILDROOT before running this script"
  156. exit 1
  157. fi
  158. # Do we need extra packages?
  159. InstallRequiredPackages || exit $?
  160. # We should be able to determine the suite now, if it's not specified
  161. if [ -z "$debootstrap_suite" ]; then
  162. debootstrap_suite=$(lsb_release -cs) ; # e.g. 'bullseye'
  163. if [ -z "$debootstrap_suite" ]; then
  164. echo "** Can't determine current suite" >&2
  165. exit 1
  166. fi
  167. if ! SuiteExists "$debootstrap_suite" ; then
  168. echo "** Current distro '$debootstrap_suite' does not appear to be supported by debootstrap" >&2
  169. echo " Need --suite switch?" >&2
  170. exit 1
  171. fi
  172. fi
  173. # Create the build root
  174. log=/var/tmp/pa-build-$USER-debootstrap.log
  175. echo "- Creating $debootstrap_suite build root. Log file in $log"
  176. sudo debootstrap \
  177. $debootstrap_switches \
  178. $debootstrap_suite $BUILDROOT "$debootstrap_mirror" >$log 2>&1 || {
  179. echo "** debootstrap failed. Check log file $log" >&2
  180. exit 1
  181. }
  182. # Create the config file for schroot
  183. schroot_conf=/etc/schroot/chroot.d/pa-build-$USER.conf
  184. echo "- Creating schroot config file $schroot_conf"
  185. {
  186. echo "[pa-build-$USER]"
  187. echo "description=Build PA on current system for $USER"
  188. echo "directory=$BUILDROOT"
  189. echo "root-users=$USER"
  190. echo "users=$USER"
  191. echo "type=directory"
  192. } | sudo tee $schroot_conf >/dev/null || exit $?
  193. # Copy some files to the build root
  194. for file in /etc/apt/sources.list; do
  195. echo "- Copying $file to the root"
  196. sudo install -m 0644 $file $BUILDROOT/$file || exit $?
  197. done
  198. # Create a separate directory in $BUILDROOT to hold the build
  199. # artefacts.
  200. #
  201. # We used to do the build on the user's home directory, but this
  202. # isn't supported by schroot out-of-the-box on all configurations -
  203. # see https://github.com/neutrinolabs/pulseaudio-module-xrdp/issues/76
  204. echo "- Creating the build directory /build"
  205. sudo install -d --mode=0775 --owner=$USER --group=$(id -g) \
  206. $BUILDROOT/build || exit $?
  207. # Copy the wrapped script to the buildroot root
  208. echo "- Copying the wrapped script to the build directory"
  209. sudo install -m 755 $WRAPPED_SCRIPT $BUILDROOT/build/wrapped_script || exit $?
  210. # Run the wrapped script
  211. log=/var/tmp/pa-build-$USER-schroot.log
  212. echo "- Building PA sources. Log file in $log"
  213. RunWrappedScript /build/wrapped_script -d /build/$PULSE_DIRNAME >$log 2>&1 || {
  214. echo "** schroot failed. Check log file $log" >&2
  215. exit 1
  216. }
  217. # Done! Copy the resulting directory out of the build root
  218. echo "- Copying sources out of the build root"
  219. cp -pr $BUILDROOT/build/$PULSE_DIRNAME $PULSE_DIR || exit $?
  220. # Remove the schroot config file as its no longer needed
  221. echo "- Removing schroot config file and build root"
  222. sudo rm -rf $schroot_conf $BUILDROOT
  223. echo "- All done. Configure PA xrdp module with PULSE_DIR=$PULSE_DIR"
  224. exit 0