]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makeworld
script/makeworld: refactor to support command line options; clean up
[CDN/Mosi.git] / script / makeworld
1 #!/bin/sh
2
3 # Makeworld dir structure should contain, at minimum:
4 # script
5 #  makeworld (this file)
6 # worlds
7 #  seed
8 #   base
9 #    <ARCH> (arch of build host: i386, amd64, etc)
10 #     base.* (virgin base tree of same arch as host, used to seed chroot for clean build)
11 #  <TARGET> (i386, amd64, etc)
12 #   <CONFIG> (GENERIC, SABA, SS4200, etc)
13 #    config
14 #     make.conf
15 #     src.conf
16 #     CONFIG (Matches config name: GENERIC, SABA, SS4200, etc)
17
18 # Load shlib and modules
19 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
20 want root log
21
22 pebkac() {
23   [ "${*}" ] && printf "%s\n\n" "${*}"
24   echo "Usage: $(basename "${0}") -m <makeopts> -t <target> -c <config> <target, ...>"
25   echo "  -m <makeopts>   Provide additional flags to make"
26   echo "  -t <target>     Target architecture (i386, amd64)"
27   echo "  -c <config>     Target configuration (in worlds/<target>/<config>)"
28   echo "  -h              Help!"
29   echo ""
30   echo "Available make targets:"
31   for target in ${make_tgts}
32   do
33     echo "  ${target}"
34   done
35   exit 1
36 }
37
38 # Prepare chroot for build
39 prepare() {
40   # Verify environment sanity
41   [ -d "${build}" ] && omg "${build}: directory exists; purging" && cleanup
42   mount | grep -q "${build}" && wtf "Stuff is mounted under ${build}; cannot continue"
43   ls -1 "${seed}"/base.?? >/dev/null 2>&1 || wtf "Populate seed directory ${seed} first"
44   [ -f /usr/src/sys/conf/newvers.sh ] || wtf "Need sources in /usr/src to build"
45
46   # Cleanup trap here, so that an abort during prepare can clean up properly
47   trap "cleanup" exit hup int term kill
48
49   meh "Preparing build chroot"
50   [ -d "${build}" ] && wtf "${build}: directory exists"
51   mkdir -p "${build}" || wtf
52   cat "${seed}"/base.?? | tar xCf "${build}" - || wtf
53   mkdir -p "${build}/usr/obj" || wtf
54
55   meh "Mounting chroot filesystems"
56   mkdir -p "${world}/obj"
57   mkdir -p "${world}/root"
58   mount -t devfs devfs "${build}/dev" || wtf
59   # Mount /usr/src as a union, so that changes to it will not affect the underlying tree
60   # unionfs suffers from deadlocks; don't use it
61   mount -t nullfs -r /usr/src "${build}/usr/src" || wtf
62   mount -t nullfs "${world}/obj" "${build}/usr/obj" || wtf
63   mount -t nullfs "${world}/root" "${build}/mnt" || wtf
64
65   if [ -d "${world}/config" ]
66   then
67     meh "Installing build-time configuration"
68     [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "/usr/src/sys/${TARGET}/conf/"
69     [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
70     [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"
71   fi
72 }
73
74 # Cleanup chroot
75 cleanup() {
76   meh "Cleaning up"
77   umount -f "${build}/mnt"
78   umount -f "${build}/usr/obj"
79   umount -f "${build}/usr/src"
80   umount -f "${build}/dev"
81   mount | grep -q "${build}" && wtf "Stuff is still mounted under ${build}; not removing"
82   chflags -R noschg "${build}"
83   rm -Rf "${build}"
84   trap "" exit hup int term kill
85 }
86
87 # Root directory of makeworld
88 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
89
90 # Compute make -j<cpus*2>
91 make_cpus="$(sysctl -n hw.ncpu)"
92 make_jobs="$(( ${make_cpus} * 2 ))"
93 make_tgts="buildworld buildkernel distrib-dirs installworld installkernel distribution"
94
95 # Defaults
96 TARGET="i386"
97 CONFIG="GENERIC"
98 MAKEOPTS="-j${make_jobs}"
99
100 while getopts "m:t:c:h" opt
101 do
102   case "${opt}" in
103     m) MAKEOPTS="${MAKEOPTS} ${OPTARG}" ;;
104     t) TARGET="${OPTARG}" ;;
105     c) CONFIG="${OPTARG}" ;;
106     h) pebkac ;;
107     [?]) pebkac "Unrecognized option ${opt}" ;;
108   esac
109 done
110 shift $(( $OPTIND - 1 ))
111
112 # Build make target sequence
113 sequence="${*:-${make_tgts}}"
114
115 # Target world directory
116 world="${ROOT}/${TARGET}/${CONFIG}"
117 # Source chroot seed directory
118 seed="${ROOT}/seed/base/$(uname -m)"
119 # Root directory for chroot
120 build="${ROOT}/seed/chroot-${TARGET}-${CONFIG}"
121
122 # Environment for chroot build
123 env="
124 USER=root
125 HOME=/root
126 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
127 SHELL=/bin/sh
128 "
129
130 date="$(date +%Y%m%d)"
131
132 # Check if target config exists
133 [ -d "${world}" ] || wtf "${TARGET}/${CONFIG} doesn't exist"
134
135 # Purge any build directory that might be left over
136 if [ -d "${build}" ]
137 then
138   omg "${build}: directory exists; purging"
139   cleanup
140 fi
141
142 meh "Making world for ${TARGET}/${CONFIG}"
143
144 prepare
145
146 meh "Seed: ${seed}"
147 meh "Config: ${TARGET}/${CONFIG}"
148 meh "Builddir: ${build}"
149 meh "make ${MAKEOPTS}"
150 meh "DESTDIR=${world}/root"
151
152 for step in ${sequence}
153 do
154   meh "==> Phase: ${step}"
155   script "${world}/${date}-${step}.log" env -i ${env} chroot "${build}" sh -c \
156     "cd /usr/src; time make ${MAKEOPTS} ${step} TARGET=${TARGET} KERNCONF=${CONFIG} DESTDIR=/mnt" || wtf "chroot-cmd ${phase}"
157 done
158
159 # Copy the config files into the target, to keep a record of the build options
160 [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "${build}/boot/kernel/"
161 [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
162 [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"