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
51 # Object tree directory
52 # default is subdir of /usr/obj
53 # XXX: MAKEOBJDIRPREFIX handling... ?
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 # Backing type of md(4) device
128 # Can be "file" or "swap"
129 NANO_MD_BACKING="file"
131 # Progress Print level
134 #######################################################################
135 # Not a variable at this time
139 #######################################################################
141 # The functions which do the real work.
142 # Can be overridden from the config file(s)
144 #######################################################################
147 pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
149 if rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
152 chflags -R noschg ${MAKEOBJDIRPREFIX}
153 rm -rf ${MAKEOBJDIRPREFIX}
155 mkdir -p ${MAKEOBJDIRPREFIX}
156 printenv > ${MAKEOBJDIRPREFIX}/_.env
159 make_conf_build ( ) (
160 pprint 2 "Construct build make.conf ($NANO_MAKE_CONF)"
162 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
163 echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF}
167 pprint 2 "run buildworld"
168 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
171 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
172 __MAKE_CONF=${NANO_MAKE_CONF} buildworld \
173 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
177 pprint 2 "build kernel ($NANO_KERNEL)"
178 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
180 if [ -f ${NANO_KERNEL} ] ; then
181 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
185 # unset these just in case to avoid compiler complaints
186 # when cross-building
188 unset TARGET_BIG_ENDIAN
189 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
190 __MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
191 > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
196 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
197 if rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
200 chflags -R noschg ${NANO_WORLDDIR}/
201 rm -rf ${NANO_WORLDDIR}/
203 mkdir -p ${NANO_WORLDDIR}/
206 make_conf_install ( ) (
207 pprint 2 "Construct install make.conf ($NANO_MAKE_CONF)"
209 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
210 echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF}
214 pprint 2 "installworld"
215 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.iw"
218 env TARGET_ARCH=${NANO_ARCH} \
219 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} installworld \
220 DESTDIR=${NANO_WORLDDIR} \
221 > ${MAKEOBJDIRPREFIX}/_.iw 2>&1
222 chflags -R noschg ${NANO_WORLDDIR}
227 pprint 2 "install /etc"
228 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.etc"
231 env TARGET_ARCH=${NANO_ARCH} \
232 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} distribution \
233 DESTDIR=${NANO_WORLDDIR} \
234 > ${MAKEOBJDIRPREFIX}/_.etc 2>&1
238 pprint 2 "install kernel"
239 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.ik"
242 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \
243 DESTDIR=${NANO_WORLDDIR} \
244 __MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
245 > ${MAKEOBJDIRPREFIX}/_.ik 2>&1
250 pprint 2 "run customize scripts"
251 for c in $NANO_CUSTOMIZE
253 pprint 2 "customize \"$c\""
254 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.cust.$c"
256 ( $c ) > ${MAKEOBJDIRPREFIX}/_.cust.$c 2>&1
260 run_late_customize() (
262 pprint 2 "run late customize scripts"
263 for c in $NANO_LATE_CUSTOMIZE
265 pprint 2 "late customize \"$c\""
266 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.late_cust.$c"
268 ( $c ) > ${MAKEOBJDIRPREFIX}/_.late_cust.$c 2>&1
273 pprint 2 "configure nanobsd setup"
274 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.dl"
279 # Move /usr/local/etc to /etc/local so that the /cfg stuff
280 # can stomp on it. Otherwise packages like ipsec-tools which
281 # have hardcoded paths under ${prefix}/etc are not tweakable.
282 if [ -d usr/local/etc ] ; then
286 find . -print | cpio -dumpl ../../../etc/local
289 ln -s ../../etc/local etc
295 # link /$d under /conf
296 # we use hard links so we have them both places.
297 # the files in /$d will be hidden by the mount.
298 # XXX: configure /$d ramdisk size
299 mkdir -p conf/base/$d conf/default/$d
300 find $d -print | cpio -dumpl conf/base/
303 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
304 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
306 # pick up config files from the special partition
307 echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
309 # Put /tmp on the /var ramdisk (could be symlink already)
314 ) > ${MAKEOBJDIRPREFIX}/_.dl 2>&1
317 setup_nanobsd_etc ( ) (
318 pprint 2 "configure nanobsd /etc"
323 # create diskless marker file
326 # Make root filesystem R/O by default
327 echo "root_rw_mount=NO" >> etc/defaults/rc.conf
329 # save config file for scripts
330 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
332 echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
333 echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
340 # Remove all empty directories in /usr
341 find ${NANO_WORLDDIR}/usr -type d -depth -print |
344 rmdir $d > /dev/null 2>&1 || true
348 create_i386_diskimage ( ) (
349 pprint 2 "build diskimage"
350 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.di"
353 echo $NANO_MEDIASIZE $NANO_IMAGES \
354 $NANO_SECTS $NANO_HEADS \
355 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
360 # size of cylinder in sectors
363 # number of full cylinders on media
366 # output fdisk geometry spec, truncate cyls to 1023
368 print "g c" cyl " h" $4 " s" $3
370 print "g c" 1023 " h" $4 " s" $3
373 # size of data partition in full cylinders
374 dsl = int (($7 + cs - 1) / cs)
379 # size of config partition in full cylinders
380 csl = int (($6 + cs - 1) / cs)
383 # size of image partition(s) in full cylinders
384 isl = int ((cyl - dsl - csl) / $2)
386 isl = int (($5 + cs - 1) / cs)
389 # First image partition start at second track
390 print "p 1 165 " $3, isl * cs - $3
393 # Second image partition (if any) also starts offset one
394 # track to keep them identical.
396 print "p 2 165 " $3 + c, isl * cs - $3
400 # Config partition starts at cylinder boundary.
401 print "p 3 165 " c, csl * cs
404 # Data partition (if any) starts at cylinder boundary.
406 print "p 4 165 " c, dsl * cs
407 } else if ($7 < 0 && $1 > c) {
408 print "p 4 165 " c, $1 - c
410 print "Disk space overcommitted by", \
411 c - $1, "sectors" > "/dev/stderr"
415 # Force slice 1 to be marked active. This is necessary
416 # for booting the image from a USB device to work.
419 ' > ${MAKEOBJDIRPREFIX}/_.fdisk
421 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
422 MNT=${MAKEOBJDIRPREFIX}/_.mnt
425 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
426 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
429 echo "Creating md backing file..."
430 dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
431 count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
432 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
436 trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
438 fdisk -i -f ${MAKEOBJDIRPREFIX}/_.fdisk ${MD}
441 # XXX: pick up cached boot* files, they may not be in image anymore.
442 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
443 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
447 newfs ${NANO_NEWFS} /dev/${MD}s1a
448 mount /dev/${MD}s1a ${MNT}
450 echo "Copying worlddir..."
451 ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
453 echo "Generating mtree..."
454 ( cd ${MNT} && mtree -c ) > ${MAKEOBJDIRPREFIX}/_.mtree
455 ( cd ${MNT} && du -k ) > ${MAKEOBJDIRPREFIX}/_.du
458 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
459 # Duplicate to second image (if present)
460 echo "Duplicating to second image..."
461 dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
462 mount /dev/${MD}s2a ${MNT}
463 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
465 sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
471 # Create Config slice
472 newfs ${NANO_NEWFS} /dev/${MD}s3
473 # XXX: fill from where ?
475 # Create Data slice, if any.
476 if [ $NANO_DATASIZE -gt 0 ] ; then
477 newfs ${NANO_NEWFS} /dev/${MD}s4
478 # XXX: fill from where ?
481 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
482 echo "Writing out _.disk.full..."
483 dd if=/dev/${MD} of=${IMG} bs=64k
486 echo "Writing out _.disk.image..."
487 dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
489 ) > ${MAKEOBJDIRPREFIX}/_.di 2>&1
493 # Redefine this function with any last orders you may have
494 # after the build completed, for instance to copy the finished
495 # image to a more convenient place:
496 # cp ${MAKEOBJDIRPREFIX}/_.disk.image /home/ftp/pub/nanobsd.disk
499 #######################################################################
501 # Optional convenience functions.
503 #######################################################################
505 #######################################################################
506 # Common Flash device geometries
510 if [ -d ${NANO_TOOLS} ] ; then
511 . ${NANO_TOOLS}/FlashDevice.sub
513 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
515 sub_FlashDevice $1 $2
518 #######################################################################
519 # USB device geometries
522 # UsbDevice Generic 1000 # a generic flash key sold as having 1GB
524 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
526 # Note that the capacity of a flash key is usually advertised in MB or
527 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
528 # for C/H/S geometry may vary depending on the actual flash geometry.
530 # The following generic device layouts are understood:
531 # generic An alias for generic-hdd.
532 # generic-hdd 255H 63S/T xxxxC with no MBR restrictions.
533 # generic-fdd 64H 32S/T xxxxC with no MBR restrictions.
535 # The generic-hdd device is preferred for flash devices larger than 1GB.
539 a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
544 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
549 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
552 echo "Unknown USB flash device"
558 #######################################################################
559 # Setup serial console
562 # Enable getty on console
563 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
565 # Disable getty on syscons devices
566 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys
568 # Tell loader to use serial console early.
569 echo " -h" > ${NANO_WORLDDIR}/boot.config
572 #######################################################################
573 # Allow root login via ssh
575 cust_allow_ssh_root () (
576 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
577 ${NANO_WORLDDIR}/etc/ssh/sshd_config
580 #######################################################################
581 # Install the stuff under ./Files
583 cust_install_files () (
584 cd ${NANO_TOOLS}/Files
585 find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
588 #######################################################################
589 # Install packages from ${NANO_PACKAGE_DIR}
593 # Copy packages into chroot
594 mkdir -p ${NANO_WORLDDIR}/Pkg
595 cp ${NANO_PACKAGE_DIR}/* ${NANO_WORLDDIR}/Pkg
597 # Count & report how many we have to install
598 todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
599 echo "=== TODO: $todo"
600 ls ${NANO_WORLDDIR}/Pkg
604 # Record how many we have now
605 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
607 # Attempt to install more packages
608 # ...but no more than 200 at a time due to pkg_add's internal
610 chroot ${NANO_WORLDDIR} sh -c \
611 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
613 # See what that got us
614 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
616 ls ${NANO_WORLDDIR}/var/db/pkg
620 if [ $now -eq $todo ] ; then
621 echo "DONE $now packages"
623 elif [ $now -eq $have ] ; then
624 echo "FAILED: Nothing happened on this pass"
628 rm -rf ${NANO_WORLDDIR}/Pkg
631 #######################################################################
632 # Convenience function:
633 # Register $1 as customize function.
636 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
639 #######################################################################
640 # Convenience function:
641 # Register $1 as late customize function to run just before
644 late_customize_cmd () {
645 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1"
648 #######################################################################
650 # All set up to go...
652 #######################################################################
655 # Print $2 at level $1.
657 if [ "$1" -le $PPLEVEL ]; then
658 printf "%.${1}s %s\n" "#####" "$2"
664 echo "Usage: $0 [-bikqvw] [-c config_file]"
665 echo " -b suppress builds (both kernel and world)"
666 echo " -i suppress disk image build"
667 echo " -k suppress buildkernel"
668 echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc"
669 echo " -q make output more quite"
670 echo " -v make output more verbose"
671 echo " -w suppress buildworld"
672 echo " -c specify config file"
677 #######################################################################
686 args=`getopt bc:hiknqvw $*`
687 if [ $? -ne 0 ] ; then
724 PPLEVEL=$(($PPLEVEL - 1))
728 PPLEVEL=$(($PPLEVEL + 1))
741 if [ $# -gt 0 ] ; then
742 echo "$0: Extraneous arguments supplied"
746 #######################################################################
747 # Setup and Export Internal variables
749 if [ "x${NANO_OBJ}" = "x" ] ; then
750 MAKEOBJDIRPREFIX=/usr/obj/nanobsd.${NANO_NAME}/
751 NANO_OBJ=${MAKEOBJDIRPREFIX}
753 MAKEOBJDIRPREFIX=${NANO_OBJ}
756 if [ "x${NANO_DISKIMGDIR}" = "x" ] ; then
757 NANO_DISKIMGDIR=${MAKEOBJDIRPREFIX}
760 NANO_WORLDDIR=${MAKEOBJDIRPREFIX}/_.w
761 NANO_MAKE_CONF=${MAKEOBJDIRPREFIX}/make.conf
763 if [ -d ${NANO_TOOLS} ] ; then
765 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
766 NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
768 echo "NANO_TOOLS directory does not exist" 1>&2
775 NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
778 export MAKEOBJDIRPREFIX
783 export NANO_CUSTOMIZE
789 export NANO_MAKE_CONF
790 export NANO_MEDIASIZE
800 export NANO_BOOTLOADER
802 #######################################################################
803 # And then it is as simple as that...
805 pprint 1 "NanoBSD image ${NANO_NAME} build starting"
811 pprint 2 "Using existing build tree (as instructed)"
816 pprint 2 "Skipping buildworld (as instructed)"
822 pprint 2 "Skipping buildkernel (as instructed)"
837 create_${NANO_ARCH}_diskimage
838 echo "# Created NanoBSD disk image: ${MAKEOBJDIRPREFIX}/${NANO_IMGNAME}"
840 pprint 2 "Skipping image build (as instructed)"
844 pprint 1 "NanoBSD image ${NANO_NAME} completed"