build.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/bin/bash -e
  2. run_sub_stage()
  3. {
  4. log "Begin ${SUB_STAGE_DIR}"
  5. pushd ${SUB_STAGE_DIR} > /dev/null
  6. for i in {00..99}; do
  7. if [ -f ${i}-debconf ]; then
  8. log "Begin ${SUB_STAGE_DIR}/${i}-debconf"
  9. on_chroot << EOF
  10. debconf-set-selections <<SELEOF
  11. `cat ${i}-debconf`
  12. SELEOF
  13. EOF
  14. log "End ${SUB_STAGE_DIR}/${i}-debconf"
  15. fi
  16. if [ -f ${i}-packages-nr ]; then
  17. log "Begin ${SUB_STAGE_DIR}/${i}-packages-nr"
  18. PACKAGES="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < ${i}-packages-nr)"
  19. if [ -n "$PACKAGES" ]; then
  20. on_chroot << EOF
  21. apt-get install --no-install-recommends -y $PACKAGES
  22. EOF
  23. fi
  24. log "End ${SUB_STAGE_DIR}/${i}-packages-nr"
  25. fi
  26. if [ -f ${i}-packages ]; then
  27. log "Begin ${SUB_STAGE_DIR}/${i}-packages"
  28. PACKAGES="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < ${i}-packages)"
  29. if [ -n "$PACKAGES" ]; then
  30. on_chroot << EOF
  31. apt-get install -y $PACKAGES
  32. EOF
  33. fi
  34. log "End ${SUB_STAGE_DIR}/${i}-packages"
  35. fi
  36. if [ -d ${i}-patches ]; then
  37. log "Begin ${SUB_STAGE_DIR}/${i}-patches"
  38. pushd ${STAGE_WORK_DIR} > /dev/null
  39. if [ "${CLEAN}" = "1" ]; then
  40. rm -rf .pc
  41. rm -rf *-pc
  42. fi
  43. QUILT_PATCHES=${SUB_STAGE_DIR}/${i}-patches
  44. SUB_STAGE_QUILT_PATCH_DIR="$(basename $SUB_STAGE_DIR)-pc"
  45. mkdir -p $SUB_STAGE_QUILT_PATCH_DIR
  46. ln -snf $SUB_STAGE_QUILT_PATCH_DIR .pc
  47. if [ -e ${SUB_STAGE_DIR}/${i}-patches/EDIT ]; then
  48. echo "Dropping into bash to edit patches..."
  49. bash
  50. fi
  51. quilt upgrade
  52. RC=0
  53. quilt push -a || RC=$?
  54. case "$RC" in
  55. 0|2)
  56. ;;
  57. *)
  58. false
  59. ;;
  60. esac
  61. popd > /dev/null
  62. log "End ${SUB_STAGE_DIR}/${i}-patches"
  63. fi
  64. if [ -x ${i}-run.sh ]; then
  65. log "Begin ${SUB_STAGE_DIR}/${i}-run.sh"
  66. ./${i}-run.sh
  67. log "End ${SUB_STAGE_DIR}/${i}-run.sh"
  68. fi
  69. if [ -f ${i}-run-chroot.sh ]; then
  70. log "Begin ${SUB_STAGE_DIR}/${i}-run-chroot.sh"
  71. on_chroot < ${i}-run-chroot.sh
  72. log "End ${SUB_STAGE_DIR}/${i}-run-chroot.sh"
  73. fi
  74. done
  75. popd > /dev/null
  76. log "End ${SUB_STAGE_DIR}"
  77. }
  78. run_stage(){
  79. log "Begin ${STAGE_DIR}"
  80. STAGE=$(basename ${STAGE_DIR})
  81. pushd ${STAGE_DIR} > /dev/null
  82. unmount ${WORK_DIR}/${STAGE}
  83. STAGE_WORK_DIR=${WORK_DIR}/${STAGE}
  84. ROOTFS_DIR=${STAGE_WORK_DIR}/rootfs
  85. if [ -f ${STAGE_DIR}/EXPORT_IMAGE ]; then
  86. EXPORT_DIRS="${EXPORT_DIRS} ${STAGE_DIR}"
  87. fi
  88. if [ ! -f SKIP ]; then
  89. if [ "${CLEAN}" = "1" ]; then
  90. if [ -d ${ROOTFS_DIR} ]; then
  91. rm -rf ${ROOTFS_DIR}
  92. fi
  93. fi
  94. if [ -x prerun.sh ]; then
  95. log "Begin ${STAGE_DIR}/prerun.sh"
  96. ./prerun.sh
  97. log "End ${STAGE_DIR}/prerun.sh"
  98. fi
  99. for SUB_STAGE_DIR in ${STAGE_DIR}/*; do
  100. if [ -d ${SUB_STAGE_DIR} ] &&
  101. [ ! -f ${SUB_STAGE_DIR}/SKIP ]; then
  102. run_sub_stage
  103. fi
  104. done
  105. fi
  106. unmount ${WORK_DIR}/${STAGE}
  107. PREV_STAGE=${STAGE}
  108. PREV_STAGE_DIR=${STAGE_DIR}
  109. PREV_ROOTFS_DIR=${ROOTFS_DIR}
  110. popd > /dev/null
  111. log "End ${STAGE_DIR}"
  112. }
  113. if [ "$(id -u)" != "0" ]; then
  114. echo "Please run as root" 1>&2
  115. exit 1
  116. fi
  117. if [ -f config ]; then
  118. source config
  119. fi
  120. if [ -z "${IMG_NAME}" ]; then
  121. echo "IMG_NAME not set" 1>&2
  122. exit 1
  123. fi
  124. export USE_QEMU=${USE_QEMU:-0}
  125. export IMG_DATE=${IMG_DATE:-"$(date +%Y-%m-%d)"}
  126. export BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  127. export SCRIPT_DIR="${BASE_DIR}/scripts"
  128. export WORK_DIR=${WORK_DIR:-"${BASE_DIR}/work/${IMG_DATE}-${IMG_NAME}"}
  129. export DEPLOY_DIR=${DEPLOY_DIR:-"${BASE_DIR}/deploy"}
  130. export LOG_FILE="${WORK_DIR}/build.log"
  131. export CLEAN
  132. export IMG_NAME
  133. export APT_PROXY
  134. export STAGE
  135. export STAGE_DIR
  136. export STAGE_WORK_DIR
  137. export PREV_STAGE
  138. export PREV_STAGE_DIR
  139. export ROOTFS_DIR
  140. export PREV_ROOTFS_DIR
  141. export IMG_SUFFIX
  142. export NOOBS_NAME
  143. export NOOBS_DESCRIPTION
  144. export EXPORT_DIR
  145. export EXPORT_ROOTFS_DIR
  146. export QUILT_PATCHES
  147. export QUILT_NO_DIFF_INDEX=1
  148. export QUILT_NO_DIFF_TIMESTAMPS=1
  149. export QUILT_REFRESH_ARGS="-p ab"
  150. source ${SCRIPT_DIR}/common
  151. source ${SCRIPT_DIR}/dependencies_check
  152. dependencies_check ${BASE_DIR}/depends
  153. mkdir -p ${WORK_DIR}
  154. log "Begin ${BASE_DIR}"
  155. for STAGE_DIR in ${BASE_DIR}/stage*; do
  156. run_stage
  157. done
  158. CLEAN=1
  159. for EXPORT_DIR in ${EXPORT_DIRS}; do
  160. STAGE_DIR=${BASE_DIR}/export-image
  161. source "${EXPORT_DIR}/EXPORT_IMAGE"
  162. EXPORT_ROOTFS_DIR=${WORK_DIR}/$(basename ${EXPORT_DIR})/rootfs
  163. run_stage
  164. if [ "${USE_QEMU}" != "1" ]; then
  165. if [ -e ${EXPORT_DIR}/EXPORT_NOOBS ]; then
  166. source ${EXPORT_DIR}/EXPORT_NOOBS
  167. STAGE_DIR=${BASE_DIR}/export-noobs
  168. run_stage
  169. fi
  170. fi
  171. done
  172. if [ -x postrun.sh ]; then
  173. log "Begin postrun.sh"
  174. cd "${BASE_DIR}"
  175. ./postrun.sh
  176. log "End postrun.sh"
  177. fi
  178. log "End ${BASE_DIR}"