lib.sh.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. # TODO: Figure out how to register the binfmt for x86_64 and for i686
  128. # to facilitate building on alien build systems.
  129. register_binfmt() {
  130. # This function sets up everything that is needed to be able to
  131. # chroot into a ROOTFS and be able to run commands there. This
  132. # really matters on platforms where the host architecture is
  133. # different from the target, and you wouldn't be able to run
  134. # things like xbps-reconfigure -a. This function is idempotent
  135. # (You can run it multiple times without modifying state). This
  136. # function takes no arguments, but does expect the global variable
  137. # $XBPS_TARGET_ARCH to be set.
  138. # This select sets up the "magic" bytes in /proc that let the
  139. # kernel select an alternate interpreter. More values for this
  140. # map can be obtained from here:
  141. # https://github.com/qemu/qemu/blob/master/scripts/qemu-binfmt-conf.sh
  142. # If the XBPS_TARGET_ARCH is unset but the PLATFORM is known, it
  143. # may be possible to set the architecture from the static
  144. # platforms map.
  145. if [ -z "$XBPS_TARGET_ARCH" ] && [ ! -z "$PLATFORM" ] ; then
  146. set_target_arch_from_platform
  147. fi
  148. case "${XBPS_TARGET_ARCH}" in
  149. armv*)
  150. _cpu=arm
  151. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00"
  152. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  153. QEMU_BIN=qemu-arm-static
  154. ;;
  155. aarch64*)
  156. _cpu=aarch64
  157. _magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7"
  158. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  159. QEMU_BIN=qemu-aarch64-static
  160. ;;
  161. ppc64le*)
  162. _cpu=ppc64le
  163. _magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00"
  164. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00"
  165. QEMU_BIN=qemu-ppc64le-static
  166. ;;
  167. ppc64*)
  168. _cpu=ppc64
  169. _magic="\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15"
  170. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  171. QEMU_BIN=qemu-ppc64-static
  172. ;;
  173. ppc*)
  174. _cpu=ppc
  175. _magic="\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x14"
  176. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  177. QEMU_BIN=qemu-ppc-static
  178. ;;
  179. mipsel*)
  180. _cpu=mipsel
  181. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"
  182. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  183. QEMU_BIN=qemu-mipsel-static
  184. ;;
  185. *86*)
  186. info_msg "FIXME: Assuming that x86 instructions are native"
  187. QEMU_BIN=NATIVE
  188. ;;
  189. *)
  190. die "Unknown target architecture!"
  191. ;;
  192. esac
  193. # In the special case where the build is native we can return
  194. # without doing anything else
  195. if [ "$QEMU_BIN" = "NATIVE" ] ; then
  196. return
  197. fi
  198. # For builds that do not match the host architecture, the correct
  199. # qemu binary will be required.
  200. if ! $QEMU_BIN -version >/dev/null 2>&1; then
  201. die "$QEMU_BIN binary is missing in your system, exiting."
  202. fi
  203. # In order to use the binfmt system the binfmt_misc mountpoint
  204. # must exist inside of proc
  205. if ! mountpoint -q /proc/sys/fs/binfmt_misc ; then
  206. modprobe -q binfmt_misc
  207. mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null
  208. fi
  209. # Only register if the map is incomplete
  210. if [ ! -f /proc/sys/fs/binfmt_misc/qemu-$_cpu ] ; then
  211. echo ":qemu-$_cpu:M::$_magic:$_mask:/usr/bin/$QEMU_BIN:" > /proc/sys/fs/binfmt_misc/register 2>/dev/null
  212. fi
  213. # If the static binary isn't in the chroot then the chroot will
  214. # fail. The kernel knows about the map but without the static
  215. # version there's no interpreter in the chroot, only the
  216. # dynamically linked one in the host. To simplify things we just
  217. # use the static one always and make sure it shows up at the same
  218. # place in the host and the chroot.
  219. if [ ! -x "$ROOTFS/usr/bin/$QEMU_BIN" ] ; then
  220. install -m755 -D "$(which "$QEMU_BIN")" "$ROOTFS/usr/bin/$QEMU_BIN" ||
  221. die "Could not install $QEMU_BIN to $ROOTFS/usr/bin/"
  222. fi
  223. }
  224. set_target_arch_from_platform() {
  225. # This function maintains a lookup from platform to target
  226. # architecture. This is required for scripts that need to know
  227. # the target architecture, but don't necessarily need to know it
  228. # internally (i.e. only run_cmd_chroot).
  229. case "$PLATFORM" in
  230. bananapi*) XBPS_TARGET_ARCH="armv7l";;
  231. beaglebone*) XBPS_TARGET_ARCH="armv7l";;
  232. cubieboard2*|cubietruck*) XBPS_TARGET_ARCH="armv7l";;
  233. dockstar*) XBPS_TARGET_ARCH="armv5tel";;
  234. pogoplugv4*) XBPS_TARGET_ARCH="armv5tel" ;;
  235. odroid-u2*) XBPS_TARGET_ARCH="armv7l";;
  236. odroid-c2*) XBPS_TARGET_ARCH="aarch64";;
  237. rpi3*) XBPS_TARGET_ARCH="aarch64";;
  238. rpi2*) XBPS_TARGET_ARCH="armv7l";;
  239. rpi*) XBPS_TARGET_ARCH="armv6l";;
  240. usbarmory*) XBPS_TARGET_ARCH="armv7l";;
  241. ci20*) XBPS_TARGET_ARCH="mipsel";;
  242. i686*) XBPS_TARGET_ARCH="i686";;
  243. x86_64*) XBPS_TARGET_ARCH="x86_64";;
  244. GCP*) XBPS_TARGET_ARCH="x86_64";;
  245. pinebookpro*) XBPS_TARGET_ARCH="aarch64";;
  246. *) die "$PROGNAME: Unable to compute target architecture from platform";;
  247. esac
  248. if [ -z "${PLATFORM##*-musl}" ] ; then
  249. XBPS_TARGET_ARCH="${XBPS_TARGET_ARCH}-musl"
  250. fi
  251. }
  252. set_dracut_args_from_platform() {
  253. # In rare cases it is necessary to set platform specific dracut
  254. # args. This is mostly the case on ARM platforms.
  255. case "$PLATFORM" in
  256. pogoplugv4*) dracut_args="-o 'btrfs drm i18n resume terminfo'" ;;
  257. *) ;;
  258. esac
  259. }
  260. set_cachedir() {
  261. # The package artifacts are cacheable, but they need to be isolated
  262. # from the host cache.
  263. : "${XBPS_CACHEDIR:=--cachedir=$PWD/xbps-cache/${XBPS_TARGET_ARCH}}"
  264. }
  265. # These should all resolve even if they won't have the appropriate
  266. # repodata files for the selected architecture.
  267. : "${XBPS_REPOSITORY:=--repository=http://alpha.de.repo.voidlinux.org/current \
  268. --repository=http://alpha.de.repo.voidlinux.org/current/musl \
  269. --repository=http://alpha.de.repo.voidlinux.org/current/aarch64}"
  270. # This library is the authoritative source of the platform map,
  271. # because of this we may need to get this information from the command
  272. # line. This select allows us to get that information out. This
  273. # fails silently if the toolname isn't known since this script is
  274. # sourced.
  275. case $1 in
  276. platform2arch)
  277. PLATFORM=$2
  278. set_target_arch_from_platform
  279. echo "$XBPS_TARGET_ARCH"
  280. ;;
  281. esac