]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - tools/tools/nanobsd/nanobsd.sh
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / tools / tools / nanobsd / nanobsd.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Poul-Henning Kamp.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
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.
14 #
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
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28 #
29
30 set -e
31
32 #######################################################################
33 #
34 # Setup default values for all controlling variables.
35 # These values can be overridden from the config file(s)
36 #
37 #######################################################################
38
39 # Name of this NanoBSD build.  (Used to construct workdir names)
40 NANO_NAME=full
41
42 # Source tree directory
43 NANO_SRC=/usr/src
44
45 # Where nanobsd additional files live under the source tree
46 NANO_TOOLS=tools/tools/nanobsd
47
48 # Where cust_pkg() finds packages to install
49 NANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
50 NANO_PACKAGE_LIST="*"
51
52 # Object tree directory
53 # default is subdir of /usr/obj
54 # XXX: MAKEOBJDIRPREFIX handling... ?
55 #NANO_OBJ=""
56
57 # The directory to put the final images
58 # default is ${NANO_OBJ}
59 #NANO_DISKIMGDIR=""
60
61 # Parallel Make
62 NANO_PMAKE="make -j 3"
63
64 # The default name for any image we create.
65 NANO_IMGNAME="_.disk.full"
66
67 # Options to put in make.conf during buildworld only
68 CONF_BUILD=' '
69
70 # Options to put in make.conf during installworld only
71 CONF_INSTALL=' '
72
73 # Options to put in make.conf during both build- & installworld.
74 CONF_WORLD=' '
75
76 # Kernel config file to use
77 NANO_KERNEL=GENERIC
78
79 # Customize commands.
80 NANO_CUSTOMIZE=""
81
82 # Late customize commands.
83 NANO_LATE_CUSTOMIZE=""
84
85 # Newfs paramters to use
86 NANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U"
87
88 # The drive name of the media at runtime
89 NANO_DRIVE=ad0
90
91 # Target media size in 512 bytes sectors
92 NANO_MEDIASIZE=1200000
93
94 # Number of code images on media (1 or 2)
95 NANO_IMAGES=2
96
97 # 0 -> Leave second image all zeroes so it compresses better.
98 # 1 -> Initialize second image with a copy of the first
99 NANO_INIT_IMG2=1
100
101 # Size of code file system in 512 bytes sectors
102 # If zero, size will be as large as possible.
103 NANO_CODESIZE=0
104
105 # Size of configuration file system in 512 bytes sectors
106 # Cannot be zero.
107 NANO_CONFSIZE=2048
108
109 # Size of data file system in 512 bytes sectors
110 # If zero: no partition configured.
111 # If negative: max size possible
112 NANO_DATASIZE=0
113
114 # Size of the /etc ramdisk in 512 bytes sectors
115 NANO_RAM_ETCSIZE=10240
116
117 # Size of the /tmp+/var ramdisk in 512 bytes sectors
118 NANO_RAM_TMPVARSIZE=10240
119
120 # Media geometry, only relevant if bios doesn't understand LBA.
121 NANO_SECTS=63
122 NANO_HEADS=16
123
124 # boot0 flags/options and configuration
125 NANO_BOOT0CFG="-o packet -s 1 -m 3"
126 NANO_BOOTLOADER="boot/boot0sio"
127
128 # Backing type of md(4) device
129 # Can be "file" or "swap"
130 NANO_MD_BACKING="file"
131
132 # Progress Print level
133 PPLEVEL=3
134
135 #######################################################################
136 # Not a variable at this time
137
138 NANO_ARCH=i386
139
140 #######################################################################
141 #
142 # The functions which do the real work.
143 # Can be overridden from the config file(s)
144 #
145 #######################################################################
146
147 clean_build ( ) (
148         pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
149
150         if rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
151                 true
152         else
153                 chflags -R noschg ${MAKEOBJDIRPREFIX}
154                 rm -rf ${MAKEOBJDIRPREFIX}
155         fi
156         mkdir -p ${MAKEOBJDIRPREFIX}
157         printenv > ${MAKEOBJDIRPREFIX}/_.env
158 )
159
160 make_conf_build ( ) (
161         pprint 2 "Construct build make.conf ($NANO_MAKE_CONF)"
162
163         echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
164         echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF}
165 )
166
167 build_world ( ) (
168         pprint 2 "run buildworld"
169         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
170
171         cd ${NANO_SRC}
172         env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
173                 __MAKE_CONF=${NANO_MAKE_CONF} buildworld \
174                 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
175 )
176
177 build_kernel ( ) (
178         pprint 2 "build kernel ($NANO_KERNEL)"
179         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
180
181         if [ -f ${NANO_KERNEL} ] ; then
182                 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
183         fi
184
185         (cd ${NANO_SRC};
186         # unset these just in case to avoid compiler complaints
187         # when cross-building
188         unset TARGET_CPUTYPE
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
193         )
194 )
195
196 clean_world ( ) (
197         pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
198         if rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
199                 true
200         else
201                 chflags -R noschg ${NANO_WORLDDIR}/
202                 rm -rf ${NANO_WORLDDIR}/
203         fi
204         mkdir -p ${NANO_WORLDDIR}/
205 )
206
207 make_conf_install ( ) (
208         pprint 2 "Construct install make.conf ($NANO_MAKE_CONF)"
209
210         echo "${CONF_WORLD}" > ${NANO_MAKE_CONF}
211         echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF}
212 )
213
214 install_world ( ) (
215         pprint 2 "installworld"
216         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.iw"
217
218         cd ${NANO_SRC}
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}
224 )
225
226 install_etc ( ) (
227
228         pprint 2 "install /etc"
229         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.etc"
230
231         cd ${NANO_SRC}
232         env TARGET_ARCH=${NANO_ARCH} \
233         ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} distribution \
234                 DESTDIR=${NANO_WORLDDIR} \
235                 > ${MAKEOBJDIRPREFIX}/_.etc 2>&1
236 )
237
238 install_kernel ( ) (
239         pprint 2 "install kernel"
240         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.ik"
241
242         cd ${NANO_SRC}
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
247 )
248
249 run_customize() (
250
251         pprint 2 "run customize scripts"
252         for c in $NANO_CUSTOMIZE
253         do
254                 pprint 2 "customize \"$c\""
255                 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.cust.$c"
256                 pprint 4 "`type $c`"
257                 ( $c ) > ${MAKEOBJDIRPREFIX}/_.cust.$c 2>&1
258         done
259 )
260
261 run_late_customize() (
262
263         pprint 2 "run late customize scripts"
264         for c in $NANO_LATE_CUSTOMIZE
265         do
266                 pprint 2 "late customize \"$c\""
267                 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.late_cust.$c"
268                 pprint 4 "`type $c`"
269                 ( $c ) > ${MAKEOBJDIRPREFIX}/_.late_cust.$c 2>&1
270         done
271 )
272
273 setup_nanobsd ( ) (
274         pprint 2 "configure nanobsd setup"
275         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.dl"
276
277         (
278         cd ${NANO_WORLDDIR}
279
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
284                 (
285                 mkdir -p etc/local
286                 cd usr/local/etc
287                 find . -print | cpio -dumpl ../../../etc/local
288                 cd ..
289                 rm -rf etc
290                 ln -s ../../etc/local etc
291                 )
292         fi
293
294         for d in var etc
295         do
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/
302         done
303
304         echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
305         echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
306
307         # pick up config files from the special partition
308         echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
309
310         # Put /tmp on the /var ramdisk (could be symlink already)
311         rmdir tmp || true
312         rm tmp || true
313         ln -s var/tmp tmp
314
315         ) > ${MAKEOBJDIRPREFIX}/_.dl 2>&1
316 )
317
318 setup_nanobsd_etc ( ) (
319         pprint 2 "configure nanobsd /etc"
320
321         (
322         cd ${NANO_WORLDDIR}
323
324         # create diskless marker file
325         touch etc/diskless
326
327         # Make root filesystem R/O by default
328         echo "root_rw_mount=NO" >> etc/defaults/rc.conf
329
330         # save config file for scripts
331         echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
332
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
335         mkdir -p cfg
336         )
337 )
338
339 prune_usr() (
340
341         # Remove all empty directories in /usr 
342         find ${NANO_WORLDDIR}/usr -type d -depth -print |
343                 while read d
344                 do
345                         rmdir $d > /dev/null 2>&1 || true 
346                 done
347 )
348
349 create_i386_diskimage ( ) (
350         pprint 2 "build diskimage"
351         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.di"
352
353         (
354         echo $NANO_MEDIASIZE $NANO_IMAGES \
355                 $NANO_SECTS $NANO_HEADS \
356                 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
357         awk '
358         {
359                 printf "# %s\n", $0
360
361                 # size of cylinder in sectors
362                 cs = $3 * $4
363
364                 # number of full cylinders on media
365                 cyl = int ($1 / cs)
366
367                 # output fdisk geometry spec, truncate cyls to 1023
368                 if (cyl <= 1023)
369                         print "g c" cyl " h" $4 " s" $3
370                 else
371                         print "g c" 1023 " h" $4 " s" $3
372
373                 if ($7 > 0) { 
374                         # size of data partition in full cylinders
375                         dsl = int (($7 + cs - 1) / cs)
376                 } else {
377                         dsl = 0;
378                 }
379
380                 # size of config partition in full cylinders
381                 csl = int (($6 + cs - 1) / cs)
382
383                 if ($5 == 0) {
384                         # size of image partition(s) in full cylinders
385                         isl = int ((cyl - dsl - csl) / $2)
386                 } else {
387                         isl = int (($5 + cs - 1) / cs)
388                 }
389
390                 # First image partition start at second track
391                 print "p 1 165 " $3, isl * cs - $3
392                 c = isl * cs;
393
394                 # Second image partition (if any) also starts offset one 
395                 # track to keep them identical.
396                 if ($2 > 1) {
397                         print "p 2 165 " $3 + c, isl * cs - $3
398                         c += isl * cs;
399                 }
400
401                 # Config partition starts at cylinder boundary.
402                 print "p 3 165 " c, csl * cs
403                 c += csl * cs
404
405                 # Data partition (if any) starts at cylinder boundary.
406                 if ($7 > 0) {
407                         print "p 4 165 " c, dsl * cs
408                 } else if ($7 < 0 && $1 > c) {
409                         print "p 4 165 " c, $1 - c
410                 } else if ($1 < c) {
411                         print "Disk space overcommitted by", \
412                             c - $1, "sectors" > "/dev/stderr"
413                         exit 2
414                 }
415
416                 # Force slice 1 to be marked active. This is necessary
417                 # for booting the image from a USB device to work.
418                 print "a 1"
419         }
420         ' > ${MAKEOBJDIRPREFIX}/_.fdisk
421
422         IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
423         MNT=${MAKEOBJDIRPREFIX}/_.mnt
424         mkdir -p ${MNT}
425
426         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
427                 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
428                         -y ${NANO_HEADS}`
429         else
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} \
434                         -y ${NANO_HEADS}`
435         fi
436
437         trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
438
439         fdisk -i -f ${MAKEOBJDIRPREFIX}/_.fdisk ${MD}
440         fdisk ${MD}
441         # XXX: params
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
445         bsdlabel ${MD}s1
446
447         # Create first image
448         newfs ${NANO_NEWFS} /dev/${MD}s1a
449         mount /dev/${MD}s1a ${MNT}
450         df -i ${MNT}
451         echo "Copying worlddir..."
452         ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
453         df -i ${MNT}
454         echo "Generating mtree..."
455         ( cd ${MNT} && mtree -c ) > ${MAKEOBJDIRPREFIX}/_.mtree
456         ( cd ${MNT} && du -k ) > ${MAKEOBJDIRPREFIX}/_.du
457         umount ${MNT}
458
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
465                 do
466                         sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f
467                 done
468                 umount ${MNT}
469
470         fi
471         
472         # Create Config slice
473         newfs ${NANO_NEWFS} /dev/${MD}s3
474         # XXX: fill from where ?
475
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 ?
480         fi
481
482         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
483                 echo "Writing out _.disk.full..."
484                 dd if=/dev/${MD} of=${IMG} bs=64k
485         fi
486
487         echo "Writing out _.disk.image..."
488         dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
489         mdconfig -d -u $MD
490         ) > ${MAKEOBJDIRPREFIX}/_.di 2>&1
491 )
492
493 last_orders () (
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
498 )
499
500 #######################################################################
501 #
502 # Optional convenience functions.
503 #
504 #######################################################################
505
506 #######################################################################
507 # Common Flash device geometries
508 #
509
510 FlashDevice () {
511         if [ -d ${NANO_TOOLS} ] ; then
512                 . ${NANO_TOOLS}/FlashDevice.sub
513         else
514                 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
515         fi
516         sub_FlashDevice $1 $2
517 }
518
519 #######################################################################
520 # USB device geometries
521 #
522 # Usage:
523 #       UsbDevice Generic 1000  # a generic flash key sold as having 1GB
524 #
525 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
526 #
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.
530 #
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.
535 #
536 # The generic-hdd device is preferred for flash devices larger than 1GB.
537 #
538
539 UsbDevice () {
540         a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
541         case $a1 in
542         generic-fdd)
543                 NANO_HEADS=64
544                 NANO_SECTS=32
545                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
546                 ;;
547         generic|generic-hdd)
548                 NANO_HEADS=255
549                 NANO_SECTS=63
550                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
551                 ;;
552         *)
553                 echo "Unknown USB flash device"
554                 exit 2
555                 ;;
556         esac
557 }
558
559 #######################################################################
560 # Setup serial console
561
562 cust_comconsole () (
563         # Enable getty on console
564         sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
565
566         # Disable getty on syscons devices
567         sed -i "" -e '/^ttyv[0-8]/s/    on/     off/' ${NANO_WORLDDIR}/etc/ttys
568
569         # Tell loader to use serial console early.
570         echo " -h" > ${NANO_WORLDDIR}/boot.config
571 )
572
573 #######################################################################
574 # Allow root login via ssh
575
576 cust_allow_ssh_root () (
577         sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
578             ${NANO_WORLDDIR}/etc/ssh/sshd_config
579 )
580
581 #######################################################################
582 # Install the stuff under ./Files
583
584 cust_install_files () (
585         cd ${NANO_TOOLS}/Files
586         find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
587 )
588
589 #######################################################################
590 # Install packages from ${NANO_PACKAGE_DIR}
591
592 cust_pkg () (
593
594         # Copy packages into chroot
595         mkdir -p ${NANO_WORLDDIR}/Pkg
596         (
597                 cd ${NANO_PACKAGE_DIR}
598                 find ${NANO_PACKAGE_LIST} -print |
599                     cpio -dumpv ${NANO_WORLDDIR}/Pkg
600         )
601
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
606         echo "==="
607         while true
608         do
609                 # Record how many we have now
610                 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
611
612                 # Attempt to install more packages
613                 # ...but no more than 200 at a time due to pkg_add's internal
614                 # limitations.
615                 chroot ${NANO_WORLDDIR} sh -c \
616                         'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
617
618                 # See what that got us
619                 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
620                 echo "=== NOW $now"
621                 ls ${NANO_WORLDDIR}/var/db/pkg
622                 echo "==="
623
624
625                 if [ $now -eq $todo ] ; then
626                         echo "DONE $now packages"
627                         break
628                 elif [ $now -eq $have ] ; then
629                         echo "FAILED: Nothing happened on this pass"
630                         exit 2
631                 fi
632         done
633         rm -rf ${NANO_WORLDDIR}/Pkg
634 )
635
636 #######################################################################
637 # Convenience function:
638 #       Register $1 as customize function.
639
640 customize_cmd () {
641         NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1"
642 }
643
644 #######################################################################
645 # Convenience function:
646 #       Register $1 as late customize function to run just before
647 #       image creation.
648
649 late_customize_cmd () {
650         NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1"
651 }
652
653 #######################################################################
654 #
655 # All set up to go...
656 #
657 #######################################################################
658
659 # Progress Print
660 #       Print $2 at level $1.
661 pprint() {
662     if [ "$1" -le $PPLEVEL ]; then
663         printf "%.${1}s %s\n" "#####" "$2"
664     fi
665 }
666
667 usage () {
668         (
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"
678         ) 1>&2
679         exit 2
680 }
681
682 #######################################################################
683 # Parse arguments
684
685 do_clean=true
686 do_kernel=true
687 do_world=true
688 do_image=true
689
690 set +e
691 args=`getopt bc:hiknqvw $*`
692 if [ $? -ne 0 ] ; then
693         usage
694         exit 2
695 fi
696 set -e
697
698 set -- $args
699 for i
700 do
701         case "$i" 
702         in
703         -b)
704                 do_world=false
705                 do_kernel=false
706                 shift
707                 ;;
708         -k)
709                 do_kernel=false
710                 shift
711                 ;;
712         -c)
713                 . "$2"
714                 shift
715                 shift
716                 ;;
717         -h)
718                 usage
719                 ;;
720         -i)
721                 do_image=false
722                 shift
723                 ;;
724         -n)
725                 do_clean=false
726                 shift
727                 ;;
728         -q)
729                 PPLEVEL=$(($PPLEVEL - 1))
730                 shift
731                 ;;
732         -v)
733                 PPLEVEL=$(($PPLEVEL + 1))
734                 shift
735                 ;;
736         -w)
737                 do_world=false
738                 shift
739                 ;;
740         --)
741                 shift
742                 break
743         esac
744 done
745
746 if [ $# -gt 0 ] ; then
747         echo "$0: Extraneous arguments supplied"
748         usage
749 fi
750
751 #######################################################################
752 # Setup and Export Internal variables
753 #
754 if [ "x${NANO_OBJ}" = "x" ] ; then
755         MAKEOBJDIRPREFIX=/usr/obj/nanobsd.${NANO_NAME}/
756         NANO_OBJ=${MAKEOBJDIRPREFIX}
757 else
758         MAKEOBJDIRPREFIX=${NANO_OBJ}
759 fi
760
761 if [ "x${NANO_DISKIMGDIR}" = "x" ] ; then
762         NANO_DISKIMGDIR=${MAKEOBJDIRPREFIX}
763 fi
764
765 NANO_WORLDDIR=${MAKEOBJDIRPREFIX}/_.w
766 NANO_MAKE_CONF=${MAKEOBJDIRPREFIX}/make.conf
767
768 if [ -d ${NANO_TOOLS} ] ; then
769         true
770 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
771         NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
772 else
773         echo "NANO_TOOLS directory does not exist" 1>&2
774         exit 1
775 fi
776
777 if $do_clean ; then
778         true
779 else
780         NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
781 fi
782
783 export MAKEOBJDIRPREFIX
784
785 export NANO_ARCH
786 export NANO_CODESIZE
787 export NANO_CONFSIZE
788 export NANO_CUSTOMIZE
789 export NANO_DATASIZE
790 export NANO_DRIVE
791 export NANO_HEADS
792 export NANO_IMAGES
793 export NANO_IMGNAME
794 export NANO_MAKE_CONF
795 export NANO_MEDIASIZE
796 export NANO_NAME
797 export NANO_NEWFS
798 export NANO_OBJ
799 export NANO_PMAKE
800 export NANO_SECTS
801 export NANO_SRC
802 export NANO_TOOLS
803 export NANO_WORLDDIR
804 export NANO_BOOT0CFG
805 export NANO_BOOTLOADER
806
807 #######################################################################
808 # And then it is as simple as that...
809
810 pprint 1 "NanoBSD image ${NANO_NAME} build starting"
811
812 if $do_world ; then
813         if $do_clean ; then
814                 clean_build
815         else
816                 pprint 2 "Using existing build tree (as instructed)"
817         fi
818         make_conf_build
819         build_world
820 else
821         pprint 2 "Skipping buildworld (as instructed)"
822 fi
823
824 if $do_kernel ; then
825         build_kernel
826 else
827         pprint 2 "Skipping buildkernel (as instructed)"
828 fi
829
830 clean_world
831 make_conf_install
832 install_world
833 install_etc
834 setup_nanobsd_etc
835 install_kernel
836
837 run_customize
838 setup_nanobsd
839 prune_usr
840 run_late_customize
841 if $do_image ; then
842         create_${NANO_ARCH}_diskimage
843         echo "# Created NanoBSD disk image: ${MAKEOBJDIRPREFIX}/${NANO_IMGNAME}"
844 else
845         pprint 2 "Skipping image build (as instructed)"
846 fi
847 last_orders
848
849 pprint 1 "NanoBSD image ${NANO_NAME} completed"