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 # Options to put in make.conf during buildworld only
66 # Options to put in make.conf during installworld only
69 # Options to put in make.conf during both build- & installworld.
72 # Kernel config file to use
78 # Newfs paramters to use
79 NANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U"
81 # The drive name of the media at runtime
84 # Target media size in 512 bytes sectors
85 NANO_MEDIASIZE=1000000
87 # Number of code images on media (1 or 2)
90 # 0 -> Leave second image all zeroes so it compresses better.
91 # 1 -> Initialize second image with a copy of the first
94 # Size of code file system in 512 bytes sectors
95 # If zero, size will be as large as possible.
98 # Size of configuration file system in 512 bytes sectors
102 # Size of data file system in 512 bytes sectors
103 # If zero: no partition configured.
104 # If negative: max size possible
107 # Size of the /etc ramdisk in 512 bytes sectors
108 NANO_RAM_ETCSIZE=10240
110 # Size of the /tmp+/var ramdisk in 512 bytes sectors
111 NANO_RAM_TMPVARSIZE=10240
113 # Media geometry, only relevant if bios doesn't understand LBA.
117 # boot0 flags/options and configuration
118 NANO_BOOT0CFG="-o packet -s 1 -m 3"
119 NANO_BOOTLOADER="boot/boot0sio"
121 # Backing type of md(4) device
122 # Can be "file" or "swap"
123 NANO_MD_BACKING="file"
125 #######################################################################
126 # Not a variable at this time
130 #######################################################################
132 # The functions which do the real work.
133 # Can be overridden from the config file(s)
135 #######################################################################
138 echo "## Clean and create object directory (${MAKEOBJDIRPREFIX})"
140 if rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
143 chflags -R noschg ${MAKEOBJDIRPREFIX}
144 rm -rf ${MAKEOBJDIRPREFIX}
146 mkdir -p ${MAKEOBJDIRPREFIX}
147 printenv > ${MAKEOBJDIRPREFIX}/_.env
150 make_conf_build ( ) (
151 echo "## Construct build make.conf ($NANO_MAKE_CONF)"
153 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
154 echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF}
158 echo "## run buildworld"
159 echo "### log: ${MAKEOBJDIRPREFIX}/_.bw"
162 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} buildworld \
163 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
167 echo "## build kernel ($NANO_KERNEL)"
168 echo "### log: ${MAKEOBJDIRPREFIX}/_.bk"
170 if [ -f ${NANO_KERNEL} ] ; then
171 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
175 # unset these just in case to avoid compiler complaints
176 # when cross-building
178 unset TARGET_BIG_ENDIAN
179 ${NANO_PMAKE} buildkernel \
180 __MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
181 > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
186 echo "## Clean and create world directory (${NANO_WORLDDIR})"
187 if rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
190 chflags -R noschg ${NANO_WORLDDIR}/
191 rm -rf ${NANO_WORLDDIR}/
193 mkdir -p ${NANO_WORLDDIR}/
196 make_conf_install ( ) (
197 echo "## Construct install make.conf ($NANO_MAKE_CONF)"
199 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
200 echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF}
204 echo "## installworld"
205 echo "### log: ${MAKEOBJDIRPREFIX}/_.iw"
208 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} installworld \
209 DESTDIR=${NANO_WORLDDIR} \
210 > ${MAKEOBJDIRPREFIX}/_.iw 2>&1
211 chflags -R noschg ${NANO_WORLDDIR}
216 echo "## install /etc"
217 echo "### log: ${MAKEOBJDIRPREFIX}/_.etc"
220 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} distribution \
221 DESTDIR=${NANO_WORLDDIR} \
222 > ${MAKEOBJDIRPREFIX}/_.etc 2>&1
226 echo "## install kernel"
227 echo "### log: ${MAKEOBJDIRPREFIX}/_.ik"
230 ${NANO_PMAKE} installkernel \
231 DESTDIR=${NANO_WORLDDIR} \
232 __MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
233 > ${MAKEOBJDIRPREFIX}/_.ik 2>&1
238 echo "## run customize scripts"
239 for c in $NANO_CUSTOMIZE
241 echo "## customize \"$c\""
242 echo "### log: ${MAKEOBJDIRPREFIX}/_.cust.$c"
244 ( $c ) > ${MAKEOBJDIRPREFIX}/_.cust.$c 2>&1
249 echo "## configure nanobsd setup"
250 echo "### log: ${MAKEOBJDIRPREFIX}/_.dl"
255 # Move /usr/local/etc to /etc/local so that the /cfg stuff
256 # can stomp on it. Otherwise packages like ipsec-tools which
257 # have hardcoded paths under ${prefix}/etc are not tweakable.
258 if [ -d usr/local/etc ] ; then
262 find . -print | cpio -dumpl ../../../etc/local
265 ln -s ../../etc/local etc
271 # link /$d under /conf
272 # we use hard links so we have them both places.
273 # the files in /$d will be hidden by the mount.
274 # XXX: configure /$d ramdisk size
275 mkdir -p conf/base/$d conf/default/$d
276 find $d -print | cpio -dumpl conf/base/
279 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
280 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
282 # pick up config files from the special partition
283 echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
285 # Put /tmp on the /var ramdisk (could be symlink already)
290 ) > ${MAKEOBJDIRPREFIX}/_.dl 2>&1
293 setup_nanobsd_etc ( ) (
294 echo "## configure nanobsd /etc"
299 # create diskless marker file
302 # Make root filesystem R/O by default
303 echo "root_rw_mount=NO" >> etc/defaults/rc.conf
305 # save config file for scripts
306 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
308 echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
309 echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
316 # Remove all empty directories in /usr
317 find ${NANO_WORLDDIR}/usr -type d -depth -print |
320 rmdir $d > /dev/null 2>&1 || true
324 create_i386_diskimage ( ) (
325 echo "## build diskimage"
326 echo "### log: ${MAKEOBJDIRPREFIX}/_.di"
329 echo $NANO_MEDIASIZE $NANO_IMAGES \
330 $NANO_SECTS $NANO_HEADS \
331 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
336 # size of cylinder in sectors
339 # number of full cylinders on media
342 # output fdisk geometry spec, truncate cyls to 1023
344 print "g c" cyl " h" $4 " s" $3
346 print "g c" 1023 " h" $4 " s" $3
349 # size of data partition in full cylinders
350 dsl = int (($7 + cs - 1) / cs)
355 # size of config partition in full cylinders
356 csl = int (($6 + cs - 1) / cs)
359 # size of image partition(s) in full cylinders
360 isl = int ((cyl - dsl - csl) / $2)
362 isl = int (($5 + cs - 1) / cs)
365 # First image partition start at second track
366 print "p 1 165 " $3, isl * cs - $3
369 # Second image partition (if any) also starts offset one
370 # track to keep them identical.
372 print "p 2 165 " $3 + c, isl * cs - $3
376 # Config partition starts at cylinder boundary.
377 print "p 3 165 " c, csl * cs
380 # Data partition (if any) starts at cylinder boundary.
382 print "p 4 165 " c, dsl * cs
383 } else if ($7 < 0 && $1 > c) {
384 print "p 4 165 " c, $1 - c
386 print "Disk space overcommitted by", \
387 c - $1, "sectors" > "/dev/stderr"
391 ' > ${MAKEOBJDIRPREFIX}/_.fdisk
393 IMG=${NANO_DISKIMGDIR}/_.disk.full
394 MNT=${MAKEOBJDIRPREFIX}/_.mnt
397 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
398 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
401 echo "Creating md backing file..."
402 dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
403 count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
404 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
408 trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
410 fdisk -i -f ${MAKEOBJDIRPREFIX}/_.fdisk ${MD}
413 # XXX: pick up cached boot* files, they may not be in image anymore.
414 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
415 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
419 newfs ${NANO_NEWFS} /dev/${MD}s1a
420 mount /dev/${MD}s1a ${MNT}
422 echo "Copying worlddir..."
423 ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
425 echo "Generating mtree..."
426 ( cd ${MNT} && mtree -c ) > ${MAKEOBJDIRPREFIX}/_.mtree
427 ( cd ${MNT} && du -k ) > ${MAKEOBJDIRPREFIX}/_.du
430 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
431 # Duplicate to second image (if present)
432 echo "Duplicating to second image..."
433 dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
434 mount /dev/${MD}s2a ${MNT}
435 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
437 sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
443 # Create Config slice
444 newfs ${NANO_NEWFS} /dev/${MD}s3
445 # XXX: fill from where ?
447 # Create Data slice, if any.
448 if [ $NANO_DATASIZE -gt 0 ] ; then
449 newfs ${NANO_NEWFS} /dev/${MD}s4
450 # XXX: fill from where ?
453 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
454 echo "Writing out _.disk.full..."
455 dd if=/dev/${MD} of=${IMG} bs=64k
458 echo "Writing out _.disk.image..."
459 dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
461 ) > ${MAKEOBJDIRPREFIX}/_.di 2>&1
465 # Redefine this function with any last orders you may have
466 # after the build completed, for instance to copy the finished
467 # image to a more convenient place:
468 # cp ${MAKEOBJDIRPREFIX}/_.disk.image /home/ftp/pub/nanobsd.disk
471 #######################################################################
473 # Optional convenience functions.
475 #######################################################################
477 #######################################################################
478 # Common Flash device geometries
482 if [ -d ${NANO_TOOLS} ] ; then
483 . ${NANO_TOOLS}/FlashDevice.sub
485 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
487 sub_FlashDevice $1 $2
491 #######################################################################
492 # Setup serial console
495 # Enable getty on console
496 sed -i "" -e /ttyu0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
498 # Disable getty on syscons devices
499 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys
501 # Tell loader to use serial console early.
502 echo " -h" > ${NANO_WORLDDIR}/boot.config
505 #######################################################################
506 # Allow root login via ssh
508 cust_allow_ssh_root () (
509 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
510 ${NANO_WORLDDIR}/etc/ssh/sshd_config
513 #######################################################################
514 # Install the stuff under ./Files
516 cust_install_files () (
517 cd ${NANO_TOOLS}/Files
518 find . -print | grep -v /CVS | cpio -dumpv ${NANO_WORLDDIR}
521 #######################################################################
522 # Install packages from ${NANO_PACKAGE_DIR}
526 # Copy packages into chroot
527 mkdir -p ${NANO_WORLDDIR}/Pkg
528 cp ${NANO_PACKAGE_DIR}/* ${NANO_WORLDDIR}/Pkg
530 # Count & report how many we have to install
531 todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
532 echo "=== TODO: $todo"
533 ls ${NANO_WORLDDIR}/Pkg
537 # Record how may we have now
538 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
540 # Attempt to install more packages
541 # ...but no more than 200 at a time due to pkg_add's internal
543 chroot ${NANO_WORLDDIR} sh -c \
544 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
546 # See what that got us
547 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
549 ls ${NANO_WORLDDIR}/var/db/pkg
553 if [ $now -eq $todo ] ; then
554 echo "DONE $now packages"
556 elif [ $now -eq $have ] ; then
557 echo "FAILED: Nothing happened on this pass"
561 rm -rf ${NANO_WORLDDIR}/Pkg
564 #######################################################################
565 # Convenience function:
566 # Register $1 as customize function.
569 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
572 #######################################################################
574 # All set up to go...
576 #######################################################################
580 echo "Usage: $0 [-b/-k/-w] [-c config_file]"
581 echo " -b suppress builds (both kernel and world)"
582 echo " -k suppress buildkernel"
583 echo " -w suppress buildworld"
584 echo " -c specify config file"
589 #######################################################################
596 args=`getopt bc:hkw $*`
597 if [ $? -ne 0 ] ; then
635 if [ $# -gt 0 ] ; then
636 echo "$0: Extraneous arguments supplied"
640 #######################################################################
641 # Setup and Export Internal variables
643 if [ "x${NANO_OBJ}" = "x" ] ; then
644 MAKEOBJDIRPREFIX=/usr/obj/nanobsd.${NANO_NAME}/
645 NANO_OBJ=${MAKEOBJDIRPREFIX}
647 MAKEOBJDIRPREFIX=${NANO_OBJ}
650 if [ "x${NANO_DISKIMGDIR}" = "x" ] ; then
651 NANO_DISKIMGDIR=${MAKEOBJDIRPREFIX}
654 NANO_WORLDDIR=${MAKEOBJDIRPREFIX}/_.w
655 NANO_MAKE_CONF=${MAKEOBJDIRPREFIX}/make.conf
657 if [ -d ${NANO_TOOLS} ] ; then
659 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
660 NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
662 echo "NANO_TOOLS directory does not exist" 1>&2
666 export MAKEOBJDIRPREFIX
671 export NANO_CUSTOMIZE
676 export NANO_MAKE_CONF
677 export NANO_MEDIASIZE
687 export NANO_BOOTLOADER
689 #######################################################################
690 # And then it is as simple as that...
697 echo "## Skipping buildworld (as instructed)"
703 echo "## Skipping buildkernel (as instructed)"
716 create_${NANO_ARCH}_diskimage
719 echo "# NanoBSD image completed"