]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makeworld
script/makeworld: autotune make jobs to 2x hardware contexts (CPUs * cores * threads)
[CDN/Mosi.git] / script / makeworld
1 #!/bin/sh
2
3 # Boilerplate
4 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
5
6 # Load needed modules
7 want root log
8
9 alias meh=log
10 alias omg=warn
11 alias wtf=err
12
13 # Root directory of makeworld
14 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
15
16 # Makeworld dir structure should contain, at minimum:
17 # script
18 #  makeworld (this file)
19 # seed
20 #  base
21 #   <ARCH> (arch of build host: i386, amd64, etc)
22 #    base.* (virgin base tree of same arch as host, used to seed chroot for clean build)
23 # <TARGET> (i386, amd64, etc)
24 #  <CONFIG> (GENERIC, SABA, SS4200, etc)
25 #   config
26 #    make.conf
27 #    src.conf
28 #    CONFIG (Matches config name: GENERIC, SABA, SS4200, etc)
29
30 # Compute number of simultaneous make jobs (usually 2x cpu/thread count) to set make -j
31 make_cpus="$(sysctl -n hw.ncpu)"
32 make_jobs="$(( ${make_cpus} * 2 ))"
33
34 # Going to build this config:
35 TARGET="${TARGET:-i386}"
36 CONFIG="${CONFIG:-GENERIC}"
37 MAKEOPTS="-j${make_jobs} ${MAKEOPTS}"
38
39
40 # Check if it exists
41 [ -d "${ROOT}/${TARGET}/${CONFIG}" ] || wtf "${TARGET}/${CONFIG} doesn't exist"
42
43 # Target world directory
44 world="${ROOT}/${TARGET}/${CONFIG}"
45 # Source chroot seed directory
46 seed="${ROOT}/seed/base/$(uname -m)"
47 # Root directory for chroot
48 build="${ROOT}/seed/chroot-${TARGET}-${CONFIG}"
49
50 # Default build phases
51 phases="${phases:-buildworld buildkernel distrib-dirs installworld installkernel distribution}"
52
53 date="$(date +%Y%m%d)"
54
55 # Functions
56
57 # Prepare chroot for build
58 prepare() {
59   mount | grep -q "${build}" && wtf "Stuff is mounted under ${build}; cannot continue"
60
61   # Cleanup trap here, so that an abort during prepare can clean up properly
62   trap "cleanup" exit hup int term kill
63
64   meh "Preparing build chroot"
65   [ -d "${build}" ] && wtf "${build}: directory exists"
66   mkdir -p "${build}" || wtf
67   ls -1 "${seed}"/base.?? >/dev/null 2>&1 || wtf "Populate seed directory ${seed} first"
68   cat "${seed}"/base.?? | tar xCf "${build}" - || wtf
69   mkdir -p "${build}/usr/obj" || wtf
70
71   meh "Mounting chroot filesystems"
72   mkdir -p "${world}/obj"
73   mkdir -p "${world}/root"
74   mount -t devfs devfs "${build}/dev" || wtf
75   # Mount /usr/src as a union, so that changes to it will not affect the underlying tree
76   # unionfs suffers from deadlocks; don't use it
77   mount -t nullfs -r /usr/src "${build}/usr/src" || wtf
78   mount -t nullfs "${world}/obj" "${build}/usr/obj" || wtf
79   mount -t nullfs "${world}/root" "${build}/mnt" || wtf
80
81   if [ -d "${world}/config" ]
82   then
83     meh "Installing build-time configuration"
84     [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "/usr/src/sys/${TARGET}/conf/"
85     [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
86     [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"
87   fi
88 }
89
90 # Cleanup chroot
91 cleanup() {
92   meh "Cleaning up"
93   umount -f "${build}/mnt"
94   umount -f "${build}/usr/obj"
95   umount -f "${build}/usr/src"
96   umount -f "${build}/dev"
97   mount | grep -q "${build}" && wtf "Stuff is still mounted under ${build}; not removing"
98   chflags -R noschg "${build}"
99   rm -Rf "${build}"
100   trap "" exit hup int term kill
101 }
102
103 if [ -d "${build}" ]
104 then
105   omg "${build}: directory exists; purging"
106   cleanup
107 fi
108
109 prepare
110
111 meh "Seed: ${seed}"
112 meh "Config: ${TARGET}/${CONFIG}"
113 meh "Builddir: ${build}"
114 meh "make ${MAKEOPTS}"
115 meh "DESTDIR=${world}/root"
116
117 # Construct environment for chroot build
118 env="
119 USER=root
120 HOME=/root
121 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
122 SHELL=/bin/sh
123 "
124
125 for phase in ${phases}
126 do
127   meh "==> Phase: ${phase}"
128   script "${world}/${date}-${phase}.log" env -i ${env} chroot "${build}" sh -c \
129     "cd /usr/src; time make ${MAKEOPTS} ${phase} TARGET=${TARGET} KERNCONF=${CONFIG} DESTDIR=/mnt" || wtf "chroot-cmd ${phase}"
130 done
131
132 # Copy the config files into the target, to keep a record of the build options
133 [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "${build}/boot/kernel/"
134 [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
135 [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"