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