lib.sh.in 7.3 KB

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