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