]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/nanobsd/defaults.sh
Upgrade to Unbound 1.5.1. Almost all our local changes to date have been
[FreeBSD/FreeBSD.git] / tools / tools / nanobsd / defaults.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 # rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build
182 # hosts for now.
183 nano_rm ( ) {
184         case $(uname -r) in
185         7*|8*|9*) rm $* ;;
186         *) rm -x $* ;;
187         esac
188 }
189
190 # run in the world chroot, errors fatal
191 CR()
192 {
193         chroot ${NANO_WORLDDIR} /bin/sh -exc "$*"
194 }
195
196 # run in the world chroot, errors not fatal
197 CR0()
198 {
199         chroot ${NANO_WORLDDIR} /bin/sh -c "$*" || true
200 }
201
202 nano_cleanup ( ) (
203         if [ $? -ne 0 ]; then
204                 echo "Error encountered.  Check for errors in last log file." 1>&2
205         fi
206         exit $?
207 )
208
209 clean_build ( ) (
210         pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
211
212         if ! nano_rm -rf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
213                 chflags -R noschg ${MAKEOBJDIRPREFIX}/
214                 nano_rm -r ${MAKEOBJDIRPREFIX}/
215         fi
216 )
217
218 make_conf_build ( ) (
219         pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
220
221         mkdir -p ${MAKEOBJDIRPREFIX}
222         printenv > ${MAKEOBJDIRPREFIX}/_.env
223
224         echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD}
225         echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD}
226 )
227
228 build_world ( ) (
229         pprint 2 "run buildworld"
230         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
231
232         cd ${NANO_SRC}
233         env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \
234                 SRCCONF=${SRCCONF} \
235                 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \
236                 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
237 )
238
239 build_kernel ( ) (
240         local extra
241
242         pprint 2 "build kernel ($NANO_KERNEL)"
243         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
244
245         (
246         if [ -f ${NANO_KERNEL} ] ; then
247                 kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
248                 kernconf=$(basename ${NANO_KERNEL})
249         else
250                 kernconf=${NANO_KERNEL}
251         fi
252
253         cd ${NANO_SRC};
254         # unset these just in case to avoid compiler complaints
255         # when cross-building
256         unset TARGET_CPUTYPE
257         # Note: We intentionally build all modules, not only the ones in
258         # NANO_MODULES so the built world can be reused by multiple images.
259         eval "TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \
260                 SRCCONF='${SRCCONF}' \
261                 __MAKE_CONF='${NANO_MAKE_CONF_BUILD}' \
262                 ${kernconfdir_arg} KERNCONF=${kernconf}"
263         ) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
264 )
265
266 clean_world ( ) (
267         if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
268                 pprint 2 "Clean and create object directory (${NANO_OBJ})"
269                 if ! nano_rm -rf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
270                         chflags -R noschg ${NANO_OBJ}
271                         nano_rm -r ${NANO_OBJ}/
272                 fi
273                 mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR}
274                 printenv > ${NANO_OBJ}/_.env
275         else
276                 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
277                 if ! nano_rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then
278                         chflags -R noschg ${NANO_WORLDDIR}
279                         nano_rm -rf ${NANO_WORLDDIR}/
280                 fi
281                 mkdir -p ${NANO_WORLDDIR}
282         fi
283 )
284
285 make_conf_install ( ) (
286         pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
287
288         echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL}
289         echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL}
290 )
291
292 install_world ( ) (
293         pprint 2 "installworld"
294         pprint 3 "log: ${NANO_OBJ}/_.iw"
295
296         cd ${NANO_SRC}
297         env TARGET_ARCH=${NANO_ARCH} \
298         ${NANO_MAKE} SRCCONF=${SRCCONF} \
299                 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \
300                 DESTDIR=${NANO_WORLDDIR} \
301                 > ${NANO_OBJ}/_.iw 2>&1
302         chflags -R noschg ${NANO_WORLDDIR}
303 )
304
305 install_etc ( ) (
306
307         pprint 2 "install /etc"
308         pprint 3 "log: ${NANO_OBJ}/_.etc"
309
310         cd ${NANO_SRC}
311         env TARGET_ARCH=${NANO_ARCH} \
312         ${NANO_MAKE} SRCCONF=${SRCCONF} \
313                 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \
314                 DESTDIR=${NANO_WORLDDIR} \
315                 > ${NANO_OBJ}/_.etc 2>&1
316         # make.conf doesn't get created by default, but some ports need it
317         # so they can spam it.
318         cp /dev/null ${NANO_WORLDDIR}/etc/make.conf
319 )
320
321 install_kernel ( ) (
322         local extra
323
324         pprint 2 "install kernel ($NANO_KERNEL)"
325         pprint 3 "log: ${NANO_OBJ}/_.ik"
326
327         (
328         if [ -f ${NANO_KERNEL} ] ; then
329                 kernconfdir_arg="KERNCONFDIR='$(realpath $(dirname ${NANO_KERNEL}))'"
330                 kernconf=$(basename ${NANO_KERNEL})
331         else
332                 kernconf=${NANO_KERNEL}
333         fi
334
335         # Install all built modules if NANO_MODULES=default,
336         # else install only listed modules (none if NANO_MODULES is empty).
337         if [ "${NANO_MODULES}" != "default" ]; then
338                 modules_override_arg="MODULES_OVERRIDE='${NANO_MODULES}'"
339         fi
340
341         cd ${NANO_SRC}
342         eval "TARGET_ARCH=${NANO_ARCH} ${NANO_MAKE} installkernel \
343                 DESTDIR='${NANO_WORLDDIR}' \
344                 SRCCONF='${SRCCONF}' \
345                 __MAKE_CONF='${NANO_MAKE_CONF_INSTALL}' \
346                 ${kernconfdir_arg} KERNCONF=${kernconf} \
347                 ${modules_override_arg}"
348         ) > ${NANO_OBJ}/_.ik 2>&1
349 )
350
351 native_xtools ( ) (
352         print 2 "Installing the optimized native build tools for cross env"
353         pprint 3 "log: ${NANO_OBJ}/_.native_xtools"
354
355         cd ${NANO_SRC}
356         env TARGET_ARCH=${NANO_ARCH} \
357         ${NANO_MAKE} SRCCONF=${SRCCONF} \
358                 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} native-xtools \
359                 DESTDIR=${NANO_WORLDDIR} \
360                 > ${NANO_OBJ}/_.native_xtools 2>&1
361 )
362
363 run_customize() (
364
365         pprint 2 "run customize scripts"
366         for c in $NANO_CUSTOMIZE
367         do
368                 pprint 2 "customize \"$c\""
369                 pprint 3 "log: ${NANO_OBJ}/_.cust.$c"
370                 pprint 4 "`type $c`"
371                 ( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1
372         done
373 )
374
375 run_late_customize() (
376
377         pprint 2 "run late customize scripts"
378         for c in $NANO_LATE_CUSTOMIZE
379         do
380                 pprint 2 "late customize \"$c\""
381                 pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c"
382                 pprint 4 "`type $c`"
383                 ( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1
384         done
385 )
386
387 setup_nanobsd ( ) (
388         pprint 2 "configure nanobsd setup"
389         pprint 3 "log: ${NANO_OBJ}/_.dl"
390
391         (
392         cd ${NANO_WORLDDIR}
393
394         # Move /usr/local/etc to /etc/local so that the /cfg stuff
395         # can stomp on it.  Otherwise packages like ipsec-tools which
396         # have hardcoded paths under ${prefix}/etc are not tweakable.
397         if [ -d usr/local/etc ] ; then
398                 (
399                 mkdir -p etc/local
400                 cd usr/local/etc
401                 find . -print | cpio -dumpl ../../../etc/local
402                 cd ..
403                 nano_rm -rf etc
404                 ln -s ../../etc/local etc
405                 )
406         fi
407
408         for d in var etc
409         do
410                 # link /$d under /conf
411                 # we use hard links so we have them both places.
412                 # the files in /$d will be hidden by the mount.
413                 # XXX: configure /$d ramdisk size
414                 mkdir -p conf/base/$d conf/default/$d
415                 find $d -print | cpio -dumpl conf/base/
416         done
417
418         echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
419         echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
420
421         # pick up config files from the special partition
422         echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount
423
424         # Put /tmp on the /var ramdisk (could be symlink already)
425         nano_rm -rf tmp
426         ln -s var/tmp tmp
427
428         ) > ${NANO_OBJ}/_.dl 2>&1
429 )
430
431 setup_nanobsd_etc ( ) (
432         pprint 2 "configure nanobsd /etc"
433
434         (
435         cd ${NANO_WORLDDIR}
436
437         # create diskless marker file
438         touch etc/diskless
439
440         # Make root filesystem R/O by default
441         echo "root_rw_mount=NO" >> etc/defaults/rc.conf
442
443         # save config file for scripts
444         echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
445
446         echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab
447         echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab
448         mkdir -p cfg
449         )
450 )
451
452 prune_usr() (
453
454         # Remove all empty directories in /usr 
455         find ${NANO_WORLDDIR}/usr -type d -depth -print |
456                 while read d
457                 do
458                         rmdir $d > /dev/null 2>&1 || true 
459                 done
460 )
461
462 newfs_part ( ) (
463         local dev mnt lbl
464         dev=$1
465         mnt=$2
466         lbl=$3
467         echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
468         newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
469         mount -o async ${dev} ${mnt}
470 )
471
472 # Convenient spot to work around any umount issues that your build environment
473 # hits by overriding this method.
474 nano_umount () (
475         umount ${1}
476 )
477
478 populate_slice ( ) (
479         local dev dir mnt lbl
480         dev=$1
481         dir=$2
482         mnt=$3
483         lbl=$4
484         echo "Creating ${dev} (mounting on ${mnt})"
485         newfs_part ${dev} ${mnt} ${lbl}
486         if [ -n "${dir}" -a -d "${dir}" ]; then
487                 echo "Populating ${lbl} from ${dir}"
488                 cd ${dir}
489                 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt}
490         fi
491         df -i ${mnt}
492         nano_umount ${mnt}
493 )
494
495 populate_cfg_slice ( ) (
496         populate_slice "$1" "$2" "$3" "$4"
497 )
498
499 populate_data_slice ( ) (
500         populate_slice "$1" "$2" "$3" "$4"
501 )
502
503 create_diskimage ( ) (
504         pprint 2 "build diskimage"
505         pprint 3 "log: ${NANO_OBJ}/_.di"
506
507         (
508         echo $NANO_MEDIASIZE $NANO_IMAGES \
509                 $NANO_SECTS $NANO_HEADS \
510                 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
511         awk '
512         {
513                 printf "# %s\n", $0
514
515                 # size of cylinder in sectors
516                 cs = $3 * $4
517
518                 # number of full cylinders on media
519                 cyl = int ($1 / cs)
520
521                 # output fdisk geometry spec, truncate cyls to 1023
522                 if (cyl <= 1023)
523                         print "g c" cyl " h" $4 " s" $3
524                 else
525                         print "g c" 1023 " h" $4 " s" $3
526
527                 if ($7 > 0) { 
528                         # size of data partition in full cylinders
529                         dsl = int (($7 + cs - 1) / cs)
530                 } else {
531                         dsl = 0;
532                 }
533
534                 # size of config partition in full cylinders
535                 csl = int (($6 + cs - 1) / cs)
536
537                 if ($5 == 0) {
538                         # size of image partition(s) in full cylinders
539                         isl = int ((cyl - dsl - csl) / $2)
540                 } else {
541                         isl = int (($5 + cs - 1) / cs)
542                 }
543
544                 # First image partition start at second track
545                 print "p 1 165 " $3, isl * cs - $3
546                 c = isl * cs;
547
548                 # Second image partition (if any) also starts offset one 
549                 # track to keep them identical.
550                 if ($2 > 1) {
551                         print "p 2 165 " $3 + c, isl * cs - $3
552                         c += isl * cs;
553                 }
554
555                 # Config partition starts at cylinder boundary.
556                 print "p 3 165 " c, csl * cs
557                 c += csl * cs
558
559                 # Data partition (if any) starts at cylinder boundary.
560                 if ($7 > 0) {
561                         print "p 4 165 " c, dsl * cs
562                 } else if ($7 < 0 && $1 > c) {
563                         print "p 4 165 " c, $1 - c
564                 } else if ($1 < c) {
565                         print "Disk space overcommitted by", \
566                             c - $1, "sectors" > "/dev/stderr"
567                         exit 2
568                 }
569
570                 # Force slice 1 to be marked active. This is necessary
571                 # for booting the image from a USB device to work.
572                 print "a 1"
573         }
574         ' > ${NANO_OBJ}/_.fdisk
575
576         IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
577         MNT=${NANO_OBJ}/_.mnt
578         mkdir -p ${MNT}
579
580         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
581                 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
582                         -y ${NANO_HEADS}`
583         else
584                 echo "Creating md backing file..."
585                 nano_rm -f ${IMG}
586                 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
587                 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
588                         -y ${NANO_HEADS}`
589         fi
590
591         trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
592
593         fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD}
594         fdisk ${MD}
595         # XXX: params
596         # XXX: pick up cached boot* files, they may not be in image anymore.
597         if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then
598                 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
599         fi
600         if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
601                 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
602         else
603                 bsdlabel -w ${MD}s1
604         fi
605         bsdlabel ${MD}s1
606
607         # Create first image
608         populate_slice /dev/${MD}s1a ${NANO_WORLDDIR} ${MNT} "s1a"
609         mount /dev/${MD}s1a ${MNT}
610         echo "Generating mtree..."
611         ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
612         ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
613         nano_umount ${MNT}
614
615         if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
616                 # Duplicate to second image (if present)
617                 echo "Duplicating to second image..."
618                 dd conv=sparse if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k
619                 mount /dev/${MD}s2a ${MNT}
620                 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
621                 do
622                         sed -i "" "s=${NANO_DRIVE}s1=${NANO_DRIVE}s2=g" $f
623                 done
624                 nano_umount ${MNT}
625                 # Override the label from the first partition so we
626                 # don't confuse glabel with duplicates.
627                 if [ ! -z ${NANO_LABEL} ]; then
628                         tunefs -L ${NANO_LABEL}"s2a" /dev/${MD}s2a
629                 fi
630         fi
631         
632         # Create Config slice
633         populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT} "s3"
634
635         # Create Data slice, if any.
636         if [ $NANO_DATASIZE -ne 0 ] ; then
637                 populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT} "s4"
638         fi
639
640         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
641                 if [ ${NANO_IMAGE_MBRONLY} ]; then
642                         echo "Writing out _.disk.mbr..."
643                         dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
644                 else
645                         echo "Writing out ${NANO_IMGNAME}..."
646                         dd if=/dev/${MD} of=${IMG} bs=64k
647                 fi
648
649                 echo "Writing out ${NANO_IMGNAME}..."
650                 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
651         fi
652
653         if ${do_copyout_partition} ; then
654                 echo "Writing out _.disk.image..."
655                 dd conv=sparse if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
656         fi
657         mdconfig -d -u $MD
658
659         trap - 1 2 15
660         trap nano_cleanup EXIT
661
662         ) > ${NANO_OBJ}/_.di 2>&1
663 )
664
665 last_orders () (
666         # Redefine this function with any last orders you may have
667         # after the build completed, for instance to copy the finished
668         # image to a more convenient place:
669         # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk
670         true
671 )
672
673 #######################################################################
674 #
675 # Optional convenience functions.
676 #
677 #######################################################################
678
679 #######################################################################
680 # Common Flash device geometries
681 #
682
683 FlashDevice () {
684         if [ -d ${NANO_TOOLS} ] ; then
685                 . ${NANO_TOOLS}/FlashDevice.sub
686         else
687                 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
688         fi
689         sub_FlashDevice $1 $2
690 }
691
692 #######################################################################
693 # USB device geometries
694 #
695 # Usage:
696 #       UsbDevice Generic 1000  # a generic flash key sold as having 1GB
697 #
698 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
699 #
700 # Note that the capacity of a flash key is usually advertised in MB or
701 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
702 # for C/H/S geometry may vary depending on the actual flash geometry.
703 #
704 # The following generic device layouts are understood:
705 #  generic           An alias for generic-hdd.
706 #  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
707 #  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
708 #
709 # The generic-hdd device is preferred for flash devices larger than 1GB.
710 #
711
712 UsbDevice () {
713         a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
714         case $a1 in
715         generic-fdd)
716                 NANO_HEADS=64
717                 NANO_SECTS=32
718                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
719                 ;;
720         generic|generic-hdd)
721                 NANO_HEADS=255
722                 NANO_SECTS=63
723                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
724                 ;;
725         *)
726                 echo "Unknown USB flash device"
727                 exit 2
728                 ;;
729         esac
730 }
731
732 #######################################################################
733 # Setup serial console
734
735 cust_comconsole () (
736         # Enable getty on console
737         sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
738
739         # Disable getty on syscons devices
740         sed -i "" -e '/^ttyv[0-8]/s/    on/     off/' ${NANO_WORLDDIR}/etc/ttys
741
742         # Tell loader to use serial console early.
743         echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
744 )
745
746 #######################################################################
747 # Allow root login via ssh
748
749 cust_allow_ssh_root () (
750         sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
751             ${NANO_WORLDDIR}/etc/ssh/sshd_config
752 )
753
754 #######################################################################
755 # Install the stuff under ./Files
756
757 cust_install_files () (
758         cd ${NANO_TOOLS}/Files
759         find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR}
760 )
761
762 #######################################################################
763 # Install packages from ${NANO_PACKAGE_DIR}
764
765 cust_pkg () (
766
767         # If the package directory doesn't exist, we're done.
768         if [ ! -d ${NANO_PACKAGE_DIR} ]; then
769                 echo "DONE 0 packages"
770                 return 0
771         fi
772
773         # Copy packages into chroot
774         mkdir -p ${NANO_WORLDDIR}/Pkg ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
775         (
776                 cd ${NANO_PACKAGE_DIR}
777                 find ${NANO_PACKAGE_LIST} -print |
778                     cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
779         )
780
781         # Count & report how many we have to install
782         todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l`
783         echo "=== TODO: $todo"
784         ls ${NANO_WORLDDIR}/Pkg
785         echo "==="
786         while true
787         do
788                 # Record how many we have now
789                 have=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
790
791                 # Attempt to install more packages
792                 # ...but no more than 200 at a time due to pkg_add's internal
793                 # limitations.
794                 CR0 'ls Pkg/*tbz | xargs -n 200 env PKG_DBDIR='${NANO_PKG_META_BASE}'/pkg pkg_add -v -F'
795
796                 # See what that got us
797                 now=`ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg | wc -l`
798                 echo "=== NOW $now"
799                 ls ${NANO_WORLDDIR}/${NANO_PKG_META_BASE}/pkg
800                 echo "==="
801
802
803                 if [ $now -eq $todo ] ; then
804                         echo "DONE $now packages"
805                         break
806                 elif [ $now -eq $have ] ; then
807                         echo "FAILED: Nothing happened on this pass"
808                         exit 2
809                 fi
810         done
811         nano_rm -rf ${NANO_WORLDDIR}/Pkg
812 )
813
814 cust_pkgng () (
815
816         # If the package directory doesn't exist, we're done.
817         if [ ! -d ${NANO_PACKAGE_DIR} ]; then
818                 echo "DONE 0 packages"
819                 return 0
820         fi
821
822         # Find a pkg-* package
823         for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do
824                 _NANO_PKG_PACKAGE=`basename "$x"`
825         done
826         if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then
827                 echo "FAILED: need a pkg/ package for bootstrapping"
828                 exit 2
829         fi
830
831         # Copy packages into chroot
832         mkdir -p ${NANO_WORLDDIR}/Pkg
833         (
834                 cd ${NANO_PACKAGE_DIR}
835                 find ${NANO_PACKAGE_LIST} -print |
836                 cpio -Ldumpv ${NANO_WORLDDIR}/Pkg
837         )
838
839         #Bootstrap pkg
840         CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE}
841         CR pkg -N >/dev/null 2>&1
842         if [ "$?" -ne "0" ]; then
843                 echo "FAILED: pkg bootstrapping faied"
844                 exit 2
845         fi
846         nano_rm -f ${NANO_WORLDDIR}/Pkg/pkg-*
847
848         # Count & report how many we have to install
849         todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l`
850         todo=$(expr $todo + 1) # add one for pkg since it is installed already
851         echo "=== TODO: $todo"
852         ls ${NANO_WORLDDIR}/Pkg
853         echo "==="
854         while true
855         do
856                 # Record how many we have now
857                 have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
858
859                 # Attempt to install more packages
860                 CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add'
861
862                 # See what that got us
863                 now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l)
864                 echo "=== NOW $now"
865                 CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info
866                 echo "==="
867                 if [ $now -eq $todo ] ; then
868                         echo "DONE $now packages"
869                         break
870                 elif [ $now -eq $have ] ; then
871                         echo "FAILED: Nothing happened on this pass"
872                         exit 2
873                 fi
874         done
875         nano_rm -rf ${NANO_WORLDDIR}/Pkg
876 )
877
878 #######################################################################
879 # Convenience function:
880 #       Register all args as customize function.
881
882 customize_cmd () {
883         NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
884 }
885
886 #######################################################################
887 # Convenience function:
888 #       Register all args as late customize function to run just before
889 #       image creation.
890
891 late_customize_cmd () {
892         NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
893 }
894
895 #######################################################################
896 #
897 # All set up to go...
898 #
899 #######################################################################
900
901 # Progress Print
902 #       Print $2 at level $1.
903 pprint() (
904     if [ "$1" -le $PPLEVEL ]; then
905         runtime=$(( `date +%s` - $NANO_STARTTIME ))
906         printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
907     fi
908 )
909
910 usage () {
911         (
912         echo "Usage: $0 [-bfiKknqvw] [-c config_file]"
913         echo "  -b      suppress builds (both kernel and world)"
914         echo "  -c      specify config file"
915         echo "  -f      suppress code slice extraction"
916         echo "  -i      suppress disk image build"
917         echo "  -K      suppress installkernel"
918         echo "  -k      suppress buildkernel"
919         echo "  -n      add -DNO_CLEAN to buildworld, buildkernel, etc"
920         echo "  -q      make output more quiet"
921         echo "  -v      make output more verbose"
922         echo "  -w      suppress buildworld"
923         ) 1>&2
924         exit 2
925 }
926
927 #######################################################################
928 # Setup and Export Internal variables
929 #
930
931 export_var() {
932         var=$1
933         # Lookup value of the variable.
934         eval val=\$$var
935         pprint 3 "Setting variable: $var=\"$val\""
936         export $1
937 }
938
939 # Call this function to set defaults _after_ parsing options.
940 set_defaults_and_export() {
941         test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}
942         test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ}
943         test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ}
944         NANO_WORLDDIR=${NANO_OBJ}/_.w
945         NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
946         NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
947
948         # Override user's NANO_DRIVE if they specified a NANO_LABEL
949         [ ! -z "${NANO_LABEL}" ] && NANO_DRIVE="ufs/${NANO_LABEL}"
950
951         # Set a default NANO_TOOLS to NANO_SRC/NANO_TOOLS if it exists.
952         [ ! -d "${NANO_TOOLS}" ] && [ -d "${NANO_SRC}/${NANO_TOOLS}" ] && \
953                 NANO_TOOLS="${NANO_SRC}/${NANO_TOOLS}"
954
955         NANO_STARTTIME=`date +%s`
956         pprint 3 "Exporting NanoBSD variables"
957         export_var MAKEOBJDIRPREFIX
958         export_var NANO_ARCH
959         export_var NANO_CODESIZE
960         export_var NANO_CONFSIZE
961         export_var NANO_CUSTOMIZE
962         export_var NANO_DATASIZE
963         export_var NANO_DRIVE
964         export_var NANO_HEADS
965         export_var NANO_IMAGES
966         export_var NANO_IMGNAME
967         export_var NANO_MAKE
968         export_var NANO_MAKE_CONF_BUILD
969         export_var NANO_MAKE_CONF_INSTALL
970         export_var NANO_MEDIASIZE
971         export_var NANO_NAME
972         export_var NANO_NEWFS
973         export_var NANO_OBJ
974         export_var NANO_PMAKE
975         export_var NANO_SECTS
976         export_var NANO_SRC
977         export_var NANO_TOOLS
978         export_var NANO_WORLDDIR
979         export_var NANO_BOOT0CFG
980         export_var NANO_BOOTLOADER
981         export_var NANO_LABEL
982         export_var NANO_MODULES
983 }