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
54 # XXX: MAKEOBJDIRPREFIX handling... ?
57 # The directory to put the final images
58 # default is ${NANO_OBJ}
62 NANO_PMAKE="make -j 3"
64 # The default name for any image we create.
65 NANO_IMGNAME="_.disk.full"
67 # Options to put in make.conf during buildworld only
70 # Options to put in make.conf during installworld only
73 # Options to put in make.conf during both build- & installworld.
76 # Kernel config file to use
82 # Late customize commands.
83 NANO_LATE_CUSTOMIZE=""
85 # Newfs paramters to use
86 NANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U"
88 # The drive name of the media at runtime
91 # Target media size in 512 bytes sectors
92 NANO_MEDIASIZE=1200000
94 # Number of code images on media (1 or 2)
97 # 0 -> Leave second image all zeroes so it compresses better.
98 # 1 -> Initialize second image with a copy of the first
101 # Size of code file system in 512 bytes sectors
102 # If zero, size will be as large as possible.
105 # Size of configuration file system in 512 bytes sectors
109 # Size of data file system in 512 bytes sectors
110 # If zero: no partition configured.
111 # If negative: max size possible
114 # Size of the /etc ramdisk in 512 bytes sectors
115 NANO_RAM_ETCSIZE=10240
117 # Size of the /tmp+/var ramdisk in 512 bytes sectors
118 NANO_RAM_TMPVARSIZE=10240
120 # Media geometry, only relevant if bios doesn't understand LBA.
124 # boot0 flags/options and configuration
125 NANO_BOOT0CFG="-o packet -s 1 -m 3"
126 NANO_BOOTLOADER="boot/boot0sio"
128 # Backing type of md(4) device
129 # Can be "file" or "swap"
130 NANO_MD_BACKING="file"
132 # Progress Print level
135 #######################################################################
136 # Not a variable at this time
140 #######################################################################
142 # The functions which do the real work.
143 # Can be overridden from the config file(s)
145 #######################################################################
148 pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
150 if rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
153 chflags -R noschg ${MAKEOBJDIRPREFIX}
154 rm -rf ${MAKEOBJDIRPREFIX}
156 mkdir -p ${MAKEOBJDIRPREFIX}
157 printenv > ${MAKEOBJDIRPREFIX}/_.env
160 make_conf_build ( ) (
161 pprint 2 "Construct build make.conf ($NANO_MAKE_CONF)"
163 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
164 echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF}
168 pprint 2 "run buildworld"
169 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
172 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
173 __MAKE_CONF=${NANO_MAKE_CONF} buildworld \
174 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
178 pprint 2 "build kernel ($NANO_KERNEL)"
179 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
181 if [ -f ${NANO_KERNEL} ] ; then
182 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
186 # unset these just in case to avoid compiler complaints
187 # when cross-building
189 unset TARGET_BIG_ENDIAN
190 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
191 __MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
192 > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
197 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
198 if rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
201 chflags -R noschg ${NANO_WORLDDIR}/
202 rm -rf ${NANO_WORLDDIR}/
204 mkdir -p ${NANO_WORLDDIR}/
207 make_conf_install ( ) (
208 pprint 2 "Construct install make.conf ($NANO_MAKE_CONF)"
210 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
211 echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF}
215 pprint 2 "installworld"
216 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.iw"
219 env TARGET_ARCH=${NANO_ARCH} \
220 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} installworld \
221 DESTDIR=${NANO_WORLDDIR} \
222 > ${MAKEOBJDIRPREFIX}/_.iw 2>&1
223 chflags -R noschg ${NANO_WORLDDIR}
228 pprint 2 "install /etc"
229 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.etc"
232 env TARGET_ARCH=${NANO_ARCH} \
233 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} distribution \
234 DESTDIR=${NANO_WORLDDIR} \
235 > ${MAKEOBJDIRPREFIX}/_.etc 2>&1
239 pprint 2 "install kernel"
240 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.ik"
243 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \
244 DESTDIR=${NANO_WORLDDIR} \
245 __MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
246 > ${MAKEOBJDIRPREFIX}/_.ik 2>&1
251 pprint 2 "run customize scripts"
252 for c in $NANO_CUSTOMIZE
254 pprint 2 "customize \"$c\""
255 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.cust.$c"
257 ( $c ) > ${MAKEOBJDIRPREFIX}/_.cust.$c 2>&1
261 run_late_customize() (
263 pprint 2 "run late customize scripts"
264 for c in $NANO_LATE_CUSTOMIZE
266 pprint 2 "late customize \"$c\""
267 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.late_cust.$c"
269 ( $c ) > ${MAKEOBJDIRPREFIX}/_.late_cust.$c 2>&1
274 pprint 2 "configure nanobsd setup"
275 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.dl"
280 # Move /usr/local/etc to /etc/local so that the /cfg stuff
281 # can stomp on it. Otherwise packages like ipsec-tools which
282 # have hardcoded paths under ${prefix}/etc are not tweakable.
283 if [ -d usr/local/etc ] ; then
287 find . -print | cpio -dumpl ../../../etc/local
290 ln -s ../../etc/local etc
296 # link /$d under /conf
297 # we use hard links so we have them both places.
298 # the files in /$d will be hidden by the mount.
299 # XXX: configure /$d ramdisk size
300 mkdir -p conf/base/$d conf/default/$d
301 find $d -print | cpio -dumpl conf/base/
304 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
305 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
307 # pick up config files from the special partition
308 echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
310 # Put /tmp on the /var ramdisk (could be symlink already)
315 ) > ${MAKEOBJDIRPREFIX}/_.dl 2>&1
318 setup_nanobsd_etc ( ) (
319 pprint 2 "configure nanobsd /etc"
324 # create diskless marker file
327 # Make root filesystem R/O by default
328 echo "root_rw_mount=NO" >> etc/defaults/rc.conf
330 # save config file for scripts
331 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
333 echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
334 echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
341 # Remove all empty directories in /usr
342 find ${NANO_WORLDDIR}/usr -type d -depth -print |
345 rmdir $d > /dev/null 2>&1 || true
349 create_i386_diskimage ( ) (
350 pprint 2 "build diskimage"
351 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.di"
354 echo $NANO_MEDIASIZE $NANO_IMAGES \
355 $NANO_SECTS $NANO_HEADS \
356 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
361 # size of cylinder in sectors
364 # number of full cylinders on media
367 # output fdisk geometry spec, truncate cyls to 1023
369 print "g c" cyl " h" $4 " s" $3
371 print "g c" 1023 " h" $4 " s" $3
374 # size of data partition in full cylinders
375 dsl = int (($7 + cs - 1) / cs)
380 # size of config partition in full cylinders
381 csl = int (($6 + cs - 1) / cs)
384 # size of image partition(s) in full cylinders
385 isl = int ((cyl - dsl - csl) / $2)
387 isl = int (($5 + cs - 1) / cs)
390 # First image partition start at second track
391 print "p 1 165 " $3, isl * cs - $3
394 # Second image partition (if any) also starts offset one
395 # track to keep them identical.
397 print "p 2 165 " $3 + c, isl * cs - $3
401 # Config partition starts at cylinder boundary.
402 print "p 3 165 " c, csl * cs
405 # Data partition (if any) starts at cylinder boundary.
407 print "p 4 165 " c, dsl * cs
408 } else if ($7 < 0 && $1 > c) {
409 print "p 4 165 " c, $1 - c
411 print "Disk space overcommitted by", \
412 c - $1, "sectors" > "/dev/stderr"
416 # Force slice 1 to be marked active. This is necessary
417 # for booting the image from a USB device to work.
420 ' > ${MAKEOBJDIRPREFIX}/_.fdisk
422 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
423 MNT=${MAKEOBJDIRPREFIX}/_.mnt
426 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
427 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
430 echo "Creating md backing file..."
431 dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
432 count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
433 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
437 trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
439 fdisk -i -f ${MAKEOBJDIRPREFIX}/_.fdisk ${MD}
442 # XXX: pick up cached boot* files, they may not be in image anymore.
443 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
444 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
448 newfs ${NANO_NEWFS} /dev/${MD}s1a
449 mount /dev/${MD}s1a ${MNT}
451 echo "Copying worlddir..."
452 ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
454 echo "Generating mtree..."
455 ( cd ${MNT} && mtree -c ) > ${MAKEOBJDIRPREFIX}/_.mtree
456 ( cd ${MNT} && du -k ) > ${MAKEOBJDIRPREFIX}/_.du
459 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
460 # Duplicate to second image (if present)
461 echo "Duplicating to second image..."
462 dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
463 mount /dev/${MD}s2a ${MNT}
464 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
466 sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
472 # Create Config slice
473 newfs ${NANO_NEWFS} /dev/${MD}s3
474 # XXX: fill from where ?
476 # Create Data slice, if any.
477 if [ $NANO_DATASIZE -gt 0 ] ; then
478 newfs ${NANO_NEWFS} /dev/${MD}s4
479 # XXX: fill from where ?
482 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
483 echo "Writing out _.disk.full..."
484 dd if=/dev/${MD} of=${IMG} bs=64k
487 echo "Writing out _.disk.image..."
488 dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
490 ) > ${MAKEOBJDIRPREFIX}/_.di 2>&1
494 # Redefine this function with any last orders you may have
495 # after the build completed, for instance to copy the finished
496 # image to a more convenient place:
497 # cp ${MAKEOBJDIRPREFIX}/_.disk.image /home/ftp/pub/nanobsd.disk
500 #######################################################################
502 # Optional convenience functions.
504 #######################################################################
506 #######################################################################
507 # Common Flash device geometries
511 if [ -d ${NANO_TOOLS} ] ; then
512 . ${NANO_TOOLS}/FlashDevice.sub
514 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
516 sub_FlashDevice $1 $2
519 #######################################################################
520 # USB device geometries
523 # UsbDevice Generic 1000 # a generic flash key sold as having 1GB
525 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
527 # Note that the capacity of a flash key is usually advertised in MB or
528 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
529 # for C/H/S geometry may vary depending on the actual flash geometry.
531 # The following generic device layouts are understood:
532 # generic An alias for generic-hdd.
533 # generic-hdd 255H 63S/T xxxxC with no MBR restrictions.
534 # generic-fdd 64H 32S/T xxxxC with no MBR restrictions.
536 # The generic-hdd device is preferred for flash devices larger than 1GB.
540 a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
545 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
550 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
553 echo "Unknown USB flash device"
559 #######################################################################
560 # Setup serial console
563 # Enable getty on console
564 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
566 # Disable getty on syscons devices
567 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys
569 # Tell loader to use serial console early.
570 echo " -h" > ${NANO_WORLDDIR}/boot.config
573 #######################################################################
574 # Allow root login via ssh
576 cust_allow_ssh_root () (
577 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
578 ${NANO_WORLDDIR}/etc/ssh/sshd_config
581 #######################################################################
582 # Install the stuff under ./Files
584 cust_install_files () (
585 cd ${NANO_TOOLS}/Files
586 find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
589 #######################################################################
590 # Install packages from ${NANO_PACKAGE_DIR}
594 # Copy packages into chroot
595 mkdir -p ${NANO_WORLDDIR}/Pkg
597 cd ${NANO_PACKAGE_DIR}
598 find ${NANO_PACKAGE_LIST} -print |
599 cpio -dumpv ${NANO_WORLDDIR}/Pkg
602 # Count & report how many we have to install
603 todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
604 echo "=== TODO: $todo"
605 ls ${NANO_WORLDDIR}/Pkg
609 # Record how many we have now
610 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
612 # Attempt to install more packages
613 # ...but no more than 200 at a time due to pkg_add's internal
615 chroot ${NANO_WORLDDIR} sh -c \
616 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
618 # See what that got us
619 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
621 ls ${NANO_WORLDDIR}/var/db/pkg
625 if [ $now -eq $todo ] ; then
626 echo "DONE $now packages"
628 elif [ $now -eq $have ] ; then
629 echo "FAILED: Nothing happened on this pass"
633 rm -rf ${NANO_WORLDDIR}/Pkg
636 #######################################################################
637 # Convenience function:
638 # Register $1 as customize function.
641 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
644 #######################################################################
645 # Convenience function:
646 # Register $1 as late customize function to run just before
649 late_customize_cmd () {
650 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1"
653 #######################################################################
655 # All set up to go...
657 #######################################################################
660 # Print $2 at level $1.
662 if [ "$1" -le $PPLEVEL ]; then
663 printf "%.${1}s %s\n" "#####" "$2"
669 echo "Usage: $0 [-bikqvw] [-c config_file]"
670 echo " -b suppress builds (both kernel and world)"
671 echo " -i suppress disk image build"
672 echo " -k suppress buildkernel"
673 echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc"
674 echo " -q make output more quite"
675 echo " -v make output more verbose"
676 echo " -w suppress buildworld"
677 echo " -c specify config file"
682 #######################################################################
691 args=`getopt bc:hiknqvw $*`
692 if [ $? -ne 0 ] ; then
729 PPLEVEL=$(($PPLEVEL - 1))
733 PPLEVEL=$(($PPLEVEL + 1))
746 if [ $# -gt 0 ] ; then
747 echo "$0: Extraneous arguments supplied"
751 #######################################################################
752 # Setup and Export Internal variables
754 if [ "x${NANO_OBJ}" = "x" ] ; then
755 MAKEOBJDIRPREFIX=/usr/obj/nanobsd.${NANO_NAME}/
756 NANO_OBJ=${MAKEOBJDIRPREFIX}
758 MAKEOBJDIRPREFIX=${NANO_OBJ}
761 if [ "x${NANO_DISKIMGDIR}" = "x" ] ; then
762 NANO_DISKIMGDIR=${MAKEOBJDIRPREFIX}
765 NANO_WORLDDIR=${MAKEOBJDIRPREFIX}/_.w
766 NANO_MAKE_CONF=${MAKEOBJDIRPREFIX}/make.conf
768 if [ -d ${NANO_TOOLS} ] ; then
770 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
771 NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
773 echo "NANO_TOOLS directory does not exist" 1>&2
780 NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
783 export MAKEOBJDIRPREFIX
788 export NANO_CUSTOMIZE
794 export NANO_MAKE_CONF
795 export NANO_MEDIASIZE
805 export NANO_BOOTLOADER
807 #######################################################################
808 # And then it is as simple as that...
810 pprint 1 "NanoBSD image ${NANO_NAME} build starting"
816 pprint 2 "Using existing build tree (as instructed)"
821 pprint 2 "Skipping buildworld (as instructed)"
827 pprint 2 "Skipping buildkernel (as instructed)"
842 create_${NANO_ARCH}_diskimage
843 echo "# Created NanoBSD disk image: ${MAKEOBJDIRPREFIX}/${NANO_IMGNAME}"
845 pprint 2 "Skipping image build (as instructed)"
849 pprint 1 "NanoBSD image ${NANO_NAME} completed"