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 # 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
150 chflags -R noschg ${MAKEOBJDIRPREFIX}
151 rm -r ${MAKEOBJDIRPREFIX}
153 mkdir -p ${MAKEOBJDIRPREFIX}
154 printenv > ${MAKEOBJDIRPREFIX}/_.env
157 make_conf_build ( ) (
158 pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
160 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
161 echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
165 pprint 2 "run buildworld"
166 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
169 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
170 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
171 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
175 pprint 2 "build kernel ($NANO_KERNEL)"
176 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
178 if [ -f ${NANO_KERNEL} ] ; then
179 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
183 # unset these just in case to avoid compiler complaints
184 # when cross-building
186 unset TARGET_BIG_ENDIAN
187 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
188 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} KERNCONF=`basename ${NANO_KERNEL}` \
189 > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
194 if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
195 pprint 2 "Clean and create object directory (${NANO_OBJ})"
196 if ! rm -rf ${NANO_OBJ} > /dev/null 2>&1 ; then
197 chflags -R noschg ${NANO_OBJ}
200 mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
201 printenv > ${NANO_OBJ}/_.env
203 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
204 if ! rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
205 chflags -R noschg ${NANO_WORLDDIR}
206 rm -rf ${NANO_WORLDDIR}
208 mkdir -p ${NANO_WORLDDIR}
212 make_conf_install ( ) (
213 pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
215 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
216 echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
220 pprint 2 "installworld"
221 pprint 3 "log: ${NANO_OBJ}/_.iw"
224 env TARGET_ARCH=${NANO_ARCH} \
225 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
226 DESTDIR=${NANO_WORLDDIR} \
227 > ${NANO_OBJ}/_.iw 2>&1
228 chflags -R noschg ${NANO_WORLDDIR}
233 pprint 2 "install /etc"
234 pprint 3 "log: ${NANO_OBJ}/_.etc"
237 env TARGET_ARCH=${NANO_ARCH} \
238 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
239 DESTDIR=${NANO_WORLDDIR} \
240 > ${NANO_OBJ}/_.etc 2>&1
244 pprint 2 "install kernel"
245 pprint 3 "log: ${NANO_OBJ}/_.ik"
248 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \
249 DESTDIR=${NANO_WORLDDIR} \
250 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} KERNCONF=`basename ${NANO_KERNEL}` \
251 > ${NANO_OBJ}/_.ik 2>&1
256 pprint 2 "run customize scripts"
257 for c in $NANO_CUSTOMIZE
259 pprint 2 "customize \"$c\""
260 pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
262 ( $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
266 run_late_customize() (
268 pprint 2 "run late customize scripts"
269 for c in $NANO_LATE_CUSTOMIZE
271 pprint 2 "late customize \"$c\""
272 pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
274 ( $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
279 pprint 2 "configure nanobsd setup"
280 pprint 3 "log: ${NANO_OBJ}/_.dl"
285 # Move /usr/local/etc to /etc/local so that the /cfg stuff
286 # can stomp on it. Otherwise packages like ipsec-tools which
287 # have hardcoded paths under ${prefix}/etc are not tweakable.
288 if [ -d usr/local/etc ] ; then
292 find . -print | cpio -dumpl ../../../etc/local
295 ln -s ../../etc/local etc
301 # link /$d under /conf
302 # we use hard links so we have them both places.
303 # the files in /$d will be hidden by the mount.
304 # XXX: configure /$d ramdisk size
305 mkdir -p conf/base/$d conf/default/$d
306 find $d -print | cpio -dumpl conf/base/
309 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
310 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
312 # pick up config files from the special partition
313 echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
315 # Put /tmp on the /var ramdisk (could be symlink already)
320 ) > ${NANO_OBJ}/_.dl 2>&1
323 setup_nanobsd_etc ( ) (
324 pprint 2 "configure nanobsd /etc"
329 # create diskless marker file
332 # Make root filesystem R/O by default
333 echo "root_rw_mount=NO" >> etc/defaults/rc.conf
335 # save config file for scripts
336 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
338 echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
339 echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
346 # Remove all empty directories in /usr
347 find ${NANO_WORLDDIR}/usr -type d -depth -print |
350 rmdir $d > /dev/null 2>&1 || true
354 create_i386_diskimage ( ) (
355 pprint 2 "build diskimage"
356 pprint 3 "log: ${NANO_OBJ}/_.di"
359 echo $NANO_MEDIASIZE $NANO_IMAGES \
360 $NANO_SECTS $NANO_HEADS \
361 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
366 # size of cylinder in sectors
369 # number of full cylinders on media
372 # output fdisk geometry spec, truncate cyls to 1023
374 print "g c" cyl " h" $4 " s" $3
376 print "g c" 1023 " h" $4 " s" $3
379 # size of data partition in full cylinders
380 dsl = int (($7 + cs - 1) / cs)
385 # size of config partition in full cylinders
386 csl = int (($6 + cs - 1) / cs)
389 # size of image partition(s) in full cylinders
390 isl = int ((cyl - dsl - csl) / $2)
392 isl = int (($5 + cs - 1) / cs)
395 # First image partition start at second track
396 print "p 1 165 " $3, isl * cs - $3
399 # Second image partition (if any) also starts offset one
400 # track to keep them identical.
402 print "p 2 165 " $3 + c, isl * cs - $3
406 # Config partition starts at cylinder boundary.
407 print "p 3 165 " c, csl * cs
410 # Data partition (if any) starts at cylinder boundary.
412 print "p 4 165 " c, dsl * cs
413 } else if ($7 < 0 && $1 > c) {
414 print "p 4 165 " c, $1 - c
416 print "Disk space overcommitted by", \
417 c - $1, "sectors" > "/dev/stderr"
421 # Force slice 1 to be marked active. This is necessary
422 # for booting the image from a USB device to work.
425 ' > ${NANO_OBJ}/_.fdisk
427 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
428 MNT=${NANO_OBJ}/_.mnt
431 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
432 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
435 echo "Creating md backing file..."
436 dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
437 count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
438 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
442 trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
444 fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
447 # XXX: pick up cached boot* files, they may not be in image anymore.
448 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
449 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
453 newfs ${NANO_NEWFS} /dev/${MD}s1a
454 mount /dev/${MD}s1a ${MNT}
456 echo "Copying worlddir..."
457 ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
459 echo "Generating mtree..."
460 ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
461 ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
464 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
465 # Duplicate to second image (if present)
466 echo "Duplicating to second image..."
467 dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
468 mount /dev/${MD}s2a ${MNT}
469 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
471 sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
476 # Create Config slice
477 newfs ${NANO_NEWFS} /dev/${MD}s3
478 # XXX: fill from where ?
480 # Create Data slice, if any.
481 if [ $NANO_DATASIZE -ne 0 ] ; then
482 newfs ${NANO_NEWFS} /dev/${MD}s4
483 # XXX: fill from where ?
486 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
487 echo "Writing out ${NANO_IMGNAME}..."
488 dd if=/dev/${MD} of=${IMG} bs=64k
491 echo "Writing out _.disk.image..."
492 dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
494 ) > ${NANO_OBJ}/_.di 2>&1
498 # Redefine this function with any last orders you may have
499 # after the build completed, for instance to copy the finished
500 # image to a more convenient place:
501 # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
504 #######################################################################
506 # Optional convenience functions.
508 #######################################################################
510 #######################################################################
511 # Common Flash device geometries
515 if [ -d ${NANO_TOOLS} ] ; then
516 . ${NANO_TOOLS}/FlashDevice.sub
518 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
520 sub_FlashDevice $1 $2
523 #######################################################################
524 # USB device geometries
527 # UsbDevice Generic 1000 # a generic flash key sold as having 1GB
529 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
531 # Note that the capacity of a flash key is usually advertised in MB or
532 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
533 # for C/H/S geometry may vary depending on the actual flash geometry.
535 # The following generic device layouts are understood:
536 # generic An alias for generic-hdd.
537 # generic-hdd 255H 63S/T xxxxC with no MBR restrictions.
538 # generic-fdd 64H 32S/T xxxxC with no MBR restrictions.
540 # The generic-hdd device is preferred for flash devices larger than 1GB.
544 a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
549 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
554 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
557 echo "Unknown USB flash device"
563 #######################################################################
564 # Setup serial console
567 # Enable getty on console
568 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
570 # Disable getty on syscons devices
571 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys
573 # Tell loader to use serial console early.
574 echo " -h" > ${NANO_WORLDDIR}/boot.config
577 #######################################################################
578 # Allow root login via ssh
580 cust_allow_ssh_root () (
581 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
582 ${NANO_WORLDDIR}/etc/ssh/sshd_config
585 #######################################################################
586 # Install the stuff under ./Files
588 cust_install_files () (
589 cd ${NANO_TOOLS}/Files
590 find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
593 #######################################################################
594 # Install packages from ${NANO_PACKAGE_DIR}
598 # Copy packages into chroot
599 mkdir -p ${NANO_WORLDDIR}/Pkg
601 cd ${NANO_PACKAGE_DIR}
602 find ${NANO_PACKAGE_LIST} -print |
603 cpio -dumpv ${NANO_WORLDDIR}/Pkg
606 # Count & report how many we have to install
607 todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
608 echo "=== TODO: $todo"
609 ls ${NANO_WORLDDIR}/Pkg
613 # Record how many we have now
614 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
616 # Attempt to install more packages
617 # ...but no more than 200 at a time due to pkg_add's internal
619 chroot ${NANO_WORLDDIR} sh -c \
620 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
622 # See what that got us
623 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
625 ls ${NANO_WORLDDIR}/var/db/pkg
629 if [ $now -eq $todo ] ; then
630 echo "DONE $now packages"
632 elif [ $now -eq $have ] ; then
633 echo "FAILED: Nothing happened on this pass"
637 rm -rf ${NANO_WORLDDIR}/Pkg
640 #######################################################################
641 # Convenience function:
642 # Register $1 as customize function.
645 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
648 #######################################################################
649 # Convenience function:
650 # Register $1 as late customize function to run just before
653 late_customize_cmd () {
654 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1"
657 #######################################################################
659 # All set up to go...
661 #######################################################################
664 # Print $2 at level $1.
666 if [ "$1" -le $PPLEVEL ]; then
667 runtime=$(( `date +%s` - $NANO_STARTTIME ))
668 printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
674 echo "Usage: $0 [-bikqvw] [-c config_file]"
675 echo " -b suppress builds (both kernel and world)"
676 echo " -i suppress disk image build"
677 echo " -k suppress buildkernel"
678 echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc"
679 echo " -q make output more quite"
680 echo " -v make output more verbose"
681 echo " -w suppress buildworld"
682 echo " -c specify config file"
687 #######################################################################
696 args=`getopt bc:hiknqvw $*`
697 if [ $? -ne 0 ] ; then
734 PPLEVEL=$(($PPLEVEL - 1))
738 PPLEVEL=$(($PPLEVEL + 1))
751 if [ $# -gt 0 ] ; then
752 echo "$0: Extraneous arguments supplied"
756 #######################################################################
757 # Setup and Export Internal variables
759 test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/
760 test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
761 test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
763 NANO_WORLDDIR=${NANO_OBJ}/_.w
764 NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
765 NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
767 if [ -d ${NANO_TOOLS} ] ; then
769 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
770 NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
772 echo "NANO_TOOLS directory does not exist" 1>&2
779 NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
782 export MAKEOBJDIRPREFIX
787 export NANO_CUSTOMIZE
793 export NANO_MAKE_CONF_BUILD
794 export NANO_MAKE_CONF_INSTALL
795 export NANO_MEDIASIZE
805 export NANO_BOOTLOADER
807 #######################################################################
808 # And then it is as simple as that...
810 # File descriptor 3 is used for logging output, see pprint
813 NANO_STARTTIME=`date +%s`
814 pprint 1 "NanoBSD image ${NANO_NAME} build starting"
820 pprint 2 "Using existing build tree (as instructed)"
825 pprint 2 "Skipping buildworld (as instructed)"
831 pprint 2 "Skipping buildkernel (as instructed)"
846 create_${NANO_ARCH}_diskimage
848 pprint 2 "Skipping image build (as instructed)"
852 pprint 1 "NanoBSD image ${NANO_NAME} completed"