lib.sh.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 $tool 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. if ! mountpoint -q "$ROOTFS/tmp" ; then
  56. mkdir -p "$ROOTFS/tmp"
  57. mount -o mode=0755,nosuid,nodev -t tmpfs tmpfs "$ROOTFS/tmp"
  58. fi
  59. }
  60. umount_pseudofs() {
  61. # This function cleans up the mounts in the chroot. Failure to
  62. # clean up these mounts will prevent the tmpdir from being
  63. # deletable instead throwing the error "Device or Resource Busy".
  64. # The '-f' option is passed to umount to account for the
  65. # contingency where the psuedofs mounts are not present.
  66. if [ -d "${ROOTFS}" ]; then
  67. for f in dev proc sys; do
  68. umount -f "$ROOTFS/$f" >/dev/null 2>&1
  69. done
  70. fi
  71. umount -f "$ROOTFS/tmp" >/dev/null 2>&1
  72. }
  73. run_cmd_target() {
  74. info_msg "Running $* for target $XBPS_TARGET_ARCH ..."
  75. if [ "$XBPS_TARGET_ARCH" = "${HOSTARCH}" ] ||
  76. [ -z "${XBPS_TARGET_ARCH##*86*}" ] &&
  77. [ -z "${HOSTARCH##*86*}" ] ; then
  78. # This is being run on the same architecture as the host,
  79. # therefore we should set XBPS_ARCH.
  80. if ! eval XBPS_ARCH="$XBPS_TARGET_ARCH" "$@" ; then
  81. die "Could not run command $*"
  82. fi
  83. else
  84. # This is being run on a foriegn arch, therefore we should set
  85. # XBPS_TARGET_ARCH. In this case XBPS will not attempt
  86. # certain actions and will require reconfiguration later.
  87. if ! eval XBPS_TARGET_ARCH="$XBPS_TARGET_ARCH" "$@" ; then
  88. die "Could not run command $*"
  89. fi
  90. fi
  91. }
  92. run_cmd() {
  93. # This is a general purpose function to run commands that a user
  94. # may wish to see. For example its useful to see the tar/xz
  95. # pipeline to not need to delve into the scripts to see what
  96. # options its set up with.
  97. info_msg "Running $*"
  98. eval "$@"
  99. }
  100. run_cmd_chroot() {
  101. # General purpose chroot function which makes sure the chroot is
  102. # prepared. This function takes 2 arguments, the location to
  103. # chroot to and the command to run.
  104. # This is an idempotent function, it is safe to call every time
  105. # before entering the chroot. This has the advantage of making
  106. # execution in the chroot appear as though it "Just Works(tm)".
  107. register_binfmt
  108. # Before we step into the chroot we need to make sure the
  109. # pseudo-filesystems are ready to go. Not all commands will need
  110. # this, but its still a good idea to call it here anyway.
  111. mount_pseudofs
  112. # With assurance that things will run now we can jump into the
  113. # chroot and run stuff!
  114. chroot "$1" sh -c "$2"
  115. }
  116. cleanup_chroot() {
  117. # This function cleans up the chroot shims that are used by QEMU
  118. # to allow builds on alien platforms. It takes no arguments but
  119. # expects the global $ROOTFS variable to be set.
  120. # Un-Mount the pseudofs mounts if they were mounted
  121. umount_pseudofs
  122. # If a QEMU binary was copied in, remove that as well
  123. if [ -x "$ROOTFS/usr/bin/$QEMU_BIN" ] ; then
  124. rm "$ROOTFS/usr/bin/$QEMU_BIN"
  125. fi
  126. }
  127. register_binfmt() {
  128. # This function sets up everything that is needed to be able to
  129. # chroot into a ROOTFS and be able to run commands there. This
  130. # really matters on platforms where the host architecture is
  131. # different from the target, and you wouldn't be able to run
  132. # things like xbps-reconfigure -a. This function is idempotent
  133. # (You can run it multiple times without modifying state). This
  134. # function takes no arguments, but does expect the global variable
  135. # $XBPS_TARGET_ARCH to be set.
  136. # This select sets up the "magic" bytes in /proc that let the
  137. # kernel select an alternate interpreter. More values for this
  138. # map can be obtained from here:
  139. # https://github.com/qemu/qemu/blob/master/scripts/qemu-binfmt-conf.sh
  140. # If the XBPS_TARGET_ARCH is unset but the PLATFORM is known, it
  141. # may be possible to set the architecture from the static
  142. # platforms map.
  143. if [ -z "$XBPS_TARGET_ARCH" ] && [ ! -z "$PLATFORM" ] ; then
  144. set_target_arch_from_platform
  145. fi
  146. # In the special case where the build is native we can return
  147. # without doing anything else
  148. if [ "${HOSTARCH%-musl}" = "${XBPS_TARGET_ARCH%-musl}" ] ; then
  149. return
  150. fi
  151. case "${XBPS_TARGET_ARCH}" in
  152. armv*)
  153. _cpu=arm
  154. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00"
  155. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  156. QEMU_BIN=qemu-arm-static
  157. ;;
  158. aarch64*)
  159. _cpu=aarch64
  160. _magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7"
  161. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  162. QEMU_BIN=qemu-aarch64-static
  163. ;;
  164. ppc64le*)
  165. _cpu=ppc64le
  166. _magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00"
  167. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00"
  168. QEMU_BIN=qemu-ppc64le-static
  169. ;;
  170. ppc64*)
  171. _cpu=ppc64
  172. _magic="\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15"
  173. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  174. QEMU_BIN=qemu-ppc64-static
  175. ;;
  176. ppc*)
  177. _cpu=ppc
  178. _magic="\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14"
  179. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  180. QEMU_BIN=qemu-ppc-static
  181. ;;
  182. mipsel*)
  183. _cpu=mipsel
  184. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"
  185. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  186. QEMU_BIN=qemu-mipsel-static
  187. ;;
  188. x86_64*)
  189. _cpu=x86_64
  190. _magic="\x7f\x45\x4c\x46\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00"
  191. _mask="\xff\xff\xff\xff\xff\xfe\xfe\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  192. QEMU_BIN=qemu-x86_64-static
  193. ;;
  194. i686*)
  195. _cpu=i386
  196. _magic="\x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00"
  197. _mask="\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  198. QEMU_BIN=qemu-i386-static
  199. ;;
  200. *)
  201. die "Unknown target architecture!"
  202. ;;
  203. esac
  204. # For builds that do not match the host architecture, the correct
  205. # qemu binary will be required.
  206. if ! $QEMU_BIN -version >/dev/null 2>&1; then
  207. die "$QEMU_BIN binary is missing in your system, exiting."
  208. fi
  209. # In order to use the binfmt system the binfmt_misc mountpoint
  210. # must exist inside of proc
  211. if ! mountpoint -q /proc/sys/fs/binfmt_misc ; then
  212. modprobe -q binfmt_misc
  213. mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null
  214. fi
  215. # Only register if the map is incomplete
  216. if [ ! -f /proc/sys/fs/binfmt_misc/qemu-$_cpu ] ; then
  217. echo ":qemu-$_cpu:M::$_magic:$_mask:/usr/bin/$QEMU_BIN:" > /proc/sys/fs/binfmt_misc/register 2>/dev/null
  218. fi
  219. # If the static binary isn't in the chroot then the chroot will
  220. # fail. The kernel knows about the map but without the static
  221. # version there's no interpreter in the chroot, only the
  222. # dynamically linked one in the host. To simplify things we just
  223. # use the static one always and make sure it shows up at the same
  224. # place in the host and the chroot.
  225. if [ ! -x "$ROOTFS/usr/bin/$QEMU_BIN" ] ; then
  226. install -m755 -D "$(which "$QEMU_BIN")" "$ROOTFS/usr/bin/$QEMU_BIN" ||
  227. die "Could not install $QEMU_BIN to $ROOTFS/usr/bin/"
  228. fi
  229. }
  230. set_target_arch_from_platform() {
  231. # This function maintains a lookup from platform to target
  232. # architecture. This is required for scripts that need to know
  233. # the target architecture, but don't necessarily need to know it
  234. # internally (i.e. only run_cmd_chroot).
  235. case "$PLATFORM" in
  236. bananapi*) XBPS_TARGET_ARCH="armv7l";;
  237. beaglebone*) XBPS_TARGET_ARCH="armv7l";;
  238. cubieboard2*|cubietruck*) XBPS_TARGET_ARCH="armv7l";;
  239. odroid-u2*) XBPS_TARGET_ARCH="armv7l";;
  240. odroid-c2*) XBPS_TARGET_ARCH="aarch64";;
  241. rpi3*) XBPS_TARGET_ARCH="aarch64";;
  242. rpi2*) XBPS_TARGET_ARCH="armv7l";;
  243. rpi*) XBPS_TARGET_ARCH="armv6l";;
  244. ci20*) XBPS_TARGET_ARCH="mipsel";;
  245. i686*) XBPS_TARGET_ARCH="i686";;
  246. x86_64*) XBPS_TARGET_ARCH="x86_64";;
  247. GCP*) XBPS_TARGET_ARCH="x86_64";;
  248. pinebookpro*) XBPS_TARGET_ARCH="aarch64";;
  249. *) die "$PROGNAME: Unable to compute target architecture from platform";;
  250. esac
  251. if [ -z "${PLATFORM##*-musl}" ] ; then
  252. XBPS_TARGET_ARCH="${XBPS_TARGET_ARCH}-musl"
  253. fi
  254. }
  255. set_dracut_args_from_platform() {
  256. # In rare cases it is necessary to set platform specific dracut
  257. # args. This is mostly the case on ARM platforms.
  258. case "$PLATFORM" in
  259. *) ;;
  260. esac
  261. }
  262. set_cachedir() {
  263. # The package artifacts are cacheable, but they need to be isolated
  264. # from the host cache.
  265. : "${XBPS_CACHEDIR:=--cachedir=$PWD/xbps-cache/${XBPS_TARGET_ARCH}}"
  266. }
  267. # These should all resolve even if they won't have the appropriate
  268. # repodata files for the selected architecture.
  269. : "${XBPS_REPOSITORY:=--repository=http://alpha.de.repo.voidlinux.org/current \
  270. --repository=http://alpha.de.repo.voidlinux.org/current/musl \
  271. --repository=http://alpha.de.repo.voidlinux.org/current/aarch64}"
  272. # This library is the authoritative source of the platform map,
  273. # because of this we may need to get this information from the command
  274. # line. This select allows us to get that information out. This
  275. # fails silently if the toolname isn't known since this script is
  276. # sourced.
  277. case $1 in
  278. platform2arch)
  279. PLATFORM=$2
  280. set_target_arch_from_platform
  281. echo "$XBPS_TARGET_ARCH"
  282. ;;
  283. esac