]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makeworld
script/makeworld: fix comment and ROOT path resolution
[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}/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 /usr/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() {
85   meh "Cleaning up"
86   umount -f "${build}/mnt"
87   umount -f "${build}/usr/obj"
88   umount -f "${build}/usr/src"
89   umount -f "${build}/dev"
90   mount | grep -q "${build}" && wtf "Stuff is still mounted under ${build}; not removing"
91   chflags -R noschg "${build}"
92   rm -Rf "${build}"
93 }
94
95 if [ -d "${build}" ]
96 then
97   omg "${build}: directory exists; purging"
98   cleanup
99 fi
100
101 prepare
102
103 meh "Seed: ${seed}"
104 meh "Config: ${TARGET}/${CONFIG}"
105 meh "Builddir: ${build}"
106 meh "make ${MAKEOPTS}"
107 meh "DESTDIR=${world}"
108
109 for phase in buildworld buildkernel distrib-dirs installworld installkernel distribution
110 do
111   meh "==> Phase: ${phase}"
112   script "${world}/${date}-${phase}.log" chroot "${build}" sh -c \
113     "cd /usr/src; time make ${MAKEOPTS} ${phase} TARGET=${TARGET} KERNCONF=${CONFIG} DESTDIR=/mnt" || wtf
114 done
115
116 # Copy the config files into the target, to keep a record of the build options
117 [ -f "${world}/config/${CONFIG}" ] && cp "${world}/config/${CONFIG}" "${build}/boot/kernel/"
118 [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
119 [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"
120 EOF