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.

200 lines
6.2 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. # The command line switches --mirror= and --keyring= can be specified if
  21. # these switches are needed for a system based on Debian (e.g. a Raspberry
  22. # Pi)
  23. # ---------------------------------------------------------------------------
  24. # G L O B A L S
  25. # ---------------------------------------------------------------------------
  26. # Where the output files are going. Must be under $HOME as schroot
  27. # assumes this.
  28. PULSE_DIR=$HOME/pulseaudio.src
  29. # Absolute path to the script we're wrapping. This picks it up from
  30. # the same directory this file is in
  31. WRAPPED_SCRIPT=$(cd $(dirname $0) && pwd)/install_pulseaudio_sources_apt.sh
  32. # The buildroot directory. Choose fast, temporary storage if available
  33. BUILDROOT=/var/lib/pa-build/$USER
  34. # Extra packages to install in the build root which the wrapped script
  35. # may be using. These are packages available by default when using
  36. # GitHub actions
  37. WRAPPED_SCRIPT_DEPS="sudo lsb-release"
  38. # -----------------------------------------------------------------------------
  39. # I N S T A L L R E Q U I R E D P A C K A G E S
  40. #
  41. # Installs packages required for the build on the host machine
  42. # -----------------------------------------------------------------------------
  43. InstallRequiredPackages()
  44. {
  45. set -- \
  46. /usr/sbin/debootstrap debootstrap \
  47. /usr/bin/schroot schroot \
  48. /usr/bin/lsb_release lsb-release
  49. pkgs=
  50. while [ $# -ge 2 ]; do
  51. if [ ! -x $1 ]; then
  52. pkgs="$pkgs $2"
  53. fi
  54. shift 2
  55. done
  56. if [ -n "$pkgs" ]; then
  57. echo "- Need to install packages :$pkgs"
  58. echo
  59. echo " These can be removed when this script completes with:-"
  60. echo " sudo apt-get purge$pkgs && apt-get autoremove"
  61. echo
  62. sudo apt-get install -y $pkgs
  63. fi
  64. }
  65. # -----------------------------------------------------------------------------
  66. # R U N W R A P P E D S C R I P T
  67. #
  68. # Runs the wrapped build script using schroot
  69. #
  70. # This function definition uses () rather than {} to create an extra
  71. # sub-process where we can run 'set -e' without affecting the parent
  72. # -----------------------------------------------------------------------------
  73. RunWrappedScript()
  74. (
  75. # In this sub-process, fail on error
  76. set -e
  77. # Install extra dependencies
  78. schroot -c pa-build-$USER -u root -- \
  79. apt-get install -y $WRAPPED_SCRIPT_DEPS
  80. # Allow normal user to sudo without a password
  81. schroot -c pa-build-$USER -u root -- \
  82. /bin/sh -c "echo '$USER ALL=(ALL) NOPASSWD:ALL'>/etc/sudoers.d/nopasswd-$USER"
  83. schroot -c pa-build-$USER -u root -- chmod 400 /etc/sudoers.d/nopasswd-$USER
  84. # Call the wrapped script
  85. schroot -c pa-build-$USER -- /wrapped_script -d $PULSE_DIR
  86. )
  87. # -----------------------------------------------------------------------------
  88. # M A I N
  89. # -----------------------------------------------------------------------------
  90. debootstrap_mirror=""
  91. debootstrap_switches=""
  92. # Parse command line switches
  93. while [ -n "$1" ]; do
  94. case "$1" in
  95. --mirror=*)
  96. debootstrap_mirror="${1#--mirror=}"
  97. ;;
  98. --keyring=*)
  99. file="${1#--keyring=}"
  100. if [ -f "$file" ]; then
  101. debootstrap_switches="$debootstrap_switches $1"
  102. else
  103. echo "** Ignoring missing keyring $1" >&2
  104. fi
  105. ;;
  106. *) echo "** Unrecognised parameter '$1'" >&2
  107. exit 1
  108. esac
  109. shift
  110. done
  111. # Start with a few sanity checks
  112. if [ -d $PULSE_DIR ]; then
  113. echo "** Target directory $PULSE_DIR already exists" >&2
  114. exit 0
  115. fi
  116. if [ ! -x $WRAPPED_SCRIPT ]; then
  117. echo "** Can't find wrapped script $WRAPPED_SCRIPT" >&2
  118. exit 1
  119. fi
  120. if [ -e $BUILDROOT ]; then
  121. echo "** Remove old build root $BUILDROOT before running this script"
  122. exit 1
  123. fi
  124. # Do we need extra packages?
  125. InstallRequiredPackages || exit $?
  126. # We should be able to determine the distro now
  127. distro=$(lsb_release -cs) ; # e.g. 'bullseye'
  128. if [ -z "$distro" ]; then
  129. echo "** Can't determine current distro" >&2
  130. exit 1
  131. fi
  132. # Create the build root
  133. log=/var/tmp/pa-build-$USER-debootstrap.log
  134. echo "- Creating $distro build root. Log file in $log"
  135. sudo debootstrap \
  136. $debootstrap_switches \
  137. $distro $BUILDROOT "$debootstrap_mirror" >$log 2>&1 || {
  138. echo "** debootstrap failed. Check log file $log" >&2
  139. exit 1
  140. }
  141. # Create the config file for schroot
  142. schroot_conf=/etc/schroot/chroot.d/pa-build-$USER.conf
  143. echo "- Creating schroot config file $schroot_conf"
  144. {
  145. echo "[pa-build-$USER]"
  146. echo "description=Build PA on current system for $USER"
  147. echo "directory=$BUILDROOT"
  148. echo "root-users=$USER"
  149. echo "users=$USER"
  150. echo "type=directory"
  151. } | sudo tee $schroot_conf >/dev/null || exit $?
  152. # Copy some files to the build root
  153. for file in /etc/apt/sources.list; do
  154. echo "- Copying $file to the root"
  155. sudo cp $file $BUILDROOT/$file || exit $?
  156. done
  157. # Copy the wrapped script to the buildroot root
  158. echo "- Copying the wrapped script to the root"
  159. sudo cp $WRAPPED_SCRIPT $BUILDROOT/wrapped_script || exit $?
  160. sudo chmod +x $BUILDROOT/wrapped_script || exit $?
  161. # Run the wrapped script
  162. log=/var/tmp/pa-build-$USER-schroot.log
  163. echo "- Building PA sources. Log file in $log"
  164. RunWrappedScript >$log 2>&1 || {
  165. echo "** schroot failed. Check log file $log" >&2
  166. exit 1
  167. }
  168. # Done! Remove the schroot config file as its no longer needed
  169. echo "- Removing schroot config file and build root"
  170. sudo rm -rf $schroot_conf $BUILDROOT
  171. echo "- All done. Configure PA xrdp module with PULSE_DIR=$PULSE_DIR"
  172. exit 0