]> CyberLeo.Net >> Repos - FreeBSD/releng/10.1.git/blob - tools/tools/nanobsd/nanobsd.sh
Document r273098, options for displaying mkimg(1) internals
[FreeBSD/releng/10.1.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 # where package metadata gets placed
53 NANO_PKG_META_BASE=/var/db
54
55 # Object tree directory
56 # default is subdir of /usr/obj
57 #NANO_OBJ=""
58
59 # The directory to put the final images
60 # default is ${NANO_OBJ}
61 #NANO_DISKIMGDIR=""
62
63 # Make & parallel Make
64 NANO_MAKE="make"
65 NANO_PMAKE="make -j 3"
66
67 # The default name for any image we create.
68 NANO_IMGNAME="_.disk.full"
69
70 # Options to put in make.conf during buildworld only
71 CONF_BUILD=' '
72
73 # Options to put in make.conf during installworld only
74 CONF_INSTALL=' '
75
76 # Options to put in make.conf during both build- & installworld.
77 CONF_WORLD=' '
78
79 # Kernel config file to use
80 NANO_KERNEL=GENERIC
81
82 # Kernel modules to install. If empty, no modules are installed.
83 # Use "default" to install all built modules.
84 NANO_MODULES=
85
86 # Customize commands.
87 NANO_CUSTOMIZE=""
88
89 # Late customize commands.
90 NANO_LATE_CUSTOMIZE=""
91
92 # Newfs paramters to use
93 NANO_NEWFS="-b 4096 -f 512 -i 8192 -U"
94
95 # The drive name of the media at runtime
96 NANO_DRIVE=ad0
97
98 # Target media size in 512 bytes sectors
99 NANO_MEDIASIZE=2000000
100
101 # Number of code images on media (1 or 2)
102 NANO_IMAGES=2
103
104 # 0 -> Leave second image all zeroes so it compresses better.
105 # 1 -> Initialize second image with a copy of the first
106 NANO_INIT_IMG2=1
107
108 # Size of code file system in 512 bytes sectors
109 # If zero, size will be as large as possible.
110 NANO_CODESIZE=0
111
112 # Size of configuration file system in 512 bytes sectors
113 # Cannot be zero.
114 NANO_CONFSIZE=2048
115
116 # Size of data file system in 512 bytes sectors
117 # If zero: no partition configured.
118 # If negative: max size possible
119 NANO_DATASIZE=0
120
121 # Size of the /etc ramdisk in 512 bytes sectors
122 NANO_RAM_ETCSIZE=10240
123
124 # Size of the /tmp+/var ramdisk in 512 bytes sectors
125 NANO_RAM_TMPVARSIZE=10240
126
127 # Media geometry, only relevant if bios doesn't understand LBA.
128 NANO_SECTS=63
129 NANO_HEADS=16
130
131 # boot0 flags/options and configuration
132 NANO_BOOT0CFG="-o packet -s 1 -m 3"
133 NANO_BOOTLOADER="boot/boot0sio"
134
135 # boot2 flags/options
136 # default force serial console
137 NANO_BOOT2CFG="-h"
138
139 # Backing type of md(4) device
140 # Can be "file" or "swap"
141 NANO_MD_BACKING="file"
142
143 # for swap type md(4) backing, write out the mbr only
144 NANO_IMAGE_MBRONLY=true
145
146 # Progress Print level
147 PPLEVEL=3
148
149 # Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label
150 # in preference to /dev/${NANO_DRIVE}
151 # Root partition will be ${NANO_LABEL}s{1,2}
152 # /cfg partition will be ${NANO_LABEL}s3
153 # /data partition will be ${NANO_LABEL}s4
154 NANO_LABEL=""
155
156 #######################################################################
157 # Architecture to build.  Corresponds to TARGET_ARCH in a buildworld.
158 # Unfortunately, there's no way to set TARGET at this time, and it
159 # conflates the two, so architectures where TARGET != TARGET_ARCH do
160 # not work.  This defaults to the arch of the current machine.
161
162 NANO_ARCH=`uname -p`
163
164 # Directory to populate /cfg from
165 NANO_CFGDIR=""
166
167 # Directory to populate /data from
168 NANO_DATADIR=""
169
170 # src.conf to use when building the image. Defaults to /dev/null for the sake
171 # of determinism.
172 SRCCONF=${SRCCONF:=/dev/null}
173  
174 #######################################################################
175 #
176 # The functions which do the real work.
177 # Can be overridden from the config file(s)
178 #
179 #######################################################################
180
181 # run in the world chroot, errors fatal
182 CR()
183 {
184         chroot ${NANO_WORLDDIR} /bin/sh -exc "$*"
185 }
186
187 # run in the world chroot, errors not fatal
188 CR0()
189 {
190         chroot ${NANO_WORLDDIR} /bin/sh -c "$*" || true
191 }
192
193 nano_cleanup ( ) (
194         if [ $? -ne 0 ]; then
195                 echo "Error encountered.  Check for errors in last log file." 1>&2
196         fi
197         exit $?
198 )
199
200 clean_build ( ) (
201         pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
202
203         if ! rm -xrf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
204                 chflags -R noschg ${MAKEOBJDIRPREFIX}/
205                 rm -xr ${MAKEOBJDIRPREFIX}/
206         fi
207         mkdir -p ${MAKEOBJDIRPREFIX}
208         printenv > ${MAKEOBJDIRPREFIX}/_.env
209 )
210
211 make_conf_build ( ) (
212         pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
213
214         echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
215         echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
216 )
217
218 build_world ( ) (
219         pprint 2 "run buildworld"
220         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
221
222         cd ${NANO_SRC}
223         env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
224                 SRCCONF=${SRCCONF} \
225                 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
226                 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
227 )
228
229 build_kernel ( ) (
230         local extra
231
232         pprint 2 "build kernel ($NANO_KERNEL)"
233         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
234
235         (
236         if [ -f ${NANO_KERNEL} ] ; then
237                 kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
238                 kernconf=$(basename ${NANO_KERNEL})
239         else
240                 kernconf=${NANO_KERNEL}
241         fi
242
243         cd ${NANO_SRC};
244         # unset these just in case to avoid compiler complaints
245         # when cross-building
246         unset TARGET_CPUTYPE
247         # Note: We intentionally build all modules, not only the ones in
248         # NANO_MODULES so the built world can be reused by multiple images.
249         eval "TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
250                 SRCCONF='${SRCCONF}' \
251                 __MAKE_CONF='${NANO_MAKE_CONF_BUILD}' \
252                 ${kernconfdir_arg} KERNCONF=${kernconf}"
253         ) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
254 )
255
256 clean_world ( ) (
257         if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
258                 pprint 2 "Clean and create object directory (${NANO_OBJ})"
259                 if ! rm -rxf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
260                         chflags -R noschg ${NANO_OBJ}
261                         rm -xr ${NANO_OBJ}/
262                 fi
263                 mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
264                 printenv > ${NANO_OBJ}/_.env
265         else
266                 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
267                 if ! rm -rxf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
268                         chflags -R noschg ${NANO_WORLDDIR}
269                         rm -rxf ${NANO_WORLDDIR}/
270                 fi
271                 mkdir -p ${NANO_WORLDDIR}
272         fi
273 )
274
275 make_conf_install ( ) (
276         pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
277
278         echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
279         echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
280 )
281
282 install_world ( ) (
283         pprint 2 "installworld"
284         pprint 3 "log: ${NANO_OBJ}/_.iw"
285
286         cd ${NANO_SRC}
287         env TARGET_ARCH=${NANO_ARCH} \
288         ${NANO_MAKE} SRCCONF=${SRCCONF} \
289                 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
290                 DESTDIR=${NANO_WORLDDIR} \
291                 > ${NANO_OBJ}/_.iw 2>&1
292         chflags -R noschg ${NANO_WORLDDIR}
293 )
294
295 install_etc ( ) (
296
297         pprint 2 "install /etc"
298         pprint 3 "log: ${NANO_OBJ}/_.etc"
299
300         cd ${NANO_SRC}
301         env TARGET_ARCH=${NANO_ARCH} \
302         ${NANO_MAKE} SRCCONF=${SRCCONF} \
303                 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
304                 DESTDIR=${NANO_WORLDDIR} \
305                 > ${NANO_OBJ}/_.etc 2>&1
306         # make.conf doesn't get created by default, but some ports need it
307         # so they can spam it.
308         cp /dev/null ${NANO_WORLDDIR}/etc/make.conf
309 )
310
311 install_kernel ( ) (
312         local extra
313
314         pprint 2 "install kernel ($NANO_KERNEL)"
315         pprint 3 "log: ${NANO_OBJ}/_.ik"
316
317         (
318         if [ -f ${NANO_KERNEL} ] ; then
319                 kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
320                 kernconf=$(basename ${NANO_KERNEL})
321         else
322                 kernconf=${NANO_KERNEL}
323         fi
324
325         # Install all built modules if NANO_MODULES=default,
326         # else install only listed modules (none if NANO_MODULES is empty).
327         if [ "${NANO_MODULES}" != "default" ]; then
328                 modules_override_arg="MODULES_OVERRIDE='${NANO_MODULES}'"
329         fi
330
331         cd ${NANO_SRC}
332         eval "TARGET_ARCH=${NANO_ARCH} ${NANO_MAKE} installkernel \
333                 DESTDIR='${NANO_WORLDDIR}' \
334                 SRCCONF='${SRCCONF}' \
335                 __MAKE_CONF='${NANO_MAKE_CONF_INSTALL}' \
336                 ${kernconfdir_arg} KERNCONF=${kernconf} \
337                 ${modules_override_arg}"
338         ) > ${NANO_OBJ}/_.ik 2>&1
339 )
340
341 run_customize() (
342
343         pprint 2 "run customize scripts"
344         for c in $NANO_CUSTOMIZE
345         do
346                 pprint 2 "customize \"$c\""
347                 pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
348                 pprint 4 "`type $c`"
349                 ( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
350         done
351 )
352
353 run_late_customize() (
354
355         pprint 2 "run late customize scripts"
356         for c in $NANO_LATE_CUSTOMIZE
357         do
358                 pprint 2 "late customize \"$c\""
359                 pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
360                 pprint 4 "`type $c`"
361                 ( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
362         done
363 )
364
365 setup_nanobsd ( ) (
366         pprint 2 "configure nanobsd setup"
367         pprint 3 "log: ${NANO_OBJ}/_.dl"
368
369         (
370         cd ${NANO_WORLDDIR}
371
372         # Move /usr/local/etc to /etc/local so that the /cfg stuff
373         # can stomp on it.  Otherwise packages like ipsec-tools which
374         # have hardcoded paths under ${prefix}/etc are not tweakable.
375         if [ -d usr/local/etc ] ; then
376                 (
377                 mkdir -p etc/local
378                 cd usr/local/etc
379                 find . -print | cpio -dumpl ../../../etc/local
380                 cd ..
381                 rm -rf etc
382                 ln -s ../../etc/local etc
383                 )
384         fi
385
386         for d in var etc
387         do
388                 # link /$d under /conf
389                 # we use hard links so we have them both places.
390                 # the files in /$d will be hidden by the mount.
391                 # XXX: configure /$d ramdisk size
392                 mkdir -p conf/base/$d conf/default/$d
393                 find $d -print | cpio -dumpl conf/base/
394         done
395
396         echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
397         echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
398
399         # pick up config files from the special partition
400         echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
401
402         # Put /tmp on the /var ramdisk (could be symlink already)
403         test -d tmp && rmdir tmp || rm -f tmp
404         ln -s var/tmp tmp
405
406         ) > ${NANO_OBJ}/_.dl 2>&1
407 )
408
409 setup_nanobsd_etc ( ) (
410         pprint 2 "configure nanobsd /etc"
411
412         (
413         cd ${NANO_WORLDDIR}
414
415         # create diskless marker file
416         touch etc/diskless
417
418         # Make root filesystem R/O by default
419         echo "root_rw_mount=NO" >> etc/defaults/rc.conf
420
421         # save config file for scripts
422         echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
423
424         echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
425         echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
426         mkdir -p cfg
427         )
428 )
429
430 prune_usr() (
431
432         # Remove all empty directories in /usr 
433         find ${NANO_WORLDDIR}/usr -type d -depth -print |
434                 while read d
435                 do
436                         rmdir $d > /dev/null 2>&1 || true 
437                 done
438 )
439
440 newfs_part ( ) (
441         local dev mnt lbl
442         dev=$1
443         mnt=$2
444         lbl=$3
445         echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
446         newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
447         mount -o async ${dev} ${mnt}
448 )
449
450 # Convenient spot to work around any umount issues that your build environment
451 # hits by overriding this method.
452 nano_umount () (
453         umount ${1}
454 )
455
456 populate_slice ( ) (
457         local dev dir mnt lbl
458         dev=$1
459         dir=$2
460         mnt=$3
461         lbl=$4
462         echo "Creating ${dev} (mounting on ${mnt})"
463         newfs_part ${dev} ${mnt} ${lbl}
464         if [ -n "${dir}" -a -d "${dir}" ]; then
465                 echo "Populating ${lbl} from ${dir}"
466                 cd ${dir}
467                 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
468         fi
469         df -i ${mnt}
470         nano_umount ${mnt}
471 )
472
473 populate_cfg_slice ( ) (
474         populate_slice "$1" "$2" "$3" "$4"
475 )
476
477 populate_data_slice ( ) (
478         populate_slice "$1" "$2" "$3" "$4"
479 )
480
481 create_i386_diskimage ( ) (
482         pprint 2 "build diskimage"
483         pprint 3 "log: ${NANO_OBJ}/_.di"
484
485         (
486         echo $NANO_MEDIASIZE $NANO_IMAGES \
487                 $NANO_SECTS $NANO_HEADS \
488                 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
489         awk '
490         {
491                 printf "# %s\n", $0
492
493                 # size of cylinder in sectors
494                 cs = $3 * $4
495
496                 # number of full cylinders on media
497                 cyl = int ($1 / cs)
498
499                 # output fdisk geometry spec, truncate cyls to 1023
500                 if (cyl <= 1023)
501                         print "g c" cyl " h" $4 " s" $3
502                 else
503                         print "g c" 1023 " h" $4 " s" $3
504
505                 if ($7 > 0) { 
506                         # size of data partition in full cylinders
507                         dsl = int (($7 + cs - 1) / cs)
508                 } else {
509                         dsl = 0;
510                 }
511
512                 # size of config partition in full cylinders
513                 csl = int (($6 + cs - 1) / cs)
514
515                 if ($5 == 0) {
516                         # size of image partition(s) in full cylinders
517                         isl = int ((cyl - dsl - csl) / $2)
518                 } else {
519                         isl = int (($5 + cs - 1) / cs)
520                 }
521
522                 # First image partition start at second track
523                 print "p 1 165 " $3, isl * cs - $3
524                 c = isl * cs;
525
526                 # Second image partition (if any) also starts offset one 
527                 # track to keep them identical.
528                 if ($2 > 1) {
529                         print "p 2 165 " $3 + c, isl * cs - $3
530                         c += isl * cs;
531                 }
532
533                 # Config partition starts at cylinder boundary.
534                 print "p 3 165 " c, csl * cs
535                 c += csl * cs
536
537                 # Data partition (if any) starts at cylinder boundary.
538                 if ($7 > 0) {
539                         print "p 4 165 " c, dsl * cs
540                 } else if ($7 < 0 && $1 > c) {
541                         print "p 4 165 " c, $1 - c
542                 } else if ($1 < c) {
543                         print "Disk space overcommitted by", \
544                             c - $1, "sectors" > "/dev/stderr"
545                         exit 2
546                 }
547
548                 # Force slice 1 to be marked active. This is necessary
549                 # for booting the image from a USB device to work.
550                 print "a 1"
551         }
552         ' > ${NANO_OBJ}/_.fdisk
553
554         IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
555         MNT=${NANO_OBJ}/_.mnt
556         mkdir -p ${MNT}
557
558         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
559                 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
560                         -y ${NANO_HEADS}`
561         else
562                 echo "Creating md backing file..."
563                 rm -f ${IMG}
564                 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
565                 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
566                         -y ${NANO_HEADS}`
567         fi
568
569         trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
570
571         fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
572         fdisk ${MD}
573         # XXX: params
574         # XXX: pick up cached boot* files, they may not be in image anymore.
575         boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
576         bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
577         bsdlabel ${MD}s1
578
579         # Create first image
580         populate_slice /dev/${MD}s1a ${NANO_WORLDDIR} ${MNT} "s1a"
581         mount /dev/${MD}s1a ${MNT}
582         echo "Generating mtree..."
583         ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
584         ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
585         nano_umount ${MNT}
586
587         if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
588                 # Duplicate to second image (if present)
589                 echo "Duplicating to second image..."
590                 dd conv=sparse if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
591                 mount /dev/${MD}s2a ${MNT}
592                 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
593                 do
594                         sed -i "" "s=${NANO_DRIVE}s1=${NANO_DRIVE}s2=g" $f
595                 done
596                 nano_umount ${MNT}
597                 # Override the label from the first partition so we
598                 # don't confuse glabel with duplicates.
599                 if [ ! -z ${NANO_LABEL} ]; then
600                         tunefs -L ${NANO_LABEL}"s2a" /dev/${MD}s2a
601                 fi
602         fi
603         
604         # Create Config slice
605         populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT} "s3"
606
607         # Create Data slice, if any.
608         if [ $NANO_DATASIZE -ne 0 ] ; then
609                 populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT} "s4"
610         fi
611
612         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
613                 if [ ${NANO_IMAGE_MBRONLY} ]; then
614                         echo "Writing out _.disk.mbr..."
615                         dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
616                 else
617                         echo "Writing out ${NANO_IMGNAME}..."
618                         dd if=/dev/${MD} of=${IMG} bs=64k
619                 fi
620
621                 echo "Writing out ${NANO_IMGNAME}..."
622                 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
623         fi
624
625         if ${do_copyout_partition} ; then
626                 echo "Writing out _.disk.image..."
627                 dd conv=sparse if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
628         fi
629         mdconfig -d -u $MD
630
631         trap - 1 2 15
632         trap nano_cleanup EXIT
633
634         ) > ${NANO_OBJ}/_.di 2>&1
635 )
636
637 # i386 and amd64 are identical for disk images
638 create_amd64_diskimage ( ) (
639         create_i386_diskimage
640 )
641
642 last_orders () (
643         # Redefine this function with any last orders you may have
644         # after the build completed, for instance to copy the finished
645         # image to a more convenient place:
646         # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
647         true
648 )
649
650 #######################################################################
651 #
652 # Optional convenience functions.
653 #
654 #######################################################################
655
656 #######################################################################
657 # Common Flash device geometries
658 #
659
660 FlashDevice () {
661         if [ -d ${NANO_TOOLS} ] ; then
662                 . ${NANO_TOOLS}/FlashDevice.sub
663         else
664                 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
665         fi
666         sub_FlashDevice $1 $2
667 }
668
669 #######################################################################
670 # USB device geometries
671 #
672 # Usage:
673 #       UsbDevice Generic 1000  # a generic flash key sold as having 1GB
674 #
675 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
676 #
677 # Note that the capacity of a flash key is usually advertised in MB or
678 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
679 # for C/H/S geometry may vary depending on the actual flash geometry.
680 #
681 # The following generic device layouts are understood:
682 #  generic           An alias for generic-hdd.
683 #  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
684 #  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
685 #
686 # The generic-hdd device is preferred for flash devices larger than 1GB.
687 #
688
689 UsbDevice () {
690         a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
691         case $a1 in
692         generic-fdd)
693                 NANO_HEADS=64
694                 NANO_SECTS=32
695                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
696                 ;;
697         generic|generic-hdd)
698                 NANO_HEADS=255
699                 NANO_SECTS=63
700                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
701                 ;;
702         *)
703                 echo "Unknown USB flash device"
704                 exit 2
705                 ;;
706         esac
707 }
708
709 #######################################################################
710 # Setup serial console
711
712 cust_comconsole () (
713         # Enable getty on console
714         sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
715
716         # Disable getty on syscons devices
717         sed -i "" -e '/^ttyv[0-8]/s/    on/     off/' ${NANO_WORLDDIR}/etc/ttys
718
719         # Tell loader to use serial console early.
720         echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
721 )
722
723 #######################################################################
724 # Allow root login via ssh
725
726 cust_allow_ssh_root () (
727         sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
728             ${NANO_WORLDDIR}/etc/ssh/sshd_config
729 )
730
731 #######################################################################
732 # Install the stuff under ./Files
733
734 cust_install_files () (
735         cd ${NANO_TOOLS}/Files
736         find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
737 )
738
739 #######################################################################
740 # Install packages from ${NANO_PACKAGE_DIR}
741
742 cust_pkg () (
743
744         # If the package directory doesn't exist, we're done.
745         if [ ! -d ${NANO_PACKAGE_DIR} ]; then
746                 echo "DONE 0 packages"
747                 return 0
748         fi
749
750         # Copy packages into chroot
751         mkdir -p ${NANO_WORLDDIR}/Pkg ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
752         (
753                 cd ${NANO_PACKAGE_DIR}
754                 find ${NANO_PACKAGE_LIST} -print |
755                     cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
756         )
757
758         # Count & report how many we have to install
759         todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
760         echo "=== TODO: $todo"
761         ls ${NANO_WORLDDIR}/Pkg
762         echo "==="
763         while true
764         do
765                 # Record how many we have now
766                 have=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
767
768                 # Attempt to install more packages
769                 # ...but no more than 200 at a time due to pkg_add's internal
770                 # limitations.
771                 CR0 'ls Pkg/*tbz | xargs -n 200 env PKG_DBDIR='${NANO_PKG_META_BASE}'/pkg pkg_add -v -F'
772
773                 # See what that got us
774                 now=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
775                 echo "=== NOW $now"
776                 ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
777                 echo "==="
778
779
780                 if [ $now -eq $todo ] ; then
781                         echo "DONE $now packages"
782                         break
783                 elif [ $now -eq $have ] ; then
784                         echo "FAILED: Nothing happened on this pass"
785                         exit 2
786                 fi
787         done
788         rm -rxf ${NANO_WORLDDIR}/Pkg
789 )
790
791 cust_pkgng () (
792
793         # If the package directory doesn't exist, we're done.
794         if [ ! -d ${NANO_PACKAGE_DIR} ]; then
795                 echo "DONE 0 packages"
796                 return 0
797         fi
798
799         # Find a pkg-* package
800         for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
801                 _NANO_PKG_PACKAGE=`basename "$x"`
802         done
803         if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
804                 echo "FAILED: need a pkg/ package for bootstrapping"
805                 exit 2
806         fi
807
808         # Copy packages into chroot
809         mkdir -p ${NANO_WORLDDIR}/Pkg
810         (
811                 cd ${NANO_PACKAGE_DIR}
812                 find ${NANO_PACKAGE_LIST} -print |
813                 cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
814         )
815
816         #Bootstrap pkg
817         CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE}
818         CR pkg -N >/dev/null 2>&1
819         if [ "$?" -ne "0" ]; then
820                 echo "FAILED: pkg bootstrapping faied"
821                 exit 2
822         fi
823         rm -f ${NANO_WORLDDIR}/Pkg/pkg-*
824
825         # Count & report how many we have to install
826         todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l`
827         todo=$(expr $todo + 1) # add one for pkg since it is installed already
828         echo "=== TODO: $todo"
829         ls ${NANO_WORLDDIR}/Pkg
830         echo "==="
831         while true
832         do
833                 # Record how many we have now
834                 have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
835
836                 # Attempt to install more packages
837                 CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add'
838
839                 # See what that got us
840                 now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
841                 echo "=== NOW $now"
842                 CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info
843                 echo "==="
844                 if [ $now -eq $todo ] ; then
845                         echo "DONE $now packages"
846                         break
847                 elif [ $now -eq $have ] ; then
848                         echo "FAILED: Nothing happened on this pass"
849                         exit 2
850                 fi
851         done
852         rm -rxf ${NANO_WORLDDIR}/Pkg
853 )
854
855 #######################################################################
856 # Convenience function:
857 #       Register all args as customize function.
858
859 customize_cmd () {
860         NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
861 }
862
863 #######################################################################
864 # Convenience function:
865 #       Register all args as late customize function to run just before
866 #       image creation.
867
868 late_customize_cmd () {
869         NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
870 }
871
872 #######################################################################
873 #
874 # All set up to go...
875 #
876 #######################################################################
877
878 # Progress Print
879 #       Print $2 at level $1.
880 pprint() (
881     if [ "$1" -le $PPLEVEL ]; then
882         runtime=$(( `date +%s` - $NANO_STARTTIME ))
883         printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
884     fi
885 )
886
887 usage () {
888         (
889         echo "Usage: $0 [-bfiknqvw] [-c config_file]"
890         echo "  -b      suppress builds (both kernel and world)"
891         echo "  -f      suppress code slice extraction"
892         echo "  -i      suppress disk image build"
893         echo "  -k      suppress buildkernel"
894         echo "  -n      add -DNO_CLEAN to buildworld, buildkernel, etc"
895         echo "  -q      make output more quiet"
896         echo "  -v      make output more verbose"
897         echo "  -w      suppress buildworld"
898         echo "  -c      specify config file"
899         ) 1>&2
900         exit 2
901 }
902
903 #######################################################################
904 # Parse arguments
905
906 do_clean=true
907 do_kernel=true
908 do_world=true
909 do_image=true
910 do_copyout_partition=true
911
912 set +e
913 args=`getopt bc:fhiknqvw $*`
914 if [ $? -ne 0 ] ; then
915         usage
916         exit 2
917 fi
918 set -e
919
920 set -- $args
921 for i
922 do
923         case "$i" 
924         in
925         -b)
926                 do_world=false
927                 do_kernel=false
928                 shift
929                 ;;
930         -k)
931                 do_kernel=false
932                 shift
933                 ;;
934         -c)
935                 # Make config file path available to the config file
936                 # itself so that it can access additional files relative
937                 # to its own location.
938                 NANO_CONFIG=$2
939                 . "$2"
940                 shift
941                 shift
942                 ;;
943         -f)
944                 do_copyout_partition=false
945                 shift
946                 ;;
947         -h)
948                 usage
949                 ;;
950         -i)
951                 do_image=false
952                 shift
953                 ;;
954         -n)
955                 do_clean=false
956                 shift
957                 ;;
958         -q)
959                 PPLEVEL=$(($PPLEVEL - 1))
960                 shift
961                 ;;
962         -v)
963                 PPLEVEL=$(($PPLEVEL + 1))
964                 shift
965                 ;;
966         -w)
967                 do_world=false
968                 shift
969                 ;;
970         --)
971                 shift
972                 break
973         esac
974 done
975
976 if [ $# -gt 0 ] ; then
977         echo "$0: Extraneous arguments supplied"
978         usage
979 fi
980
981 trap nano_cleanup EXIT
982
983 #######################################################################
984 # Setup and Export Internal variables
985 #
986 test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/
987 test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
988 test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
989
990 NANO_WORLDDIR=${NANO_OBJ}/_.w
991 NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
992 NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
993
994 if [ -d ${NANO_TOOLS} ] ; then
995         true
996 elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then
997         NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS}
998 else
999         echo "NANO_TOOLS directory does not exist" 1>&2
1000         exit 1
1001 fi
1002
1003 if $do_clean ; then
1004         true
1005 else
1006         NANO_MAKE="${NANO_MAKE} -DNO_CLEAN"
1007         NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN"
1008 fi
1009
1010 # Override user's NANO_DRIVE if they specified a NANO_LABEL
1011 if [ ! -z "${NANO_LABEL}" ]; then
1012         NANO_DRIVE=ufs/${NANO_LABEL}
1013 fi
1014
1015 export MAKEOBJDIRPREFIX
1016
1017 export NANO_ARCH
1018 export NANO_CODESIZE
1019 export NANO_CONFSIZE
1020 export NANO_CUSTOMIZE
1021 export NANO_DATASIZE
1022 export NANO_DRIVE
1023 export NANO_HEADS
1024 export NANO_IMAGES
1025 export NANO_IMGNAME
1026 export NANO_MAKE
1027 export NANO_MAKE_CONF_BUILD
1028 export NANO_MAKE_CONF_INSTALL
1029 export NANO_MEDIASIZE
1030 export NANO_NAME
1031 export NANO_NEWFS
1032 export NANO_OBJ
1033 export NANO_PMAKE
1034 export NANO_SECTS
1035 export NANO_SRC
1036 export NANO_TOOLS
1037 export NANO_WORLDDIR
1038 export NANO_BOOT0CFG
1039 export NANO_BOOTLOADER
1040 export NANO_LABEL
1041
1042 #######################################################################
1043 # And then it is as simple as that...
1044
1045 # File descriptor 3 is used for logging output, see pprint
1046 exec 3>&1
1047
1048 NANO_STARTTIME=`date +%s`
1049 pprint 1 "NanoBSD image ${NANO_NAME} build starting"
1050
1051 if $do_world ; then
1052         if $do_clean ; then
1053                 clean_build
1054         else
1055                 pprint 2 "Using existing build tree (as instructed)"
1056         fi
1057         make_conf_build
1058         build_world
1059 else
1060         pprint 2 "Skipping buildworld (as instructed)"
1061 fi
1062
1063 if $do_kernel ; then
1064         if ! $do_world ; then
1065                 make_conf_build
1066         fi
1067         build_kernel
1068 else
1069         pprint 2 "Skipping buildkernel (as instructed)"
1070 fi
1071
1072 clean_world
1073 make_conf_install
1074 install_world
1075 install_etc
1076 setup_nanobsd_etc
1077 install_kernel
1078
1079 run_customize
1080 setup_nanobsd
1081 prune_usr
1082 run_late_customize
1083 if $do_image ; then
1084         create_${NANO_ARCH}_diskimage
1085 else
1086         pprint 2 "Skipping image build (as instructed)"
1087 fi
1088 last_orders
1089
1090 pprint 1 "NanoBSD image ${NANO_NAME} completed"