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