build_resflash.sub 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/sh
  2. # Subroutines for build_resflash.sh
  3. # Copyright Brian Conway <bconway@rcesoftware.com>, see LICENSE for details
  4. usage_and_exit() {
  5. ${ESCECHO} 'usage: build_resflash.sh [-huV] [-p pkg_dir]' \
  6. '[--pkg_list pkg1,pkg2]\n' \
  7. ' [--pkg_path path1:path2] [-s com0_speed]\n' \
  8. ' [--swap reserve_swap_in_mb] [--syspatch]\n' \
  9. ' img_size_in_mb openbsd_base_dir'
  10. exit 1
  11. }
  12. # Args: 1 - basedir, 2 - imgsizemb, 3 - swapsizemb
  13. # Assigns: imgsizereqkb, fssizekb
  14. calc_fs_img_sizes() {
  15. # Minimum filesystem size, leave 25% headroom for fw_update, packages, other
  16. minfssizekb=$(($(du -ks ${1}|awk -safe '{ print $1 }') * 4 / 3))
  17. # Set img size requirements in KB: 2 * 16 + 4 MB DOS + /mbr, optional swap,
  18. # two /, 100 MB /cfg + 2 MB headroom
  19. imgsizereqkb=$((36 * 1024 + ${3} * 1024 + minfssizekb * 2 + 102 * 1024))
  20. # Calculate / sizes based on available room in image, with 1 MB headroom each
  21. fssizekb=$(((${2} - 36 - ${3} - 102) * 1024 / 2))
  22. }
  23. # Args: 1 - basedir, 2 - imgsizemb, 3 - swapsizemb
  24. # Assigns: MACHINE, CROSSARCH, KERNEL
  25. validate_base_dir() {
  26. fail=0
  27. echo "Validating OpenBSD base dir: ${1}"
  28. if [ ! -f ${1}/bsd ]; then
  29. fail=1
  30. echo "/bsd missing from ${1}, a kernel is required."
  31. fi
  32. if [ ! -f ${1}/dev/MAKEDEV ]; then
  33. fail=1
  34. echo "Missing some base files from ${1}, baseXY.tgz was not unpacked."
  35. fi
  36. if [ ! -f ${1}/etc/login.conf ]; then
  37. fail=1
  38. echo "Missing some /etc files, ${1}/var/sysmerge/etc.tgz was not unpacked."
  39. fi
  40. if [ -f ${1}/usr/bin/su ] && \
  41. [ ! -O ${1}/usr/bin/su -o ! -u ${1}/usr/bin/su ]; then
  42. fail=1
  43. echo "Incorrect permissions found, baseXY.tgz must be unpacked as root" \
  44. "using 'tar zxfph set.tgz'."
  45. fi
  46. # Inspect kernel and su to verify matching arches and set MACHINE
  47. if file ${1}/bsd*|grep -q x86-64 && \
  48. file ${1}/usr/bin/su|grep -q x86-64; then
  49. MACHINE=amd64
  50. elif file ${1}/bsd*|grep -q Intel\ 80386 && \
  51. file ${1}/usr/bin/su|grep -q Intel\ 80386; then
  52. MACHINE=i386
  53. elif file ${1}/bsd*|grep -q AArch64 && \
  54. file ${1}/usr/bin/su|grep -q AArch64; then
  55. MACHINE=arm64
  56. else
  57. fail=1
  58. MACHINE=unknown
  59. echo 'Mixed kernel and userland arches or unsupported arch.'
  60. fi
  61. # Validate for cross-arch use
  62. if [ ${MACHINE} != $(machine) ]; then
  63. CROSSARCH=1
  64. else
  65. CROSSARCH=0
  66. fi
  67. if [ ${CROSSARCH} -eq 1 ] && [ -n "${syspatch+1}" ]; then
  68. fail=1
  69. echo 'Cross-arch builds do not support syspatch at build time.'
  70. fi
  71. if [ -n "${pkgdir+1}" ] || [ -n "${pkglist+1}" ] && \
  72. [ ${CROSSARCH} -eq 1 ]; then
  73. fail=1
  74. echo 'Cross-arch builds do not support package installation at build time.'
  75. fi
  76. if [ ${CROSSARCH} -eq 1 ] && ! grep -q "$(uname -s -r)" ${1}/var/mail/root
  77. then
  78. fail=1
  79. echo 'Cross-arch builds must use the same OpenBSD version as build system.'
  80. fi
  81. # Validate for syspatch use, of which there are many constraints
  82. if [ -n "${syspatch+1}" ]; then
  83. buildkernel=$(sysctl -n kern.osversion)
  84. KERNEL=$(what ${1}/bsd|sed -n -e 's/[()]//g' -e '2p'|awk -safe \
  85. '{ print $3 }')
  86. ncpu=$(sysctl -n hw.ncpufound)
  87. if [ ${buildkernel%#*} != ${KERNEL} ]; then
  88. fail=1
  89. echo "Syspatch build system must use the same kernel configuration as" \
  90. "base dir: ${KERNEL}"
  91. fi
  92. if [ ${KERNEL} == 'GENERIC.MP' ] && [ ${ncpu} -lt 2 ]; then
  93. fail=1
  94. echo "Syspatch build system must have > 1 CPU when using GENERIC.MP:" \
  95. "${ncpu}"
  96. fi
  97. fi
  98. # Validate for arm64 use
  99. if [ ${MACHINE} == 'arm64' ] && \
  100. [ $(pkg_info|egrep '(raspberrypi-firmware|u-boot-aarch64)'|wc -l) -ne 2 ]
  101. then
  102. fail=1
  103. echo 'raspberrypi-firmware and u-boot-aarch64 packages are required for' \
  104. 'arm64 builds.'
  105. fi
  106. # Check for enough space
  107. calc_fs_img_sizes ${1} ${2} ${3}
  108. if [ ${imgsizereqkb} -gt $((${2} * 1024)) ]; then
  109. fail=1
  110. echo "Disk image size is too small for the required filesystems, at least" \
  111. "$((imgsizereqkb / 1024)) MB needed."
  112. fi
  113. if [ ${fail} -eq 1 ]; then
  114. exit 1
  115. fi
  116. }