lib.sh.in 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. # If the XBPS_TARGET_ARCH is unset but the PLATFORM is known, it
  135. # may be possible to set the architecture from the static
  136. # platforms map.
  137. if [ -z "$XBPS_TARGET_ARCH" ] && [ ! -z "$PLATFORM" ] ; then
  138. set_target_arch_from_platform
  139. fi
  140. case "${XBPS_TARGET_ARCH}" in
  141. armv*)
  142. _cpu=arm
  143. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00"
  144. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  145. QEMU_BIN=qemu-arm-static
  146. ;;
  147. aarch64*)
  148. _cpu=aarch64
  149. _magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7"
  150. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  151. QEMU_BIN=qemu-aarch64-static
  152. ;;
  153. mipsel*)
  154. _cpu=mipsel
  155. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"
  156. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  157. QEMU_BIN=qemu-mipsel-static
  158. ;;
  159. *86*)
  160. info_msg "FIXME: Assuming that x86 instructions are native"
  161. QEMU_BIN=NATIVE
  162. ;;
  163. *)
  164. die "Unknown target architecture!"
  165. ;;
  166. esac
  167. # In the special case where the build is native we can return
  168. # without doing anything else
  169. if [ "$QEMU_BIN" = "NATIVE" ] ; then
  170. return
  171. fi
  172. # For builds that do not match the host architecture, the correct
  173. # qemu binary will be required.
  174. if ! $QEMU_BIN -version >/dev/null 2>&1; then
  175. die "$QEMU_BIN binary is missing in your system, exiting."
  176. fi
  177. # In order to use the binfmt system the binfmt_misc mountpoint
  178. # must exist inside of proc
  179. if ! mountpoint -q /proc/sys/fs/binfmt_misc ; then
  180. modprobe -q binfmt_misc
  181. mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null
  182. fi
  183. # Only register if the map is incomplete
  184. if [ ! -f /proc/sys/fs/binfmt_misc/qemu-$_cpu ] ; then
  185. echo ":qemu-$_cpu:M::$_magic:$_mask:/usr/bin/$QEMU_BIN:" > /proc/sys/fs/binfmt_misc/register 2>/dev/null
  186. fi
  187. # If the static binary isn't in the chroot then the chroot will
  188. # fail. The kernel knows about the map but without the static
  189. # version there's no interpreter in the chroot, only the
  190. # dynamically linked one in the host. To simplify things we just
  191. # use the static one always and make sure it shows up at the same
  192. # place in the host and the chroot.
  193. if [ ! -x "$ROOTFS/usr/bin/$QEMU_BIN" ] ; then
  194. cp -f "$(which "$QEMU_BIN")" "$ROOTFS/usr/bin" ||
  195. die "Could not install $QEMU_BIN to $ROOTFS/usr/bin/"
  196. fi
  197. }
  198. set_target_arch_from_platform() {
  199. # This function maintains a lookup from platform to target
  200. # architecture. This is required for scripts that need to know
  201. # the target architecture, but don't necessarily need to know it
  202. # internally (i.e. only run_cmd_chroot).
  203. case "$PLATFORM" in
  204. bananapi*) XBPS_TARGET_ARCH="armv7l";;
  205. beaglebone*) XBPS_TARGET_ARCH="armv7l";;
  206. cubieboard2*|cubietruck*) XBPS_TARGET_ARCH="armv7l";;
  207. dockstar*) XBPS_TARGET_ARCH="armv5tel";;
  208. odroid-u2*) XBPS_TARGET_ARCH="armv7l";;
  209. odroid-c2*) XBPS_TARGET_ARCH="aarch64";;
  210. rpi3*) XBPS_TARGET_ARCH="aarch64";;
  211. rpi2*) XBPS_TARGET_ARCH="armv7l";;
  212. rpi*) XBPS_TARGET_ARCH="armv6l";;
  213. usbarmory*) XBPS_TARGET_ARCH="armv7l";;
  214. ci20*) XBPS_TARGET_ARCH="mipsel";;
  215. i686*) XBPS_TARGET_ARCH="i686";;
  216. x86_64*) XBPS_TARGET_ARCH="x86_64";;
  217. GCP*) XBPS_TARGET_ARCH="x86_64";;
  218. *) die "$PROGNAME: Unable to compute target architecture from platform";;
  219. esac
  220. if [ -z "${PLATFORM##*-musl}" ] ; then
  221. XBPS_TARGET_ARCH="${XBPS_TARGET_ARCH}-musl"
  222. fi
  223. }
  224. set_cachedir() {
  225. # The package artifacts are cacheable, but they need to be isolated
  226. # from the host cache.
  227. : "${XBPS_CACHEDIR:=--cachedir=$PWD/xbps-cache/${XBPS_TARGET_ARCH}}"
  228. }
  229. # These should all resolve even if they won't have the appropriate
  230. # repodata files for the selected architecture.
  231. : "${XBPS_REPOSITORY:=--repository=http://repo.voidlinux.eu/current \
  232. --repository=http://repo.voidlinux.eu/current/musl \
  233. --repository=http://repo.voidlinux.eu/current/aarch64}"