lib.sh.in 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. info_msg() {
  2. # This function handles the printing that is bold within all
  3. # scripts. This is a convenience function so that the rather ugly
  4. # looking ASCII escape codes live in only one place.
  5. printf "\033[1m%s\n\033[m" "$@"
  6. }
  7. die() {
  8. # This function is registered in all the scripts to make sure that
  9. # the important mounts get cleaned up and the $ROOTFS location is
  10. # removed.
  11. printf "FATAL: %s\n" "$@"
  12. umount_pseudofs
  13. [ -d "$ROOTFS" ] && rm -rf "$ROOTFS"
  14. exit 1
  15. }
  16. mount_pseudofs() {
  17. # This function ensures that the psuedofs mountpoints are present
  18. # in the chroot. Strictly they are not necessary to have for many
  19. # commands, but bind-mounts are cheap and it isn't too bad to just
  20. # mount them all the time.
  21. for f in dev proc sys; do
  22. # In a naked chroot there is nothing to bind the mounts to, so
  23. # we need to create directories for these first.
  24. [ ! -d "$ROOTFS/$f" ] && mkdir -p "$ROOTFS/$f"
  25. if ! mountpoint -q "$ROOTFS/$f" ; then
  26. # It is VERY important that this only happen if the
  27. # pseudofs isn't already mounted. If it already is then
  28. # this is virtually impossible to troubleshoot because it
  29. # looks like the subsequent umount just isn't working.
  30. mount -r --bind /$f "$ROOTFS/$f"
  31. fi
  32. done
  33. }
  34. umount_pseudofs() {
  35. # This function cleans up the mounts in the chroot. Failure to
  36. # clean up these mounts will prevent the tmpdir from being
  37. # deletable instead throwing the error "Device or Resource Busy".
  38. # The '-f' option is passed to umount to account for the
  39. # contingency where the psuedofs mounts are not present.
  40. if [ -d "${ROOTFS}" ]; then
  41. for f in dev proc sys; do
  42. umount -f "$ROOTFS/$f" >/dev/null 2>&1
  43. done
  44. fi
  45. }
  46. run_cmd_target() {
  47. info_msg "Running $* for target $XBPS_TARGET_ARCH ..."
  48. if [ "$XBPS_TARGET_ARCH" = "$(xbps-uhelper arch)" ] ; then
  49. # This is being run on the same architecture as the host,
  50. # therefore we should set XBPS_ARCH.
  51. if ! eval XBPS_ARCH="$XBPS_TARGET_ARCH" "$@" ; then
  52. die "Could not run command $*"
  53. fi
  54. else
  55. # This is being run on a foriegn arch, therefore we should set
  56. # XBPS_TARGET_ARCH. In this case XBPS will not attempt
  57. # certain actions and will require reconfiguration later.
  58. if ! eval XBPS_TARGET_ARCH="$XBPS_TARGET_ARCH" "$@" ; then
  59. die "Could not run command $*"
  60. fi
  61. fi
  62. }
  63. run_cmd() {
  64. # This is a general purpose function to run commands that a user
  65. # may wish to see. For example its useful to see the tar/xz
  66. # pipeline to not need to delve into the scripts to see what
  67. # options its set up with.
  68. info_msg "Running $*"
  69. eval "$@"
  70. }
  71. run_cmd_chroot() {
  72. # General purpose chroot function which makes sure the chroot is
  73. # prepared. This function takes 2 arguments, the location to
  74. # chroot to and the command to run.
  75. # This is an idempotent function, it is safe to call every time
  76. # before entering the chroot. This has the advantage of making
  77. # execution in the chroot appear as though it "Just Works(tm)".
  78. register_binfmt
  79. # Before we step into the chroot we need to make sure the
  80. # pseudo-filesystems are ready to go. Not all commands will need
  81. # this, but its still a good idea to call it here anyway.
  82. mount_pseudofs
  83. # With assurance that things will run now we can jump into the
  84. # chroot and run stuff!
  85. chroot "$1" sh -c "$2"
  86. }
  87. cleanup_chroot() {
  88. # This function cleans up the chroot shims that are used by QEMU
  89. # to allow builds on alien platforms. It takes no arguments but
  90. # expects the global $ROOTFS variable to be set.
  91. # Un-Mount the pseudofs mounts if they were mounted
  92. umount_pseudofs
  93. # If a QEMU binary was copied in, remove that as well
  94. if [ -x "$ROOTFS/usr/bin/$QEMU_BIN" ] ; then
  95. rm "$ROOTFS/usr/bin/$QEMU_BIN"
  96. fi
  97. }
  98. # TODO: Figure out how to register the binfmt for x86_64 and for i686
  99. # to facilitate building on alien build systems.
  100. register_binfmt() {
  101. # This function sets up everything that is needed to be able to
  102. # chroot into a ROOTFS and be able to run commands there. This
  103. # really matters on platforms where the host architecture is
  104. # different from the target, and you wouldn't be able to run
  105. # things like xbps-reconfigure -a. This function is idempotent
  106. # (You can run it multiple times without modifying state). This
  107. # function takes no arguments, but does expect the global variable
  108. # $XBPS_TARGET_ARCH to be set.
  109. # This select sets up the "magic" bytes in /proc that let the
  110. # kernel select an alternate interpreter. More values for this
  111. # map can be obtained from here:
  112. # https://github.com/qemu/qemu/blob/master/scripts/qemu-binfmt-conf.sh
  113. case "${XBPS_TARGET_ARCH}" in
  114. armv*)
  115. _cpu=arm
  116. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00"
  117. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  118. QEMU_BIN=qemu-arm-static
  119. ;;
  120. aarch64*)
  121. _cpu=aarch64
  122. _magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7"
  123. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  124. QEMU_BIN=qemu-aarch64-static
  125. ;;
  126. mipsel*)
  127. _cpu=mipsel
  128. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"
  129. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  130. QEMU_BIN=qemu-mipsel-static
  131. ;;
  132. *86*)
  133. info_msg "FIXME: Assuming that x86 instructions are native"
  134. QEMU_BIN=NATIVE
  135. ;;
  136. *)
  137. die "Unknown target architecture!"
  138. ;;
  139. esac
  140. # In the special case where the build is native we can return
  141. # without doing anything else
  142. if [ "$QEMU_BIN" = "NATIVE" ] ; then
  143. return
  144. fi
  145. # For builds that do not match the host architecture, the correct
  146. # qemu binary will be required.
  147. if ! $QEMU_BIN -version >/dev/null 2>&1; then
  148. die "$QEMU_BIN binary is missing in your system, exiting."
  149. fi
  150. # In order to use the binfmt system the binfmt_misc mountpoint
  151. # must exist inside of proc
  152. if ! mountpoint -q /proc/sys/fs/binfmt_misc ; then
  153. modprobe -q binfmt_misc
  154. mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null
  155. fi
  156. # Only register if the map is incomplete
  157. if [ ! -f /proc/sys/fs/binfmt_misc/qemu-$_cpu ] ; then
  158. echo ":qemu-$_cpu:M::$_magic:$_mask:$QEMU_BIN:" > /proc/sys/fs/binfmt_misc/register 2>/dev/null
  159. fi
  160. # If the static binary isn't in the chroot then the chroot will
  161. # fail. The kernel knows about the map but without the static
  162. # version there's no interpreter in the chroot, only the
  163. # dynamically linked one in the host. To simplify things we just
  164. # use the static one always and make sure it shows up at the same
  165. # place in the host and the chroot.
  166. if [ ! -x "$ROOTFS/usr/bin/$QEMU_BIN" ] ; then
  167. cp -f "$(which "$QEMU_BIN")" "$ROOTFS/usr/bin" ||
  168. die "Could not install $QEMU_BIN to $ROOTFS/usr/bin/"
  169. fi
  170. }