]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/nanobsd/defaults.sh
nanobsd: default to -j $hw.ncpu
[FreeBSD/FreeBSD.git] / tools / tools / nanobsd / defaults.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Poul-Henning Kamp.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 #    notice, this list of conditions and the following disclaimer in the
13 #    documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 # SUCH DAMAGE.
26 #
27 # $FreeBSD$
28 #
29
30 set -e
31
32 #######################################################################
33 #
34 # Setup default values for all controlling variables.
35 # These values can be overridden from the config file(s)
36 #
37 #######################################################################
38
39 # Name of this NanoBSD build.  (Used to construct workdir names)
40 NANO_NAME=full
41
42 # Source tree directory
43 NANO_SRC=/usr/src
44
45 # Where nanobsd additional files live under the source tree
46 NANO_TOOLS=tools/tools/nanobsd
47
48 # Where cust_pkgng() finds packages to install
49 NANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
50 NANO_PACKAGE_LIST="*"
51
52 # where package metadata gets placed
53 NANO_PKG_META_BASE=/var/db
54
55 # Path to mtree file to apply to anything copied by cust_install_files().
56 # If you specify this, the mtree file *must* have an entry for every file and
57 # directory located in Files.
58 #NANO_CUST_FILES_MTREE=""
59
60 # Object tree directory
61 # default is subdir of /usr/obj
62 #NANO_OBJ=""
63
64 # The directory to put the final images
65 # default is ${NANO_OBJ}
66 #NANO_DISKIMGDIR=""
67
68 # Make & parallel Make
69 NANO_MAKE="make"
70 NANO_NCPU=$(sysctl -n hw.ncpu)
71 NANO_PMAKE="make -j $NANO_NCPU"
72
73 # The default name for any image we create.
74 NANO_IMGNAME="_.disk.full"
75 NANO_IMG1NAME="_.disk.image"
76
77 # Options to put in make.conf during buildworld only
78 CONF_BUILD=' '
79
80 # Options to put in make.conf during installworld only
81 CONF_INSTALL=' '
82
83 # Options to put in make.conf during both build- & installworld.
84 CONF_WORLD=' '
85
86 # Kernel config file to use
87 NANO_KERNEL=GENERIC
88
89 # Kernel modules to install. If empty, no modules are installed.
90 # Use "default" to install all built modules.
91 NANO_MODULES=
92
93 # Early customize commands.
94 NANO_EARLY_CUSTOMIZE=""
95
96 # Customize commands.
97 NANO_CUSTOMIZE=""
98
99 # Late customize commands.
100 NANO_LATE_CUSTOMIZE=""
101
102 # Newfs parameters to use
103 NANO_NEWFS="-b 4096 -f 512 -i 8192 -U"
104
105 # The drive name of the media at runtime
106 NANO_DRIVE=ada0
107
108 # Target media size in 512 bytes sectors
109 NANO_MEDIASIZE=2000000
110
111 # Number of code images on media (1 or 2)
112 NANO_IMAGES=2
113
114 # 0 -> Leave second image all zeroes so it compresses better.
115 # 1 -> Initialize second image with a copy of the first
116 NANO_INIT_IMG2=1
117
118 # Size of code file system in 512 bytes sectors
119 # If zero, size will be as large as possible.
120 NANO_CODESIZE=0
121
122 # Size of configuration file system in 512 bytes sectors
123 # Cannot be zero.
124 NANO_CONFSIZE=2048
125
126 # Size of data file system in 512 bytes sectors
127 # If zero: no partition configured.
128 # If negative: max size possible
129 NANO_DATASIZE=0
130
131 # Size of the /etc ramdisk in 512 bytes sectors
132 NANO_RAM_ETCSIZE=10240
133
134 # Size of the /tmp+/var ramdisk in 512 bytes sectors
135 NANO_RAM_TMPVARSIZE=10240
136
137 # boot0 flags/options and configuration
138 NANO_BOOT0CFG="-o packet -s 1 -m 3"
139 NANO_BOOTLOADER="boot/boot0sio"
140
141 # boot2 flags/options
142 # default force serial console
143 NANO_BOOT2CFG="-h -S115200"
144
145 # Backing type of md(4) device
146 # Can be "file" or "swap"
147 NANO_MD_BACKING="file"
148
149 # for swap type md(4) backing, write out the mbr only
150 NANO_IMAGE_MBRONLY=true
151
152 # Progress Print level
153 PPLEVEL=3
154
155 # Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label
156 # in preference to /dev/${NANO_DRIVE}
157 # Root partition will be ${NANO_LABEL}s{1,2}
158 # /cfg partition will be ${NANO_LABEL}s3
159 # /data partition will be ${NANO_LABEL}s4
160 NANO_LABEL=""
161 NANO_SLICE_ROOT=s1
162 NANO_SLICE_ALTROOT=s2
163 NANO_SLICE_CFG=s3
164 NANO_SLICE_DATA=s4
165 NANO_PARTITION_ROOT=a
166 NANO_PARTITION_ALTROOT=a
167 NANO_ROOT=s1a
168 NANO_ALTROOT=s2a
169
170 # Default ownwership for nopriv build
171 NANO_DEF_UNAME=root
172 NANO_DEF_GNAME=wheel
173
174 #######################################################################
175 # Architecture to build.  Corresponds to TARGET_ARCH in a buildworld.
176 # Unfortunately, there's no way to set TARGET at this time, and it
177 # conflates the two, so architectures where TARGET != TARGET_ARCH and
178 # TARGET can't be guessed from TARGET_ARCH do not work.  This defaults
179 # to the arch of the current machine.
180 NANO_ARCH=`uname -p`
181
182 # CPUTYPE defaults to "" which is the default when CPUTYPE isn't
183 # defined.
184 NANO_CPUTYPE=""
185
186 # Directory to populate /cfg from
187 NANO_CFGDIR=""
188
189 # Directory to populate /data from
190 NANO_DATADIR=""
191
192 # We don't need SRCCONF or SRC_ENV_CONF. NanoBSD puts everything we
193 # need for the build in files included with __MAKE_CONF. Override in your
194 # config file if you really must. We set them unconditionally here, though
195 # in case they are stray in the build environment
196 SRCCONF=/dev/null
197 SRC_ENV_CONF=/dev/null
198
199 #######################################################################
200 #
201 # The functions which do the real work.
202 # Can be overridden from the config file(s)
203 #
204 #######################################################################
205
206 # Export values into the shell. Must use { } instead of ( ) like
207 # other functions to avoid a subshell.
208 # We set __MAKE_CONF as a global since it is easier to get quoting
209 # right for paths with spaces in them.
210 make_export ( ) {
211         # Similar to export_var, except puts the data out to stdout
212         var=$1
213         eval val=\$$var
214         echo "Setting variable: $var=\"$val\""
215         export $1
216 }
217
218 nano_make_build_env ( ) {
219         __MAKE_CONF="${NANO_MAKE_CONF_BUILD}"
220         make_export __MAKE_CONF
221 }
222
223 nano_make_install_env ( ) {
224         __MAKE_CONF="${NANO_MAKE_CONF_INSTALL}"
225         make_export __MAKE_CONF
226 }
227
228 # Extra environment variables for kernel builds
229 nano_make_kernel_env ( ) {
230         if [ -f "${NANO_KERNEL}" ] ; then
231                 KERNCONFDIR="$(realpath $(dirname ${NANO_KERNEL}))"
232                 KERNCONF="$(basename ${NANO_KERNEL})"
233                 make_export KERNCONFDIR
234                 make_export KERNCONF
235         else
236                 export KERNCONF="${NANO_KERNEL}"
237                 make_export KERNCONF
238         fi
239 }
240
241 nano_global_make_env ( ) (
242         # global settings for the make.conf file, if set
243         [ -z "${NANO_ARCH}" ] || echo TARGET_ARCH="${NANO_ARCH}"
244         [ -z "${NANO_CPUTYPE}" ] || echo TARGET_CPUTYPE="${NANO_CPUTYPE}"
245 )
246
247 # rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build
248 # hosts for now. This will go away when support in the base goes away.
249 rm ( ) {
250     echo "NANO RM $*"
251         case $(uname -r) in
252         7*|8*|9*) command rm $* ;;
253         *) command rm -x $* ;;
254         esac
255 }
256
257 #
258 # Create empty files in the target tree, and record the fact.  All paths
259 # are relative to NANO_WORLDDIR.
260 #
261 tgt_touch ( ) (
262         cd "${NANO_WORLDDIR}"
263         for i; do
264                 touch $i
265                 echo "./${i} type=file" >> ${NANO_METALOG}
266         done
267 )
268
269 #
270 # Convert a directory into a symlink. Takes two arguments, the
271 # current directory and what it should become a symlink to. The
272 # directory is removed and a symlink is created. If we're doing
273 # a nopriv build, then append this fact to the metalog
274 #
275 tgt_dir2symlink ( ) (
276         dir=$1
277         symlink=$2
278
279         cd "${NANO_WORLDDIR}"
280         rm -rf "$dir"
281         ln -s "$symlink" "$dir"
282         if [ -n "$NANO_METALOG" ]; then
283                 echo "./${dir} type=link mode=0777 link=${symlink}" >> ${NANO_METALOG}
284         fi
285 )
286
287 # run in the world chroot, errors fatal
288 CR ( ) {
289         chroot "${NANO_WORLDDIR}" /bin/sh -exc "$*"
290 }
291
292 # run in the world chroot, errors not fatal
293 CR0 ( ) {
294         chroot "${NANO_WORLDDIR}" /bin/sh -c "$*" || true
295 }
296
297 clean_build ( ) (
298         pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})"
299
300         if ! rm -rf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then
301                 chflags -R noschg ${MAKEOBJDIRPREFIX}/
302                 rm -r ${MAKEOBJDIRPREFIX}/
303         fi
304 )
305
306 make_conf_build ( ) (
307         pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)"
308
309         mkdir -p ${MAKEOBJDIRPREFIX}
310         printenv > ${MAKEOBJDIRPREFIX}/_.env
311
312         # Make sure we get all the global settings that NanoBSD wants
313         # in addition to the user's global settings
314         (
315         nano_global_make_env
316         echo "${CONF_WORLD}"
317         echo "${CONF_BUILD}"
318         ) > ${NANO_MAKE_CONF_BUILD}
319 )
320
321 build_world ( ) (
322         pprint 2 "run buildworld"
323         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw"
324
325         (
326         nano_make_build_env
327         set -o xtrace
328         cd "${NANO_SRC}"
329         ${NANO_PMAKE} buildworld
330         ) > ${MAKEOBJDIRPREFIX}/_.bw 2>&1
331 )
332
333 build_kernel ( ) (
334         pprint 2 "build kernel ($NANO_KERNEL)"
335         pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk"
336
337         (
338         nano_make_build_env
339         nano_make_kernel_env
340
341         # Note: We intentionally build all modules, not only the ones in
342         # NANO_MODULES so the built world can be reused by multiple images.
343         # Although MODULES_OVERRIDE can be defined in the kernel config
344         # file to override this behavior. Just set NANO_MODULES=default.
345         set -o xtrace
346         cd "${NANO_SRC}"
347         ${NANO_PMAKE} buildkernel
348         ) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1
349 )
350
351 clean_world ( ) (
352         if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then
353                 pprint 2 "Clean and create object directory (${NANO_OBJ})"
354                 if ! rm -rf ${NANO_OBJ}/ > /dev/null 2>&1 ; then
355                         chflags -R noschg ${NANO_OBJ}
356                         rm -r ${NANO_OBJ}/
357                 fi
358                 mkdir -p "${NANO_OBJ}" "${NANO_WORLDDIR}"
359                 printenv > ${NANO_LOG}/_.env
360         else
361                 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})"
362                 if ! rm -rf "${NANO_WORLDDIR}/" > /dev/null 2>&1 ; then
363                         chflags -R noschg "${NANO_WORLDDIR}"
364                         rm -rf "${NANO_WORLDDIR}/"
365                 fi
366                 mkdir -p "${NANO_WORLDDIR}"
367         fi
368 )
369
370 make_conf_install ( ) (
371         pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)"
372
373         # Make sure we get all the global settings that NanoBSD wants
374         # in addition to the user's global settings
375         (
376         nano_global_make_env
377         echo "${CONF_WORLD}"
378         echo "${CONF_INSTALL}"
379         if [ -n "${NANO_NOPRIV_BUILD}" ]; then
380             echo NO_ROOT=t
381             echo METALOG=${NANO_METALOG}
382         fi
383         ) >  ${NANO_MAKE_CONF_INSTALL}
384 )
385
386 install_world ( ) (
387         pprint 2 "installworld"
388         pprint 3 "log: ${NANO_LOG}/_.iw"
389
390         (
391         nano_make_install_env
392         set -o xtrace
393         cd "${NANO_SRC}"
394         ${NANO_MAKE} installworld DESTDIR="${NANO_WORLDDIR}" DB_FROM_SRC=yes
395         chflags -R noschg "${NANO_WORLDDIR}"
396         ) > ${NANO_LOG}/_.iw 2>&1
397 )
398
399 install_etc ( ) (
400         pprint 2 "install /etc"
401         pprint 3 "log: ${NANO_LOG}/_.etc"
402
403         (
404         nano_make_install_env
405         set -o xtrace
406         cd "${NANO_SRC}"
407         ${NANO_MAKE} distribution DESTDIR="${NANO_WORLDDIR}" DB_FROM_SRC=yes
408         # make.conf doesn't get created by default, but some ports need it
409         # so they can spam it.
410         cp /dev/null "${NANO_WORLDDIR}"/etc/make.conf
411         ) > ${NANO_LOG}/_.etc 2>&1
412 )
413
414 install_kernel ( ) (
415         pprint 2 "install kernel ($NANO_KERNEL)"
416         pprint 3 "log: ${NANO_LOG}/_.ik"
417
418         (
419
420         nano_make_install_env
421         nano_make_kernel_env
422
423         if [ "${NANO_MODULES}" != "default" ]; then
424                 MODULES_OVERRIDE="${NANO_MODULES}"
425                 make_export MODULES_OVERRIDE
426         fi
427
428         set -o xtrace
429         cd "${NANO_SRC}"
430         ${NANO_MAKE} installkernel DESTDIR="${NANO_WORLDDIR}" DB_FROM_SRC=yes
431
432         ) > ${NANO_LOG}/_.ik 2>&1
433 )
434
435 native_xtools ( ) (
436         print 2 "Installing the optimized native build tools for cross env"
437         pprint 3 "log: ${NANO_LOG}/_.native_xtools"
438
439         (
440
441         nano_make_install_env
442         set -o xtrace
443         cd "${NANO_SRC}"
444         ${NANO_MAKE} native-xtools
445         ${NANO_MAKE} native-xtools-install DESTDIR="${NANO_WORLDDIR}"
446
447         ) > ${NANO_LOG}/_.native_xtools 2>&1
448 )
449
450 #
451 # Run the requested set of early customization scripts, run before
452 # buildworld.
453 #
454 run_early_customize ( ) {
455         pprint 2 "run early customize scripts"
456         for c in $NANO_EARLY_CUSTOMIZE
457         do
458                 pprint 2 "early customize \"$c\""
459                 pprint 3 "log: ${NANO_LOG}/_.early_cust.$c"
460                 pprint 4 "`type $c`"
461                 { set -x ; $c ; set +x ; } >${NANO_LOG}/_.early_cust.$c 2>&1
462         done
463 }
464
465 #
466 # Run the requested set of customization scripts, run after we've
467 # done an installworld, installed the etc files, installed the kernel
468 # and tweaked them in the standard way.
469 #
470 run_customize ( ) (
471
472         pprint 2 "run customize scripts"
473         for c in $NANO_CUSTOMIZE
474         do
475                 pprint 2 "customize \"$c\""
476                 pprint 3 "log: ${NANO_LOG}/_.cust.$c"
477                 pprint 4 "`type $c`"
478                 ( set -x ; $c ) > ${NANO_LOG}/_.cust.$c 2>&1
479         done
480 )
481
482 #
483 # Run any last-minute customization commands after we've had a chance to
484 # setup nanobsd, prune empty dirs from /usr, etc
485 #
486 run_late_customize ( ) (
487         pprint 2 "run late customize scripts"
488         for c in $NANO_LATE_CUSTOMIZE
489         do
490                 pprint 2 "late customize \"$c\""
491                 pprint 3 "log: ${NANO_LOG}/_.late_cust.$c"
492                 pprint 4 "`type $c`"
493                 ( set -x ; $c ) > ${NANO_LOG}/_.late_cust.$c 2>&1
494         done
495 )
496
497 #
498 # Hook called after we run all the late customize commands, but
499 # before we invoke the disk imager. The nopriv build uses it to
500 # read in the meta log, apply the changes other parts of nanobsd
501 # have been recording their actions. It's not anticipated that
502 # a user's cfg file would override this.
503 #
504 fixup_before_diskimage ( ) (
505         # Run the deduplication script that takes the matalog journal and
506         # combines multiple entries for the same file (see source for
507         # details). We take the extra step of removing the size keywords. This
508         # script, and many of the user scripts, copies, appeneds and otherwise
509         # modifies files in the build, changing their sizes.  These actions are
510         # impossible to trap, so go ahead remove the size= keyword. For this
511         # narrow use, it doesn't buy us any protection and just gets in the way.
512         # The dedup tool's output must be sorted due to limitations in awk.
513         if [ -n "${NANO_METALOG}" ]; then
514                 pprint 2 "Fixing metalog"
515                 cp ${NANO_METALOG} ${NANO_METALOG}.pre
516                 echo "/set uname=${NANO_DEF_UNAME} gname=${NANO_DEF_GNAME}" > ${NANO_METALOG}
517                 cat ${NANO_METALOG}.pre | ${NANO_TOOLS}/mtree-dedup.awk | \
518                     sed -e 's/ size=[0-9][0-9]*//' | sort >> ${NANO_METALOG}
519         fi
520 )
521
522 setup_nanobsd ( ) (
523         pprint 2 "configure nanobsd setup"
524         pprint 3 "log: ${NANO_LOG}/_.dl"
525
526         (
527         cd "${NANO_WORLDDIR}"
528
529         # Move /usr/local/etc to /etc/local so that the /cfg stuff
530         # can stomp on it.  Otherwise packages like ipsec-tools which
531         # have hardcoded paths under ${prefix}/etc are not tweakable.
532         if [ -d usr/local/etc ] ; then
533                 (
534                 cd usr/local/etc
535                 find . -print | cpio -dumpl ../../../etc/local
536                 cd ..
537                 rm -rf etc
538                 )
539         fi
540
541         # Always setup the usr/local/etc -> etc/local symlink.
542         # usr/local/etc gets created by packages, but if no packages
543         # are installed by this point, but are later in the process,
544         # the symlink not being here causes problems. It never hurts
545         # to have the symlink in error though.
546         ln -s ../../etc/local usr/local/etc
547
548         for d in var etc
549         do
550                 # link /$d under /conf
551                 # we use hard links so we have them both places.
552                 # the files in /$d will be hidden by the mount.
553                 mkdir -p conf/base/$d conf/default/$d
554                 find $d -print | cpio -dumpl conf/base/
555         done
556
557         echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size
558         echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size
559
560         # pick up config files from the special partition
561         echo "mount -o ro /dev/${NANO_DRIVE}${NANO_SLICE_CFG}" > conf/default/etc/remount
562
563         # Put /tmp on the /var ramdisk (could be symlink already)
564         tgt_dir2symlink tmp var/tmp
565
566         ) > ${NANO_LOG}/_.dl 2>&1
567 )
568
569 setup_nanobsd_etc ( ) (
570         pprint 2 "configure nanobsd /etc"
571
572         (
573         cd "${NANO_WORLDDIR}"
574
575         # create diskless marker file
576         touch etc/diskless
577
578         [ -n "${NANO_NOPRIV_BUILD}" ] && chmod 666 etc/defaults/rc.conf
579
580         # Make root filesystem R/O by default
581         echo "root_rw_mount=NO" >> etc/defaults/rc.conf
582         # Disable entropy file, since / is read-only /var/db/entropy should be enough?
583         echo "entropy_file=NO" >> etc/defaults/rc.conf
584
585         [ -n "${NANO_NOPRIV_BUILD}" ] && chmod 444 etc/defaults/rc.conf
586
587         # save config file for scripts
588         echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf
589
590         echo "/dev/${NANO_DRIVE}${NANO_ROOT} / ufs ro 1 1" > etc/fstab
591         echo "/dev/${NANO_DRIVE}${NANO_SLICE_CFG} /cfg ufs rw,noauto 2 2" >> etc/fstab
592         mkdir -p cfg
593
594         # Create directory for eventual /usr/local/etc contents
595         mkdir -p etc/local
596         )
597 )
598
599 prune_usr ( ) (
600         # Remove all empty directories in /usr
601         find "${NANO_WORLDDIR}"/usr -type d -depth -print |
602                 while read d
603                 do
604                         rmdir $d > /dev/null 2>&1 || true
605                 done
606 )
607
608 newfs_part ( ) (
609         local dev mnt lbl
610         dev=$1
611         mnt=$2
612         lbl=$3
613         echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
614         newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev}
615         mount -o async ${dev} ${mnt}
616 )
617
618 # Convenient spot to work around any umount issues that your build environment
619 # hits by overriding this method.
620 nano_umount ( ) (
621         umount ${1}
622 )
623
624 populate_slice ( ) (
625         local dev dir mnt lbl
626         dev=$1
627         dir=$2
628         mnt=$3
629         lbl=$4
630         echo "Creating ${dev} (mounting on ${mnt})"
631         newfs_part ${dev} ${mnt} ${lbl}
632         if [ -n "${dir}" -a -d "${dir}" ]; then
633                 echo "Populating ${lbl} from ${dir}"
634                 cd "${dir}"
635                 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)/' | cpio -dumpv ${mnt}
636         fi
637         df -i ${mnt}
638         nano_umount ${mnt}
639 )
640
641 populate_cfg_slice ( ) (
642         populate_slice "$1" "$2" "$3" "$4"
643 )
644
645 populate_data_slice ( ) (
646         populate_slice "$1" "$2" "$3" "$4"
647 )
648
649 last_orders ( ) (
650         # Redefine this function with any last orders you may have
651         # after the build completed, for instance to copy the finished
652         # image to a more convenient place:
653         # cp ${NANO_DISKIMGDIR}/${NANO_IMG1NAME} /home/ftp/pub/nanobsd.disk
654         true
655 )
656
657 #######################################################################
658 #
659 # Optional convenience functions.
660 #
661 #######################################################################
662
663 #######################################################################
664 # Common Flash device geometries
665 #
666
667 FlashDevice ( ) {
668         if [ -d ${NANO_TOOLS} ] ; then
669                 . ${NANO_TOOLS}/FlashDevice.sub
670         else
671                 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub
672         fi
673         sub_FlashDevice $1 $2
674 }
675
676 #######################################################################
677 # USB device geometries
678 #
679 # Usage:
680 #       UsbDevice Generic 1000  # a generic flash key sold as having 1GB
681 #
682 # This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you.
683 #
684 # Note that the capacity of a flash key is usually advertised in MB or
685 # GB, *not* MiB/GiB. As such, the precise number of cylinders available
686 # for C/H/S geometry may vary depending on the actual flash geometry.
687 #
688 # The following generic device layouts are understood:
689 #  generic           An alias for generic-hdd.
690 #  generic-hdd       255H 63S/T xxxxC with no MBR restrictions.
691 #  generic-fdd       64H 32S/T xxxxC with no MBR restrictions.
692 #
693 # The generic-hdd device is preferred for flash devices larger than 1GB.
694 #
695
696 UsbDevice ( ) {
697         a1=`echo $1 | tr '[:upper:]' '[:lower:]'`
698         case $a1 in
699         generic-fdd)
700                 NANO_HEADS=64
701                 NANO_SECTS=32
702                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
703                 ;;
704         generic|generic-hdd)
705                 NANO_HEADS=255
706                 NANO_SECTS=63
707                 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 ))
708                 ;;
709         *)
710                 echo "Unknown USB flash device"
711                 exit 2
712                 ;;
713         esac
714 }
715
716 #######################################################################
717 # Setup serial console
718
719 cust_comconsole ( ) (
720         # Enable getty on console
721         sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys
722
723         # Disable getty on syscons devices
724         sed -i "" -e '/^ttyv[0-8]/s/    on/     off/' ${NANO_WORLDDIR}/etc/ttys
725
726         # Tell loader to use serial console early.
727         echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config
728 )
729
730 #######################################################################
731 # Allow root login via ssh
732
733 cust_allow_ssh_root ( ) (
734         sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \
735             ${NANO_WORLDDIR}/etc/ssh/sshd_config
736 )
737
738 #######################################################################
739 # Install the stuff under ./Files
740
741 cust_install_files ( ) (
742         cd "${NANO_TOOLS}/Files"
743         find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)/' | cpio -Ldumpv ${NANO_WORLDDIR}
744
745         if [ -n "${NANO_CUST_FILES_MTREE}" -a -f ${NANO_CUST_FILES_MTREE} ]; then
746                 CR "mtree -eiU -p /" <${NANO_CUST_FILES_MTREE}
747         fi
748 )
749
750 #######################################################################
751 # Install packages from ${NANO_PACKAGE_DIR}
752
753 cust_pkgng ( ) (
754         mkdir -p ${NANO_WORLDDIR}/usr/local/etc
755         local PKG_CONF="${NANO_WORLDDIR}/usr/local/etc/pkg.conf"
756         local PKGCMD="env BATCH=YES ASSUME_ALWAYS_YES=YES PKG_DBDIR=${NANO_PKG_META_BASE}/pkg SIGNATURE_TYPE=none /usr/sbin/pkg"
757
758         # Ensure pkg.conf points pkg to where the package meta data lives.
759         touch ${PKG_CONF}
760         if grep -Eiq '^PKG_DBDIR:.*' ${PKG_CONF}; then
761                 sed -i -e "\|^PKG_DBDIR:.*|Is||PKG_DBDIR: "\"${NANO_PKG_META_BASE}/pkg\""|" ${PKG_CONF}
762         else
763                 echo "PKG_DBDIR: \"${NANO_PKG_META_BASE}/pkg\"" >> ${PKG_CONF}
764         fi
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         # Mount packages into chroot
782         mkdir -p ${NANO_WORLDDIR}/_.p
783         mount -t nullfs -o noatime -o ro ${NANO_PACKAGE_DIR} ${NANO_WORLDDIR}/_.p
784         mount -t devfs devfs ${NANO_WORLDDIR}/dev
785
786         trap "umount ${NANO_WORLDDIR}/dev; umount ${NANO_WORLDDIR}/_.p ; rm -rf ${NANO_WORLDDIR}/_.p" 1 2 15 EXIT
787
788         # Install pkg-* package
789         CR "${PKGCMD} add /_.p/${_NANO_PKG_PACKAGE}"
790
791         (
792                 # Expand any glob characters in pacakge list
793                 cd "${NANO_PACKAGE_DIR}"
794                 _PKGS=`find ${NANO_PACKAGE_LIST} -not -name "${_NANO_PKG_PACKAGE}" -print | sort | uniq`
795
796                 # Show todo
797                 todo=`echo "$_PKGS" | wc -l`
798                 echo "=== TODO: $todo"
799                 echo "$_PKGS"
800                 echo "==="
801
802                 # Install packages
803                 for _PKG in $_PKGS; do
804                         CR "${PKGCMD} add /_.p/${_PKG}"
805                 done
806         )
807
808         CR0 "${PKGCMD} info"
809
810         trap - 1 2 15 EXIT
811         umount ${NANO_WORLDDIR}/dev
812         umount ${NANO_WORLDDIR}/_.p
813         rm -rf ${NANO_WORLDDIR}/_.p
814 )
815
816 #######################################################################
817 # Convenience function:
818 #       Register all args as early customize function to run just before
819 #       build commences.
820
821 early_customize_cmd ( ) {
822         NANO_EARLY_CUSTOMIZE="$NANO_EARLY_CUSTOMIZE $*"
823 }
824
825 #######################################################################
826 # Convenience function:
827 #       Register all args as customize function.
828
829 customize_cmd ( ) {
830         NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*"
831 }
832
833 #######################################################################
834 # Convenience function:
835 #       Register all args as late customize function to run just before
836 #       image creation.
837
838 late_customize_cmd ( ) {
839         NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*"
840 }
841
842 #######################################################################
843 #
844 # All set up to go...
845 #
846 #######################################################################
847
848 # Progress Print
849 #       Print $2 at level $1.
850 pprint ( ) (
851     if [ "$1" -le $PPLEVEL ]; then
852         runtime=$(( `date +%s` - $NANO_STARTTIME ))
853         printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3
854     fi
855 )
856
857 usage ( ) {
858         (
859         echo "Usage: $0 [-bfhiKknqvwX] [-c config_file]"
860         echo "  -b      suppress builds (both kernel and world)"
861         echo "  -c      specify config file"
862         echo "  -f      suppress code slice extraction (implies -i)"
863         echo "  -h      print this help summary page"
864         echo "  -i      suppress disk image build"
865         echo "  -K      suppress installkernel"
866         echo "  -k      suppress buildkernel"
867         echo "  -n      add -DNO_CLEAN to buildworld, buildkernel, etc"
868         echo "  -q      make output more quiet"
869         echo "  -v      make output more verbose"
870         echo "  -w      suppress buildworld"
871         echo "  -X      make native-xtools"
872         ) 1>&2
873         exit 2
874 }
875
876 #######################################################################
877 # Setup and Export Internal variables
878 #
879
880 export_var ( ) {                # Don't wawnt a subshell
881         var=$1
882         # Lookup value of the variable.
883         eval val=\$$var
884         pprint 3 "Setting variable: $var=\"$val\""
885         export $1
886 }
887
888 # Call this function to set defaults _after_ parsing options.
889 # dont want a subshell otherwise variable setting is thrown away.
890 set_defaults_and_export ( ) {
891         : ${NANO_OBJ:=/usr/obj/nanobsd.${NANO_NAME}}
892         : ${MAKEOBJDIRPREFIX:=${NANO_OBJ}}
893         : ${NANO_DISKIMGDIR:=${NANO_OBJ}}
894         : ${NANO_WORLDDIR:=${NANO_OBJ}/_.w}
895         : ${NANO_LOG:=${NANO_OBJ}}
896         NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build
897         NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install
898
899         # Override user's NANO_DRIVE if they specified a NANO_LABEL
900         [ -n "${NANO_LABEL}" ] && NANO_DRIVE="ufs/${NANO_LABEL}" || true
901
902         # Set a default NANO_TOOLS to NANO_SRC/NANO_TOOLS if it exists.
903         [ ! -d "${NANO_TOOLS}" ] && [ -d "${NANO_SRC}/${NANO_TOOLS}" ] && \
904                 NANO_TOOLS="${NANO_SRC}/${NANO_TOOLS}" || true
905
906         [ -n "${NANO_NOPRIV_BUILD}" ] && [ -z "${NANO_METALOG}" ] && \
907                 NANO_METALOG=${NANO_OBJ}/_.metalog || true
908
909         NANO_STARTTIME=`date +%s`
910         pprint 3 "Exporting NanoBSD variables"
911         export_var MAKEOBJDIRPREFIX
912         export_var NANO_ARCH
913         export_var NANO_CODESIZE
914         export_var NANO_CONFSIZE
915         export_var NANO_CUSTOMIZE
916         export_var NANO_DATASIZE
917         export_var NANO_DRIVE
918         export_var NANO_HEADS
919         export_var NANO_IMAGES
920         export_var NANO_IMGNAME
921         export_var NANO_IMG1NAME
922         export_var NANO_MAKE
923         export_var NANO_MAKE_CONF_BUILD
924         export_var NANO_MAKE_CONF_INSTALL
925         export_var NANO_MEDIASIZE
926         export_var NANO_NAME
927         export_var NANO_NCPU
928         export_var NANO_NEWFS
929         export_var NANO_OBJ
930         export_var NANO_PMAKE
931         export_var NANO_SECTS
932         export_var NANO_SRC
933         export_var NANO_TOOLS
934         export_var NANO_WORLDDIR
935         export_var NANO_BOOT0CFG
936         export_var NANO_BOOTLOADER
937         export_var NANO_LABEL
938         export_var NANO_MODULES
939         export_var NANO_NOPRIV_BUILD
940         export_var NANO_METALOG
941         export_var NANO_LOG
942         export_var SRCCONF
943         export_var SRC_ENV_CONF
944 }