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