defaults.sh revision 297621
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: head/tools/tools/nanobsd/defaults.sh 297621 2016-04-06 19:34:44Z imp $ 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_pkgng() finds packages to install 49NANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg 50NANO_PACKAGE_LIST="*" 51 52# where package metadata gets placed 53NANO_PKG_META_BASE=/var/db 54 55# Object tree directory 56# default is subdir of /usr/obj 57#NANO_OBJ="" 58 59# The directory to put the final images 60# default is ${NANO_OBJ} 61#NANO_DISKIMGDIR="" 62 63# Make & parallel Make 64NANO_MAKE="make" 65NANO_PMAKE="make -j 3" 66 67# The default name for any image we create. 68NANO_IMGNAME="_.disk.full" 69 70# Options to put in make.conf during buildworld only 71CONF_BUILD=' ' 72 73# Options to put in make.conf during installworld only 74CONF_INSTALL=' ' 75 76# Options to put in make.conf during both build- & installworld. 77CONF_WORLD=' ' 78 79# Kernel config file to use 80NANO_KERNEL=GENERIC 81 82# Kernel modules to install. If empty, no modules are installed. 83# Use "default" to install all built modules. 84NANO_MODULES= 85 86# Early customize commands. 87NANO_EARLY_CUSTOMIZE="" 88 89# Customize commands. 90NANO_CUSTOMIZE="" 91 92# Late customize commands. 93NANO_LATE_CUSTOMIZE="" 94 95# Newfs paramters to use 96NANO_NEWFS="-b 4096 -f 512 -i 8192 -U" 97 98# The drive name of the media at runtime 99NANO_DRIVE=ada0 100 101# Target media size in 512 bytes sectors 102NANO_MEDIASIZE=2000000 103 104# Number of code images on media (1 or 2) 105NANO_IMAGES=2 106 107# 0 -> Leave second image all zeroes so it compresses better. 108# 1 -> Initialize second image with a copy of the first 109NANO_INIT_IMG2=1 110 111# Size of code file system in 512 bytes sectors 112# If zero, size will be as large as possible. 113NANO_CODESIZE=0 114 115# Size of configuration file system in 512 bytes sectors 116# Cannot be zero. 117NANO_CONFSIZE=2048 118 119# Size of data file system in 512 bytes sectors 120# If zero: no partition configured. 121# If negative: max size possible 122NANO_DATASIZE=0 123 124# Size of the /etc ramdisk in 512 bytes sectors 125NANO_RAM_ETCSIZE=10240 126 127# Size of the /tmp+/var ramdisk in 512 bytes sectors 128NANO_RAM_TMPVARSIZE=10240 129 130# Media geometry, only relevant if bios doesn't understand LBA. 131NANO_SECTS=63 132NANO_HEADS=16 133 134# boot0 flags/options and configuration 135NANO_BOOT0CFG="-o packet -s 1 -m 3" 136NANO_BOOTLOADER="boot/boot0sio" 137 138# boot2 flags/options 139# default force serial console 140NANO_BOOT2CFG="-h -S115200" 141 142# Backing type of md(4) device 143# Can be "file" or "swap" 144NANO_MD_BACKING="file" 145 146# for swap type md(4) backing, write out the mbr only 147NANO_IMAGE_MBRONLY=true 148 149# Progress Print level 150PPLEVEL=3 151 152# Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label 153# in preference to /dev/${NANO_DRIVE} 154# Root partition will be ${NANO_LABEL}s{1,2} 155# /cfg partition will be ${NANO_LABEL}s3 156# /data partition will be ${NANO_LABEL}s4 157NANO_LABEL="" 158NANO_SLICE_ROOT=s1 159NANO_SLICE_ALTROOT=s2 160NANO_SLICE_CFG=s3 161NANO_SLICE_DATA=s4 162NANO_ROOT=s1a 163NANO_ALTROOT=s2a 164 165# Default ownwership for nopriv build 166NANO_DEF_UNAME=root 167NANO_DEF_GNAME=wheel 168 169####################################################################### 170# Architecture to build. Corresponds to TARGET_ARCH in a buildworld. 171# Unfortunately, there's no way to set TARGET at this time, and it 172# conflates the two, so architectures where TARGET != TARGET_ARCH and 173# TARGET can't be guessed from TARGET_ARCH do not work. This defaults 174# to the arch of the current machine. 175NANO_ARCH=`uname -p` 176 177# CPUTYPE defaults to "" which is the default when CPUTYPE isn't 178# defined. 179NANO_CPUTYPE="" 180 181# Directory to populate /cfg from 182NANO_CFGDIR="" 183 184# Directory to populate /data from 185NANO_DATADIR="" 186 187# We don't need SRCCONF or SRC_ENV_CONF. NanoBSD puts everything we 188# need for the build in files included with __MAKE_CONF. Override in your 189# config file if you really must. We set them unconditionally here, though 190# in case they are stray in the build environment 191SRCCONF=/dev/null 192SRC_ENV_CONF=/dev/null 193 194####################################################################### 195# 196# The functions which do the real work. 197# Can be overridden from the config file(s) 198# 199####################################################################### 200 201# Export values into the shell. Must use { } instead of ( ) like 202# other functions to avoid a subshell. 203# We set __MAKE_CONF as a global since it is easier to get quoting 204# right for paths with spaces in them. 205make_export ( ) { 206 # Similar to export_var, except puts the data out to stdout 207 var=$1 208 eval val=\$$var 209 echo "Setting variable: $var=\"$val\"" 210 export $1 211} 212 213nano_make_build_env ( ) { 214 __MAKE_CONF="${NANO_MAKE_CONF_BUILD}" 215 make_export __MAKE_CONF 216} 217 218nano_make_install_env ( ) { 219 __MAKE_CONF="${NANO_MAKE_CONF_INSTALL}" 220 make_export __MAKE_CONF 221} 222 223# Extra environment variables for kernel builds 224nano_make_kernel_env ( ) { 225 if [ -f ${NANO_KERNEL} ] ; then 226 KERNCONFDIR="$(realpath $(dirname ${NANO_KERNEL}))" 227 KERNCONF="$(basename ${NANO_KERNEL})" 228 make_export KERNCONFDIR 229 make_export KERNCONF 230 else 231 export KERNCONF="${NANO_KERNEL}" 232 make_export KERNCONF 233 fi 234} 235 236nano_global_make_env ( ) ( 237 # global settings for the make.conf file, if set 238 [ -z "${NANO_ARCH}" ] || echo TARGET_ARCH="${NANO_ARCH}" 239 [ -z "${NANO_CPUTYPE}" ] || echo TARGET_CPUTYPE="${NANO_CPUTYPE}" 240) 241 242# rm doesn't know -x prior to FreeBSD 10, so cope with a variety of build 243# hosts for now. This will go away when support in the base goes away. 244rm ( ) { 245 echo "NANO RM $*" 246 case $(uname -r) in 247 7*|8*|9*) command rm $* ;; 248 *) command rm -x $* ;; 249 esac 250} 251 252# 253# Create empty files in the target tree, and record the fact. All paths 254# are relative to NANO_WORLDDIR. 255# 256tgt_touch ( ) ( 257 258 cd "${NANO_WORLDDIR}" 259 for i; do 260 touch $i 261 echo "./${i} type=file" >> ${NANO_METALOG} 262 done 263) 264 265# 266# Convert a directory into a symlink. Takes two arguments, the 267# current directory and what it should become a symlink to. The 268# directory is removed and a symlink is created. If we're doing 269# a nopriv build, then append this fact to the metalog 270# 271tgt_dir2symlink () ( 272 dir=$1 273 symlink=$2 274 275 cd "${NANO_WORLDDIR}" 276 rm -rf "$dir" 277 ln -s "$symlink" "$dir" 278 if [ -n "$NANO_METALOG" ]; then 279 echo "./${dir} type=link mode=0777 link=${symlink}" >> ${NANO_METALOG} 280 fi 281) 282 283# run in the world chroot, errors fatal 284CR ( ) { 285 chroot "${NANO_WORLDDIR}" /bin/sh -exc "$*" 286} 287 288# run in the world chroot, errors not fatal 289CR0 ( ) { 290 chroot "${NANO_WORLDDIR}" /bin/sh -c "$*" || true 291} 292 293nano_cleanup ( ) ( 294 [ $? -eq 0 ] || echo "Error encountered. Check for errors in last log file." 1>&2 295 exit $? 296) 297 298clean_build ( ) ( 299 pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})" 300 301 if ! rm -rf ${MAKEOBJDIRPREFIX}/ > /dev/null 2>&1 ; then 302 chflags -R noschg ${MAKEOBJDIRPREFIX}/ 303 rm -r ${MAKEOBJDIRPREFIX}/ 304 fi 305) 306 307make_conf_build ( ) ( 308 pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)" 309 310 mkdir -p ${MAKEOBJDIRPREFIX} 311 printenv > ${MAKEOBJDIRPREFIX}/_.env 312 313 # Make sure we get all the global settings that NanoBSD wants 314 # in addition to the user's global settings 315 ( 316 nano_global_make_env 317 echo "${CONF_WORLD}" 318 echo "${CONF_BUILD}" 319 ) > ${NANO_MAKE_CONF_BUILD} 320) 321 322build_world ( ) ( 323 pprint 2 "run buildworld" 324 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw" 325 326 ( 327 nano_make_build_env 328 set -o xtrace 329 cd "${NANO_SRC}" 330 ${NANO_PMAKE} buildworld 331 ) > ${MAKEOBJDIRPREFIX}/_.bw 2>&1 332) 333 334build_kernel ( ) ( 335 local extra 336 337 pprint 2 "build kernel ($NANO_KERNEL)" 338 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk" 339 340 ( 341 nano_make_build_env 342 nano_make_kernel_env 343 344 # Note: We intentionally build all modules, not only the ones in 345 # NANO_MODULES so the built world can be reused by multiple images. 346 # Although MODULES_OVERRIDE can be defined in the kenrel config 347 # file to override this behavior. Just set NANO_MODULES=default. 348 set -o xtrace 349 cd "${NANO_SRC}" 350 ${NANO_PMAKE} buildkernel 351 ) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1 352) 353 354clean_world ( ) ( 355 if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then 356 pprint 2 "Clean and create object directory (${NANO_OBJ})" 357 if ! rm -rf ${NANO_OBJ}/ > /dev/null 2>&1 ; then 358 chflags -R noschg ${NANO_OBJ} 359 rm -r ${NANO_OBJ}/ 360 fi 361 mkdir -p "${NANO_OBJ}" "${NANO_WORLDDIR}" 362 printenv > ${NANO_LOG}/_.env 363 else 364 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})" 365 if ! rm -rf "${NANO_WORLDDIR}/" > /dev/null 2>&1 ; then 366 chflags -R noschg "${NANO_WORLDDIR}" 367 rm -rf "${NANO_WORLDDIR}/" 368 fi 369 mkdir -p "${NANO_WORLDDIR}" 370 fi 371) 372 373make_conf_install ( ) ( 374 pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)" 375 376 # Make sure we get all the global settings that NanoBSD wants 377 # in addition to the user's global settings 378 ( 379 nano_global_make_env 380 echo "${CONF_WORLD}" 381 echo "${CONF_INSTALL}" 382 if [ -n "${NANO_NOPRIV_BUILD}" ]; then 383 echo NO_ROOT=t 384 echo METALOG=${NANO_METALOG} 385 fi 386 ) > ${NANO_MAKE_CONF_INSTALL} 387) 388 389install_world ( ) ( 390 pprint 2 "installworld" 391 pprint 3 "log: ${NANO_LOG}/_.iw" 392 393 ( 394 nano_make_install_env 395 set -o xtrace 396 cd "${NANO_SRC}" 397 ${NANO_MAKE} installworld DESTDIR="${NANO_WORLDDIR}" 398 chflags -R noschg "${NANO_WORLDDIR}" 399 ) > ${NANO_LOG}/_.iw 2>&1 400) 401 402install_etc ( ) ( 403 404 pprint 2 "install /etc" 405 pprint 3 "log: ${NANO_LOG}/_.etc" 406 407 ( 408 nano_make_install_env 409 set -o xtrace 410 cd "${NANO_SRC}" 411 ${NANO_MAKE} distribution DESTDIR="${NANO_WORLDDIR}" 412 # make.conf doesn't get created by default, but some ports need it 413 # so they can spam it. 414 cp /dev/null "${NANO_WORLDDIR}"/etc/make.conf 415 ) > ${NANO_LOG}/_.etc 2>&1 416) 417 418install_kernel ( ) ( 419 local extra 420 421 pprint 2 "install kernel ($NANO_KERNEL)" 422 pprint 3 "log: ${NANO_LOG}/_.ik" 423 424 ( 425 426 nano_make_install_env 427 nano_make_kernel_env 428 429 if [ "${NANO_MODULES}" != "default" ]; then 430 MODULES_OVERRIDE="${NANO_MODULES}" 431 make_export MODULES_OVERRIDE 432 fi 433 434 set -o xtrace 435 cd "${NANO_SRC}" 436 ${NANO_MAKE} installkernel DESTDIR="${NANO_WORLDDIR}" 437 438 ) > ${NANO_LOG}/_.ik 2>&1 439) 440 441native_xtools ( ) ( 442 print 2 "Installing the optimized native build tools for cross env" 443 pprint 3 "log: ${NANO_LOG}/_.native_xtools" 444 445 ( 446 447 nano_make_install_env 448 set -o xtrace 449 cd "${NANO_SRC}" 450 ${NANO_MAKE} native-xtools DESTDIR="${NANO_WORLDDIR}" 451 452 ) > ${NANO_LOG}/_.native_xtools 2>&1 453) 454 455# 456# Run the requested set of early customization scripts, run before 457# buildworld. 458# 459run_early_customize() { 460 461 pprint 2 "run early customize scripts" 462 for c in $NANO_EARLY_CUSTOMIZE 463 do 464 pprint 2 "early customize \"$c\"" 465 pprint 3 "log: ${NANO_LOG}/_.early_cust.$c" 466 pprint 4 "`type $c`" 467 { set -x ; $c ; set +x ; } >${NANO_LOG}/_.early_cust.$c 2>&1 468 done 469} 470 471# 472# Run the requested set of customization scripts, run after we've 473# done an installworld, installed the etc files, installed the kernel 474# and tweaked them in the standard way. 475# 476run_customize ( ) ( 477 478 pprint 2 "run customize scripts" 479 for c in $NANO_CUSTOMIZE 480 do 481 pprint 2 "customize \"$c\"" 482 pprint 3 "log: ${NANO_LOG}/_.cust.$c" 483 pprint 4 "`type $c`" 484 ( set -x ; $c ) > ${NANO_LOG}/_.cust.$c 2>&1 485 done 486) 487 488# 489# Run any last-minute customization commands after we've had a chance to 490# setup nanobsd, prune empty dirs from /usr, etc 491# 492run_late_customize ( ) ( 493 494 pprint 2 "run late customize scripts" 495 for c in $NANO_LATE_CUSTOMIZE 496 do 497 pprint 2 "late customize \"$c\"" 498 pprint 3 "log: ${NANO_LOG}/_.late_cust.$c" 499 pprint 4 "`type $c`" 500 ( set -x ; $c ) > ${NANO_LOG}/_.late_cust.$c 2>&1 501 done 502) 503 504# 505# Hook called after we run all the late customize commands, but 506# before we invoke the disk imager. The nopriv build uses it to 507# read in the meta log, apply the changes other parts of nanobsd 508# have been recording their actions. It's not anticipated that 509# a user's cfg file would override this. 510# 511fixup_before_diskimage ( ) ( 512 513 # Run the deduplication script that takes the matalog journal and 514 # combines multiple entries for the same file (see source for 515 # details). We take the extra step of removing the size keywords. This 516 # script, and many of the user scripts, copies, appeneds and otherwise 517 # modifies files in the build, changing their sizes. These actions are 518 # impossible to trap, so go ahead remove the size= keyword. For this 519 # narrow use, it doesn't buy us any protection and just gets in the way. 520 # The dedup tool's output must be sorted due to limitations in awk. 521 if [ -n "${NANO_METALOG}" ]; then 522 pprint 2 "Fixing metalog" 523 cp ${NANO_METALOG} ${NANO_METALOG}.pre 524 echo "/set uname=${NANO_DEF_UNAME} gname=${NANO_DEF_GNAME}" > ${NANO_METALOG} 525 cat ${NANO_METALOG}.pre | ${NANO_TOOLS}/mtree-dedup.awk | \ 526 sed -e 's/ size=[0-9][0-9]*//' | sort >> ${NANO_METALOG} 527 fi 528) 529 530setup_nanobsd ( ) ( 531 pprint 2 "configure nanobsd setup" 532 pprint 3 "log: ${NANO_LOG}/_.dl" 533 534 ( 535 cd "${NANO_WORLDDIR}" 536 537 # Move /usr/local/etc to /etc/local so that the /cfg stuff 538 # can stomp on it. Otherwise packages like ipsec-tools which 539 # have hardcoded paths under ${prefix}/etc are not tweakable. 540 if [ -d usr/local/etc ] ; then 541 ( 542 mkdir -p etc/local 543 cd usr/local/etc 544 find . -print | cpio -dumpl ../../../etc/local 545 cd .. 546 rm -rf etc 547 ln -s ../../etc/local etc 548 ) 549 fi 550 551 for d in var etc 552 do 553 # link /$d under /conf 554 # we use hard links so we have them both places. 555 # the files in /$d will be hidden by the mount. 556 mkdir -p conf/base/$d conf/default/$d 557 find $d -print | cpio -dumpl conf/base/ 558 done 559 560 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size 561 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size 562 563 # pick up config files from the special partition 564 echo "mount -o ro /dev/${NANO_DRIVE}${NANO_SLICE_CFG}" > conf/default/etc/remount 565 566 # Put /tmp on the /var ramdisk (could be symlink already) 567 tgt_dir2symlink tmp var/tmp 568 569 ) > ${NANO_LOG}/_.dl 2>&1 570) 571 572setup_nanobsd_etc ( ) ( 573 pprint 2 "configure nanobsd /etc" 574 575 ( 576 cd "${NANO_WORLDDIR}" 577 578 # create diskless marker file 579 touch etc/diskless 580 581 [ -n "${NANO_NOPRIV_BUILD}" ] && chmod 666 etc/defaults/rc.conf 582 583 # Make root filesystem R/O by default 584 echo "root_rw_mount=NO" >> etc/defaults/rc.conf 585 # Disable entropy file, since / is read-only /var/db/entropy should be enough? 586 echo "entropy_file=NO" >> etc/defaults/rc.conf 587 588 [ -n "${NANO_NOPRIV_BUILD}" ] && chmod 444 etc/defaults/rc.conf 589 590 # save config file for scripts 591 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf 592 593 echo "/dev/${NANO_DRIVE}${NANO_ROOT} / ufs ro 1 1" > etc/fstab 594 echo "/dev/${NANO_DRIVE}${NANO_SLICE_CFG} /cfg ufs rw,noauto 2 2" >> etc/fstab 595 mkdir -p cfg 596 ) 597) 598 599prune_usr ( ) ( 600 601 # Remove all empty directories in /usr 602 find "${NANO_WORLDDIR}"/usr -type d -depth -print | 603 while read d 604 do 605 rmdir $d > /dev/null 2>&1 || true 606 done 607) 608 609newfs_part ( ) ( 610 local dev mnt lbl 611 dev=$1 612 mnt=$2 613 lbl=$3 614 echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev} 615 newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev} 616 mount -o async ${dev} ${mnt} 617) 618 619# Convenient spot to work around any umount issues that your build environment 620# hits by overriding this method. 621nano_umount ( ) ( 622 umount ${1} 623) 624 625populate_slice ( ) ( 626 local dev dir mnt lbl 627 dev=$1 628 dir=$2 629 mnt=$3 630 lbl=$4 631 echo "Creating ${dev} (mounting on ${mnt})" 632 newfs_part ${dev} ${mnt} ${lbl} 633 if [ -n "${dir}" -a -d "${dir}" ]; then 634 echo "Populating ${lbl} from ${dir}" 635 cd "${dir}" 636 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt} 637 fi 638 df -i ${mnt} 639 nano_umount ${mnt} 640) 641 642populate_cfg_slice ( ) ( 643 populate_slice "$1" "$2" "$3" "$4" 644) 645 646populate_data_slice ( ) ( 647 populate_slice "$1" "$2" "$3" "$4" 648) 649 650create_diskimage ( ) ( 651 pprint 2 "build diskimage" 652 pprint 3 "log: ${NANO_LOG}/_.di" 653 654 ( 655 echo $NANO_MEDIASIZE $NANO_IMAGES \ 656 $NANO_SECTS $NANO_HEADS \ 657 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE | 658 awk ' 659 { 660 printf "# %s\n", $0 661 662 # size of cylinder in sectors 663 cs = $3 * $4 664 665 # number of full cylinders on media 666 cyl = int ($1 / cs) 667 668 # output fdisk geometry spec, truncate cyls to 1023 669 if (cyl <= 1023) 670 print "g c" cyl " h" $4 " s" $3 671 else 672 print "g c" 1023 " h" $4 " s" $3 673 674 if ($7 > 0) { 675 # size of data partition in full cylinders 676 dsl = int (($7 + cs - 1) / cs) 677 } else { 678 dsl = 0; 679 } 680 681 # size of config partition in full cylinders 682 csl = int (($6 + cs - 1) / cs) 683 684 if ($5 == 0) { 685 # size of image partition(s) in full cylinders 686 isl = int ((cyl - dsl - csl) / $2) 687 } else { 688 isl = int (($5 + cs - 1) / cs) 689 } 690 691 # First image partition start at second track 692 print "p 1 165 " $3, isl * cs - $3 693 c = isl * cs; 694 695 # Second image partition (if any) also starts offset one 696 # track to keep them identical. 697 if ($2 > 1) { 698 print "p 2 165 " $3 + c, isl * cs - $3 699 c += isl * cs; 700 } 701 702 # Config partition starts at cylinder boundary. 703 print "p 3 165 " c, csl * cs 704 c += csl * cs 705 706 # Data partition (if any) starts at cylinder boundary. 707 if ($7 > 0) { 708 print "p 4 165 " c, dsl * cs 709 } else if ($7 < 0 && $1 > c) { 710 print "p 4 165 " c, $1 - c 711 } else if ($1 < c) { 712 print "Disk space overcommitted by", \ 713 c - $1, "sectors" > "/dev/stderr" 714 exit 2 715 } 716 717 # Force slice 1 to be marked active. This is necessary 718 # for booting the image from a USB device to work. 719 print "a 1" 720 } 721 ' > ${NANO_LOG}/_.fdisk 722 723 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME} 724 MNT=${NANO_OBJ}/_.mnt 725 mkdir -p ${MNT} 726 727 if [ "${NANO_MD_BACKING}" = "swap" ] ; then 728 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \ 729 -y ${NANO_HEADS}` 730 else 731 echo "Creating md backing file..." 732 rm -f ${IMG} 733 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0 734 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \ 735 -y ${NANO_HEADS}` 736 fi 737 738 trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT 739 740 fdisk -i -f ${NANO_LOG}/_.fdisk ${MD} 741 fdisk ${MD} 742 # XXX: params 743 # XXX: pick up cached boot* files, they may not be in image anymore. 744 if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then 745 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD} 746 fi 747 if [ -f ${NANO_WORLDDIR}/boot/boot ]; then 748 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}${NANO_SLICE_ROOT} 749 else 750 bsdlabel -w ${MD}${NANO_SLICE_ROOT} 751 fi 752 bsdlabel ${MD}${NANO_SLICE_ROOT} 753 754 # Create first image 755 populate_slice /dev/${MD}${NANO_ROOT} ${NANO_WORLDDIR} ${MNT} "${NANO_ROOT}" 756 mount /dev/${MD}${NANO_ROOT} ${MNT} 757 echo "Generating mtree..." 758 ( cd "${MNT}" && mtree -c ) > ${NANO_LOG}/_.mtree 759 ( cd "${MNT}" && du -k ) > ${NANO_LOG}/_.du 760 nano_umount "${MNT}" 761 762 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then 763 # Duplicate to second image (if present) 764 echo "Duplicating to second image..." 765 dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=/dev/${MD}${NANO_SLICE_ALTROOT} bs=64k 766 mount /dev/${MD}${NANO_ALTROOT} ${MNT} 767 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab 768 do 769 sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f 770 done 771 nano_umount ${MNT} 772 # Override the label from the first partition so we 773 # don't confuse glabel with duplicates. 774 if [ -n "${NANO_LABEL}" ]; then 775 tunefs -L ${NANO_LABEL}"${NANO_ALTROOT}" /dev/${MD}${NANO_ALTROOT} 776 fi 777 fi 778 779 # Create Config slice 780 populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}" 781 782 # Create Data slice, if any. 783 if [ -n "$NANO_SLICE_DATA" -a "$NANO_SLICE_CFG" = "$NANO_SLICE_DATA" -a \ 784 "$NANO_DATASIZE" -ne 0 ]; then 785 pprint 2 "NANO_SLICE_DATA is the same as NANO_SLICE_CFG, fix." 786 exit 2 787 fi 788 if [ $NANO_DATASIZE -ne 0 -a -n "$NANO_SLICE_DATA" ] ; then 789 populate_data_slice /dev/${MD}${NANO_SLICE_DATA} "${NANO_DATADIR}" ${MNT} "${NANO_SLICE_DATA}" 790 fi 791 792 if [ "${NANO_MD_BACKING}" = "swap" ] ; then 793 if [ ${NANO_IMAGE_MBRONLY} ]; then 794 echo "Writing out _.disk.mbr..." 795 dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1 796 else 797 echo "Writing out ${NANO_IMGNAME}..." 798 dd if=/dev/${MD} of=${IMG} bs=64k 799 fi 800 801 echo "Writing out ${NANO_IMGNAME}..." 802 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k 803 fi 804 805 if ${do_copyout_partition} ; then 806 echo "Writing out _.disk.image..." 807 dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=${NANO_DISKIMGDIR}/_.disk.image bs=64k 808 fi 809 mdconfig -d -u $MD 810 811 trap - 1 2 15 812 trap nano_cleanup EXIT 813 814 ) > ${NANO_LOG}/_.di 2>&1 815) 816 817last_orders ( ) ( 818 # Redefine this function with any last orders you may have 819 # after the build completed, for instance to copy the finished 820 # image to a more convenient place: 821 # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk 822 true 823) 824 825####################################################################### 826# 827# Optional convenience functions. 828# 829####################################################################### 830 831####################################################################### 832# Common Flash device geometries 833# 834 835FlashDevice ( ) { 836 if [ -d ${NANO_TOOLS} ] ; then 837 . ${NANO_TOOLS}/FlashDevice.sub 838 else 839 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub 840 fi 841 sub_FlashDevice $1 $2 842} 843 844####################################################################### 845# USB device geometries 846# 847# Usage: 848# UsbDevice Generic 1000 # a generic flash key sold as having 1GB 849# 850# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you. 851# 852# Note that the capacity of a flash key is usually advertised in MB or 853# GB, *not* MiB/GiB. As such, the precise number of cylinders available 854# for C/H/S geometry may vary depending on the actual flash geometry. 855# 856# The following generic device layouts are understood: 857# generic An alias for generic-hdd. 858# generic-hdd 255H 63S/T xxxxC with no MBR restrictions. 859# generic-fdd 64H 32S/T xxxxC with no MBR restrictions. 860# 861# The generic-hdd device is preferred for flash devices larger than 1GB. 862# 863 864UsbDevice ( ) { 865 a1=`echo $1 | tr '[:upper:]' '[:lower:]'` 866 case $a1 in 867 generic-fdd) 868 NANO_HEADS=64 869 NANO_SECTS=32 870 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 871 ;; 872 generic|generic-hdd) 873 NANO_HEADS=255 874 NANO_SECTS=63 875 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 876 ;; 877 *) 878 echo "Unknown USB flash device" 879 exit 2 880 ;; 881 esac 882} 883 884####################################################################### 885# Setup serial console 886 887cust_comconsole ( ) ( 888 # Enable getty on console 889 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys 890 891 # Disable getty on syscons devices 892 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys 893 894 # Tell loader to use serial console early. 895 echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config 896) 897 898####################################################################### 899# Allow root login via ssh 900 901cust_allow_ssh_root ( ) ( 902 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \ 903 ${NANO_WORLDDIR}/etc/ssh/sshd_config 904) 905 906####################################################################### 907# Install the stuff under ./Files 908 909cust_install_files ( ) ( 910 cd "${NANO_TOOLS}/Files" 911 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR} 912) 913 914####################################################################### 915# Install packages from ${NANO_PACKAGE_DIR} 916 917cust_pkgng ( ) ( 918 919 local PKG_CONF="${NANO_WORLDDIR}/usr/local/etc/pkg.conf" 920 local PKGCMD="env ASSUME_ALWAYS_YES=YES PKG_DBDIR=${NANO_PKG_META_BASE}/pkg SIGNATURE_TYPE=none /usr/sbin/pkg" 921 922 # Ensure pkg.conf points pkg to where the package meta data lives. 923 touch ${PKG_CONF} 924 if grep -Eiq '^PKG_DBDIR:.*' ${PKG_CONF}; then 925 sed -i -e "\|^PKG_DBDIR:.*|Is||PKG_DBDIR: "\"${NANO_PKG_META_BASE}/pkg\""|" ${PKG_CONF} 926 else 927 echo "PKG_DBDIR: \"${NANO_PKG_META_BASE}/pkg\"" >> ${PKG_CONF} 928 fi 929 930 # If the package directory doesn't exist, we're done. 931 if [ ! -d ${NANO_PACKAGE_DIR} ]; then 932 echo "DONE 0 packages" 933 return 0 934 fi 935 936 # Find a pkg-* package 937 for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do 938 _NANO_PKG_PACKAGE=`basename "$x"` 939 done 940 if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then 941 echo "FAILED: need a pkg/ package for bootstrapping" 942 exit 2 943 fi 944 NANO_PACKAGE_LIST="${_NANO_PKG_PACKAGE} ${NANO_PACKAGE_LIST}" 945 946 # Mount packages into chroot 947 mkdir -p ${NANO_WORLDDIR}/_.p 948 mount -t nullfs -o noatime -o ro ${NANO_PACKAGE_DIR} ${NANO_WORLDDIR}/_.p 949 950 trap "umount ${NANO_WORLDDIR}/_.p ; rm -rf ${NANO_WORLDDIR}/_.p" 1 2 15 951 952 # Install packages 953 todo="$(echo "${NANO_PACKAGE_LIST}" | awk '{ print NF }')" 954 echo "=== TODO: $todo" 955 echo "${NANO_PACKAGE_LIST}" 956 echo "===" 957 for _PKG in ${NANO_PACKAGE_LIST}; do 958 CR "${PKGCMD} add /_.p/${_PKG}" 959 done 960 961 CR0 "${PKGCMD} info" 962 963 trap - 1 2 15 EXIT 964 umount ${NANO_WORLDDIR}/_.p 965 rm -rf ${NANO_WORLDDIR}/_.p 966) 967 968####################################################################### 969# Convenience function: 970# Register all args as early customize function to run just before 971# build commences. 972 973early_customize_cmd () { 974 NANO_EARLY_CUSTOMIZE="$NANO_EARLY_CUSTOMIZE $*" 975} 976 977####################################################################### 978# Convenience function: 979# Register all args as customize function. 980 981customize_cmd ( ) { 982 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*" 983} 984 985####################################################################### 986# Convenience function: 987# Register all args as late customize function to run just before 988# image creation. 989 990late_customize_cmd ( ) { 991 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*" 992} 993 994####################################################################### 995# 996# All set up to go... 997# 998####################################################################### 999 1000# Progress Print 1001# Print $2 at level $1. 1002pprint ( ) ( 1003 if [ "$1" -le $PPLEVEL ]; then 1004 runtime=$(( `date +%s` - $NANO_STARTTIME )) 1005 printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3 1006 fi 1007) 1008 1009usage ( ) { 1010 ( 1011 echo "Usage: $0 [-bfiKknqvw] [-c config_file]" 1012 echo " -b suppress builds (both kernel and world)" 1013 echo " -c specify config file" 1014 echo " -f suppress code slice extraction" 1015 echo " -i suppress disk image build" 1016 echo " -K suppress installkernel" 1017 echo " -k suppress buildkernel" 1018 echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc" 1019 echo " -q make output more quiet" 1020 echo " -v make output more verbose" 1021 echo " -w suppress buildworld" 1022 ) 1>&2 1023 exit 2 1024} 1025 1026####################################################################### 1027# Setup and Export Internal variables 1028# 1029 1030export_var ( ) { # Don't wawnt a subshell 1031 var=$1 1032 # Lookup value of the variable. 1033 eval val=\$$var 1034 pprint 3 "Setting variable: $var=\"$val\"" 1035 export $1 1036} 1037 1038# Call this function to set defaults _after_ parsing options. 1039# dont want a subshell otherwise variable setting is thrown away. 1040set_defaults_and_export ( ) { 1041 : ${NANO_OBJ:=/usr/obj/nanobsd.${NANO_NAME}} 1042 : ${MAKEOBJDIRPREFIX:=${NANO_OBJ}} 1043 : ${NANO_DISKIMGDIR:=${NANO_OBJ}} 1044 : ${NANO_WORLDDIR:=${NANO_OBJ}/_.w} 1045 : ${NANO_LOG:=${NANO_OBJ}} 1046 NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build 1047 NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install 1048 1049 # Override user's NANO_DRIVE if they specified a NANO_LABEL 1050 [ -n "${NANO_LABEL}" ] && NANO_DRIVE="ufs/${NANO_LABEL}" || true 1051 1052 # Set a default NANO_TOOLS to NANO_SRC/NANO_TOOLS if it exists. 1053 [ ! -d "${NANO_TOOLS}" ] && [ -d "${NANO_SRC}/${NANO_TOOLS}" ] && \ 1054 NANO_TOOLS="${NANO_SRC}/${NANO_TOOLS}" || true 1055 1056 [ -n "${NANO_NOPRIV_BUILD}" ] && [ -z "${NANO_METALOG}" ] && \ 1057 NANO_METALOG=${NANO_OBJ}/_.metalog || true 1058 1059 NANO_STARTTIME=`date +%s` 1060 pprint 3 "Exporting NanoBSD variables" 1061 export_var MAKEOBJDIRPREFIX 1062 export_var NANO_ARCH 1063 export_var NANO_CODESIZE 1064 export_var NANO_CONFSIZE 1065 export_var NANO_CUSTOMIZE 1066 export_var NANO_DATASIZE 1067 export_var NANO_DRIVE 1068 export_var NANO_HEADS 1069 export_var NANO_IMAGES 1070 export_var NANO_IMGNAME 1071 export_var NANO_MAKE 1072 export_var NANO_MAKE_CONF_BUILD 1073 export_var NANO_MAKE_CONF_INSTALL 1074 export_var NANO_MEDIASIZE 1075 export_var NANO_NAME 1076 export_var NANO_NEWFS 1077 export_var NANO_OBJ 1078 export_var NANO_PMAKE 1079 export_var NANO_SECTS 1080 export_var NANO_SRC 1081 export_var NANO_TOOLS 1082 export_var NANO_WORLDDIR 1083 export_var NANO_BOOT0CFG 1084 export_var NANO_BOOTLOADER 1085 export_var NANO_LABEL 1086 export_var NANO_MODULES 1087 export_var NANO_NOPRIV_BUILD 1088 export_var NANO_METALOG 1089 export_var NANO_LOG 1090 export_var SRCCONF 1091 export_var SRC_ENV_CONF 1092} 1093