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