]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makeworld
script/loadconf: stub script to import Mosi config from a config partition
[CDN/Mosi.git] / script / makeworld
1 #!/bin/sh
2
3 # Building this config:
4 makeopts=-j8
5 target=amd64
6 config=SS4200
7
8 # Useful defines
9 meh() { printf " \033[1;32m*\033[0m %s\n" "${*}"; }
10 omg() { printf " \033[1;33m*\033[0m %s\n" "${*}"; }
11 wtf() { printf " \033[1;31m* FAIL\033[0m %s\n" "${*}"; exit 1; }
12
13 # Root directory of worlds
14 ROOT="$(realpath "$(dirname "${0}")/..")"
15
16 # Target directory
17 tgtdir="${target}/${config}"
18
19 # Check if it exists
20 [ -d "${ROOT}/${tgtdir}" ] || wtf "${tgtdir} doesn't exist"
21
22 # Construct environment
23 env="
24 USER=root
25 HOME=/root
26 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
27 SHELL=/bin/sh
28
29 ROOT=${ROOT}
30 MAKEOPTS=${makeopts}
31 TARGET=${target}
32 CONFIG=${config}
33 "
34
35 # Invoke sanitized environment
36 sudo env -i ${env} sh <<"EOF"
37 # Inherited by env
38 #ROOT=/usr/home/cyberleo/worlds
39 #MAKEOPTS=-j8
40 #TARGET=amd64
41 #CONFIG=GENERIC
42
43 world="${ROOT}/${TARGET}/${CONFIG}"
44
45 seed="${ROOT}/seed/src"
46 build="${ROOT}/seed/root"
47
48 date="$(date +%Y%m%d)"
49
50 # Useful methods
51 meh() { printf " \033[1;32m*\033[0m %s\n" "${*}"; }
52 omg() { printf " \033[1;33m*\033[0m %s\n" "${*}"; }
53 wtf() { printf " \033[1;31m* FAIL\033[0m %s\n" "${*}"; exit 1; }
54 prepare() {
55   mount | grep -q "${build}" && wtf "Stuff is mounted under ${build}; cannot continue"
56
57   # Cleanup trap here, so that an abort during prepare can clean up properly
58   trap "cleanup" exit hup int term kill
59
60   meh "Preparing build environment"
61   [ -d "${build}" ] && wtf "${build}: directory exists"
62   mkdir -p "${build}" || wtf
63   cat "${seed}"/base.?? | tar xCf "${build}" - || wtf
64   mkdir -p "${build}/usr/obj" || wtf
65
66   meh "Mounting chroot directories"
67   mkdir -p "${world}/obj"
68   mkdir -p "${world}/root"
69   mount -t devfs devfs "${build}/dev" || wtf
70   # Mount /usr/src as a union, so that changes to it will not affect the underlying tree
71   # unionfs suffers from deadlocks; don't use it
72   mount -t nullfs -r /usr/src "${build}/usr/src" || wtf
73   mount -t nullfs "${world}/obj" "${build}/usr/obj" || wtf
74   mount -t nullfs "${world}/root" "${build}/mnt" || wtf
75
76   if [ -d "${world}/config" ]
77   then
78     meh "Installing build-time configuration"
79     [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "/usr/src/sys/${TARGET}/conf/"
80     [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
81     [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"
82   fi
83 }
84
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}"
110
111 for phase in buildworld buildkernel distrib-dirs installworld installkernel distribution
112 do
113   meh "==> Phase: ${phase}"
114   script "${world}/${date}-${phase}.log" chroot "${build}" sh -c \
115     "cd /usr/src; time make ${MAKEOPTS} ${phase} TARGET=${TARGET} KERNCONF=${CONFIG} DESTDIR=/mnt" || wtf "chroot-cmd ${phase}"
116 done
117
118 # Copy the config files into the target, to keep a record of the build options
119 [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "${build}/boot/kernel/"
120 [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
121 [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"
122 EOF