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 # Object tree directory
53 # default is subdir of /usr/obj
56 # The directory to put the final images
57 # default is ${NANO_OBJ}
61 NANO_PMAKE="make -j 3"
63 # The default name for any image we create.
64 NANO_IMGNAME="_.disk.full"
66 # Options to put in make.conf during buildworld only
69 # Options to put in make.conf during installworld only
72 # Options to put in make.conf during both build- & installworld.
75 # Kernel config file to use
81 # Late customize commands.
82 NANO_LATE_CUSTOMIZE=""
84 # Newfs paramters to use
85 NANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U"
87 # The drive name of the media at runtime
90 # Target media size in 512 bytes sectors
91 NANO_MEDIASIZE=1200000
93 # Number of code images on media (1 or 2)
96 # 0 -> Leave second image all zeroes so it compresses better.
97 # 1 -> Initialize second image with a copy of the first
100 # Size of code file system in 512 bytes sectors
101 # If zero, size will be as large as possible.
104 # Size of configuration file system in 512 bytes sectors
108 # Size of data file system in 512 bytes sectors
109 # If zero: no partition configured.
110 # If negative: max size possible
113 # Size of the /etc ramdisk in 512 bytes sectors
114 NANO_RAM_ETCSIZE=10240
116 # Size of the /tmp+/var ramdisk in 512 bytes sectors
117 NANO_RAM_TMPVARSIZE=10240
119 # Media geometry, only relevant if bios doesn't understand LBA.
123 # boot0 flags/options and configuration
124 NANO_BOOT0CFG="-o packet -s 1 -m 3"
125 NANO_BOOTLOADER="boot/boot0sio"
127 # boot2 flags/options
128 # default force serial console
131 # Backing type of md(4) device
132 # Can be "file" or "swap"
133 NANO_MD_BACKING="file"
135 # Progress Print level
138 #######################################################################
139 # Architecture to build. Corresponds to TARGET_ARCH in a buildworld.
140 # Unfortunately, there's no way to set TARGET at this time, and it
141 # conflates the two, so architectures where TARGET != TARGET_ARCH do
142 # not work. This defaults to the arch of the current machine.
146 #######################################################################
148 # The functions which do the real work.
149 # Can be overridden from the config file(s)
151 #######################################################################
154 pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
156 if ! rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
157 chflags -R noschg ${MAKEOBJDIRPREFIX}
158 rm -r ${MAKEOBJDIRPREFIX}
160 mkdir -p ${MAKEOBJDIRPREFIX}
161 printenv > ${MAKEOBJDIRPREFIX}/_.env
164 make_conf_build ( ) (
165 pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
167 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
168 echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
172 pprint 2 "run buildworld"
173 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
176 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
177 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
178 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
182 pprint 2 "build kernel ($NANO_KERNEL)"
183 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
185 if [ -f ${NANO_KERNEL} ] ; then
186 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
190 # unset these just in case to avoid compiler complaints
191 # when cross-building
193 unset TARGET_BIG_ENDIAN
194 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
195 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} KERNCONF=`basename ${NANO_KERNEL}` \
196 > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
201 if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
202 pprint 2 "Clean and create object directory (${NANO_OBJ})"
203 if ! rm -rf ${NANO_OBJ} > /dev/null 2>&1 ; then
204 chflags -R noschg ${NANO_OBJ}
207 mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
208 printenv > ${NANO_OBJ}/_.env
210 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
211 if ! rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
212 chflags -R noschg ${NANO_WORLDDIR}
213 rm -rf ${NANO_WORLDDIR}
215 mkdir -p ${NANO_WORLDDIR}
219 make_conf_install ( ) (
220 pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
222 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
223 echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
227 pprint 2 "installworld"
228 pprint 3 "log: ${NANO_OBJ}/_.iw"
231 env TARGET_ARCH=${NANO_ARCH} \
232 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
233 DESTDIR=${NANO_WORLDDIR} \
234 > ${NANO_OBJ}/_.iw 2>&1
235 chflags -R noschg ${NANO_WORLDDIR}
240 pprint 2 "install /etc"
241 pprint 3 "log: ${NANO_OBJ}/_.etc"
244 env TARGET_ARCH=${NANO_ARCH} \
245 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
246 DESTDIR=${NANO_WORLDDIR} \
247 > ${NANO_OBJ}/_.etc 2>&1
248 # make.conf doesn't get created by default, but some ports need it
249 # so they can spam it.
250 cp /dev/null ${NANO_WORLDDIR}/etc/make.conf
254 pprint 2 "install kernel"
255 pprint 3 "log: ${NANO_OBJ}/_.ik"
258 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \
259 DESTDIR=${NANO_WORLDDIR} \
260 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} KERNCONF=`basename ${NANO_KERNEL}` \
261 > ${NANO_OBJ}/_.ik 2>&1
266 pprint 2 "run customize scripts"
267 for c in $NANO_CUSTOMIZE
269 pprint 2 "customize \"$c\""
270 pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
272 ( $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
276 run_late_customize() (
278 pprint 2 "run late customize scripts"
279 for c in $NANO_LATE_CUSTOMIZE
281 pprint 2 "late customize \"$c\""
282 pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
284 ( $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
289 pprint 2 "configure nanobsd setup"
290 pprint 3 "log: ${NANO_OBJ}/_.dl"
295 # Move /usr/local/etc to /etc/local so that the /cfg stuff
296 # can stomp on it. Otherwise packages like ipsec-tools which
297 # have hardcoded paths under ${prefix}/etc are not tweakable.
298 if [ -d usr/local/etc ] ; then
302 find . -print | cpio -dumpl ../../../etc/local
305 ln -s ../../etc/local etc
311 # link /$d under /conf
312 # we use hard links so we have them both places.
313 # the files in /$d will be hidden by the mount.
314 # XXX: configure /$d ramdisk size
315 mkdir -p conf/base/$d conf/default/$d
316 find $d -print | cpio -dumpl conf/base/
319 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
320 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
322 # pick up config files from the special partition
323 echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
325 # Put /tmp on the /var ramdisk (could be symlink already)
330 ) > ${NANO_OBJ}/_.dl 2>&1
333 setup_nanobsd_etc ( ) (
334 pprint 2 "configure nanobsd /etc"
339 # create diskless marker file
342 # Make root filesystem R/O by default
343 echo "root_rw_mount=NO" >> etc/defaults/rc.conf
345 # save config file for scripts
346 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
348 echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
349 echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
356 # Remove all empty directories in /usr
357 find ${NANO_WORLDDIR}/usr -type d -depth -print |
360 rmdir $d > /dev/null 2>&1 || true
369 test -z $2 && dir=/var/empty
370 test -d $d || dir=/var/empty
371 echo "Creating ${dev} with ${dir} (mounting on ${mnt})"
372 newfs ${NANO_NEWFS} ${dev}
375 find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${mnt}
380 populate_cfg_slice ( ) (
381 populate_slice "$1" "$2" "$3"
384 populate_data_slice ( ) (
385 populate_slice "$1" "$2" "$3"
388 create_i386_diskimage ( ) (
389 pprint 2 "build diskimage"
390 pprint 3 "log: ${NANO_OBJ}/_.di"
393 echo $NANO_MEDIASIZE $NANO_IMAGES \
394 $NANO_SECTS $NANO_HEADS \
395 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
400 # size of cylinder in sectors
403 # number of full cylinders on media
406 # output fdisk geometry spec, truncate cyls to 1023
408 print "g c" cyl " h" $4 " s" $3
410 print "g c" 1023 " h" $4 " s" $3
413 # size of data partition in full cylinders
414 dsl = int (($7 + cs - 1) / cs)
419 # size of config partition in full cylinders
420 csl = int (($6 + cs - 1) / cs)
423 # size of image partition(s) in full cylinders
424 isl = int ((cyl - dsl - csl) / $2)
426 isl = int (($5 + cs - 1) / cs)
429 # First image partition start at second track
430 print "p 1 165 " $3, isl * cs - $3
433 # Second image partition (if any) also starts offset one
434 # track to keep them identical.
436 print "p 2 165 " $3 + c, isl * cs - $3
440 # Config partition starts at cylinder boundary.
441 print "p 3 165 " c, csl * cs
444 # Data partition (if any) starts at cylinder boundary.
446 print "p 4 165 " c, dsl * cs
447 } else if ($7 < 0 && $1 > c) {
448 print "p 4 165 " c, $1 - c
450 print "Disk space overcommitted by", \
451 c - $1, "sectors" > "/dev/stderr"
455 # Force slice 1 to be marked active. This is necessary
456 # for booting the image from a USB device to work.
459 ' > ${NANO_OBJ}/_.fdisk
461 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
462 MNT=${NANO_OBJ}/_.mnt
465 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
466 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
469 echo "Creating md backing file..."
470 dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
471 count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
472 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
476 trap "echo 'Running exit trap code' ; df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
478 fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
481 # XXX: pick up cached boot* files, they may not be in image anymore.
482 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
483 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
487 # XXX: should use populate_slice for easier override
488 newfs ${NANO_NEWFS} /dev/${MD}s1a
489 mount /dev/${MD}s1a ${MNT}
491 echo "Copying worlddir..."
492 ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
494 echo "Generating mtree..."
495 ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
496 ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
499 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
500 # Duplicate to second image (if present)
501 echo "Duplicating to second image..."
502 dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
503 mount /dev/${MD}s2a ${MNT}
504 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
506 sed -i "" "s=${NANO_DRIVE}s1=${NANO_DRIVE}s2=g" $f
511 # Create Config slice
512 populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT}
514 # Create Data slice, if any.
515 if [ $NANO_DATASIZE -ne 0 ] ; then
516 populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT}
519 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
520 echo "Writing out ${NANO_IMGNAME}..."
521 dd if=/dev/${MD} of=${IMG} bs=64k
524 echo "Writing out _.disk.image..."
525 dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
530 ) > ${NANO_OBJ}/_.di 2>&1
533 # i386 and amd64 are identical for disk images
534 create_amd64_diskimage ( ) (
535 create_i386_diskimage
539 # Redefine this function with any last orders you may have
540 # after the build completed, for instance to copy the finished
541 # image to a more convenient place:
542 # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
545 #######################################################################
547 # Optional convenience functions.
549 #######################################################################
551 #######################################################################
552 # Common Flash device geometries
556 if [ -d ${NANO_TOOLS} ] ; then
557 . ${NANO_TOOLS}/FlashDevice.sub
559 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
561 sub_FlashDevice $1 $2
564 #######################################################################
565 # USB device geometries
568 # UsbDevice Generic 1000 # a generic flash key sold as having 1GB
570 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
572 # Note that the capacity of a flash key is usually advertised in MB or
573 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
574 # for C/H/S geometry may vary depending on the actual flash geometry.
576 # The following generic device layouts are understood:
577 # generic An alias for generic-hdd.
578 # generic-hdd 255H 63S/T xxxxC with no MBR restrictions.
579 # generic-fdd 64H 32S/T xxxxC with no MBR restrictions.
581 # The generic-hdd device is preferred for flash devices larger than 1GB.
585 a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
590 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
595 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
598 echo "Unknown USB flash device"
604 #######################################################################
605 # Setup serial console
608 # Enable getty on console
609 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
611 # Disable getty on syscons devices
612 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys
614 # Tell loader to use serial console early.
615 echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
618 #######################################################################
619 # Allow root login via ssh
621 cust_allow_ssh_root () (
622 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
623 ${NANO_WORLDDIR}/etc/ssh/sshd_config
626 #######################################################################
627 # Install the stuff under ./Files
629 cust_install_files () (
630 cd ${NANO_TOOLS}/Files
631 find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
634 #######################################################################
635 # Install packages from ${NANO_PACKAGE_DIR}
639 # Copy packages into chroot
640 mkdir -p ${NANO_WORLDDIR}/Pkg
642 cd ${NANO_PACKAGE_DIR}
643 find ${NANO_PACKAGE_LIST} -print |
644 cpio -dumpv ${NANO_WORLDDIR}/Pkg
647 # Count & report how many we have to install
648 todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
649 echo "=== TODO: $todo"
650 ls ${NANO_WORLDDIR}/Pkg
654 # Record how many we have now
655 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
657 # Attempt to install more packages
658 # ...but no more than 200 at a time due to pkg_add's internal
660 chroot ${NANO_WORLDDIR} sh -c \
661 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
663 # See what that got us
664 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
666 ls ${NANO_WORLDDIR}/var/db/pkg
670 if [ $now -eq $todo ] ; then
671 echo "DONE $now packages"
673 elif [ $now -eq $have ] ; then
674 echo "FAILED: Nothing happened on this pass"
678 rm -rf ${NANO_WORLDDIR}/Pkg
681 #######################################################################
682 # Convenience function:
683 # Register all args as customize function.
686 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
689 #######################################################################
690 # Convenience function:
691 # Register all args as late customize function to run just before
694 late_customize_cmd () {
695 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
698 #######################################################################
700 # All set up to go...
702 #######################################################################
705 # Print $2 at level $1.
707 if [ "$1" -le $PPLEVEL ]; then
708 runtime=$(( `date +%s` - $NANO_STARTTIME ))
709 printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
715 echo "Usage: $0 [-biknqvw] [-c config_file]"
716 echo " -b suppress builds (both kernel and world)"
717 echo " -i suppress disk image build"
718 echo " -k suppress buildkernel"
719 echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc"
720 echo " -q make output more quiet"
721 echo " -v make output more verbose"
722 echo " -w suppress buildworld"
723 echo " -c specify config file"
728 #######################################################################
737 args=`getopt bc:hiknqvw $*`
738 if [ $? -ne 0 ] ; then
775 PPLEVEL=$(($PPLEVEL - 1))
779 PPLEVEL=$(($PPLEVEL + 1))
792 if [ $# -gt 0 ] ; then
793 echo "$0: Extraneous arguments supplied"
797 #######################################################################
798 # Setup and Export Internal variables
800 test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/
801 test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
802 test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
804 NANO_WORLDDIR=${NANO_OBJ}/_.w
805 NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
806 NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
808 if [ -d ${NANO_TOOLS} ] ; then
810 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
811 NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
813 echo "NANO_TOOLS directory does not exist" 1>&2
820 NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
823 export MAKEOBJDIRPREFIX
828 export NANO_CUSTOMIZE
834 export NANO_MAKE_CONF_BUILD
835 export NANO_MAKE_CONF_INSTALL
836 export NANO_MEDIASIZE
846 export NANO_BOOTLOADER
848 #######################################################################
849 # And then it is as simple as that...
851 # File descriptor 3 is used for logging output, see pprint
854 NANO_STARTTIME=`date +%s`
855 pprint 1 "NanoBSD image ${NANO_NAME} build starting"
861 pprint 2 "Using existing build tree (as instructed)"
866 pprint 2 "Skipping buildworld (as instructed)"
872 pprint 2 "Skipping buildkernel (as instructed)"
887 create_${NANO_ARCH}_diskimage
889 pprint 2 "Skipping image build (as instructed)"
893 pprint 1 "NanoBSD image ${NANO_NAME} completed"