lib.sh 12 KB

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