lib.sh.in 8.8 KB

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