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