]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - tools/tools/nanobsd/nanobsd.sh
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.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 #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 # boot2 flags/options
128 # default force serial console
129 NANO_BOOT2CFG="-h"
130
131 # Backing type of md(4) device
132 # Can be "file" or "swap"
133 NANO_MD_BACKING="file"
134
135 # Progress Print level
136 PPLEVEL=3
137
138 #######################################################################
139 # Architecture to build.  Corresponds to TARGET_ARCH in a buildworld.
140 # Unfortunately, there's no way to set TARGET at this time, and it 
141 # conflates the two, so architectures where TARGET != TARGET_ARCH do
142 # not work.  This defaults to the arch of the current machine.
143
144 NANO_ARCH=`uname -p`
145
146 #######################################################################
147 #
148 # The functions which do the real work.
149 # Can be overridden from the config file(s)
150 #
151 #######################################################################
152
153 clean_build ( ) (
154         pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
155
156         if ! rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
157                 chflags -R noschg ${MAKEOBJDIRPREFIX}
158                 rm -r ${MAKEOBJDIRPREFIX}
159         fi
160         mkdir -p ${MAKEOBJDIRPREFIX}
161         printenv > ${MAKEOBJDIRPREFIX}/_.env
162 )
163
164 make_conf_build ( ) (
165         pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
166
167         echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
168         echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
169 )
170
171 build_world ( ) (
172         pprint 2 "run buildworld"
173         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
174
175         cd ${NANO_SRC}
176         env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
177                 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
178                 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
179 )
180
181 build_kernel ( ) (
182         pprint 2 "build kernel ($NANO_KERNEL)"
183         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
184
185         if [ -f ${NANO_KERNEL} ] ; then
186                 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf
187         fi
188
189         (cd ${NANO_SRC};
190         # unset these just in case to avoid compiler complaints
191         # when cross-building
192         unset TARGET_CPUTYPE
193         unset TARGET_BIG_ENDIAN
194         env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
195                 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} KERNCONF=`basename ${NANO_KERNEL}` \
196                 > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
197         )
198 )
199
200 clean_world ( ) (
201         if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
202                 pprint 2 "Clean and create object directory (${NANO_OBJ})"
203                 if ! rm -rf ${NANO_OBJ} > /dev/null 2>&1 ; then
204                         chflags -R noschg ${NANO_OBJ}
205                         rm -r ${NANO_OBJ}
206                 fi
207                 mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
208                 printenv > ${NANO_OBJ}/_.env
209         else
210                 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
211                 if ! rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
212                         chflags -R noschg ${NANO_WORLDDIR}
213                         rm -rf ${NANO_WORLDDIR}
214                 fi
215                 mkdir -p ${NANO_WORLDDIR}
216         fi
217 )
218
219 make_conf_install ( ) (
220         pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
221
222         echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
223         echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
224 )
225
226 install_world ( ) (
227         pprint 2 "installworld"
228         pprint 3 "log: ${NANO_OBJ}/_.iw"
229
230         cd ${NANO_SRC}
231         env TARGET_ARCH=${NANO_ARCH} \
232         ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
233                 DESTDIR=${NANO_WORLDDIR} \
234                 > ${NANO_OBJ}/_.iw 2>&1
235         chflags -R noschg ${NANO_WORLDDIR}
236 )
237
238 install_etc ( ) (
239
240         pprint 2 "install /etc"
241         pprint 3 "log: ${NANO_OBJ}/_.etc"
242
243         cd ${NANO_SRC}
244         env TARGET_ARCH=${NANO_ARCH} \
245         ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
246                 DESTDIR=${NANO_WORLDDIR} \
247                 > ${NANO_OBJ}/_.etc 2>&1
248         # make.conf doesn't get created by default, but some ports need it
249         # so they can spam it.
250         cp /dev/null ${NANO_WORLDDIR}/etc/make.conf
251 )
252
253 install_kernel ( ) (
254         pprint 2 "install kernel"
255         pprint 3 "log: ${NANO_OBJ}/_.ik"
256
257         cd ${NANO_SRC}
258         env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \
259                 DESTDIR=${NANO_WORLDDIR} \
260                 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} KERNCONF=`basename ${NANO_KERNEL}` \
261                 > ${NANO_OBJ}/_.ik 2>&1
262 )
263
264 run_customize() (
265
266         pprint 2 "run customize scripts"
267         for c in $NANO_CUSTOMIZE
268         do
269                 pprint 2 "customize \"$c\""
270                 pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
271                 pprint 4 "`type $c`"
272                 ( $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
273         done
274 )
275
276 run_late_customize() (
277
278         pprint 2 "run late customize scripts"
279         for c in $NANO_LATE_CUSTOMIZE
280         do
281                 pprint 2 "late customize \"$c\""
282                 pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
283                 pprint 4 "`type $c`"
284                 ( $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
285         done
286 )
287
288 setup_nanobsd ( ) (
289         pprint 2 "configure nanobsd setup"
290         pprint 3 "log: ${NANO_OBJ}/_.dl"
291
292         (
293         cd ${NANO_WORLDDIR}
294
295         # Move /usr/local/etc to /etc/local so that the /cfg stuff
296         # can stomp on it.  Otherwise packages like ipsec-tools which
297         # have hardcoded paths under ${prefix}/etc are not tweakable.
298         if [ -d usr/local/etc ] ; then
299                 (
300                 mkdir -p etc/local
301                 cd usr/local/etc
302                 find . -print | cpio -dumpl ../../../etc/local
303                 cd ..
304                 rm -rf etc
305                 ln -s ../../etc/local etc
306                 )
307         fi
308
309         for d in var etc
310         do
311                 # link /$d under /conf
312                 # we use hard links so we have them both places.
313                 # the files in /$d will be hidden by the mount.
314                 # XXX: configure /$d ramdisk size
315                 mkdir -p conf/base/$d conf/default/$d
316                 find $d -print | cpio -dumpl conf/base/
317         done
318
319         echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
320         echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
321
322         # pick up config files from the special partition
323         echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
324
325         # Put /tmp on the /var ramdisk (could be symlink already)
326         rmdir tmp || true
327         rm tmp || true
328         ln -s var/tmp tmp
329
330         ) > ${NANO_OBJ}/_.dl 2>&1
331 )
332
333 setup_nanobsd_etc ( ) (
334         pprint 2 "configure nanobsd /etc"
335
336         (
337         cd ${NANO_WORLDDIR}
338
339         # create diskless marker file
340         touch etc/diskless
341
342         # Make root filesystem R/O by default
343         echo "root_rw_mount=NO" >> etc/defaults/rc.conf
344
345         # save config file for scripts
346         echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
347
348         echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
349         echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
350         mkdir -p cfg
351         )
352 )
353
354 prune_usr() (
355
356         # Remove all empty directories in /usr 
357         find ${NANO_WORLDDIR}/usr -type d -depth -print |
358                 while read d
359                 do
360                         rmdir $d > /dev/null 2>&1 || true 
361                 done
362 )
363
364 populate_slice ( ) (
365         local dev dir mnt
366         dev=$1
367         dir=$2
368         mnt=$3
369         test -z $2 && dir=/var/empty
370         test -d $d || dir=/var/empty
371         echo "Creating ${dev} with ${dir} (mounting on ${mnt})"
372         newfs ${NANO_NEWFS} ${dev}
373         mount ${dev} ${mnt}
374         cd ${dir}
375         find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${mnt}
376         df -i ${mnt}
377         umount ${mnt}
378 )
379
380 populate_cfg_slice ( ) (
381         populate_slice "$1" "$2" "$3"
382 )
383
384 populate_data_slice ( ) (
385         populate_slice "$1" "$2" "$3"
386 )
387
388 create_i386_diskimage ( ) (
389         pprint 2 "build diskimage"
390         pprint 3 "log: ${NANO_OBJ}/_.di"
391
392         (
393         echo $NANO_MEDIASIZE $NANO_IMAGES \
394                 $NANO_SECTS $NANO_HEADS \
395                 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
396         awk '
397         {
398                 printf "# %s\n", $0
399
400                 # size of cylinder in sectors
401                 cs = $3 * $4
402
403                 # number of full cylinders on media
404                 cyl = int ($1 / cs)
405
406                 # output fdisk geometry spec, truncate cyls to 1023
407                 if (cyl <= 1023)
408                         print "g c" cyl " h" $4 " s" $3
409                 else
410                         print "g c" 1023 " h" $4 " s" $3
411
412                 if ($7 > 0) { 
413                         # size of data partition in full cylinders
414                         dsl = int (($7 + cs - 1) / cs)
415                 } else {
416                         dsl = 0;
417                 }
418
419                 # size of config partition in full cylinders
420                 csl = int (($6 + cs - 1) / cs)
421
422                 if ($5 == 0) {
423                         # size of image partition(s) in full cylinders
424                         isl = int ((cyl - dsl - csl) / $2)
425                 } else {
426                         isl = int (($5 + cs - 1) / cs)
427                 }
428
429                 # First image partition start at second track
430                 print "p 1 165 " $3, isl * cs - $3
431                 c = isl * cs;
432
433                 # Second image partition (if any) also starts offset one 
434                 # track to keep them identical.
435                 if ($2 > 1) {
436                         print "p 2 165 " $3 + c, isl * cs - $3
437                         c += isl * cs;
438                 }
439
440                 # Config partition starts at cylinder boundary.
441                 print "p 3 165 " c, csl * cs
442                 c += csl * cs
443
444                 # Data partition (if any) starts at cylinder boundary.
445                 if ($7 > 0) {
446                         print "p 4 165 " c, dsl * cs
447                 } else if ($7 < 0 && $1 > c) {
448                         print "p 4 165 " c, $1 - c
449                 } else if ($1 < c) {
450                         print "Disk space overcommitted by", \
451                             c - $1, "sectors" > "/dev/stderr"
452                         exit 2
453                 }
454
455                 # Force slice 1 to be marked active. This is necessary
456                 # for booting the image from a USB device to work.
457                 print "a 1"
458         }
459         ' > ${NANO_OBJ}/_.fdisk
460
461         IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
462         MNT=${NANO_OBJ}/_.mnt
463         mkdir -p ${MNT}
464
465         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
466                 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
467                         -y ${NANO_HEADS}`
468         else
469                 echo "Creating md backing file..."
470                 dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \
471                         count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}`
472                 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
473                         -y ${NANO_HEADS}`
474         fi
475
476         trap "echo 'Running exit trap code' ; df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
477
478         fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
479         fdisk ${MD}
480         # XXX: params
481         # XXX: pick up cached boot* files, they may not be in image anymore.
482         boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
483         bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
484         bsdlabel ${MD}s1
485
486         # Create first image
487         # XXX: should use populate_slice for easier override
488         newfs ${NANO_NEWFS} /dev/${MD}s1a
489         mount /dev/${MD}s1a ${MNT}
490         df -i ${MNT}
491         echo "Copying worlddir..."
492         ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} )
493         df -i ${MNT}
494         echo "Generating mtree..."
495         ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
496         ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
497         umount ${MNT}
498
499         if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
500                 # Duplicate to second image (if present)
501                 echo "Duplicating to second image..."
502                 dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
503                 mount /dev/${MD}s2a ${MNT}
504                 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
505                 do
506                         sed -i "" "s=${NANO_DRIVE}s1=${NANO_DRIVE}s2=g" $f
507                 done
508                 umount ${MNT}
509         fi
510         
511         # Create Config slice
512         populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT}
513
514         # Create Data slice, if any.
515         if [ $NANO_DATASIZE -ne 0 ] ; then
516                 populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT}
517         fi
518
519         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
520                 echo "Writing out ${NANO_IMGNAME}..."
521                 dd if=/dev/${MD} of=${IMG} bs=64k
522         fi
523
524         echo "Writing out _.disk.image..."
525         dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
526         mdconfig -d -u $MD
527
528         trap - 1 2 15 EXIT
529
530         ) > ${NANO_OBJ}/_.di 2>&1
531 )
532
533 # i386 and amd64 are identical for disk images
534 create_amd64_diskimage ( ) (
535         create_i386_diskimage
536 )
537
538 last_orders () (
539         # Redefine this function with any last orders you may have
540         # after the build completed, for instance to copy the finished
541         # image to a more convenient place:
542         # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
543 )
544
545 #######################################################################
546 #
547 # Optional convenience functions.
548 #
549 #######################################################################
550
551 #######################################################################
552 # Common Flash device geometries
553 #
554
555 FlashDevice () {
556         if [ -d ${NANO_TOOLS} ] ; then
557                 . ${NANO_TOOLS}/FlashDevice.sub
558         else
559                 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
560         fi
561         sub_FlashDevice $1 $2
562 }
563
564 #######################################################################
565 # USB device geometries
566 #
567 # Usage:
568 #       UsbDevice Generic 1000  # a generic flash key sold as having 1GB
569 #
570 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
571 #
572 # Note that the capacity of a flash key is usually advertised in MB or
573 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
574 # for C/H/S geometry may vary depending on the actual flash geometry.
575 #
576 # The following generic device layouts are understood:
577 #  generic           An alias for generic-hdd.
578 #  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
579 #  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
580 #
581 # The generic-hdd device is preferred for flash devices larger than 1GB.
582 #
583
584 UsbDevice () {
585         a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
586         case $a1 in
587         generic-fdd)
588                 NANO_HEADS=64
589                 NANO_SECTS=32
590                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
591                 ;;
592         generic|generic-hdd)
593                 NANO_HEADS=255
594                 NANO_SECTS=63
595                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
596                 ;;
597         *)
598                 echo "Unknown USB flash device"
599                 exit 2
600                 ;;
601         esac
602 }
603
604 #######################################################################
605 # Setup serial console
606
607 cust_comconsole () (
608         # Enable getty on console
609         sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
610
611         # Disable getty on syscons devices
612         sed -i "" -e '/^ttyv[0-8]/s/    on/     off/' ${NANO_WORLDDIR}/etc/ttys
613
614         # Tell loader to use serial console early.
615         echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
616 )
617
618 #######################################################################
619 # Allow root login via ssh
620
621 cust_allow_ssh_root () (
622         sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
623             ${NANO_WORLDDIR}/etc/ssh/sshd_config
624 )
625
626 #######################################################################
627 # Install the stuff under ./Files
628
629 cust_install_files () (
630         cd ${NANO_TOOLS}/Files
631         find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
632 )
633
634 #######################################################################
635 # Install packages from ${NANO_PACKAGE_DIR}
636
637 cust_pkg () (
638
639         # Copy packages into chroot
640         mkdir -p ${NANO_WORLDDIR}/Pkg
641         (
642                 cd ${NANO_PACKAGE_DIR}
643                 find ${NANO_PACKAGE_LIST} -print |
644                     cpio -dumpv ${NANO_WORLDDIR}/Pkg
645         )
646
647         # Count & report how many we have to install
648         todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
649         echo "=== TODO: $todo"
650         ls ${NANO_WORLDDIR}/Pkg
651         echo "==="
652         while true
653         do
654                 # Record how many we have now
655                 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
656
657                 # Attempt to install more packages
658                 # ...but no more than 200 at a time due to pkg_add's internal
659                 # limitations.
660                 chroot ${NANO_WORLDDIR} sh -c \
661                         'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true
662
663                 # See what that got us
664                 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l`
665                 echo "=== NOW $now"
666                 ls ${NANO_WORLDDIR}/var/db/pkg
667                 echo "==="
668
669
670                 if [ $now -eq $todo ] ; then
671                         echo "DONE $now packages"
672                         break
673                 elif [ $now -eq $have ] ; then
674                         echo "FAILED: Nothing happened on this pass"
675                         exit 2
676                 fi
677         done
678         rm -rf ${NANO_WORLDDIR}/Pkg
679 )
680
681 #######################################################################
682 # Convenience function:
683 #       Register all args as customize function.
684
685 customize_cmd () {
686         NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
687 }
688
689 #######################################################################
690 # Convenience function:
691 #       Register all args as late customize function to run just before
692 #       image creation.
693
694 late_customize_cmd () {
695         NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
696 }
697
698 #######################################################################
699 #
700 # All set up to go...
701 #
702 #######################################################################
703
704 # Progress Print
705 #       Print $2 at level $1.
706 pprint() {
707     if [ "$1" -le $PPLEVEL ]; then
708         runtime=$(( `date +%s` - $NANO_STARTTIME ))
709         printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
710     fi
711 }
712
713 usage () {
714         (
715         echo "Usage: $0 [-biknqvw] [-c config_file]"
716         echo "  -b      suppress builds (both kernel and world)"
717         echo "  -i      suppress disk image build"
718         echo "  -k      suppress buildkernel"
719         echo "  -n      add -DNO_CLEAN to buildworld, buildkernel, etc"
720         echo "  -q      make output more quiet"
721         echo "  -v      make output more verbose"
722         echo "  -w      suppress buildworld"
723         echo "  -c      specify config file"
724         ) 1>&2
725         exit 2
726 }
727
728 #######################################################################
729 # Parse arguments
730
731 do_clean=true
732 do_kernel=true
733 do_world=true
734 do_image=true
735
736 set +e
737 args=`getopt bc:hiknqvw $*`
738 if [ $? -ne 0 ] ; then
739         usage
740         exit 2
741 fi
742 set -e
743
744 set -- $args
745 for i
746 do
747         case "$i" 
748         in
749         -b)
750                 do_world=false
751                 do_kernel=false
752                 shift
753                 ;;
754         -k)
755                 do_kernel=false
756                 shift
757                 ;;
758         -c)
759                 . "$2"
760                 shift
761                 shift
762                 ;;
763         -h)
764                 usage
765                 ;;
766         -i)
767                 do_image=false
768                 shift
769                 ;;
770         -n)
771                 do_clean=false
772                 shift
773                 ;;
774         -q)
775                 PPLEVEL=$(($PPLEVEL - 1))
776                 shift
777                 ;;
778         -v)
779                 PPLEVEL=$(($PPLEVEL + 1))
780                 shift
781                 ;;
782         -w)
783                 do_world=false
784                 shift
785                 ;;
786         --)
787                 shift
788                 break
789         esac
790 done
791
792 if [ $# -gt 0 ] ; then
793         echo "$0: Extraneous arguments supplied"
794         usage
795 fi
796
797 #######################################################################
798 # Setup and Export Internal variables
799 #
800 test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/
801 test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
802 test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
803
804 NANO_WORLDDIR=${NANO_OBJ}/_.w
805 NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
806 NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
807
808 if [ -d ${NANO_TOOLS} ] ; then
809         true
810 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
811         NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
812 else
813         echo "NANO_TOOLS directory does not exist" 1>&2
814         exit 1
815 fi
816
817 if $do_clean ; then
818         true
819 else
820         NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
821 fi
822
823 export MAKEOBJDIRPREFIX
824
825 export NANO_ARCH
826 export NANO_CODESIZE
827 export NANO_CONFSIZE
828 export NANO_CUSTOMIZE
829 export NANO_DATASIZE
830 export NANO_DRIVE
831 export NANO_HEADS
832 export NANO_IMAGES
833 export NANO_IMGNAME
834 export NANO_MAKE_CONF_BUILD
835 export NANO_MAKE_CONF_INSTALL
836 export NANO_MEDIASIZE
837 export NANO_NAME
838 export NANO_NEWFS
839 export NANO_OBJ
840 export NANO_PMAKE
841 export NANO_SECTS
842 export NANO_SRC
843 export NANO_TOOLS
844 export NANO_WORLDDIR
845 export NANO_BOOT0CFG
846 export NANO_BOOTLOADER
847
848 #######################################################################
849 # And then it is as simple as that...
850
851 # File descriptor 3 is used for logging output, see pprint
852 exec 3>&1
853
854 NANO_STARTTIME=`date +%s`
855 pprint 1 "NanoBSD image ${NANO_NAME} build starting"
856
857 if $do_world ; then
858         if $do_clean ; then
859                 clean_build
860         else
861                 pprint 2 "Using existing build tree (as instructed)"
862         fi
863         make_conf_build
864         build_world
865 else
866         pprint 2 "Skipping buildworld (as instructed)"
867 fi
868
869 if $do_kernel ; then
870         build_kernel
871 else
872         pprint 2 "Skipping buildkernel (as instructed)"
873 fi
874
875 clean_world
876 make_conf_install
877 install_world
878 install_etc
879 setup_nanobsd_etc
880 install_kernel
881
882 run_customize
883 setup_nanobsd
884 prune_usr
885 run_late_customize
886 if $do_image ; then
887         create_${NANO_ARCH}_diskimage
888 else
889         pprint 2 "Skipping image build (as instructed)"
890 fi
891 last_orders
892
893 pprint 1 "NanoBSD image ${NANO_NAME} completed"