3 # Copyright (c) 2005 Poul-Henning Kamp.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #######################################################################
34 # Setup default values for all controlling variables.
35 # These values can be overridden from the config file(s)
37 #######################################################################
39 # Name of this NanoBSD build. (Used to construct workdir names)
42 # Source tree directory
45 # Where nanobsd additional files live under the source tree
46 NANO_TOOLS=tools/tools/nanobsd
48 # Where cust_pkg() finds packages to install
49 NANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
52 # where package metadata gets placed
53 NANO_PKG_META_BASE=/var/db
55 # Object tree directory
56 # default is subdir of /usr/obj
59 # The directory to put the final images
60 # default is ${NANO_OBJ}
63 # Make & parallel Make
65 NANO_PMAKE="make -j 3"
67 # The default name for any image we create.
68 NANO_IMGNAME="_.disk.full"
70 # Options to put in make.conf during buildworld only
73 # Options to put in make.conf during installworld only
76 # Options to put in make.conf during both build- & installworld.
79 # Kernel config file to use
82 # Kernel modules to install. If empty, no modules are installed.
83 # Use "default" to install all built modules.
89 # Late customize commands.
90 NANO_LATE_CUSTOMIZE=""
92 # Newfs paramters to use
93 NANO_NEWFS="-b 4096 -f 512 -i 8192 -U"
95 # The drive name of the media at runtime
98 # Target media size in 512 bytes sectors
99 NANO_MEDIASIZE=2000000
101 # Number of code images on media (1 or 2)
104 # 0 -> Leave second image all zeroes so it compresses better.
105 # 1 -> Initialize second image with a copy of the first
108 # Size of code file system in 512 bytes sectors
109 # If zero, size will be as large as possible.
112 # Size of configuration file system in 512 bytes sectors
116 # Size of data file system in 512 bytes sectors
117 # If zero: no partition configured.
118 # If negative: max size possible
121 # Size of the /etc ramdisk in 512 bytes sectors
122 NANO_RAM_ETCSIZE=10240
124 # Size of the /tmp+/var ramdisk in 512 bytes sectors
125 NANO_RAM_TMPVARSIZE=10240
127 # Media geometry, only relevant if bios doesn't understand LBA.
131 # boot0 flags/options and configuration
132 NANO_BOOT0CFG="-o packet -s 1 -m 3"
133 NANO_BOOTLOADER="boot/boot0sio"
135 # boot2 flags/options
136 # default force serial console
139 # Backing type of md(4) device
140 # Can be "file" or "swap"
141 NANO_MD_BACKING="file"
143 # for swap type md(4) backing, write out the mbr only
144 NANO_IMAGE_MBRONLY=true
146 # Progress Print level
149 # Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label
150 # in preference to /dev/${NANO_DRIVE}
151 # Root partition will be ${NANO_LABEL}s{1,2}
152 # /cfg partition will be ${NANO_LABEL}s3
153 # /data partition will be ${NANO_LABEL}s4
156 #######################################################################
157 # Architecture to build. Corresponds to TARGET_ARCH in a buildworld.
158 # Unfortunately, there's no way to set TARGET at this time, and it
159 # conflates the two, so architectures where TARGET != TARGET_ARCH do
160 # not work. This defaults to the arch of the current machine.
164 # Directory to populate /cfg from
167 # Directory to populate /data from
170 # src.conf to use when building the image. Defaults to /dev/null for the sake
172 SRCCONF=${SRCCONF:=/dev/null}
174 #######################################################################
176 # The functions which do the real work.
177 # Can be overridden from the config file(s)
179 #######################################################################
181 # run in the world chroot, errors fatal
184 chroot ${NANO_WORLDDIR} /bin/sh -exc "$*"
187 # run in the world chroot, errors not fatal
190 chroot ${NANO_WORLDDIR} /bin/sh -c "$*" || true
194 if [ $? -ne 0 ]; then
195 echo "Error encountered. Check for errors in last log file." 1>&2
201 pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
203 if ! rm -xrf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
204 chflags -R noschg ${MAKEOBJDIRPREFIX}/
205 rm -xr ${MAKEOBJDIRPREFIX}/
207 mkdir -p ${MAKEOBJDIRPREFIX}
208 printenv > ${MAKEOBJDIRPREFIX}/_.env
211 make_conf_build ( ) (
212 pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
214 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
215 echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
219 pprint 2 "run buildworld"
220 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
223 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
225 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
226 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
232 pprint 2 "build kernel ($NANO_KERNEL)"
233 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
236 if [ -f ${NANO_KERNEL} ] ; then
237 kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
238 kernconf=$(basename ${NANO_KERNEL})
240 kernconf=${NANO_KERNEL}
244 # unset these just in case to avoid compiler complaints
245 # when cross-building
247 # Note: We intentionally build all modules, not only the ones in
248 # NANO_MODULES so the built world can be reused by multiple images.
249 eval "TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
250 SRCCONF='${SRCCONF}' \
251 __MAKE_CONF='${NANO_MAKE_CONF_BUILD}' \
252 ${kernconfdir_arg} KERNCONF=${kernconf}"
253 ) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
257 if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
258 pprint 2 "Clean and create object directory (${NANO_OBJ})"
259 if ! rm -rxf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
260 chflags -R noschg ${NANO_OBJ}
263 mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
264 printenv > ${NANO_OBJ}/_.env
266 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
267 if ! rm -rxf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
268 chflags -R noschg ${NANO_WORLDDIR}
269 rm -rxf ${NANO_WORLDDIR}/
271 mkdir -p ${NANO_WORLDDIR}
275 make_conf_install ( ) (
276 pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
278 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
279 echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
283 pprint 2 "installworld"
284 pprint 3 "log: ${NANO_OBJ}/_.iw"
287 env TARGET_ARCH=${NANO_ARCH} \
288 ${NANO_MAKE} SRCCONF=${SRCCONF} \
289 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
290 DESTDIR=${NANO_WORLDDIR} \
291 > ${NANO_OBJ}/_.iw 2>&1
292 chflags -R noschg ${NANO_WORLDDIR}
297 pprint 2 "install /etc"
298 pprint 3 "log: ${NANO_OBJ}/_.etc"
301 env TARGET_ARCH=${NANO_ARCH} \
302 ${NANO_MAKE} SRCCONF=${SRCCONF} \
303 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
304 DESTDIR=${NANO_WORLDDIR} \
305 > ${NANO_OBJ}/_.etc 2>&1
306 # make.conf doesn't get created by default, but some ports need it
307 # so they can spam it.
308 cp /dev/null ${NANO_WORLDDIR}/etc/make.conf
314 pprint 2 "install kernel ($NANO_KERNEL)"
315 pprint 3 "log: ${NANO_OBJ}/_.ik"
318 if [ -f ${NANO_KERNEL} ] ; then
319 kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
320 kernconf=$(basename ${NANO_KERNEL})
322 kernconf=${NANO_KERNEL}
325 # Install all built modules if NANO_MODULES=default,
326 # else install only listed modules (none if NANO_MODULES is empty).
327 if [ "${NANO_MODULES}" != "default" ]; then
328 modules_override_arg="MODULES_OVERRIDE='${NANO_MODULES}'"
332 eval "TARGET_ARCH=${NANO_ARCH} ${NANO_MAKE} installkernel \
333 DESTDIR='${NANO_WORLDDIR}' \
334 SRCCONF='${SRCCONF}' \
335 __MAKE_CONF='${NANO_MAKE_CONF_INSTALL}' \
336 ${kernconfdir_arg} KERNCONF=${kernconf} \
337 ${modules_override_arg}"
338 ) > ${NANO_OBJ}/_.ik 2>&1
343 pprint 2 "run customize scripts"
344 for c in $NANO_CUSTOMIZE
346 pprint 2 "customize \"$c\""
347 pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
349 ( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
353 run_late_customize() (
355 pprint 2 "run late customize scripts"
356 for c in $NANO_LATE_CUSTOMIZE
358 pprint 2 "late customize \"$c\""
359 pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
361 ( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
366 pprint 2 "configure nanobsd setup"
367 pprint 3 "log: ${NANO_OBJ}/_.dl"
372 # Move /usr/local/etc to /etc/local so that the /cfg stuff
373 # can stomp on it. Otherwise packages like ipsec-tools which
374 # have hardcoded paths under ${prefix}/etc are not tweakable.
375 if [ -d usr/local/etc ] ; then
379 find . -print | cpio -dumpl ../../../etc/local
382 ln -s ../../etc/local etc
388 # link /$d under /conf
389 # we use hard links so we have them both places.
390 # the files in /$d will be hidden by the mount.
391 # XXX: configure /$d ramdisk size
392 mkdir -p conf/base/$d conf/default/$d
393 find $d -print | cpio -dumpl conf/base/
396 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
397 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
399 # pick up config files from the special partition
400 echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
402 # Put /tmp on the /var ramdisk (could be symlink already)
403 test -d tmp && rmdir tmp || rm -f tmp
406 ) > ${NANO_OBJ}/_.dl 2>&1
409 setup_nanobsd_etc ( ) (
410 pprint 2 "configure nanobsd /etc"
415 # create diskless marker file
418 # Make root filesystem R/O by default
419 echo "root_rw_mount=NO" >> etc/defaults/rc.conf
421 # save config file for scripts
422 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
424 echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
425 echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
432 # Remove all empty directories in /usr
433 find ${NANO_WORLDDIR}/usr -type d -depth -print |
436 rmdir $d > /dev/null 2>&1 || true
445 echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
446 newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
447 mount -o async ${dev} ${mnt}
450 # Convenient spot to work around any umount issues that your build environment
451 # hits by overriding this method.
457 local dev dir mnt lbl
462 echo "Creating ${dev} (mounting on ${mnt})"
463 newfs_part ${dev} ${mnt} ${lbl}
464 if [ -n "${dir}" -a -d "${dir}" ]; then
465 echo "Populating ${lbl} from ${dir}"
467 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
473 populate_cfg_slice ( ) (
474 populate_slice "$1" "$2" "$3" "$4"
477 populate_data_slice ( ) (
478 populate_slice "$1" "$2" "$3" "$4"
481 create_i386_diskimage ( ) (
482 pprint 2 "build diskimage"
483 pprint 3 "log: ${NANO_OBJ}/_.di"
486 echo $NANO_MEDIASIZE $NANO_IMAGES \
487 $NANO_SECTS $NANO_HEADS \
488 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
493 # size of cylinder in sectors
496 # number of full cylinders on media
499 # output fdisk geometry spec, truncate cyls to 1023
501 print "g c" cyl " h" $4 " s" $3
503 print "g c" 1023 " h" $4 " s" $3
506 # size of data partition in full cylinders
507 dsl = int (($7 + cs - 1) / cs)
512 # size of config partition in full cylinders
513 csl = int (($6 + cs - 1) / cs)
516 # size of image partition(s) in full cylinders
517 isl = int ((cyl - dsl - csl) / $2)
519 isl = int (($5 + cs - 1) / cs)
522 # First image partition start at second track
523 print "p 1 165 " $3, isl * cs - $3
526 # Second image partition (if any) also starts offset one
527 # track to keep them identical.
529 print "p 2 165 " $3 + c, isl * cs - $3
533 # Config partition starts at cylinder boundary.
534 print "p 3 165 " c, csl * cs
537 # Data partition (if any) starts at cylinder boundary.
539 print "p 4 165 " c, dsl * cs
540 } else if ($7 < 0 && $1 > c) {
541 print "p 4 165 " c, $1 - c
543 print "Disk space overcommitted by", \
544 c - $1, "sectors" > "/dev/stderr"
548 # Force slice 1 to be marked active. This is necessary
549 # for booting the image from a USB device to work.
552 ' > ${NANO_OBJ}/_.fdisk
554 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
555 MNT=${NANO_OBJ}/_.mnt
558 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
559 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
562 echo "Creating md backing file..."
564 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
565 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
569 trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
571 fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
574 # XXX: pick up cached boot* files, they may not be in image anymore.
575 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
576 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
580 populate_slice /dev/${MD}s1a ${NANO_WORLDDIR} ${MNT} "s1a"
581 mount /dev/${MD}s1a ${MNT}
582 echo "Generating mtree..."
583 ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
584 ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
587 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
588 # Duplicate to second image (if present)
589 echo "Duplicating to second image..."
590 dd conv=sparse if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
591 mount /dev/${MD}s2a ${MNT}
592 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
594 sed -i "" "s=${NANO_DRIVE}s1=${NANO_DRIVE}s2=g" $f
597 # Override the label from the first partition so we
598 # don't confuse glabel with duplicates.
599 if [ ! -z ${NANO_LABEL} ]; then
600 tunefs -L ${NANO_LABEL}"s2a" /dev/${MD}s2a
604 # Create Config slice
605 populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT} "s3"
607 # Create Data slice, if any.
608 if [ $NANO_DATASIZE -ne 0 ] ; then
609 populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT} "s4"
612 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
613 if [ ${NANO_IMAGE_MBRONLY} ]; then
614 echo "Writing out _.disk.mbr..."
615 dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
617 echo "Writing out ${NANO_IMGNAME}..."
618 dd if=/dev/${MD} of=${IMG} bs=64k
621 echo "Writing out ${NANO_IMGNAME}..."
622 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
625 if ${do_copyout_partition} ; then
626 echo "Writing out _.disk.image..."
627 dd conv=sparse if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
632 trap nano_cleanup EXIT
634 ) > ${NANO_OBJ}/_.di 2>&1
637 # i386 and amd64 are identical for disk images
638 create_amd64_diskimage ( ) (
639 create_i386_diskimage
643 # Redefine this function with any last orders you may have
644 # after the build completed, for instance to copy the finished
645 # image to a more convenient place:
646 # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
650 #######################################################################
652 # Optional convenience functions.
654 #######################################################################
656 #######################################################################
657 # Common Flash device geometries
661 if [ -d ${NANO_TOOLS} ] ; then
662 . ${NANO_TOOLS}/FlashDevice.sub
664 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
666 sub_FlashDevice $1 $2
669 #######################################################################
670 # USB device geometries
673 # UsbDevice Generic 1000 # a generic flash key sold as having 1GB
675 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
677 # Note that the capacity of a flash key is usually advertised in MB or
678 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
679 # for C/H/S geometry may vary depending on the actual flash geometry.
681 # The following generic device layouts are understood:
682 # generic An alias for generic-hdd.
683 # generic-hdd 255H 63S/T xxxxC with no MBR restrictions.
684 # generic-fdd 64H 32S/T xxxxC with no MBR restrictions.
686 # The generic-hdd device is preferred for flash devices larger than 1GB.
690 a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
695 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
700 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
703 echo "Unknown USB flash device"
709 #######################################################################
710 # Setup serial console
713 # Enable getty on console
714 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
716 # Disable getty on syscons devices
717 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys
719 # Tell loader to use serial console early.
720 echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
723 #######################################################################
724 # Allow root login via ssh
726 cust_allow_ssh_root () (
727 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
728 ${NANO_WORLDDIR}/etc/ssh/sshd_config
731 #######################################################################
732 # Install the stuff under ./Files
734 cust_install_files () (
735 cd ${NANO_TOOLS}/Files
736 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
739 #######################################################################
740 # Install packages from ${NANO_PACKAGE_DIR}
744 # If the package directory doesn't exist, we're done.
745 if [ ! -d ${NANO_PACKAGE_DIR} ]; then
746 echo "DONE 0 packages"
750 # Copy packages into chroot
751 mkdir -p ${NANO_WORLDDIR}/Pkg ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
753 cd ${NANO_PACKAGE_DIR}
754 find ${NANO_PACKAGE_LIST} -print |
755 cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
758 # Count & report how many we have to install
759 todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
760 echo "=== TODO: $todo"
761 ls ${NANO_WORLDDIR}/Pkg
765 # Record how many we have now
766 have=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
768 # Attempt to install more packages
769 # ...but no more than 200 at a time due to pkg_add's internal
771 CR0 'ls Pkg/*tbz | xargs -n 200 env PKG_DBDIR='${NANO_PKG_META_BASE}'/pkg pkg_add -v -F'
773 # See what that got us
774 now=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
776 ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
780 if [ $now -eq $todo ] ; then
781 echo "DONE $now packages"
783 elif [ $now -eq $have ] ; then
784 echo "FAILED: Nothing happened on this pass"
788 rm -rxf ${NANO_WORLDDIR}/Pkg
793 # If the package directory doesn't exist, we're done.
794 if [ ! -d ${NANO_PACKAGE_DIR} ]; then
795 echo "DONE 0 packages"
799 # Find a pkg-* package
800 for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
801 _NANO_PKG_PACKAGE=`basename "$x"`
803 if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
804 echo "FAILED: need a pkg/ package for bootstrapping"
808 # Copy packages into chroot
809 mkdir -p ${NANO_WORLDDIR}/Pkg
811 cd ${NANO_PACKAGE_DIR}
812 find ${NANO_PACKAGE_LIST} -print |
813 cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
817 CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE}
818 CR pkg -N >/dev/null 2>&1
819 if [ "$?" -ne "0" ]; then
820 echo "FAILED: pkg bootstrapping faied"
823 rm -f ${NANO_WORLDDIR}/Pkg/pkg-*
825 # Count & report how many we have to install
826 todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l`
827 todo=$(expr $todo + 1) # add one for pkg since it is installed already
828 echo "=== TODO: $todo"
829 ls ${NANO_WORLDDIR}/Pkg
833 # Record how many we have now
834 have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
836 # Attempt to install more packages
837 CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add'
839 # See what that got us
840 now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
842 CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info
844 if [ $now -eq $todo ] ; then
845 echo "DONE $now packages"
847 elif [ $now -eq $have ] ; then
848 echo "FAILED: Nothing happened on this pass"
852 rm -rxf ${NANO_WORLDDIR}/Pkg
855 #######################################################################
856 # Convenience function:
857 # Register all args as customize function.
860 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
863 #######################################################################
864 # Convenience function:
865 # Register all args as late customize function to run just before
868 late_customize_cmd () {
869 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
872 #######################################################################
874 # All set up to go...
876 #######################################################################
879 # Print $2 at level $1.
881 if [ "$1" -le $PPLEVEL ]; then
882 runtime=$(( `date +%s` - $NANO_STARTTIME ))
883 printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
889 echo "Usage: $0 [-bfiknqvw] [-c config_file]"
890 echo " -b suppress builds (both kernel and world)"
891 echo " -f suppress code slice extraction"
892 echo " -i suppress disk image build"
893 echo " -k suppress buildkernel"
894 echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc"
895 echo " -q make output more quiet"
896 echo " -v make output more verbose"
897 echo " -w suppress buildworld"
898 echo " -c specify config file"
903 #######################################################################
910 do_copyout_partition=true
913 args=`getopt bc:fhiknqvw $*`
914 if [ $? -ne 0 ] ; then
935 # Make config file path available to the config file
936 # itself so that it can access additional files relative
937 # to its own location.
944 do_copyout_partition=false
959 PPLEVEL=$(($PPLEVEL - 1))
963 PPLEVEL=$(($PPLEVEL + 1))
976 if [ $# -gt 0 ] ; then
977 echo "$0: Extraneous arguments supplied"
981 trap nano_cleanup EXIT
983 #######################################################################
984 # Setup and Export Internal variables
986 test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/
987 test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
988 test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
990 NANO_WORLDDIR=${NANO_OBJ}/_.w
991 NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
992 NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
994 if [ -d ${NANO_TOOLS} ] ; then
996 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
997 NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
999 echo "NANO_TOOLS directory does not exist" 1>&2
1006 NANO_MAKE="${NANO_MAKE} -DNO_CLEAN"
1007 NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
1010 # Override user's NANO_DRIVE if they specified a NANO_LABEL
1011 if [ ! -z "${NANO_LABEL}" ]; then
1012 NANO_DRIVE=ufs/${NANO_LABEL}
1015 export MAKEOBJDIRPREFIX
1018 export NANO_CODESIZE
1019 export NANO_CONFSIZE
1020 export NANO_CUSTOMIZE
1021 export NANO_DATASIZE
1027 export NANO_MAKE_CONF_BUILD
1028 export NANO_MAKE_CONF_INSTALL
1029 export NANO_MEDIASIZE
1037 export NANO_WORLDDIR
1038 export NANO_BOOT0CFG
1039 export NANO_BOOTLOADER
1042 #######################################################################
1043 # And then it is as simple as that...
1045 # File descriptor 3 is used for logging output, see pprint
1048 NANO_STARTTIME=`date +%s`
1049 pprint 1 "NanoBSD image ${NANO_NAME} build starting"
1055 pprint 2 "Using existing build tree (as instructed)"
1060 pprint 2 "Skipping buildworld (as instructed)"
1063 if $do_kernel ; then
1064 if ! $do_world ; then
1069 pprint 2 "Skipping buildkernel (as instructed)"
1084 create_${NANO_ARCH}_diskimage
1086 pprint 2 "Skipping image build (as instructed)"
1090 pprint 1 "NanoBSD image ${NANO_NAME} completed"