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}" > ${NANO_METALOG} 504 cat ${NANO_METALOG}.pre | ${NANO_TOOLS}/mtree-dedup.awk | \ 505 sed -e 's/ size=[0-9][0-9]*//' | sort >> ${NANO_METALOG} 506 fi 507) 508 509setup_nanobsd ( ) ( 510 pprint 2 "configure nanobsd setup" 511 pprint 3 "log: ${NANO_OBJ}/_.dl" 512 513 ( 514 cd "${NANO_WORLDDIR}" 515 516 # Move /usr/local/etc to /etc/local so that the /cfg stuff 517 # can stomp on it. Otherwise packages like ipsec-tools which 518 # have hardcoded paths under ${prefix}/etc are not tweakable. 519 if [ -d usr/local/etc ] ; then 520 ( 521 mkdir -p etc/local 522 cd usr/local/etc 523 find . -print | cpio -dumpl ../../../etc/local 524 cd .. 525 rm -rf etc 526 ln -s ../../etc/local etc 527 ) 528 fi 529 530 for d in var etc 531 do 532 # link /$d under /conf 533 # we use hard links so we have them both places. 534 # the files in /$d will be hidden by the mount. 535 mkdir -p conf/base/$d conf/default/$d 536 find $d -print | cpio -dumpl conf/base/ 537 done 538 539 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size 540 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size 541 542 # pick up config files from the special partition 543 echo "mount -o ro /dev/${NANO_DRIVE}${NANO_SLICE_CFG}" > conf/default/etc/remount 544 545 # Put /tmp on the /var ramdisk (could be symlink already) 546 tgt_dir2symlink tmp var/tmp 547 548 ) > ${NANO_OBJ}/_.dl 2>&1 549) 550 551setup_nanobsd_etc ( ) ( 552 pprint 2 "configure nanobsd /etc" 553 554 ( 555 cd "${NANO_WORLDDIR}" 556 557 # create diskless marker file 558 touch etc/diskless 559 560 [ -n "${NANO_NOPRIV_BUILD}" ] && chmod 666 etc/defaults/rc.conf 561 562 # Make root filesystem R/O by default 563 echo "root_rw_mount=NO" >> etc/defaults/rc.conf 564 # Disable entropy file, since / is read-only /var/db/entropy should be enough? 565 echo "entropy_file=NO" >> etc/defaults/rc.conf 566 567 [ -n "${NANO_NOPRIV_BUILD}" ] && chmod 444 etc/defaults/rc.conf 568 569 # save config file for scripts 570 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf 571 572 echo "/dev/${NANO_DRIVE}${NANO_SLICE_ROOT}a / ufs ro 1 1" > etc/fstab 573 echo "/dev/${NANO_DRIVE}${NANO_SLICE_CFG} /cfg ufs rw,noauto 2 2" >> etc/fstab 574 mkdir -p cfg 575 ) 576) 577 578prune_usr ( ) ( 579 580 # Remove all empty directories in /usr 581 find "${NANO_WORLDDIR}"/usr -type d -depth -print | 582 while read d 583 do 584 rmdir $d > /dev/null 2>&1 || true 585 done 586) 587 588newfs_part ( ) ( 589 local dev mnt lbl 590 dev=$1 591 mnt=$2 592 lbl=$3 593 echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev} 594 newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev} 595 mount -o async ${dev} ${mnt} 596) 597 598# Convenient spot to work around any umount issues that your build environment 599# hits by overriding this method. 600nano_umount ( ) ( 601 umount ${1} 602) 603 604populate_slice ( ) ( 605 local dev dir mnt lbl 606 dev=$1 607 dir=$2 608 mnt=$3 609 lbl=$4 610 echo "Creating ${dev} (mounting on ${mnt})" 611 newfs_part ${dev} ${mnt} ${lbl} 612 if [ -n "${dir}" -a -d "${dir}" ]; then 613 echo "Populating ${lbl} from ${dir}" 614 cd "${dir}" 615 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${mnt} 616 fi 617 df -i ${mnt} 618 nano_umount ${mnt} 619) 620 621populate_cfg_slice ( ) ( 622 populate_slice "$1" "$2" "$3" "$4" 623) 624 625populate_data_slice ( ) ( 626 populate_slice "$1" "$2" "$3" "$4" 627) 628 629create_diskimage ( ) ( 630 pprint 2 "build diskimage" 631 pprint 3 "log: ${NANO_OBJ}/_.di" 632 633 ( 634 echo $NANO_MEDIASIZE $NANO_IMAGES \ 635 $NANO_SECTS $NANO_HEADS \ 636 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE | 637 awk ' 638 { 639 printf "# %s\n", $0 640 641 # size of cylinder in sectors 642 cs = $3 * $4 643 644 # number of full cylinders on media 645 cyl = int ($1 / cs) 646 647 # output fdisk geometry spec, truncate cyls to 1023 648 if (cyl <= 1023) 649 print "g c" cyl " h" $4 " s" $3 650 else 651 print "g c" 1023 " h" $4 " s" $3 652 653 if ($7 > 0) { 654 # size of data partition in full cylinders 655 dsl = int (($7 + cs - 1) / cs) 656 } else { 657 dsl = 0; 658 } 659 660 # size of config partition in full cylinders 661 csl = int (($6 + cs - 1) / cs) 662 663 if ($5 == 0) { 664 # size of image partition(s) in full cylinders 665 isl = int ((cyl - dsl - csl) / $2) 666 } else { 667 isl = int (($5 + cs - 1) / cs) 668 } 669 670 # First image partition start at second track 671 print "p 1 165 " $3, isl * cs - $3 672 c = isl * cs; 673 674 # Second image partition (if any) also starts offset one 675 # track to keep them identical. 676 if ($2 > 1) { 677 print "p 2 165 " $3 + c, isl * cs - $3 678 c += isl * cs; 679 } 680 681 # Config partition starts at cylinder boundary. 682 print "p 3 165 " c, csl * cs 683 c += csl * cs 684 685 # Data partition (if any) starts at cylinder boundary. 686 if ($7 > 0) { 687 print "p 4 165 " c, dsl * cs 688 } else if ($7 < 0 && $1 > c) { 689 print "p 4 165 " c, $1 - c 690 } else if ($1 < c) { 691 print "Disk space overcommitted by", \ 692 c - $1, "sectors" > "/dev/stderr" 693 exit 2 694 } 695 696 # Force slice 1 to be marked active. This is necessary 697 # for booting the image from a USB device to work. 698 print "a 1" 699 } 700 ' > ${NANO_OBJ}/_.fdisk 701 702 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME} 703 MNT=${NANO_OBJ}/_.mnt 704 mkdir -p ${MNT} 705 706 if [ "${NANO_MD_BACKING}" = "swap" ] ; then 707 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \ 708 -y ${NANO_HEADS}` 709 else 710 echo "Creating md backing file..." 711 rm -f ${IMG} 712 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0 713 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \ 714 -y ${NANO_HEADS}` 715 fi 716 717 trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT 718 719 fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD} 720 fdisk ${MD} 721 # XXX: params 722 # XXX: pick up cached boot* files, they may not be in image anymore. 723 if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then 724 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD} 725 fi 726 if [ -f ${NANO_WORLDDIR}/boot/boot ]; then 727 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}${NANO_SLICE_ROOT} 728 else 729 bsdlabel -w ${MD}${NANO_SLICE_ROOT} 730 fi 731 bsdlabel ${MD}${NANO_SLICE_ROOT} 732 733 # Create first image 734 populate_slice /dev/${MD}${NANO_SLICE_ROOT}a ${NANO_WORLDDIR} ${MNT} "${NANO_SLICE_ROOT}a" 735 mount /dev/${MD}${NANO_SLICE_ROOT}a ${MNT} 736 echo "Generating mtree..." 737 ( cd "${MNT}" && mtree -c ) > ${NANO_OBJ}/_.mtree 738 ( cd "${MNT}" && du -k ) > ${NANO_OBJ}/_.du 739 nano_umount "${MNT}" 740 741 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then 742 # Duplicate to second image (if present) 743 echo "Duplicating to second image..." 744 dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=/dev/${MD}${NANO_SLICE_ALTROOT} bs=64k 745 mount /dev/${MD}${NANO_SLICE_ALTROOT}a ${MNT} 746 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab 747 do 748 sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f 749 done 750 nano_umount ${MNT} 751 # Override the label from the first partition so we 752 # don't confuse glabel with duplicates. 753 if [ -n "${NANO_LABEL}" ]; then 754 tunefs -L ${NANO_LABEL}"${NANO_SLICE_ALTROOT}a" /dev/${MD}${NANO_SLICE_ALTROOT}a 755 fi 756 fi 757 758 # Create Config slice 759 populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}" 760 761 # Create Data slice, if any. 762 if [ -n "$NANO_SLICE_DATA" -a "$NANO_SLICE_CFG" = "$NANO_SLICE_DATA" -a \ 763 "$NANO_DATASIZE" -ne 0 ]; then 764 pprint 2 "NANO_SLICE_DATA is the same as NANO_SLICE_CFG, fix." 765 exit 2 766 fi 767 if [ $NANO_DATASIZE -ne 0 -a -n "$NANO_SLICE_DATA" ] ; then 768 populate_data_slice /dev/${MD}${NANO_SLICE_DATA} "${NANO_DATADIR}" ${MNT} "${NANO_SLICE_DATA}" 769 fi 770 771 if [ "${NANO_MD_BACKING}" = "swap" ] ; then 772 if [ ${NANO_IMAGE_MBRONLY} ]; then 773 echo "Writing out _.disk.mbr..." 774 dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1 775 else 776 echo "Writing out ${NANO_IMGNAME}..." 777 dd if=/dev/${MD} of=${IMG} bs=64k 778 fi 779 780 echo "Writing out ${NANO_IMGNAME}..." 781 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k 782 fi 783 784 if ${do_copyout_partition} ; then 785 echo "Writing out _.disk.image..." 786 dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=${NANO_DISKIMGDIR}/_.disk.image bs=64k 787 fi 788 mdconfig -d -u $MD 789 790 trap - 1 2 15 791 trap nano_cleanup EXIT 792 793 ) > ${NANO_OBJ}/_.di 2>&1 794) 795 796last_orders ( ) ( 797 # Redefine this function with any last orders you may have 798 # after the build completed, for instance to copy the finished 799 # image to a more convenient place: 800 # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk 801 true 802) 803 804####################################################################### 805# 806# Optional convenience functions. 807# 808####################################################################### 809 810####################################################################### 811# Common Flash device geometries 812# 813 814FlashDevice ( ) { 815 if [ -d ${NANO_TOOLS} ] ; then 816 . ${NANO_TOOLS}/FlashDevice.sub 817 else 818 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub 819 fi 820 sub_FlashDevice $1 $2 821} 822 823####################################################################### 824# USB device geometries 825# 826# Usage: 827# UsbDevice Generic 1000 # a generic flash key sold as having 1GB 828# 829# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you. 830# 831# Note that the capacity of a flash key is usually advertised in MB or 832# GB, *not* MiB/GiB. As such, the precise number of cylinders available 833# for C/H/S geometry may vary depending on the actual flash geometry. 834# 835# The following generic device layouts are understood: 836# generic An alias for generic-hdd. 837# generic-hdd 255H 63S/T xxxxC with no MBR restrictions. 838# generic-fdd 64H 32S/T xxxxC with no MBR restrictions. 839# 840# The generic-hdd device is preferred for flash devices larger than 1GB. 841# 842 843UsbDevice ( ) { 844 a1=`echo $1 | tr '[:upper:]' '[:lower:]'` 845 case $a1 in 846 generic-fdd) 847 NANO_HEADS=64 848 NANO_SECTS=32 849 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 850 ;; 851 generic|generic-hdd) 852 NANO_HEADS=255 853 NANO_SECTS=63 854 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 855 ;; 856 *) 857 echo "Unknown USB flash device" 858 exit 2 859 ;; 860 esac 861} 862 863####################################################################### 864# Setup serial console 865 866cust_comconsole ( ) ( 867 # Enable getty on console 868 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys 869 870 # Disable getty on syscons devices 871 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys 872 873 # Tell loader to use serial console early. 874 echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config 875) 876 877####################################################################### 878# Allow root login via ssh 879 880cust_allow_ssh_root ( ) ( 881 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \ 882 ${NANO_WORLDDIR}/etc/ssh/sshd_config 883) 884 885####################################################################### 886# Install the stuff under ./Files 887 888cust_install_files ( ) ( 889 cd "${NANO_TOOLS}/Files" 890 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -Ldumpv ${NANO_WORLDDIR} 891) 892 893####################################################################### 894# Install packages from ${NANO_PACKAGE_DIR} 895 896cust_pkgng ( ) ( 897 898 # If the package directory doesn't exist, we're done. 899 if [ ! -d ${NANO_PACKAGE_DIR} ]; then 900 echo "DONE 0 packages" 901 return 0 902 fi 903 904 # Find a pkg-* package 905 for x in `find -s ${NANO_PACKAGE_DIR} -iname 'pkg-*'`; do 906 _NANO_PKG_PACKAGE=`basename "$x"` 907 done 908 if [ -z "${_NANO_PKG_PACKAGE}" -o ! -f "${NANO_PACKAGE_DIR}/${_NANO_PKG_PACKAGE}" ]; then 909 echo "FAILED: need a pkg/ package for bootstrapping" 910 exit 2 911 fi 912 913 # Copy packages into chroot 914 mkdir -p ${NANO_WORLDDIR}/Pkg 915 ( 916 cd "${NANO_PACKAGE_DIR}" 917 find "${NANO_PACKAGE_LIST}" -print | 918 cpio -Ldumpv ${NANO_WORLDDIR}/Pkg 919 ) 920 921 #Bootstrap pkg 922 CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /Pkg/${_NANO_PKG_PACKAGE} 923 CR pkg -N >/dev/null 2>&1 924 if [ "$?" -ne "0" ]; then 925 echo "FAILED: pkg bootstrapping faied" 926 exit 2 927 fi 928 rm -f ${NANO_WORLDDIR}/Pkg/pkg-* 929 930 # Count & report how many we have to install 931 todo=`ls ${NANO_WORLDDIR}/Pkg | /usr/bin/wc -l` 932 todo=$(expr $todo + 1) # add one for pkg since it is installed already 933 echo "=== TODO: $todo" 934 ls ${NANO_WORLDDIR}/Pkg 935 echo "===" 936 while true 937 do 938 # Record how many we have now 939 have=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l) 940 941 # Attempt to install more packages 942 CR0 'ls 'Pkg/*txz' | xargs env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg add' 943 944 # See what that got us 945 now=$(CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info | /usr/bin/wc -l) 946 echo "=== NOW $now" 947 CR env ASSUME_ALWAYS_YES=YES /usr/sbin/pkg info 948 echo "===" 949 if [ $now -eq $todo ] ; then 950 echo "DONE $now packages" 951 break 952 elif [ $now -eq $have ] ; then 953 echo "FAILED: Nothing happened on this pass" 954 exit 2 955 fi 956 done 957 rm -rf ${NANO_WORLDDIR}/Pkg 958) 959 960####################################################################### 961# Convenience function: 962# Register all args as customize function. 963 964customize_cmd ( ) { 965 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*" 966} 967 968####################################################################### 969# Convenience function: 970# Register all args as late customize function to run just before 971# image creation. 972 973late_customize_cmd ( ) { 974 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*" 975} 976 977####################################################################### 978# 979# All set up to go... 980# 981####################################################################### 982 983# Progress Print 984# Print $2 at level $1. 985pprint ( ) ( 986 if [ "$1" -le $PPLEVEL ]; then 987 runtime=$(( `date +%s` - $NANO_STARTTIME )) 988 printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3 989 fi 990) 991 992usage ( ) { 993 ( 994 echo "Usage: $0 [-bfiKknqvw] [-c config_file]" 995 echo " -b suppress builds (both kernel and world)" 996 echo " -c specify config file" 997 echo " -f suppress code slice extraction" 998 echo " -i suppress disk image build" 999 echo " -K suppress installkernel" 1000 echo " -k suppress buildkernel" 1001 echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc" 1002 echo " -q make output more quiet" 1003 echo " -v make output more verbose" 1004 echo " -w suppress buildworld" 1005 ) 1>&2 1006 exit 2 1007} 1008 1009####################################################################### 1010# Setup and Export Internal variables 1011# 1012 1013export_var ( ) { # Don't wawnt a subshell 1014 var=$1 1015 # Lookup value of the variable. 1016 eval val=\$$var 1017 pprint 3 "Setting variable: $var=\"$val\"" 1018 export $1 1019} 1020 1021# Call this function to set defaults _after_ parsing options. 1022# dont want a subshell otherwise variable setting is thrown away. 1023set_defaults_and_export ( ) { 1024 : ${NANO_OBJ:=/usr/obj/nanobsd.${NANO_NAME}} 1025 : ${MAKEOBJDIRPREFIX:=${NANO_OBJ}} 1026 : ${NANO_DISKIMGDIR:=${NANO_OBJ}} 1027 NANO_WORLDDIR=${NANO_OBJ}/_.w 1028 NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build 1029 NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install 1030 1031 # Override user's NANO_DRIVE if they specified a NANO_LABEL 1032 [ -n "${NANO_LABEL}" ] && NANO_DRIVE="ufs/${NANO_LABEL}" || true 1033 1034 # Set a default NANO_TOOLS to NANO_SRC/NANO_TOOLS if it exists. 1035 [ ! -d "${NANO_TOOLS}" ] && [ -d "${NANO_SRC}/${NANO_TOOLS}" ] && \ 1036 NANO_TOOLS="${NANO_SRC}/${NANO_TOOLS}" || true 1037 1038 [ -n "${NANO_NOPRIV_BUILD}" ] && [ -z "${NANO_METALOG}" ] && \ 1039 NANO_METALOG=${NANO_OBJ}/_.metalog || true 1040 1041 NANO_STARTTIME=`date +%s` 1042 pprint 3 "Exporting NanoBSD variables" 1043 export_var MAKEOBJDIRPREFIX 1044 export_var NANO_ARCH 1045 export_var NANO_CODESIZE 1046 export_var NANO_CONFSIZE 1047 export_var NANO_CUSTOMIZE 1048 export_var NANO_DATASIZE 1049 export_var NANO_DRIVE 1050 export_var NANO_HEADS 1051 export_var NANO_IMAGES 1052 export_var NANO_IMGNAME 1053 export_var NANO_MAKE 1054 export_var NANO_MAKE_CONF_BUILD 1055 export_var NANO_MAKE_CONF_INSTALL 1056 export_var NANO_MEDIASIZE 1057 export_var NANO_NAME 1058 export_var NANO_NEWFS 1059 export_var NANO_OBJ 1060 export_var NANO_PMAKE 1061 export_var NANO_SECTS 1062 export_var NANO_SRC 1063 export_var NANO_TOOLS 1064 export_var NANO_WORLDDIR 1065 export_var NANO_BOOT0CFG 1066 export_var NANO_BOOTLOADER 1067 export_var NANO_LABEL 1068 export_var NANO_MODULES 1069 export_var NANO_NOPRIV_BUILD 1070 export_var NANO_METALOG 1071 export_var SRCCONF 1072 export_var SRC_ENV_CONF 1073} 1074