nanobsd.sh revision 209225
11638Srgrimes#!/bin/sh 21638Srgrimes# 31638Srgrimes# Copyright (c) 2005 Poul-Henning Kamp. 41638Srgrimes# All rights reserved. 51638Srgrimes# 61638Srgrimes# Redistribution and use in source and binary forms, with or without 71638Srgrimes# modification, are permitted provided that the following conditions 81638Srgrimes# are met: 91638Srgrimes# 1. Redistributions of source code must retain the above copyright 101638Srgrimes# notice, this list of conditions and the following disclaimer. 111638Srgrimes# 2. Redistributions in binary form must reproduce the above copyright 121638Srgrimes# notice, this list of conditions and the following disclaimer in the 131638Srgrimes# documentation and/or other materials provided with the distribution. 141638Srgrimes# 151638Srgrimes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 161638Srgrimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 171638Srgrimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 181638Srgrimes# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 191638Srgrimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 201638Srgrimes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 211638Srgrimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 221638Srgrimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 231638Srgrimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 241638Srgrimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 251638Srgrimes# SUCH DAMAGE. 261638Srgrimes# 271638Srgrimes# $FreeBSD: head/tools/tools/nanobsd/nanobsd.sh 209225 2010-06-15 23:55:42Z imp $ 281638Srgrimes# 291638Srgrimes 301638Srgrimesset -e 311638Srgrimes 321638Srgrimes####################################################################### 331638Srgrimes# 341638Srgrimes# Setup default values for all controlling variables. 351638Srgrimes# These values can be overridden from the config file(s) 361638Srgrimes# 371638Srgrimes####################################################################### 381638Srgrimes 391638Srgrimes# Name of this NanoBSD build. (Used to construct workdir names) 401638SrgrimesNANO_NAME=full 411638Srgrimes 421638Srgrimes# Source tree directory 431638SrgrimesNANO_SRC=/usr/src 441638Srgrimes 451638Srgrimes# Where nanobsd additional files live under the source tree 461638SrgrimesNANO_TOOLS=tools/tools/nanobsd 471638Srgrimes 481638Srgrimes# Where cust_pkg() finds packages to install 491638SrgrimesNANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg 501638SrgrimesNANO_PACKAGE_LIST="*" 511638Srgrimes 521638Srgrimes# Object tree directory 531638Srgrimes# default is subdir of /usr/obj 541638Srgrimes#NANO_OBJ="" 551638Srgrimes 561638Srgrimes# The directory to put the final images 571638Srgrimes# default is ${NANO_OBJ} 581638Srgrimes#NANO_DISKIMGDIR="" 591638Srgrimes 601638Srgrimes# Parallel Make 611638SrgrimesNANO_PMAKE="make -j 3" 621638Srgrimes 631638Srgrimes# The default name for any image we create. 641638SrgrimesNANO_IMGNAME="_.disk.full" 651638Srgrimes 661638Srgrimes# Options to put in make.conf during buildworld only 671638SrgrimesCONF_BUILD=' ' 681638Srgrimes 691638Srgrimes# Options to put in make.conf during installworld only 701638SrgrimesCONF_INSTALL=' ' 711638Srgrimes 721638Srgrimes# Options to put in make.conf during both build- & installworld. 731638SrgrimesCONF_WORLD=' ' 741638Srgrimes 751638Srgrimes# Kernel config file to use 761638SrgrimesNANO_KERNEL=GENERIC 771638Srgrimes 781638Srgrimes# Customize commands. 791638SrgrimesNANO_CUSTOMIZE="" 801638Srgrimes 811638Srgrimes# Late customize commands. 821638SrgrimesNANO_LATE_CUSTOMIZE="" 831638Srgrimes 841638Srgrimes# Newfs paramters to use 851638SrgrimesNANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U" 861638Srgrimes 871638Srgrimes# The drive name of the media at runtime 881638SrgrimesNANO_DRIVE=ad0 891638Srgrimes 901638Srgrimes# Target media size in 512 bytes sectors 911638SrgrimesNANO_MEDIASIZE=1200000 921638Srgrimes 931638Srgrimes# Number of code images on media (1 or 2) 941638SrgrimesNANO_IMAGES=2 951638Srgrimes 961638Srgrimes# 0 -> Leave second image all zeroes so it compresses better. 971638Srgrimes# 1 -> Initialize second image with a copy of the first 981638SrgrimesNANO_INIT_IMG2=1 991638Srgrimes 1001638Srgrimes# Size of code file system in 512 bytes sectors 1011638Srgrimes# If zero, size will be as large as possible. 1021638SrgrimesNANO_CODESIZE=0 1031638Srgrimes 1041638Srgrimes# Size of configuration file system in 512 bytes sectors 1051638Srgrimes# Cannot be zero. 1061638SrgrimesNANO_CONFSIZE=2048 1071638Srgrimes 1081638Srgrimes# Size of data file system in 512 bytes sectors 1091638Srgrimes# If zero: no partition configured. 1101638Srgrimes# If negative: max size possible 1111638SrgrimesNANO_DATASIZE=0 1121638Srgrimes 1131638Srgrimes# Size of the /etc ramdisk in 512 bytes sectors 1141638SrgrimesNANO_RAM_ETCSIZE=10240 1151638Srgrimes 1161638Srgrimes# Size of the /tmp+/var ramdisk in 512 bytes sectors 1171638SrgrimesNANO_RAM_TMPVARSIZE=10240 1181638Srgrimes 1191638Srgrimes# Media geometry, only relevant if bios doesn't understand LBA. 1201638SrgrimesNANO_SECTS=63 1211638SrgrimesNANO_HEADS=16 1221638Srgrimes 1231638Srgrimes# boot0 flags/options and configuration 1241638SrgrimesNANO_BOOT0CFG="-o packet -s 1 -m 3" 1251638SrgrimesNANO_BOOTLOADER="boot/boot0sio" 1261638Srgrimes 1271638Srgrimes# boot2 flags/options 1281638Srgrimes# default force serial console 1291638SrgrimesNANO_BOOT2CFG="-h" 1301638Srgrimes 1311638Srgrimes# Backing type of md(4) device 1321638Srgrimes# Can be "file" or "swap" 1331638SrgrimesNANO_MD_BACKING="file" 1341638Srgrimes 1351638Srgrimes# Progress Print level 1361638SrgrimesPPLEVEL=3 1371638Srgrimes 1381638Srgrimes####################################################################### 1391638Srgrimes# Architecture to build. Corresponds to TARGET_ARCH in a buildworld. 1401638Srgrimes# Unfortunately, there's no way to set TARGET at this time, and it 1411638Srgrimes# conflates the two, so architectures where TARGET != TARGET_ARCH do 1421638Srgrimes# not work. This defaults to the arch of the current machine. 1431638Srgrimes 1441638SrgrimesNANO_ARCH=`uname -p` 1451638Srgrimes 1461638Srgrimes####################################################################### 1471638Srgrimes# 1481638Srgrimes# The functions which do the real work. 1491638Srgrimes# Can be overridden from the config file(s) 1501638Srgrimes# 1511638Srgrimes####################################################################### 1521638Srgrimes 1531638Srgrimesclean_build ( ) ( 1541638Srgrimes pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})" 1551638Srgrimes 1561638Srgrimes if ! rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then 1571638Srgrimes chflags -R noschg ${MAKEOBJDIRPREFIX} 1581638Srgrimes rm -r ${MAKEOBJDIRPREFIX} 1591638Srgrimes fi 1601638Srgrimes mkdir -p ${MAKEOBJDIRPREFIX} 1611638Srgrimes printenv > ${MAKEOBJDIRPREFIX}/_.env 1621638Srgrimes) 1631638Srgrimes 1641638Srgrimesmake_conf_build ( ) ( 1651638Srgrimes pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)" 1661638Srgrimes 1671638Srgrimes echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD} 1681638Srgrimes echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD} 1691638Srgrimes) 1701638Srgrimes 1711638Srgrimesbuild_world ( ) ( 172 pprint 2 "run buildworld" 173 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw" 174 175 cd ${NANO_SRC} 176 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \ 177 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \ 178 > ${MAKEOBJDIRPREFIX}/_.bw 2>&1 179) 180 181build_kernel ( ) ( 182 pprint 2 "build kernel ($NANO_KERNEL)" 183 pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk" 184 185 if [ -f ${NANO_KERNEL} ] ; then 186 cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf 187 fi 188 189 (cd ${NANO_SRC}; 190 # unset these just in case to avoid compiler complaints 191 # when cross-building 192 unset TARGET_CPUTYPE 193 unset TARGET_BIG_ENDIAN 194 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \ 195 __MAKE_CONF=${NANO_MAKE_CONF_BUILD} KERNCONF=`basename ${NANO_KERNEL}` \ 196 > ${MAKEOBJDIRPREFIX}/_.bk 2>&1 197 ) 198) 199 200clean_world ( ) ( 201 if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then 202 pprint 2 "Clean and create object directory (${NANO_OBJ})" 203 if ! rm -rf ${NANO_OBJ} > /dev/null 2>&1 ; then 204 chflags -R noschg ${NANO_OBJ} 205 rm -r ${NANO_OBJ} 206 fi 207 mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR} 208 printenv > ${NANO_OBJ}/_.env 209 else 210 pprint 2 "Clean and create world directory (${NANO_WORLDDIR})" 211 if ! rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then 212 chflags -R noschg ${NANO_WORLDDIR} 213 rm -rf ${NANO_WORLDDIR} 214 fi 215 mkdir -p ${NANO_WORLDDIR} 216 fi 217) 218 219make_conf_install ( ) ( 220 pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)" 221 222 echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL} 223 echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL} 224) 225 226install_world ( ) ( 227 pprint 2 "installworld" 228 pprint 3 "log: ${NANO_OBJ}/_.iw" 229 230 cd ${NANO_SRC} 231 env TARGET_ARCH=${NANO_ARCH} \ 232 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \ 233 DESTDIR=${NANO_WORLDDIR} \ 234 > ${NANO_OBJ}/_.iw 2>&1 235 chflags -R noschg ${NANO_WORLDDIR} 236) 237 238install_etc ( ) ( 239 240 pprint 2 "install /etc" 241 pprint 3 "log: ${NANO_OBJ}/_.etc" 242 243 cd ${NANO_SRC} 244 env TARGET_ARCH=${NANO_ARCH} \ 245 ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \ 246 DESTDIR=${NANO_WORLDDIR} \ 247 > ${NANO_OBJ}/_.etc 2>&1 248 # make.conf doesn't get created by default, but some ports need it 249 # so they can spam it. 250 cp /dev/null ${NANO_WORLDDIR}/etc/make.conf 251) 252 253install_kernel ( ) ( 254 pprint 2 "install kernel" 255 pprint 3 "log: ${NANO_OBJ}/_.ik" 256 257 cd ${NANO_SRC} 258 env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \ 259 DESTDIR=${NANO_WORLDDIR} \ 260 __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} KERNCONF=`basename ${NANO_KERNEL}` \ 261 > ${NANO_OBJ}/_.ik 2>&1 262) 263 264run_customize() ( 265 266 pprint 2 "run customize scripts" 267 for c in $NANO_CUSTOMIZE 268 do 269 pprint 2 "customize \"$c\"" 270 pprint 3 "log: ${NANO_OBJ}/_.cust.$c" 271 pprint 4 "`type $c`" 272 ( $c ) > ${NANO_OBJ}/_.cust.$c 2>&1 273 done 274) 275 276run_late_customize() ( 277 278 pprint 2 "run late customize scripts" 279 for c in $NANO_LATE_CUSTOMIZE 280 do 281 pprint 2 "late customize \"$c\"" 282 pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c" 283 pprint 4 "`type $c`" 284 ( $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1 285 done 286) 287 288setup_nanobsd ( ) ( 289 pprint 2 "configure nanobsd setup" 290 pprint 3 "log: ${NANO_OBJ}/_.dl" 291 292 ( 293 cd ${NANO_WORLDDIR} 294 295 # Move /usr/local/etc to /etc/local so that the /cfg stuff 296 # can stomp on it. Otherwise packages like ipsec-tools which 297 # have hardcoded paths under ${prefix}/etc are not tweakable. 298 if [ -d usr/local/etc ] ; then 299 ( 300 mkdir -p etc/local 301 cd usr/local/etc 302 find . -print | cpio -dumpl ../../../etc/local 303 cd .. 304 rm -rf etc 305 ln -s ../../etc/local etc 306 ) 307 fi 308 309 for d in var etc 310 do 311 # link /$d under /conf 312 # we use hard links so we have them both places. 313 # the files in /$d will be hidden by the mount. 314 # XXX: configure /$d ramdisk size 315 mkdir -p conf/base/$d conf/default/$d 316 find $d -print | cpio -dumpl conf/base/ 317 done 318 319 echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size 320 echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size 321 322 # pick up config files from the special partition 323 echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount 324 325 # Put /tmp on the /var ramdisk (could be symlink already) 326 rmdir tmp || true 327 rm tmp || true 328 ln -s var/tmp tmp 329 330 ) > ${NANO_OBJ}/_.dl 2>&1 331) 332 333setup_nanobsd_etc ( ) ( 334 pprint 2 "configure nanobsd /etc" 335 336 ( 337 cd ${NANO_WORLDDIR} 338 339 # create diskless marker file 340 touch etc/diskless 341 342 # Make root filesystem R/O by default 343 echo "root_rw_mount=NO" >> etc/defaults/rc.conf 344 345 # save config file for scripts 346 echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf 347 348 echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab 349 echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab 350 mkdir -p cfg 351 ) 352) 353 354prune_usr() ( 355 356 # Remove all empty directories in /usr 357 find ${NANO_WORLDDIR}/usr -type d -depth -print | 358 while read d 359 do 360 rmdir $d > /dev/null 2>&1 || true 361 done 362) 363 364populate_slice ( ) ( 365 local dev dir mnt 366 dev=$1 367 dir=$2 368 mnt=$3 369 test -z $2 && dir=/var/empty 370 test -d $d || dir=/var/empty 371 echo "Creating ${dev} with ${dir} (mounting on ${mnt})" 372 newfs ${NANO_NEWFS} ${dev} 373 mount ${dev} ${mnt} 374 cd ${dir} 375 find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${mnt} 376 df -i ${mnt} 377 umount ${mnt} 378) 379 380populate_cfg_slice ( ) ( 381 populate_slice "$1" "$2" "$3" 382) 383 384populate_data_slice ( ) ( 385 populate_slice "$1" "$2" "$3" 386) 387 388create_i386_diskimage ( ) ( 389 pprint 2 "build diskimage" 390 pprint 3 "log: ${NANO_OBJ}/_.di" 391 392 ( 393 echo $NANO_MEDIASIZE $NANO_IMAGES \ 394 $NANO_SECTS $NANO_HEADS \ 395 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE | 396 awk ' 397 { 398 printf "# %s\n", $0 399 400 # size of cylinder in sectors 401 cs = $3 * $4 402 403 # number of full cylinders on media 404 cyl = int ($1 / cs) 405 406 # output fdisk geometry spec, truncate cyls to 1023 407 if (cyl <= 1023) 408 print "g c" cyl " h" $4 " s" $3 409 else 410 print "g c" 1023 " h" $4 " s" $3 411 412 if ($7 > 0) { 413 # size of data partition in full cylinders 414 dsl = int (($7 + cs - 1) / cs) 415 } else { 416 dsl = 0; 417 } 418 419 # size of config partition in full cylinders 420 csl = int (($6 + cs - 1) / cs) 421 422 if ($5 == 0) { 423 # size of image partition(s) in full cylinders 424 isl = int ((cyl - dsl - csl) / $2) 425 } else { 426 isl = int (($5 + cs - 1) / cs) 427 } 428 429 # First image partition start at second track 430 print "p 1 165 " $3, isl * cs - $3 431 c = isl * cs; 432 433 # Second image partition (if any) also starts offset one 434 # track to keep them identical. 435 if ($2 > 1) { 436 print "p 2 165 " $3 + c, isl * cs - $3 437 c += isl * cs; 438 } 439 440 # Config partition starts at cylinder boundary. 441 print "p 3 165 " c, csl * cs 442 c += csl * cs 443 444 # Data partition (if any) starts at cylinder boundary. 445 if ($7 > 0) { 446 print "p 4 165 " c, dsl * cs 447 } else if ($7 < 0 && $1 > c) { 448 print "p 4 165 " c, $1 - c 449 } else if ($1 < c) { 450 print "Disk space overcommitted by", \ 451 c - $1, "sectors" > "/dev/stderr" 452 exit 2 453 } 454 455 # Force slice 1 to be marked active. This is necessary 456 # for booting the image from a USB device to work. 457 print "a 1" 458 } 459 ' > ${NANO_OBJ}/_.fdisk 460 461 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME} 462 MNT=${NANO_OBJ}/_.mnt 463 mkdir -p ${MNT} 464 465 if [ "${NANO_MD_BACKING}" = "swap" ] ; then 466 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \ 467 -y ${NANO_HEADS}` 468 else 469 echo "Creating md backing file..." 470 dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \ 471 count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}` 472 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \ 473 -y ${NANO_HEADS}` 474 fi 475 476 trap "echo 'Running exit trap code' ; df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT 477 478 fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD} 479 fdisk ${MD} 480 # XXX: params 481 # XXX: pick up cached boot* files, they may not be in image anymore. 482 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD} 483 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1 484 bsdlabel ${MD}s1 485 486 # Create first image 487 # XXX: should use populate_slice for easier override 488 newfs ${NANO_NEWFS} /dev/${MD}s1a 489 mount /dev/${MD}s1a ${MNT} 490 df -i ${MNT} 491 echo "Copying worlddir..." 492 ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} ) 493 df -i ${MNT} 494 echo "Generating mtree..." 495 ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree 496 ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du 497 umount ${MNT} 498 499 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then 500 # Duplicate to second image (if present) 501 echo "Duplicating to second image..." 502 dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k 503 mount /dev/${MD}s2a ${MNT} 504 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab 505 do 506 sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f 507 done 508 umount ${MNT} 509 fi 510 511 # Create Config slice 512 populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT} 513 514 # Create Data slice, if any. 515 if [ $NANO_DATASIZE -ne 0 ] ; then 516 populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT} 517 fi 518 519 if [ "${NANO_MD_BACKING}" = "swap" ] ; then 520 echo "Writing out ${NANO_IMGNAME}..." 521 dd if=/dev/${MD} of=${IMG} bs=64k 522 fi 523 524 echo "Writing out _.disk.image..." 525 dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k 526 mdconfig -d -u $MD 527 528 trap - 1 2 15 EXIT 529 530 ) > ${NANO_OBJ}/_.di 2>&1 531) 532 533# i386 and amd64 are identical for disk images 534create_amd64_diskimage ( ) ( 535 create_i386_diskimage 536) 537 538last_orders () ( 539 # Redefine this function with any last orders you may have 540 # after the build completed, for instance to copy the finished 541 # image to a more convenient place: 542 # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk 543) 544 545####################################################################### 546# 547# Optional convenience functions. 548# 549####################################################################### 550 551####################################################################### 552# Common Flash device geometries 553# 554 555FlashDevice () { 556 if [ -d ${NANO_TOOLS} ] ; then 557 . ${NANO_TOOLS}/FlashDevice.sub 558 else 559 . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub 560 fi 561 sub_FlashDevice $1 $2 562} 563 564####################################################################### 565# USB device geometries 566# 567# Usage: 568# UsbDevice Generic 1000 # a generic flash key sold as having 1GB 569# 570# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you. 571# 572# Note that the capacity of a flash key is usually advertised in MB or 573# GB, *not* MiB/GiB. As such, the precise number of cylinders available 574# for C/H/S geometry may vary depending on the actual flash geometry. 575# 576# The following generic device layouts are understood: 577# generic An alias for generic-hdd. 578# generic-hdd 255H 63S/T xxxxC with no MBR restrictions. 579# generic-fdd 64H 32S/T xxxxC with no MBR restrictions. 580# 581# The generic-hdd device is preferred for flash devices larger than 1GB. 582# 583 584UsbDevice () { 585 a1=`echo $1 | tr '[:upper:]' '[:lower:]'` 586 case $a1 in 587 generic-fdd) 588 NANO_HEADS=64 589 NANO_SECTS=32 590 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 591 ;; 592 generic|generic-hdd) 593 NANO_HEADS=255 594 NANO_SECTS=63 595 NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 596 ;; 597 *) 598 echo "Unknown USB flash device" 599 exit 2 600 ;; 601 esac 602} 603 604####################################################################### 605# Setup serial console 606 607cust_comconsole () ( 608 # Enable getty on console 609 sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys 610 611 # Disable getty on syscons devices 612 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys 613 614 # Tell loader to use serial console early. 615 echo "${NANO_BOOT2CFG}" > ${NANO_WORLDDIR}/boot.config 616) 617 618####################################################################### 619# Allow root login via ssh 620 621cust_allow_ssh_root () ( 622 sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \ 623 ${NANO_WORLDDIR}/etc/ssh/sshd_config 624) 625 626####################################################################### 627# Install the stuff under ./Files 628 629cust_install_files () ( 630 cd ${NANO_TOOLS}/Files 631 find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR} 632) 633 634####################################################################### 635# Install packages from ${NANO_PACKAGE_DIR} 636 637cust_pkg () ( 638 639 # Copy packages into chroot 640 mkdir -p ${NANO_WORLDDIR}/Pkg 641 ( 642 cd ${NANO_PACKAGE_DIR} 643 find ${NANO_PACKAGE_LIST} -print | 644 cpio -dumpv ${NANO_WORLDDIR}/Pkg 645 ) 646 647 # Count & report how many we have to install 648 todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l` 649 echo "=== TODO: $todo" 650 ls ${NANO_WORLDDIR}/Pkg 651 echo "===" 652 while true 653 do 654 # Record how many we have now 655 have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l` 656 657 # Attempt to install more packages 658 # ...but no more than 200 at a time due to pkg_add's internal 659 # limitations. 660 chroot ${NANO_WORLDDIR} sh -c \ 661 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true 662 663 # See what that got us 664 now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l` 665 echo "=== NOW $now" 666 ls ${NANO_WORLDDIR}/var/db/pkg 667 echo "===" 668 669 670 if [ $now -eq $todo ] ; then 671 echo "DONE $now packages" 672 break 673 elif [ $now -eq $have ] ; then 674 echo "FAILED: Nothing happened on this pass" 675 exit 2 676 fi 677 done 678 rm -rf ${NANO_WORLDDIR}/Pkg 679) 680 681####################################################################### 682# Convenience function: 683# Register all args as customize function. 684 685customize_cmd () { 686 NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*" 687} 688 689####################################################################### 690# Convenience function: 691# Register all args as late customize function to run just before 692# image creation. 693 694late_customize_cmd () { 695 NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*" 696} 697 698####################################################################### 699# 700# All set up to go... 701# 702####################################################################### 703 704# Progress Print 705# Print $2 at level $1. 706pprint() { 707 if [ "$1" -le $PPLEVEL ]; then 708 runtime=$(( `date +%s` - $NANO_STARTTIME )) 709 printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3 710 fi 711} 712 713usage () { 714 ( 715 echo "Usage: $0 [-biknqvw] [-c config_file]" 716 echo " -b suppress builds (both kernel and world)" 717 echo " -i suppress disk image build" 718 echo " -k suppress buildkernel" 719 echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc" 720 echo " -q make output more quiet" 721 echo " -v make output more verbose" 722 echo " -w suppress buildworld" 723 echo " -c specify config file" 724 ) 1>&2 725 exit 2 726} 727 728####################################################################### 729# Parse arguments 730 731do_clean=true 732do_kernel=true 733do_world=true 734do_image=true 735 736set +e 737args=`getopt bc:hiknqvw $*` 738if [ $? -ne 0 ] ; then 739 usage 740 exit 2 741fi 742set -e 743 744set -- $args 745for i 746do 747 case "$i" 748 in 749 -b) 750 do_world=false 751 do_kernel=false 752 shift 753 ;; 754 -k) 755 do_kernel=false 756 shift 757 ;; 758 -c) 759 . "$2" 760 shift 761 shift 762 ;; 763 -h) 764 usage 765 ;; 766 -i) 767 do_image=false 768 shift 769 ;; 770 -n) 771 do_clean=false 772 shift 773 ;; 774 -q) 775 PPLEVEL=$(($PPLEVEL - 1)) 776 shift 777 ;; 778 -v) 779 PPLEVEL=$(($PPLEVEL + 1)) 780 shift 781 ;; 782 -w) 783 do_world=false 784 shift 785 ;; 786 --) 787 shift 788 break 789 esac 790done 791 792if [ $# -gt 0 ] ; then 793 echo "$0: Extraneous arguments supplied" 794 usage 795fi 796 797####################################################################### 798# Setup and Export Internal variables 799# 800test -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/ 801test -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ} 802test -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ} 803 804NANO_WORLDDIR=${NANO_OBJ}/_.w 805NANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build 806NANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install 807 808if [ -d ${NANO_TOOLS} ] ; then 809 true 810elif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then 811 NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS} 812else 813 echo "NANO_TOOLS directory does not exist" 1>&2 814 exit 1 815fi 816 817if $do_clean ; then 818 true 819else 820 NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN" 821fi 822 823export MAKEOBJDIRPREFIX 824 825export NANO_ARCH 826export NANO_CODESIZE 827export NANO_CONFSIZE 828export NANO_CUSTOMIZE 829export NANO_DATASIZE 830export NANO_DRIVE 831export NANO_HEADS 832export NANO_IMAGES 833export NANO_IMGNAME 834export NANO_MAKE_CONF_BUILD 835export NANO_MAKE_CONF_INSTALL 836export NANO_MEDIASIZE 837export NANO_NAME 838export NANO_NEWFS 839export NANO_OBJ 840export NANO_PMAKE 841export NANO_SECTS 842export NANO_SRC 843export NANO_TOOLS 844export NANO_WORLDDIR 845export NANO_BOOT0CFG 846export NANO_BOOTLOADER 847 848####################################################################### 849# And then it is as simple as that... 850 851# File descriptor 3 is used for logging output, see pprint 852exec 3>&1 853 854NANO_STARTTIME=`date +%s` 855pprint 1 "NanoBSD image ${NANO_NAME} build starting" 856 857if $do_world ; then 858 if $do_clean ; then 859 clean_build 860 else 861 pprint 2 "Using existing build tree (as instructed)" 862 fi 863 make_conf_build 864 build_world 865else 866 pprint 2 "Skipping buildworld (as instructed)" 867fi 868 869if $do_kernel ; then 870 build_kernel 871else 872 pprint 2 "Skipping buildkernel (as instructed)" 873fi 874 875clean_world 876make_conf_install 877install_world 878install_etc 879setup_nanobsd_etc 880install_kernel 881 882run_customize 883setup_nanobsd 884prune_usr 885run_late_customize 886if $do_image ; then 887 create_${NANO_ARCH}_diskimage 888else 889 pprint 2 "Skipping image build (as instructed)" 890fi 891last_orders 892 893pprint 1 "NanoBSD image ${NANO_NAME} completed" 894