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.

257 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. REPO_ROOT_PATH="$(realpath $(dirname "$0")/..)"
  39. # Where the output files are going
  40. PULSE_DIRNAME=pulseaudio.src
  41. PULSE_DIR=$REPO_ROOT_PATH/$PULSE_DIRNAME
  42. # Absolute path to the script we're wrapping. This picks it up from
  43. # the same directory this file is in
  44. WRAPPED_SCRIPT=$(cd $(dirname $0) && pwd)/install_pulseaudio_sources_apt.sh
  45. # The buildroot directory. Choose fast, temporary storage if available
  46. BUILDROOT=/var/lib/pa-build/$USER
  47. # Extra packages to install in the build root which the wrapped script
  48. # may be using. These are packages available by default when using
  49. # GitHub actions
  50. WRAPPED_SCRIPT_DEPS="sudo lsb-release doxygen"
  51. # -----------------------------------------------------------------------------
  52. # S U I T E E X I S T S
  53. #
  54. # Does the specified debootstrap suite exist?
  55. # -----------------------------------------------------------------------------
  56. SuiteExists()
  57. {
  58. [ -f "/usr/share/debootstrap/scripts/$1" ]
  59. }
  60. # -----------------------------------------------------------------------------
  61. # I N S T A L L R E Q U I R E D P A C K A G E S
  62. #
  63. # Installs packages required for the build on the host machine
  64. # -----------------------------------------------------------------------------
  65. InstallRequiredPackages()
  66. {
  67. set -- \
  68. /usr/sbin/debootstrap debootstrap \
  69. /usr/bin/schroot schroot \
  70. /usr/bin/lsb_release lsb-release
  71. pkgs=
  72. while [ $# -ge 2 ]; do
  73. if [ ! -x $1 ]; then
  74. pkgs="$pkgs $2"
  75. fi
  76. shift 2
  77. done
  78. if [ -n "$pkgs" ]; then
  79. echo "- Need to install packages :$pkgs"
  80. echo
  81. echo " These can be removed when this script completes with:-"
  82. echo " sudo apt-get purge$pkgs && apt-get autoremove"
  83. echo
  84. sudo apt-get install -y $pkgs
  85. fi
  86. }
  87. # -----------------------------------------------------------------------------
  88. # R U N W R A P P E D S C R I P T
  89. #
  90. # Runs the wrapped build script using schroot
  91. #
  92. # This function definition uses () rather than {} to create an extra
  93. # sub-process where we can run 'set -e' without affecting the parent
  94. #
  95. # Parameters : <script> [<script params>...]
  96. # -----------------------------------------------------------------------------
  97. RunWrappedScript()
  98. (
  99. # In this sub-process, fail on error
  100. set -e
  101. # Define default args for running program
  102. # -c : Define the schroot config to use
  103. # -d : Directory to switch to before running command
  104. schroot="schroot -c pa-build-$USER -d /build"
  105. # Install extra dependencies
  106. $schroot -u root -- apt-get install -y $WRAPPED_SCRIPT_DEPS
  107. # Allow normal user to sudo without a password
  108. $schroot -u root -- \
  109. /bin/sh -c "echo '$USER ALL=(ALL) NOPASSWD:ALL'>/etc/sudoers.d/nopasswd-$USER"
  110. $schroot -u root -- chmod 400 /etc/sudoers.d/nopasswd-$USER
  111. # Call the wrapped script
  112. $schroot -- "$@"
  113. )
  114. # -----------------------------------------------------------------------------
  115. # M A I N
  116. # -----------------------------------------------------------------------------
  117. debootstrap_mirror=""
  118. debootstrap_switches=""
  119. debootstrap_suite=""
  120. # Parse command line switches
  121. while [ -n "$1" ]; do
  122. case "$1" in
  123. --mirror=*)
  124. debootstrap_mirror="${1#--mirror=}"
  125. ;;
  126. --keyring=*)
  127. file="${1#--keyring=}"
  128. if [ -f "$file" ]; then
  129. debootstrap_switches="$debootstrap_switches $1"
  130. else
  131. echo "** Ignoring missing keyring $1" >&2
  132. fi
  133. ;;
  134. --suite=*)
  135. debootstrap_suite="${1#--suite=}"
  136. if ! SuiteExists "$debootstrap_suite"; then
  137. echo "** Unsupported suite '$debootstrap_suite'" >&2
  138. exit 1
  139. fi
  140. ;;
  141. *) echo "** Unrecognised parameter '$1'" >&2
  142. exit 1
  143. esac
  144. shift
  145. done
  146. # Start with a few sanity checks
  147. if [ -d $PULSE_DIR ]; then
  148. echo "** Target directory $PULSE_DIR already exists" >&2
  149. exit 0
  150. fi
  151. if [ ! -x $WRAPPED_SCRIPT ]; then
  152. echo "** Can't find wrapped script $WRAPPED_SCRIPT" >&2
  153. exit 1
  154. fi
  155. if [ -e $BUILDROOT ]; then
  156. echo "** Remove old build root $BUILDROOT before running this script"
  157. exit 1
  158. fi
  159. # Do we need extra packages?
  160. InstallRequiredPackages || exit $?
  161. # We should be able to determine the suite now, if it's not specified
  162. if [ -z "$debootstrap_suite" ]; then
  163. debootstrap_suite=$(lsb_release -cs) ; # e.g. 'bullseye'
  164. if [ -z "$debootstrap_suite" ]; then
  165. echo "** Can't determine current suite" >&2
  166. exit 1
  167. fi
  168. if ! SuiteExists "$debootstrap_suite" ; then
  169. echo "** Current distro '$debootstrap_suite' does not appear to be supported by debootstrap" >&2
  170. echo " Need --suite switch?" >&2
  171. exit 1
  172. fi
  173. fi
  174. # Create the build root
  175. log=/var/tmp/pa-build-$USER-debootstrap.log
  176. echo "- Creating $debootstrap_suite build root. Log file in $log"
  177. sudo debootstrap \
  178. $debootstrap_switches \
  179. $debootstrap_suite $BUILDROOT "$debootstrap_mirror" >$log 2>&1 || {
  180. echo "** debootstrap failed. Check log file $log" >&2
  181. exit 1
  182. }
  183. # Create the config file for schroot
  184. schroot_conf=/etc/schroot/chroot.d/pa-build-$USER.conf
  185. echo "- Creating schroot config file $schroot_conf"
  186. {
  187. echo "[pa-build-$USER]"
  188. echo "description=Build PA on current system for $USER"
  189. echo "directory=$BUILDROOT"
  190. echo "root-users=$USER"
  191. echo "users=$USER"
  192. echo "type=directory"
  193. } | sudo tee $schroot_conf >/dev/null || exit $?
  194. # Copy some files to the build root
  195. for file in /etc/apt/sources.list; do
  196. echo "- Copying $file to the root"
  197. sudo install -m 0644 $file $BUILDROOT/$file || exit $?
  198. done
  199. # Create a separate directory in $BUILDROOT to hold the build
  200. # artefacts.
  201. #
  202. # We used to do the build on the user's home directory, but this
  203. # isn't supported by schroot out-of-the-box on all configurations -
  204. # see https://github.com/neutrinolabs/pulseaudio-module-xrdp/issues/76
  205. echo "- Creating the build directory /build"
  206. sudo install -d --mode=0775 --owner=$USER --group=$(id -g) \
  207. $BUILDROOT/build || exit $?
  208. # Copy the wrapped script to the buildroot root
  209. echo "- Copying the wrapped script to the build directory"
  210. sudo install -m 755 $WRAPPED_SCRIPT $BUILDROOT/build/wrapped_script || exit $?
  211. # Run the wrapped script
  212. log=/var/tmp/pa-build-$USER-schroot.log
  213. echo "- Building PA sources. Log file in $log"
  214. RunWrappedScript /build/wrapped_script -d /build/$PULSE_DIRNAME >$log 2>&1 || {
  215. echo "** schroot failed. Check log file $log" >&2
  216. exit 1
  217. }
  218. # Done! Copy the resulting directory out of the build root
  219. echo "- Copying sources out of the build root"
  220. cp -pr $BUILDROOT/build/$PULSE_DIRNAME $PULSE_DIR || exit $?
  221. # Remove the schroot config file as its no longer needed
  222. echo "- Removing schroot config file and build root"
  223. sudo rm -rf $schroot_conf $BUILDROOT
  224. echo "- All done. Configure PA xrdp module with PULSE_DIR=$PULSE_DIR"
  225. exit 0