lib.sh.in 13 KB

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