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 # Late customize commands.
79 NANO_LATE_CUSTOMIZE=""
81 # Newfs paramters to use
82 NANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U"
84 # The drive name of the media at runtime
87 # Target media size in 512 bytes sectors
88 NANO_MEDIASIZE=1000000
90 # Number of code images on media (1 or 2)
93 # 0 -> Leave second image all zeroes so it compresses better.
94 # 1 -> Initialize second image with a copy of the first
97 # Size of code file system in 512 bytes sectors
98 # If zero, size will be as large as possible.
101 # Size of configuration file system in 512 bytes sectors
105 # Size of data file system in 512 bytes sectors
106 # If zero: no partition configured.
107 # If negative: max size possible
110 # Size of the /etc ramdisk in 512 bytes sectors
111 NANO_RAM_ETCSIZE=10240
113 # Size of the /tmp+/var ramdisk in 512 bytes sectors
114 NANO_RAM_TMPVARSIZE=10240
116 # Media geometry, only relevant if bios doesn't understand LBA.
120 # boot0 flags/options and configuration
121 NANO_BOOT0CFG="-o packet -s 1 -m 3"
122 NANO_BOOTLOADER="boot/boot0sio"
124 # Backing type of md(4) device
125 # Can be "file" or "swap"
126 NANO_MD_BACKING="file"
128 #######################################################################
129 # Not a variable at this time
133 #######################################################################
135 # The functions which do the real work.
136 # Can be overridden from the config file(s)
138 #######################################################################
141 echo "## Clean and create object directory (${MAKEOBJDIRPREFIX})"
143 if rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
146 chflags -R noschg ${MAKEOBJDIRPREFIX}
147 rm -rf ${MAKEOBJDIRPREFIX}
149 mkdir -p ${MAKEOBJDIRPREFIX}
150 printenv > ${MAKEOBJDIRPREFIX}/_.env
153 make_conf_build ( ) (
154 echo "## Construct build make.conf ($NANO_MAKE_CONF)"
156 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
157 echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF}
161 echo "## run buildworld"
162 echo "### log: ${MAKEOBJDIRPREFIX}/_.bw"
165 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} buildworld \
166 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
170 echo "## build kernel ($NANO_KERNEL)"
171 echo "### log: ${MAKEOBJDIRPREFIX}/_.bk"
173 if [ -f ${NANO_KERNEL} ] ; then
174 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
178 # unset these just in case to avoid compiler complaints
179 # when cross-building
181 unset TARGET_BIG_ENDIAN
182 ${NANO_PMAKE} buildkernel \
183 __MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
184 > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
189 echo "## Clean and create world directory (${NANO_WORLDDIR})"
190 if rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
193 chflags -R noschg ${NANO_WORLDDIR}/
194 rm -rf ${NANO_WORLDDIR}/
196 mkdir -p ${NANO_WORLDDIR}/
199 make_conf_install ( ) (
200 echo "## Construct install make.conf ($NANO_MAKE_CONF)"
202 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
203 echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF}
207 echo "## installworld"
208 echo "### log: ${MAKEOBJDIRPREFIX}/_.iw"
211 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} installworld \
212 DESTDIR=${NANO_WORLDDIR} \
213 > ${MAKEOBJDIRPREFIX}/_.iw 2>&1
214 chflags -R noschg ${NANO_WORLDDIR}
219 echo "## install /etc"
220 echo "### log: ${MAKEOBJDIRPREFIX}/_.etc"
223 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} distribution \
224 DESTDIR=${NANO_WORLDDIR} \
225 > ${MAKEOBJDIRPREFIX}/_.etc 2>&1
229 echo "## install kernel"
230 echo "### log: ${MAKEOBJDIRPREFIX}/_.ik"
233 ${NANO_PMAKE} installkernel \
234 DESTDIR=${NANO_WORLDDIR} \
235 __MAKE_CONF=${NANO_MAKE_CONF} KERNCONF=`basename ${NANO_KERNEL}` \
236 > ${MAKEOBJDIRPREFIX}/_.ik 2>&1
241 echo "## run customize scripts"
242 for c in $NANO_CUSTOMIZE
244 echo "## customize \"$c\""
245 echo "### log: ${MAKEOBJDIRPREFIX}/_.cust.$c"
247 ( $c ) > ${MAKEOBJDIRPREFIX}/_.cust.$c 2>&1
251 run_late_customize() (
253 echo "## run late customize scripts"
254 for c in $NANO_LATE_CUSTOMIZE
256 echo "## late customize \"$c\""
257 echo "### log: ${MAKEOBJDIRPREFIX}/_.late_cust.$c"
259 ( $c ) > ${MAKEOBJDIRPREFIX}/_.late_cust.$c 2>&1
264 echo "## configure nanobsd setup"
265 echo "### log: ${MAKEOBJDIRPREFIX}/_.dl"
270 # Move /usr/local/etc to /etc/local so that the /cfg stuff
271 # can stomp on it. Otherwise packages like ipsec-tools which
272 # have hardcoded paths under ${prefix}/etc are not tweakable.
273 if [ -d usr/local/etc ] ; then
277 find . -print | cpio -dumpl ../../../etc/local
280 ln -s ../../etc/local etc
286 # link /$d under /conf
287 # we use hard links so we have them both places.
288 # the files in /$d will be hidden by the mount.
289 # XXX: configure /$d ramdisk size
290 mkdir -p conf/base/$d conf/default/$d
291 find $d -print | cpio -dumpl conf/base/
294 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
295 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
297 # pick up config files from the special partition
298 echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
300 # Put /tmp on the /var ramdisk (could be symlink already)
305 ) > ${MAKEOBJDIRPREFIX}/_.dl 2>&1
308 setup_nanobsd_etc ( ) (
309 echo "## configure nanobsd /etc"
314 # create diskless marker file
317 # Make root filesystem R/O by default
318 echo "root_rw_mount=NO" >> etc/defaults/rc.conf
320 # save config file for scripts
321 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
323 echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
324 echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
331 # Remove all empty directories in /usr
332 find ${NANO_WORLDDIR}/usr -type d -depth -print |
335 rmdir $d > /dev/null 2>&1 || true
339 create_i386_diskimage ( ) (
340 echo "## build diskimage"
341 echo "### log: ${MAKEOBJDIRPREFIX}/_.di"
344 echo $NANO_MEDIASIZE $NANO_IMAGES \
345 $NANO_SECTS $NANO_HEADS \
346 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
351 # size of cylinder in sectors
354 # number of full cylinders on media
357 # output fdisk geometry spec, truncate cyls to 1023
359 print "g c" cyl " h" $4 " s" $3
361 print "g c" 1023 " h" $4 " s" $3
364 # size of data partition in full cylinders
365 dsl = int (($7 + cs - 1) / cs)
370 # size of config partition in full cylinders
371 csl = int (($6 + cs - 1) / cs)
374 # size of image partition(s) in full cylinders
375 isl = int ((cyl - dsl - csl) / $2)
377 isl = int (($5 + cs - 1) / cs)
380 # First image partition start at second track
381 print "p 1 165 " $3, isl * cs - $3
384 # Second image partition (if any) also starts offset one
385 # track to keep them identical.
387 print "p 2 165 " $3 + c, isl * cs - $3
391 # Config partition starts at cylinder boundary.
392 print "p 3 165 " c, csl * cs
395 # Data partition (if any) starts at cylinder boundary.
397 print "p 4 165 " c, dsl * cs
398 } else if ($7 < 0 && $1 > c) {
399 print "p 4 165 " c, $1 - c
401 print "Disk space overcommitted by", \
402 c - $1, "sectors" > "/dev/stderr"
406 ' > ${MAKEOBJDIRPREFIX}/_.fdisk
408 IMG=${NANO_DISKIMGDIR}/_.disk.full
409 MNT=${MAKEOBJDIRPREFIX}/_.mnt
412 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
413 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
416 echo "Creating md backing file..."
417 dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
418 count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
419 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
423 trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
425 fdisk -i -f ${MAKEOBJDIRPREFIX}/_.fdisk ${MD}
428 # XXX: pick up cached boot* files, they may not be in image anymore.
429 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
430 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
434 newfs ${NANO_NEWFS} /dev/${MD}s1a
435 mount /dev/${MD}s1a ${MNT}
437 echo "Copying worlddir..."
438 ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
440 echo "Generating mtree..."
441 ( cd ${MNT} && mtree -c ) > ${MAKEOBJDIRPREFIX}/_.mtree
442 ( cd ${MNT} && du -k ) > ${MAKEOBJDIRPREFIX}/_.du
445 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
446 # Duplicate to second image (if present)
447 echo "Duplicating to second image..."
448 dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
449 mount /dev/${MD}s2a ${MNT}
450 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
452 sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
458 # Create Config slice
459 newfs ${NANO_NEWFS} /dev/${MD}s3
460 # XXX: fill from where ?
462 # Create Data slice, if any.
463 if [ $NANO_DATASIZE -gt 0 ] ; then
464 newfs ${NANO_NEWFS} /dev/${MD}s4
465 # XXX: fill from where ?
468 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
469 echo "Writing out _.disk.full..."
470 dd if=/dev/${MD} of=${IMG} bs=64k
473 echo "Writing out _.disk.image..."
474 dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
476 ) > ${MAKEOBJDIRPREFIX}/_.di 2>&1
480 # Redefine this function with any last orders you may have
481 # after the build completed, for instance to copy the finished
482 # image to a more convenient place:
483 # cp ${MAKEOBJDIRPREFIX}/_.disk.image /home/ftp/pub/nanobsd.disk
486 #######################################################################
488 # Optional convenience functions.
490 #######################################################################
492 #######################################################################
493 # Common Flash device geometries
497 if [ -d ${NANO_TOOLS} ] ; then
498 . ${NANO_TOOLS}/FlashDevice.sub
500 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
502 sub_FlashDevice $1 $2
506 #######################################################################
507 # Setup serial console
510 # Enable getty on console
511 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
513 # Disable getty on syscons devices
514 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys
516 # Tell loader to use serial console early.
517 echo " -h" > ${NANO_WORLDDIR}/boot.config
520 #######################################################################
521 # Allow root login via ssh
523 cust_allow_ssh_root () (
524 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
525 ${NANO_WORLDDIR}/etc/ssh/sshd_config
528 #######################################################################
529 # Install the stuff under ./Files
531 cust_install_files () (
532 cd ${NANO_TOOLS}/Files
533 find . -print | grep -v /CVS | cpio -dumpv ${NANO_WORLDDIR}
536 #######################################################################
537 # Install packages from ${NANO_PACKAGE_DIR}
541 # Copy packages into chroot
542 mkdir -p ${NANO_WORLDDIR}/Pkg
543 cp ${NANO_PACKAGE_DIR}/* ${NANO_WORLDDIR}/Pkg
545 # Count & report how many we have to install
546 todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
547 echo "=== TODO: $todo"
548 ls ${NANO_WORLDDIR}/Pkg
552 # Record how many we have now
553 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
555 # Attempt to install more packages
556 # ...but no more than 200 at a time due to pkg_add's internal
558 chroot ${NANO_WORLDDIR} sh -c \
559 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
561 # See what that got us
562 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
564 ls ${NANO_WORLDDIR}/var/db/pkg
568 if [ $now -eq $todo ] ; then
569 echo "DONE $now packages"
571 elif [ $now -eq $have ] ; then
572 echo "FAILED: Nothing happened on this pass"
576 rm -rf ${NANO_WORLDDIR}/Pkg
579 #######################################################################
580 # Convenience function:
581 # Register $1 as customize function.
584 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
587 #######################################################################
588 # Convenience function:
589 # Register $1 as late customize function to run just before
592 late_customize_cmd () {
593 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1"
596 #######################################################################
598 # All set up to go...
600 #######################################################################
604 echo "Usage: $0 [-b/-k/-w] [-c config_file]"
605 echo " -b suppress builds (both kernel and world)"
606 echo " -k suppress buildkernel"
607 echo " -w suppress buildworld"
608 echo " -i suppress disk image build"
609 echo " -c specify config file"
614 #######################################################################
622 args=`getopt bc:hkwi $*`
623 if [ $? -ne 0 ] ; then
664 if [ $# -gt 0 ] ; then
665 echo "$0: Extraneous arguments supplied"
669 #######################################################################
670 # Setup and Export Internal variables
672 if [ "x${NANO_OBJ}" = "x" ] ; then
673 MAKEOBJDIRPREFIX=/usr/obj/nanobsd.${NANO_NAME}/
674 NANO_OBJ=${MAKEOBJDIRPREFIX}
676 MAKEOBJDIRPREFIX=${NANO_OBJ}
679 if [ "x${NANO_DISKIMGDIR}" = "x" ] ; then
680 NANO_DISKIMGDIR=${MAKEOBJDIRPREFIX}
683 NANO_WORLDDIR=${MAKEOBJDIRPREFIX}/_.w
684 NANO_MAKE_CONF=${MAKEOBJDIRPREFIX}/make.conf
686 if [ -d ${NANO_TOOLS} ] ; then
688 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
689 NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
691 echo "NANO_TOOLS directory does not exist" 1>&2
695 export MAKEOBJDIRPREFIX
700 export NANO_CUSTOMIZE
705 export NANO_MAKE_CONF
706 export NANO_MEDIASIZE
716 export NANO_BOOTLOADER
718 #######################################################################
719 # And then it is as simple as that...
726 echo "## Skipping buildworld (as instructed)"
732 echo "## Skipping buildkernel (as instructed)"
747 create_${NANO_ARCH}_diskimage
749 echo "## Skipping image build (as instructed)"
753 echo "# NanoBSD image completed"