]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makeworld
script/makeworld: use shlib for common functions
[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 # Going to build this config:
31 TARGET="${TARGET:-i386}"
32 CONFIG="${CONFIG:-GENERIC}"
33 MAKEOPTS="${MAKEOPTS:-"-j4"}"
34
35 # Check if it exists
36 [ -d "${ROOT}/${TARGET}/${CONFIG}" ] || wtf "${TARGET}/${CONFIG} doesn't exist"
37
38 # Target world directory
39 world="${ROOT}/${TARGET}/${CONFIG}"
40 # Source chroot seed directory
41 seed="${ROOT}/seed/base/$(uname -m)"
42 # Root directory for chroot
43 build="${ROOT}/seed/chroot-${TARGET}-${CONFIG}"
44
45 # Default build phases
46 phases="${phases:-buildworld buildkernel distrib-dirs installworld installkernel distribution}"
47
48 date="$(date +%Y%m%d)"
49
50 # Functions
51
52 # Prepare chroot for build
53 prepare() {
54   mount | grep -q "${build}" && wtf "Stuff is mounted under ${build}; cannot continue"
55
56   # Cleanup trap here, so that an abort during prepare can clean up properly
57   trap "cleanup" exit hup int term kill
58
59   meh "Preparing build chroot"
60   [ -d "${build}" ] && wtf "${build}: directory exists"
61   mkdir -p "${build}" || wtf
62   cat "${seed}"/base.?? | tar xCf "${build}" - || wtf
63   mkdir -p "${build}/usr/obj" || wtf
64
65   meh "Mounting chroot filesystems"
66   mkdir -p "${world}/obj"
67   mkdir -p "${world}/root"
68   mount -t devfs devfs "${build}/dev" || wtf
69   # Mount /usr/src as a union, so that changes to it will not affect the underlying tree
70   # unionfs suffers from deadlocks; don't use it
71   mount -t nullfs -r /usr/src "${build}/usr/src" || wtf
72   mount -t nullfs "${world}/obj" "${build}/usr/obj" || wtf
73   mount -t nullfs "${world}/root" "${build}/mnt" || wtf
74
75   if [ -d "${world}/config" ]
76   then
77     meh "Installing build-time configuration"
78     [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "/usr/src/sys/${TARGET}/conf/"
79     [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
80     [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"
81   fi
82 }
83
84 # Cleanup chroot
85 cleanup() {
86   meh "Cleaning up"
87   umount -f "${build}/mnt"
88   umount -f "${build}/usr/obj"
89   umount -f "${build}/usr/src"
90   umount -f "${build}/dev"
91   mount | grep -q "${build}" && wtf "Stuff is still mounted under ${build}; not removing"
92   chflags -R noschg "${build}"
93   rm -Rf "${build}"
94   trap "" exit hup int term kill
95 }
96
97 if [ -d "${build}" ]
98 then
99   omg "${build}: directory exists; purging"
100   cleanup
101 fi
102
103 prepare
104
105 meh "Seed: ${seed}"
106 meh "Config: ${TARGET}/${CONFIG}"
107 meh "Builddir: ${build}"
108 meh "make ${MAKEOPTS}"
109 meh "DESTDIR=${world}/root"
110
111 # Construct environment for chroot build
112 env="
113 USER=root
114 HOME=/root
115 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
116 SHELL=/bin/sh
117 "
118
119 for phase in ${phases}
120 do
121   meh "==> Phase: ${phase}"
122   script "${world}/${date}-${phase}.log" env -i ${env} chroot "${build}" sh -c \
123     "cd /usr/src; time make ${MAKEOPTS} ${phase} TARGET=${TARGET} KERNCONF=${CONFIG} DESTDIR=/mnt" || wtf "chroot-cmd ${phase}"
124 done
125
126 # Copy the config files into the target, to keep a record of the build options
127 [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "${build}/boot/kernel/"
128 [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
129 [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"