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.

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