Burrow Raspberry Pi Image
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.

262 lines
6.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
6 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
  1. #!/bin/bash -e
  2. # shellcheck disable=SC2119
  3. run_sub_stage()
  4. {
  5. log "Begin ${SUB_STAGE_DIR}"
  6. pushd "${SUB_STAGE_DIR}" > /dev/null
  7. for i in {00..99}; do
  8. if [ -f "${i}-debconf" ]; then
  9. log "Begin ${SUB_STAGE_DIR}/${i}-debconf"
  10. on_chroot << EOF
  11. debconf-set-selections <<SELEOF
  12. $(cat "${i}-debconf")
  13. SELEOF
  14. EOF
  15. log "End ${SUB_STAGE_DIR}/${i}-debconf"
  16. fi
  17. if [ -f "${i}-packages-nr" ]; then
  18. log "Begin ${SUB_STAGE_DIR}/${i}-packages-nr"
  19. PACKAGES="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < "${i}-packages-nr")"
  20. if [ -n "$PACKAGES" ]; then
  21. on_chroot << EOF
  22. apt-get install --no-install-recommends -y $PACKAGES
  23. EOF
  24. fi
  25. log "End ${SUB_STAGE_DIR}/${i}-packages-nr"
  26. fi
  27. if [ -f "${i}-packages" ]; then
  28. log "Begin ${SUB_STAGE_DIR}/${i}-packages"
  29. PACKAGES="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < "${i}-packages")"
  30. if [ -n "$PACKAGES" ]; then
  31. on_chroot << EOF
  32. apt-get install -y $PACKAGES
  33. EOF
  34. fi
  35. log "End ${SUB_STAGE_DIR}/${i}-packages"
  36. fi
  37. if [ -d "${i}-patches" ]; then
  38. log "Begin ${SUB_STAGE_DIR}/${i}-patches"
  39. pushd "${STAGE_WORK_DIR}" > /dev/null
  40. if [ "${CLEAN}" = "1" ]; then
  41. rm -rf .pc
  42. rm -rf ./*-pc
  43. fi
  44. QUILT_PATCHES="${SUB_STAGE_DIR}/${i}-patches"
  45. SUB_STAGE_QUILT_PATCH_DIR="$(basename "$SUB_STAGE_DIR")-pc"
  46. mkdir -p "$SUB_STAGE_QUILT_PATCH_DIR"
  47. ln -snf "$SUB_STAGE_QUILT_PATCH_DIR" .pc
  48. quilt upgrade
  49. if [ -e "${SUB_STAGE_DIR}/${i}-patches/EDIT" ]; then
  50. echo "Dropping into bash to edit patches..."
  51. bash
  52. fi
  53. RC=0
  54. quilt push -a || RC=$?
  55. case "$RC" in
  56. 0|2)
  57. ;;
  58. *)
  59. false
  60. ;;
  61. esac
  62. popd > /dev/null
  63. log "End ${SUB_STAGE_DIR}/${i}-patches"
  64. fi
  65. if [ -x ${i}-run.sh ]; then
  66. log "Begin ${SUB_STAGE_DIR}/${i}-run.sh"
  67. ./${i}-run.sh
  68. log "End ${SUB_STAGE_DIR}/${i}-run.sh"
  69. fi
  70. if [ -f ${i}-run-chroot.sh ]; then
  71. log "Begin ${SUB_STAGE_DIR}/${i}-run-chroot.sh"
  72. on_chroot < ${i}-run-chroot.sh
  73. log "End ${SUB_STAGE_DIR}/${i}-run-chroot.sh"
  74. fi
  75. done
  76. popd > /dev/null
  77. log "End ${SUB_STAGE_DIR}"
  78. }
  79. run_stage(){
  80. log "Begin ${STAGE_DIR}"
  81. STAGE="$(basename "${STAGE_DIR}")"
  82. pushd "${STAGE_DIR}" > /dev/null
  83. unmount "${WORK_DIR}/${STAGE}"
  84. STAGE_WORK_DIR="${WORK_DIR}/${STAGE}"
  85. ROOTFS_DIR="${STAGE_WORK_DIR}"/rootfs
  86. if [ ! -f SKIP_IMAGES ]; then
  87. if [ -f "${STAGE_DIR}/EXPORT_IMAGE" ]; then
  88. EXPORT_DIRS="${EXPORT_DIRS} ${STAGE_DIR}"
  89. fi
  90. fi
  91. if [ ! -f SKIP ]; then
  92. if [ "${CLEAN}" = "1" ]; then
  93. if [ -d "${ROOTFS_DIR}" ]; then
  94. rm -rf "${ROOTFS_DIR}"
  95. fi
  96. fi
  97. if [ -x prerun.sh ]; then
  98. log "Begin ${STAGE_DIR}/prerun.sh"
  99. ./prerun.sh
  100. log "End ${STAGE_DIR}/prerun.sh"
  101. fi
  102. for SUB_STAGE_DIR in "${STAGE_DIR}"/*; do
  103. if [ -d "${SUB_STAGE_DIR}" ] &&
  104. [ ! -f "${SUB_STAGE_DIR}/SKIP" ]; then
  105. run_sub_stage
  106. fi
  107. done
  108. fi
  109. unmount "${WORK_DIR}/${STAGE}"
  110. PREV_STAGE="${STAGE}"
  111. PREV_STAGE_DIR="${STAGE_DIR}"
  112. PREV_ROOTFS_DIR="${ROOTFS_DIR}"
  113. popd > /dev/null
  114. log "End ${STAGE_DIR}"
  115. }
  116. if [ "$(id -u)" != "0" ]; then
  117. echo "Please run as root" 1>&2
  118. exit 1
  119. fi
  120. BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  121. export BASE_DIR
  122. if [ -f config ]; then
  123. # shellcheck disable=SC1091
  124. source config
  125. fi
  126. while getopts "c:" flag
  127. do
  128. case "$flag" in
  129. c)
  130. EXTRA_CONFIG="$OPTARG"
  131. # shellcheck disable=SC1090
  132. source "$EXTRA_CONFIG"
  133. ;;
  134. *)
  135. ;;
  136. esac
  137. done
  138. export PI_GEN=${PI_GEN:-pi-gen}
  139. export PI_GEN_REPO=${PI_GEN_REPO:-https://github.com/RPi-Distro/pi-gen}
  140. if [ -z "${IMG_NAME}" ]; then
  141. echo "IMG_NAME not set" 1>&2
  142. exit 1
  143. fi
  144. export USE_QEMU="${USE_QEMU:-0}"
  145. export IMG_DATE="${IMG_DATE:-"$(date +%Y-%m-%d)"}"
  146. export IMG_FILENAME="${IMG_FILENAME:-"${IMG_DATE}-${IMG_NAME}"}"
  147. export ZIP_FILENAME="${ZIP_FILENAME:-"image_${IMG_DATE}-${IMG_NAME}"}"
  148. export SCRIPT_DIR="${BASE_DIR}/scripts"
  149. export WORK_DIR="${WORK_DIR:-"${BASE_DIR}/work/${IMG_DATE}-${IMG_NAME}"}"
  150. export DEPLOY_DIR=${DEPLOY_DIR:-"${BASE_DIR}/deploy"}
  151. export DEPLOY_ZIP="${DEPLOY_ZIP:-1}"
  152. export LOG_FILE="${WORK_DIR}/build.log"
  153. export TARGET_HOSTNAME=${TARGET_HOSTNAME:-raspberrypi}
  154. export FIRST_USER_NAME=${FIRST_USER_NAME:-pi}
  155. export FIRST_USER_PASS=${FIRST_USER_PASS:-raspberry}
  156. export WPA_ESSID
  157. export WPA_PASSWORD
  158. export WPA_COUNTRY
  159. export ENABLE_SSH="${ENABLE_SSH:-0}"
  160. export LOCALE_DEFAULT="${LOCALE_DEFAULT:-en_GB.UTF-8}"
  161. export KEYBOARD_KEYMAP="${KEYBOARD_KEYMAP:-gb}"
  162. export KEYBOARD_LAYOUT="${KEYBOARD_LAYOUT:-English (UK)}"
  163. export TIMEZONE_DEFAULT="${TIMEZONE_DEFAULT:-Europe/London}"
  164. export GIT_HASH=${GIT_HASH:-"$(git rev-parse HEAD)"}
  165. export CLEAN
  166. export IMG_NAME
  167. export APT_PROXY
  168. export STAGE
  169. export STAGE_DIR
  170. export STAGE_WORK_DIR
  171. export PREV_STAGE
  172. export PREV_STAGE_DIR
  173. export ROOTFS_DIR
  174. export PREV_ROOTFS_DIR
  175. export IMG_SUFFIX
  176. export NOOBS_NAME
  177. export NOOBS_DESCRIPTION
  178. export EXPORT_DIR
  179. export EXPORT_ROOTFS_DIR
  180. export QUILT_PATCHES
  181. export QUILT_NO_DIFF_INDEX=1
  182. export QUILT_NO_DIFF_TIMESTAMPS=1
  183. export QUILT_REFRESH_ARGS="-p ab"
  184. # shellcheck source=scripts/common
  185. source "${SCRIPT_DIR}/common"
  186. # shellcheck source=scripts/dependencies_check
  187. source "${SCRIPT_DIR}/dependencies_check"
  188. dependencies_check "${BASE_DIR}/depends"
  189. #check username is valid
  190. if [[ ! "$FIRST_USER_NAME" =~ ^[a-z][-a-z0-9_]*$ ]]; then
  191. echo "Invalid FIRST_USER_NAME: $FIRST_USER_NAME"
  192. exit 1
  193. fi
  194. if [[ -n "${APT_PROXY}" ]] && ! curl --silent "${APT_PROXY}" >/dev/null ; then
  195. echo "Could not reach APT_PROXY server: ${APT_PROXY}"
  196. exit 1
  197. fi
  198. if [[ -n "${WPA_PASSWORD}" && ${#WPA_PASSWORD} -lt 8 || ${#WPA_PASSWORD} -gt 63 ]] ; then
  199. echo "WPA_PASSWORD" must be between 8 and 63 characters
  200. exit 1
  201. fi
  202. mkdir -p "${WORK_DIR}"
  203. log "Begin ${BASE_DIR}"
  204. STAGE_LIST=${STAGE_LIST:-${BASE_DIR}/stage*}
  205. for STAGE_DIR in $STAGE_LIST; do
  206. STAGE_DIR=$(realpath "${STAGE_DIR}")
  207. run_stage
  208. done
  209. CLEAN=1
  210. for EXPORT_DIR in ${EXPORT_DIRS}; do
  211. STAGE_DIR=${BASE_DIR}/export-image
  212. # shellcheck source=/dev/null
  213. source "${EXPORT_DIR}/EXPORT_IMAGE"
  214. EXPORT_ROOTFS_DIR=${WORK_DIR}/$(basename "${EXPORT_DIR}")/rootfs
  215. run_stage
  216. if [ "${USE_QEMU}" != "1" ]; then
  217. if [ -e "${EXPORT_DIR}/EXPORT_NOOBS" ]; then
  218. # shellcheck source=/dev/null
  219. source "${EXPORT_DIR}/EXPORT_NOOBS"
  220. STAGE_DIR="${BASE_DIR}/export-noobs"
  221. run_stage
  222. fi
  223. fi
  224. done
  225. if [ -x ${BASE_DIR}/postrun.sh ]; then
  226. log "Begin postrun.sh"
  227. cd "${BASE_DIR}"
  228. ./postrun.sh
  229. log "End postrun.sh"
  230. fi
  231. log "End ${BASE_DIR}"