4 _root="$(dirname "${0}")"
5 . "${_root}/lib/env.sh"
10 targets="prepwork admin overlay packages patch whiteout prepboot preptmp prepetc imgboot imgconf imgetc imgall custom"
13 [ "${*}" ] && printf "${*}\n\n"
14 echo "********************************************************************************"
15 echo "Usage: $(basename "${0}") <-a arch> <-c conf> <-b basedir> <-t stagedir>"
16 echo ' <-i pkgsdir> <-p patchdir> <-r rootdir> <-l logfile> [-h] <targets>'
17 echo " -a arch Arch for image target (default: current arch: $(uname -m))"
18 echo ' -c conf Conf name for image target (default: GENERIC)'
19 echo ' -b basedir Basedir for automagic defaults'
20 echo ' -t stagedir Staging directory name (${target}/tree)'
21 echo ' -r rootdir Directory holding the virgin source tree (${target}/world/root)'
22 echo ' -o overlaydir Directory holding an overlay tree (${target}/config/overlay)'
23 echo ' -i pkgsdir Directory holding packages to install (${target}/pkg)'
24 echo ' -p patchdir Directory holding patches to apply (${target}/config/patch)'
25 echo ' -w whiteout Listfile of paths to remove (${target}/config/whiteout.lst)'
26 echo ' -d confdir Conf md_size files and old cpios (${target}/config/conf)'
27 echo ' -l logfile File to hold stderr spam (${stage}/gentree.log'
28 echo ' -h Hello! >^-^<'
30 echo 'Available targets:'
31 for target in ${targets}
38 while getopts "a:c:b:t:r:o:i:p:w:l:h" opt
41 a) arch="${OPTARG}" ;;
42 c) conf="${OPTARG}" ;;
43 b) base="${OPTARG}" ;;
44 t) tree="${OPTARG}" ;;
45 r) root="${OPTARG}" ;;
46 o) ovly="${OPTARG}" ;;
47 i) pkgs="${OPTARG}" ;;
48 p) ptch="${OPTARG}" ;;
49 w) rmrf="${OPTARG}" ;;
50 d) cnfd="${OPTARG}" ;;
51 l) logf="${OPTARG}" ;;
53 [?]) pebkac "Unrecognized option ${opt}" ;;
56 shift $(( $OPTIND - 1 ))
58 sequence="${*:-${targets}}"
60 base="${base:-$(dirname "${0}")/..}" #"
61 base="$(realpath "${base}")"
63 arch="${arch:-$(uname -m)}"
64 conf="${conf:-GENERIC}"
66 target="${base}/targets/${arch}/${conf}"
68 tree="${tree:-${target}/tree}"
69 root="${root:-${target}/world/root}"
70 ovly="${ovly:-${target}/config/overlay}"
71 pkgs="${pkgs:-${target}/pkg}"
72 ptch="${ptch:-${target}/config/patch}"
73 rmrf="${rmrf:-${target}/config/whiteout.lst}"
74 cnfd="${cnfd:-${target}/config/conf}"
75 logf="${logf:-${tree}/gentree.log}"
77 stage="${stage:-${base}/tree}"
78 pkgs="${pkgs:-${base}/pkg}"
79 patch="${patch:-${base}/patch}"
80 overlay="${overlay:-${base}/overlay}"
81 root="${root:-${base}/root}"
82 logfile="${logfile:-${stage}/gentree.log}"
85 tree="$(realpath "${tree}")"
92 meh "Logging to ${logf}"
96 # Make sure the provided file(s) have only one link!
99 if [ -f "${1}" -a "$(stat -f '%l' "${1}")" -gt 1 ]
101 cp -p "${1}" "${1}.tmp" && mv "${1}.tmp" "${1}" || err "breaklink failed"
109 log Prepare workspace
110 if [ -d "${tree}" -a \( -d "${sroot}" -o -d "${sboot}" -o -d "${sconf}" \) ]
112 yn n " ${a_yellow}*${a_normal} Workspace already exists. Delete? [y/N]" || err Aborting
113 chk rm -Rf "${sroot}" "${sboot}" "${sconf}"
115 chk mkdir -p "${sroot}"
116 # Eliminate schg, because it interferes with hardlinks
117 chk chflags -R noschg "${root}/lib"
118 chk chflags -R noschg "${root}/libexec"
119 chk chflags -R noschg "${root}/sbin"
120 chk chflags -R noschg "${root}/usr"
121 ( cd "${root}" && find . | cpio -p --link "${sroot}" ) || chk
125 log Create an emergency user admin/admin
126 # delink passwd to ensure it doesn't get patched in-place
127 chk onelink "${sroot}/etc/passwd" "${sroot}/etc/master.passwd"
128 echo '$1$2rXOWsK/$eiBHA6K7xL96DZbcY24YR0' | chk chroot "${sroot}" /usr/sbin/pw useradd admin -u 999 -g wheel -G operator -c Administrator -d /usr/home/admin -m -s /bin/csh -H 0
132 [ -d "${ovly}" ] || return
133 log Apply overlay from "${ovly##${base}/}"
134 ( cd "${ovly}" && find . | cpio -p "${sroot}" ) || chk
138 [ -d "${pkgs}" ] || return
139 count="$(ls -1 "${pkgs}" | wc -l)"
140 [ "${count}" -gt 0 ] || return
141 log Install ${count} packages from "${pkgs##${base}/}"
142 chk mkdir -p "${sroot}/pkg"
143 ( cd "${pkgs}" && find . | cpio -p --link "${sroot}/pkg" ) || chk
144 chk chroot "${sroot}" /bin/sh -c 'cd /pkg; exec pkg_add -F *'
145 chk rm -Rf "${sroot}/pkg"
149 [ -d "${ptch}" ] || return
150 log Apply patches from "${ptch##${base}/}"
151 for file in "${ptch}"/*
153 note "... $(basename "${file}")"
154 ( cd "${sroot}" && patch < "${file}" ) || chk
156 sed -e '/^+++ /!d; s/^+++ //; s/[ ]*[0-9: .+-]*$//' "${file}" | while read orig
158 rm -f "${sroot}/${orig}.orig"
164 [ -f "${rmrf}" ] || return
165 log Whiteout files from "${rmrf##${base}/}"
166 while read entry < "${rmrf}"
168 # Strip off terminating slash
170 # Obtain directory name, for path resolution
171 entry_path="$(dirname "${entry}")"
172 entry_path="$(realpath "${sroot}/${entry_path}" 2>/dev/null)"
173 entry_file="$(basename "${entry}")"
175 # Ignore paths that do not exist
176 [ "${entry_path}" ] || continue
178 # Warn and ignore paths that fall outside ${sroot} after resolution
179 if [ "$(echo ${entry_path} | sed -e "s/^\(.\{${#sroot}\}\).*$/\1/")" != "${sroot}" ]
181 warn "Whiteout '${entry}' malformed: leads outside '${sroot}'"
185 # Warn and ignore paths that cannot be chdir()'d
186 if [ ! -d "${entry_path}" ]
188 warn "Whiteout '${entry}' malformed: non-directory in path"
195 echo rm -Rf "${entry}"
197 echo rm -f "${entry}"
205 chk mv "${sroot}/boot/boot" "${sroot}/boot/boot.blk"
206 chk ln -sf . "${sroot}/boot/boot"
208 # Move /boot/zfs to /etc/zfs and symlink
209 ls -1 "${sroot}/boot/zfs"/* 2>&- && chk mv "${sroot}/boot/zfs"/* "${sroot}/etc/zfs/"
210 chk rmdir "${sroot}/boot/zfs"
211 chk ln -svf ../etc/zfs "${sroot}/boot/zfs"
216 ( cd "${sroot}/tmp" && find . | cpio -p --link ../var/tmp ) || chk
217 chk rm -Rf "${sroot}/tmp"
218 chk ln -sf var/tmp "${sroot}/tmp"
223 chk mkdir -p "${sroot}/etc/local"
224 chk mkdir -p "${sroot}/usr/local/etc" # Silence warnings
225 ( cd "${sroot}/usr/local/etc" && find . | cpio -p --link ../../../etc/local ) || chk
226 chk rm -Rf "${sroot}/usr/local/etc"
227 chk ln -sf ../../etc/local "${sroot}/usr/local/etc"
231 log Create boot imgsrc
232 chk mv "${sroot}/boot" "${tree}"
233 chk mkdir -p "${sroot}/boot"
234 chk mkdir -p "${sroot}/modules"
235 chk find "${sboot}/kernel" -type f -name '*.symbols' -delete
237 # Link all modules into /modules, as an alternative search path
238 ( cd "${sboot}/kernel" && find . -name '*.ko' -o -name "${conf}" | cpio -p --link "${sroot}/modules" ) || chk
239 ( cd "${sboot}/modules" && find . -name '*.ko' -o -name "${conf}" | cpio -p --link "${sroot}/modules" ) || chk
241 # Remove all modules from the root fs that are preloaded by the preloader
242 cat "${sboot}/loader.conf" | grep '_load=' | sed -e 's#^\(.*\)_load=.*$#'"${sroot}/modules/"'\1.ko#' | xargs rm -f
244 # Remove all modules from the boot fs that are present in the root fs
245 ( cd "${sroot}/modules" && ls -1 ) | sed -e 's#^#'"${sboot}/kernel/"'#' | xargs rm -f
246 ( cd "${sroot}/modules" && ls -1 ) | sed -e 's#^#'"${sboot}/modules/"'#' | xargs rm -f
248 # Rebuild linker hints in both places, to make sure the kernel can find everything once running
249 kldxref "${sboot}/kernel" "${sboot}/modules" "${sroot}/modules"
251 # Gzip kernel to save boot space
252 chk onelink "${sboot}/kernel/kernel"
253 chk gzip -9 "${sboot}/kernel/kernel"
255 # Compress all the modules as well, since /loader can handle them; kldload cannot,
256 # but I think I can safely assume you will not be kldunloading boot-loaded modules
257 ls -1 "${sboot}/kernel/"*.ko "${sboot}/modules/"*.ko 2>/dev/null | while read kmod
259 chk onelink "${kmod}"
260 chk gzip -9 "${kmod}"
263 # Make sure /boot/zfs -> /etc/zfs symlink exists even without /boot mounted
264 [ -L "${sroot}/boot/zfs" ] || chk ln -sf ../etc/zfs "${sroot}/boot/zfs"
269 chk mkdir -p "${sroot}/conf"
270 echo "ufs:/dev/ufs/conf" > "${sroot}/conf/diskless_remount" || chk
271 chk mkdir -p "${sconf}/backup"
272 chk mkdir -p "${sconf}/base"
273 chk mkdir -p "${sconf}/default"
275 # Create packdirs for each
276 for pack in "${cnfd}"/*.md_size
278 pack="$(basename "${pack%%.md_size}")"
279 chk mkdir -p "${sconf}/base/${pack}"
280 chk mkdir -p "${sconf}/default/${pack}"
281 ( cat "${cnfd}/${pack}.md_size" > "${sconf}/base/${pack}/md_size" ) || chk
283 chk chown -R :operator "${sconf}"
284 ( find "${sconf}" -print0 | xargs -0 chmod o-rwx ) || chk
288 # etc requires special handling to ensure everything is properly arranged.
289 log Create etc confpack
290 chk touch "${sroot}/etc/diskless"
291 # Touch /COPYRIGHT to provide an immutable time anchor for saveconfig
292 chk touch "${sroot}/COPYRIGHT"
293 chk sleep 1 # Make sure anchor is at least one second older than everything in custom
294 chk mkdir -p "${tree}/pack"
295 chk mv "${sroot}/etc" "${tree}/pack"
296 chk mkdir -p "${sroot}/etc"
297 chk cp -p "${tree}/pack/etc/rc" "${tree}/pack/etc/rc.subr" "${tree}/pack/etc/rc.initdiskless" "${tree}/pack/etc/login.conf.db" "${tree}/pack/etc/diskless" "${sroot}/etc"
299 # Make sure etc confpack exists; default if necessary
300 chk mkdir -p "${sconf}/base/etc"
301 chk mkdir -p "${sconf}/default/etc"
302 [ -e "${sconf}/base/etc/md_size" ] || echo "10240" > "${sconf}/base/etc/md_size"
304 ( cd "${tree}/pack" && find etc | cpio -o ) | gzip -9 > "${sconf}/base/etc.cpio.gz" || chk
305 chk rm -Rf "${tree}/pack"
309 log Create remaining confpacks
310 chk mkdir -p "${tree}/pack"
311 for pack in "${cnfd}"/*.md_size
313 pack="$(basename "${pack%%.md_size}")"
314 # Ignore etc, as it was processed in do_imgetc
315 [ "${pack}" = "etc" ] && continue
317 chk mv "${sroot}/${pack}" "${tree}/pack"
318 chk mkdir -p "${sroot}/${pack}"
319 ( cd "${tree}/pack" && find "${pack}" | cpio -o ) | gzip -9 > "${sconf}/base/${pack}.cpio.gz" || chk
320 chk rm -Rf "${tree}/pack/${pack}"
322 chk rm -Rf "${tree}/pack"
326 log Patch in custom config
327 ( cd "${cnfd}" && find . -name '*.md_size' -o -print | cpio -p --link "${sconf}/default" ) || chk
328 # Make sure files in default are newer than the tagfile, so they will be caught by saveconfig
329 find "${sconf}/default" -type f -print0 | xargs -0 touch
330 # If there are scavenged cpio.gz confpacks, touch their contents as well.
331 chk mkdir -p "${tree}/pack"
332 if ls -1 "${sconf}/default" | grep -q '.cpio.gz'
334 chk rm -Rf "${tree}/pack"/*
335 for file in "${sconf}/default"/*.cpio.gz
337 pack="$(basename "${file%%.cpio.gz}")"
338 zcat "${file}" | ( cd "${tree}/pack"; cpio -id ) || chk
339 find "${tree}/pack" -type f -print0 | xargs -0 touch
340 ( cd "${tree}/pack"; find . | cpio -o ) | gzip -9 > "${file}"
341 chk rm -Rf "${tree}/pack"/*
344 chk rm -Rf "${tree}/pack"
347 for step in ${sequence}
349 echo "${targets}" | grep -q "${step}" || err Unrecognized target "${step}"