lib.sh.in 11 KB

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