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.
 
 
 
 
 

105 lines
2.6 KiB

  1. log (){
  2. date +"[%T] $*" | tee -a "${LOG_FILE}"
  3. }
  4. export -f log
  5. bootstrap(){
  6. local BOOTSTRAP_CMD=debootstrap
  7. local BOOTSTRAP_ARGS=()
  8. #export http_proxy=${APT_PROXY}
  9. if [ "$(dpkg --print-architecture)" != "armhf" ] && [ "$(dpkg --print-architecture)" != "aarch64" ]; then
  10. BOOTSTRAP_CMD=qemu-debootstrap
  11. fi
  12. BOOTSTRAP_ARGS+=(--arch arm64)
  13. BOOTSTRAP_ARGS+=(--include gnupg)
  14. BOOTSTRAP_ARGS+=(--components "main,contrib,non-free")
  15. #BOOTSTRAP_ARGS+=(--keyring "${STAGE_DIR}/files/raspberrypi.gpg")
  16. BOOTSTRAP_ARGS+=(--exclude=info)
  17. BOOTSTRAP_ARGS+=("$@")
  18. printf -v BOOTSTRAP_STR '%q ' "${BOOTSTRAP_ARGS[@]}"
  19. capsh --drop=cap_setfcap -- -c "'${BOOTSTRAP_CMD}' $BOOTSTRAP_STR" || true
  20. if [ -d "$2/debootstrap" ] && ! rmdir "$2/debootstrap"; then
  21. cp "$2/debootstrap/debootstrap.log" "${STAGE_WORK_DIR}"
  22. log "bootstrap failed: please check ${STAGE_WORK_DIR}/debootstrap.log"
  23. return 1
  24. fi
  25. }
  26. export -f bootstrap
  27. copy_previous(){
  28. if [ ! -d "${PREV_ROOTFS_DIR}" ]; then
  29. echo "Previous stage rootfs not found"
  30. false
  31. fi
  32. mkdir -p "${ROOTFS_DIR}"
  33. rsync -aHAXx --exclude var/cache/apt/archives "${PREV_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
  34. }
  35. export -f copy_previous
  36. unmount(){
  37. if [ -z "$1" ]; then
  38. DIR=$PWD
  39. else
  40. DIR=$1
  41. fi
  42. while mount | grep -q "$DIR"; do
  43. local LOCS
  44. LOCS=$(mount | grep "$DIR" | cut -f 3 -d ' ' | sort -r)
  45. for loc in $LOCS; do
  46. umount "$loc"
  47. done
  48. done
  49. }
  50. export -f unmount
  51. unmount_image(){
  52. sync
  53. sleep 1
  54. local LOOP_DEVICES
  55. LOOP_DEVICES=$(losetup --list | grep "$(basename "${1}")" | cut -f1 -d' ')
  56. for LOOP_DEV in ${LOOP_DEVICES}; do
  57. if [ -n "${LOOP_DEV}" ]; then
  58. local MOUNTED_DIR
  59. MOUNTED_DIR=$(mount | grep "$(basename "${LOOP_DEV}")" | head -n 1 | cut -f 3 -d ' ')
  60. if [ -n "${MOUNTED_DIR}" ] && [ "${MOUNTED_DIR}" != "/" ]; then
  61. unmount "$(dirname "${MOUNTED_DIR}")"
  62. fi
  63. sleep 1
  64. losetup -d "${LOOP_DEV}"
  65. fi
  66. done
  67. }
  68. export -f unmount_image
  69. on_chroot() {
  70. if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/proc)"; then
  71. mount -t proc proc "${ROOTFS_DIR}/proc"
  72. fi
  73. if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/dev)"; then
  74. mount --bind /dev "${ROOTFS_DIR}/dev"
  75. fi
  76. if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/dev/pts)"; then
  77. mount --bind /dev/pts "${ROOTFS_DIR}/dev/pts"
  78. fi
  79. if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/sys)"; then
  80. mount --bind /sys "${ROOTFS_DIR}/sys"
  81. fi
  82. capsh --drop=cap_setfcap "--chroot=${ROOTFS_DIR}/" -- -e "$@"
  83. }
  84. export -f on_chroot
  85. update_issue() {
  86. echo -e "Raspberry Pi reference ${IMG_DATE}\nGenerated using ${PI_GEN}, ${PI_GEN_REPO}, ${GIT_HASH}, ${1}" > "${ROOTFS_DIR}/etc/rpi-issue"
  87. }
  88. export -f update_issue