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.

common 2.4 KiB

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