nanobsd.sh revision 221856
1148422Sphk#!/bin/sh 2150366Sphk# 3148422Sphk# Copyright (c) 2005 Poul-Henning Kamp. 4150366Sphk# All rights reserved. 5148422Sphk# 6150366Sphk# Redistribution and use in source and binary forms, with or without 7150366Sphk# modification, are permitted provided that the following conditions 8150366Sphk# are met: 9150366Sphk# 1. Redistributions of source code must retain the above copyright 10150366Sphk# notice, this list of conditions and the following disclaimer. 11150366Sphk# 2. Redistributions in binary form must reproduce the above copyright 12150366Sphk# notice, this list of conditions and the following disclaimer in the 13150366Sphk# documentation and/or other materials provided with the distribution. 14148422Sphk# 15150366Sphk# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16150366Sphk# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17150366Sphk# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18150366Sphk# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19150366Sphk# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20150366Sphk# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21150366Sphk# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22150366Sphk# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23150366Sphk# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24150366Sphk# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25150366Sphk# SUCH DAMAGE. 26150366Sphk# 27148422Sphk# $FreeBSD: head/tools/tools/nanobsd/nanobsd.sh 221856 2011-05-13 19:40:02Z imp $ 28148422Sphk# 29148422Sphk 30148422Sphkset -e 31148422Sphk 32150366Sphk####################################################################### 33150366Sphk# 34150366Sphk# Setup default values for all controlling variables. 35150366Sphk# These values can be overridden from the config file(s) 36150366Sphk# 37150366Sphk####################################################################### 38150366Sphk 39148422Sphk# Name of this NanoBSD build. (Used to construct workdir names) 40148422SphkNANO_NAME=full 41148422Sphk 42148422Sphk# Source tree directory 43148422SphkNANO_SRC=/usr/src 44148422Sphk 45150366Sphk# Where nanobsd additional files live under the source tree 46150366SphkNANO_TOOLS=tools/tools/nanobsd 47150366Sphk 48170607Sphk# Where cust_pkg() finds packages to install 49170607SphkNANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg 50186181SphkNANO_PACKAGE_LIST="*" 51170607Sphk 52148422Sphk# Object tree directory 53148422Sphk# default is subdir of /usr/obj 54148648Sphk#NANO_OBJ="" 55148422Sphk 56175258Ssimon# The directory to put the final images 57175258Ssimon# default is ${NANO_OBJ} 58175258Ssimon#NANO_DISKIMGDIR="" 59175258Ssimon 60148422Sphk# Parallel Make 61148422SphkNANO_PMAKE="make -j 3" 62148422Sphk 63183284Sbms# The default name for any image we create. 64183284SbmsNANO_IMGNAME="_.disk.full" 65183284Sbms 66148422Sphk# Options to put in make.conf during buildworld only 67148422SphkCONF_BUILD=' ' 68148422Sphk 69148422Sphk# Options to put in make.conf during installworld only 70148422SphkCONF_INSTALL=' ' 71148422Sphk 72148422Sphk# Options to put in make.conf during both build- & installworld. 73148422SphkCONF_WORLD=' ' 74148422Sphk 75148422Sphk# Kernel config file to use 76148422SphkNANO_KERNEL=GENERIC 77148422Sphk 78214955Sn_hibma# Kernel modules to build; default is none 79214955Sn_hibmaNANO_MODULES= 80214955Sn_hibma 81149073Sphk# Customize commands. 82149073SphkNANO_CUSTOMIZE="" 83149073Sphk 84181461Sphk# Late customize commands. 85181461SphkNANO_LATE_CUSTOMIZE="" 86181461Sphk 87148648Sphk# Newfs paramters to use 88148648SphkNANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U" 89148648Sphk 90149082Sphk# The drive name of the media at runtime 91149082SphkNANO_DRIVE=ad0 92149082Sphk 93148648Sphk# Target media size in 512 bytes sectors 94216929SimpNANO_MEDIASIZE=1500000 95148648Sphk 96148648Sphk# Number of code images on media (1 or 2) 97148422SphkNANO_IMAGES=2 98148648Sphk 99171986Sphk# 0 -> Leave second image all zeroes so it compresses better. 100171986Sphk# 1 -> Initialize second image with a copy of the first 101171986SphkNANO_INIT_IMG2=1 102171986Sphk 103149535Ssimon# Size of code file system in 512 bytes sectors 104149120Sphk# If zero, size will be as large as possible. 105149120SphkNANO_CODESIZE=0 106149120Sphk 107149120Sphk# Size of configuration file system in 512 bytes sectors 108149120Sphk# Cannot be zero. 109148422SphkNANO_CONFSIZE=2048 110148648Sphk 111148648Sphk# Size of data file system in 512 bytes sectors 112149120Sphk# If zero: no partition configured. 113149120Sphk# If negative: max size possible 114149120SphkNANO_DATASIZE=0 115148422Sphk 116154466Sphk# Size of the /etc ramdisk in 512 bytes sectors 117154466SphkNANO_RAM_ETCSIZE=10240 118154466Sphk 119154466Sphk# Size of the /tmp+/var ramdisk in 512 bytes sectors 120154466SphkNANO_RAM_TMPVARSIZE=10240 121154466Sphk 122148422Sphk# Media geometry, only relevant if bios doesn't understand LBA. 123170607SphkNANO_SECTS=63 124148422SphkNANO_HEADS=16 125148422Sphk 126168894Sadrian# boot0 flags/options and configuration 127168894SadrianNANO_BOOT0CFG="-o packet -s 1 -m 3" 128168894SadrianNANO_BOOTLOADER="boot/boot0sio" 129168894Sadrian 130209225Simp# boot2 flags/options 131209225Simp# default force serial console 132209225SimpNANO_BOOT2CFG="-h" 133209225Simp 134175258Ssimon# Backing type of md(4) device 135175258Ssimon# Can be "file" or "swap" 136175258SsimonNANO_MD_BACKING="file" 137175258Ssimon 138183314Ssimon# Progress Print level 139183314SsimonPPLEVEL=3 140183314Ssimon 141212938Simp# Set NANO_LABEL to non-blank to form the basis for using /dev/ufs/label 142212938Simp# in preference to /dev/${NANO_DRIVE} 143212938Simp# Root partition will be ${NANO_LABEL}s{1,2} 144212938Simp# /cfg partition will be ${NANO_LABEL}s3 145212938Simp# /data partition will be ${NANO_LABEL}s4 146212938SimpNANO_LABEL="" 147212938Simp 148148422Sphk####################################################################### 149206276Simp# Architecture to build. Corresponds to TARGET_ARCH in a buildworld. 150212938Simp# Unfortunately, there's no way to set TARGET at this time, and it 151206276Simp# conflates the two, so architectures where TARGET != TARGET_ARCH do 152206276Simp# not work. This defaults to the arch of the current machine. 153148648Sphk 154205980SimpNANO_ARCH=`uname -p` 155148648Sphk 156215069Simp# Directory to populate /cfg from 157215069SimpNANO_CFGDIR="" 158215069Simp 159215069Simp# Directory to populate /data from 160215069SimpNANO_DATADIR="" 161215069Simp 162148648Sphk####################################################################### 163150366Sphk# 164150366Sphk# The functions which do the real work. 165150366Sphk# Can be overridden from the config file(s) 166150366Sphk# 167150366Sphk####################################################################### 168148422Sphk 169149120Sphkclean_build ( ) ( 170183314Ssimon pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})" 171148422Sphk 172194431Sn_hibma if ! rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then 173148422Sphk chflags -R noschg ${MAKEOBJDIRPREFIX} 174194431Sn_hibma rm -r ${MAKEOBJDIRPREFIX} 175148422Sphk fi 176148422Sphk mkdir -p ${MAKEOBJDIRPREFIX} 177150343Sphk printenv > ${MAKEOBJDIRPREFIX}/_.env 178148422Sphk) 179148422Sphk 180148422Sphkmake_conf_build ( ) ( 181194431Sn_hibma pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)" 182148422Sphk 183194431Sn_hibma echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD} 184194431Sn_hibma echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD} 185216144Simp echo "SRCCONF=/dev/null" >> ${NANO_MAKE_CONF_BUILD} 186148422Sphk) 187148422Sphk 188148422Sphkbuild_world ( ) ( 189183314Ssimon pprint 2 "run buildworld" 190183314Ssimon pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw" 191148422Sphk 192148422Sphk cd ${NANO_SRC} 193183284Sbms env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \ 194194431Sn_hibma __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \ 195148422Sphk > ${MAKEOBJDIRPREFIX}/_.bw 2>&1 196148422Sphk) 197148422Sphk 198148422Sphkbuild_kernel ( ) ( 199183314Ssimon pprint 2 "build kernel ($NANO_KERNEL)" 200183314Ssimon pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk" 201148422Sphk 202214955Sn_hibma ( 203148422Sphk if [ -f ${NANO_KERNEL} ] ; then 204214955Sn_hibma kernconfdir=$(realpath $(dirname ${NANO_KERNEL})) 205214955Sn_hibma kernconf=$(basename ${NANO_KERNEL}) 206214955Sn_hibma else 207214955Sn_hibma kernconf=${NANO_KERNEL} 208148422Sphk fi 209148422Sphk 210214955Sn_hibma cd ${NANO_SRC}; 211181083Ssam # unset these just in case to avoid compiler complaints 212181083Ssam # when cross-building 213181083Ssam unset TARGET_CPUTYPE 214181083Ssam unset TARGET_BIG_ENDIAN 215214955Sn_hibma # Note: We intentionally build all modules, not only the ones in 216214955Sn_hibma # NANO_MODULES so the built world can be reused by multiple images. 217183284Sbms env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \ 218214955Sn_hibma __MAKE_CONF=${NANO_MAKE_CONF_BUILD} \ 219214955Sn_hibma ${kernconfdir:+"KERNCONFDIR="}${kernconfdir} \ 220214955Sn_hibma KERNCONF=${kernconf} 221214955Sn_hibma ) > ${MAKEOBJDIRPREFIX}/_.bk 2>&1 222148422Sphk) 223148422Sphk 224148422Sphkclean_world ( ) ( 225194431Sn_hibma if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then 226194431Sn_hibma pprint 2 "Clean and create object directory (${NANO_OBJ})" 227194431Sn_hibma if ! rm -rf ${NANO_OBJ} > /dev/null 2>&1 ; then 228194431Sn_hibma chflags -R noschg ${NANO_OBJ} 229194431Sn_hibma rm -r ${NANO_OBJ} 230194431Sn_hibma fi 231194431Sn_hibma mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR} 232194431Sn_hibma printenv > ${NANO_OBJ}/_.env 233148422Sphk else 234194431Sn_hibma pprint 2 "Clean and create world directory (${NANO_WORLDDIR})" 235194431Sn_hibma if ! rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then 236194431Sn_hibma chflags -R noschg ${NANO_WORLDDIR} 237194431Sn_hibma rm -rf ${NANO_WORLDDIR} 238194431Sn_hibma fi 239194431Sn_hibma mkdir -p ${NANO_WORLDDIR} 240148422Sphk fi 241148422Sphk) 242148422Sphk 243148422Sphkmake_conf_install ( ) ( 244194431Sn_hibma pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)" 245148422Sphk 246194431Sn_hibma echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL} 247194431Sn_hibma echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL} 248216928Simp echo "SRCCONF=/dev/null" >> ${NANO_MAKE_CONF_INSTALL} 249148422Sphk) 250148422Sphk 251148422Sphkinstall_world ( ) ( 252183314Ssimon pprint 2 "installworld" 253194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.iw" 254148422Sphk 255148422Sphk cd ${NANO_SRC} 256183284Sbms env TARGET_ARCH=${NANO_ARCH} \ 257194431Sn_hibma ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \ 258148422Sphk DESTDIR=${NANO_WORLDDIR} \ 259194431Sn_hibma > ${NANO_OBJ}/_.iw 2>&1 260148422Sphk chflags -R noschg ${NANO_WORLDDIR} 261148422Sphk) 262148422Sphk 263148422Sphkinstall_etc ( ) ( 264148422Sphk 265183314Ssimon pprint 2 "install /etc" 266194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.etc" 267148422Sphk 268160783Sthomas cd ${NANO_SRC} 269183284Sbms env TARGET_ARCH=${NANO_ARCH} \ 270194431Sn_hibma ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \ 271148422Sphk DESTDIR=${NANO_WORLDDIR} \ 272194431Sn_hibma > ${NANO_OBJ}/_.etc 2>&1 273209209Simp # make.conf doesn't get created by default, but some ports need it 274209209Simp # so they can spam it. 275209209Simp cp /dev/null ${NANO_WORLDDIR}/etc/make.conf 276148422Sphk) 277148422Sphk 278148422Sphkinstall_kernel ( ) ( 279214955Sn_hibma pprint 2 "install kernel ($NANO_KERNEL)" 280194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.ik" 281148422Sphk 282214955Sn_hibma ( 283214955Sn_hibma if [ -f ${NANO_KERNEL} ] ; then 284214955Sn_hibma kernconfdir=$(realpath $(dirname ${NANO_KERNEL})) 285214955Sn_hibma kernconf=$(basename ${NANO_KERNEL}) 286214955Sn_hibma else 287214955Sn_hibma kernconf=${NANO_KERNEL} 288214955Sn_hibma fi 289214955Sn_hibma 290148422Sphk cd ${NANO_SRC} 291183284Sbms env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \ 292148422Sphk DESTDIR=${NANO_WORLDDIR} \ 293214955Sn_hibma __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} \ 294214955Sn_hibma ${kernconfdir:+"KERNCONFDIR="}${kernconfdir} \ 295214955Sn_hibma KERNCONF=${kernconf} \ 296214955Sn_hibma MODULES_OVERRIDE="${NANO_MODULES}" 297214955Sn_hibma ) > ${NANO_OBJ}/_.ik 2>&1 298148422Sphk) 299148422Sphk 300150366Sphkrun_customize() ( 301150366Sphk 302183314Ssimon pprint 2 "run customize scripts" 303150366Sphk for c in $NANO_CUSTOMIZE 304150366Sphk do 305183314Ssimon pprint 2 "customize \"$c\"" 306194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.cust.$c" 307183314Ssimon pprint 4 "`type $c`" 308214955Sn_hibma ( set -x ; $c ) > ${NANO_OBJ}/_.cust.$c 2>&1 309150366Sphk done 310150366Sphk) 311150366Sphk 312181461Sphkrun_late_customize() ( 313181461Sphk 314183314Ssimon pprint 2 "run late customize scripts" 315181461Sphk for c in $NANO_LATE_CUSTOMIZE 316181461Sphk do 317183314Ssimon pprint 2 "late customize \"$c\"" 318194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c" 319183314Ssimon pprint 4 "`type $c`" 320214955Sn_hibma ( set -x ; $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1 321181461Sphk done 322181461Sphk) 323181461Sphk 324150366Sphksetup_nanobsd ( ) ( 325183314Ssimon pprint 2 "configure nanobsd setup" 326194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.dl" 327148422Sphk 328149103Sphk ( 329148422Sphk cd ${NANO_WORLDDIR} 330148422Sphk 331170607Sphk # Move /usr/local/etc to /etc/local so that the /cfg stuff 332170607Sphk # can stomp on it. Otherwise packages like ipsec-tools which 333170607Sphk # have hardcoded paths under ${prefix}/etc are not tweakable. 334170607Sphk if [ -d usr/local/etc ] ; then 335170607Sphk ( 336183727Sn_hibma mkdir -p etc/local 337170607Sphk cd usr/local/etc 338170607Sphk find . -print | cpio -dumpl ../../../etc/local 339170607Sphk cd .. 340170607Sphk rm -rf etc 341170607Sphk ln -s ../../etc/local etc 342170607Sphk ) 343170607Sphk fi 344170607Sphk 345149082Sphk for d in var etc 346149082Sphk do 347149082Sphk # link /$d under /conf 348149082Sphk # we use hard links so we have them both places. 349149082Sphk # the files in /$d will be hidden by the mount. 350149082Sphk # XXX: configure /$d ramdisk size 351149082Sphk mkdir -p conf/base/$d conf/default/$d 352149082Sphk find $d -print | cpio -dumpl conf/base/ 353149082Sphk done 354148422Sphk 355154466Sphk echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size 356154466Sphk echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size 357154466Sphk 358148422Sphk # pick up config files from the special partition 359149082Sphk echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount 360148422Sphk 361149082Sphk # Put /tmp on the /var ramdisk (could be symlink already) 362149082Sphk rmdir tmp || true 363149082Sphk rm tmp || true 364148422Sphk ln -s var/tmp tmp 365149082Sphk 366194431Sn_hibma ) > ${NANO_OBJ}/_.dl 2>&1 367148422Sphk) 368148422Sphk 369161341Ssimonsetup_nanobsd_etc ( ) ( 370183314Ssimon pprint 2 "configure nanobsd /etc" 371161341Ssimon 372161341Ssimon ( 373161341Ssimon cd ${NANO_WORLDDIR} 374161341Ssimon 375161341Ssimon # create diskless marker file 376161341Ssimon touch etc/diskless 377161341Ssimon 378170324Sphk # Make root filesystem R/O by default 379170324Sphk echo "root_rw_mount=NO" >> etc/defaults/rc.conf 380170324Sphk 381161341Ssimon # save config file for scripts 382161341Ssimon echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf 383161341Ssimon 384161341Ssimon echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab 385161341Ssimon echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab 386161341Ssimon mkdir -p cfg 387161341Ssimon ) 388161341Ssimon) 389161341Ssimon 390149014Sphkprune_usr() ( 391149014Sphk 392149014Sphk # Remove all empty directories in /usr 393149014Sphk find ${NANO_WORLDDIR}/usr -type d -depth -print | 394149014Sphk while read d 395149014Sphk do 396149014Sphk rmdir $d > /dev/null 2>&1 || true 397149014Sphk done 398149014Sphk) 399149014Sphk 400212938Simpnewfs_part ( ) ( 401212938Simp local dev mnt lbl 402212938Simp dev=$1 403212938Simp mnt=$2 404212938Simp lbl=$3 405212938Simp echo newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev} 406212938Simp newfs ${NANO_NEWFS} ${NANO_LABEL:+-L${NANO_LABEL}${lbl}} ${dev} 407216145Simp mount -o async ${dev} ${mnt} 408212938Simp) 409212938Simp 410209210Simppopulate_slice ( ) ( 411212938Simp local dev dir mnt lbl 412209210Simp dev=$1 413209210Simp dir=$2 414209210Simp mnt=$3 415212938Simp lbl=$4 416220091Smr test -z $2 && dir=${NANO_WORLDDIR}/var/empty 417220091Smr test -d $dir || dir=${NANO_WORLDDIR}/var/empty 418209210Simp echo "Creating ${dev} with ${dir} (mounting on ${mnt})" 419212938Simp newfs_part $dev $mnt $lbl 420209210Simp cd ${dir} 421221850Simp find . -print | grep -Ev '/(CVS|\.svn)' | cpio -Ldumpv ${mnt} 422209210Simp df -i ${mnt} 423209210Simp umount ${mnt} 424209210Simp) 425209210Simp 426209210Simppopulate_cfg_slice ( ) ( 427212938Simp populate_slice "$1" "$2" "$3" "$4" 428209210Simp) 429209210Simp 430209210Simppopulate_data_slice ( ) ( 431212938Simp populate_slice "$1" "$2" "$3" "$4" 432209210Simp) 433209210Simp 434148422Sphkcreate_i386_diskimage ( ) ( 435183314Ssimon pprint 2 "build diskimage" 436194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.di" 437148422Sphk 438149103Sphk ( 439149120Sphk echo $NANO_MEDIASIZE $NANO_IMAGES \ 440149120Sphk $NANO_SECTS $NANO_HEADS \ 441149120Sphk $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE | 442148422Sphk awk ' 443150717Sphk { 444149120Sphk printf "# %s\n", $0 445149120Sphk 446148422Sphk # size of cylinder in sectors 447149120Sphk cs = $3 * $4 448148422Sphk 449148422Sphk # number of full cylinders on media 450148422Sphk cyl = int ($1 / cs) 451148422Sphk 452149120Sphk # output fdisk geometry spec, truncate cyls to 1023 453148422Sphk if (cyl <= 1023) 454149120Sphk print "g c" cyl " h" $4 " s" $3 455148422Sphk else 456149120Sphk print "g c" 1023 " h" $4 " s" $3 457148422Sphk 458149120Sphk if ($7 > 0) { 459149120Sphk # size of data partition in full cylinders 460149120Sphk dsl = int (($7 + cs - 1) / cs) 461149120Sphk } else { 462149120Sphk dsl = 0; 463149120Sphk } 464148422Sphk 465148422Sphk # size of config partition in full cylinders 466149120Sphk csl = int (($6 + cs - 1) / cs) 467148422Sphk 468150717Sphk if ($5 == 0) { 469149120Sphk # size of image partition(s) in full cylinders 470149120Sphk isl = int ((cyl - dsl - csl) / $2) 471149120Sphk } else { 472149120Sphk isl = int (($5 + cs - 1) / cs) 473149120Sphk } 474148422Sphk 475148422Sphk # First image partition start at second track 476149120Sphk print "p 1 165 " $3, isl * cs - $3 477148422Sphk c = isl * cs; 478148422Sphk 479148422Sphk # Second image partition (if any) also starts offset one 480148422Sphk # track to keep them identical. 481148422Sphk if ($2 > 1) { 482149120Sphk print "p 2 165 " $3 + c, isl * cs - $3 483148422Sphk c += isl * cs; 484148422Sphk } 485148422Sphk 486148422Sphk # Config partition starts at cylinder boundary. 487148422Sphk print "p 3 165 " c, csl * cs 488148422Sphk c += csl * cs 489148422Sphk 490148422Sphk # Data partition (if any) starts at cylinder boundary. 491149120Sphk if ($7 > 0) { 492148422Sphk print "p 4 165 " c, dsl * cs 493174936Ssimon } else if ($7 < 0 && $1 > c) { 494174936Ssimon print "p 4 165 " c, $1 - c 495150717Sphk } else if ($1 < c) { 496150717Sphk print "Disk space overcommitted by", \ 497150717Sphk c - $1, "sectors" > "/dev/stderr" 498150717Sphk exit 2 499148422Sphk } 500183279Sbms 501183279Sbms # Force slice 1 to be marked active. This is necessary 502183279Sbms # for booting the image from a USB device to work. 503183279Sbms print "a 1" 504150717Sphk } 505194431Sn_hibma ' > ${NANO_OBJ}/_.fdisk 506148422Sphk 507183284Sbms IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME} 508194431Sn_hibma MNT=${NANO_OBJ}/_.mnt 509148422Sphk mkdir -p ${MNT} 510148422Sphk 511175258Ssimon if [ "${NANO_MD_BACKING}" = "swap" ] ; then 512175258Ssimon MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \ 513175258Ssimon -y ${NANO_HEADS}` 514175258Ssimon else 515175258Ssimon echo "Creating md backing file..." 516216145Simp rm -f ${IMG} 517216145Simp dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0 518175258Ssimon MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \ 519175258Ssimon -y ${NANO_HEADS}` 520175258Ssimon fi 521148422Sphk 522197542Ssimon trap "echo 'Running exit trap code' ; df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT 523148422Sphk 524194431Sn_hibma fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD} 525149120Sphk fdisk ${MD} 526148422Sphk # XXX: params 527148422Sphk # XXX: pick up cached boot* files, they may not be in image anymore. 528168894Sadrian boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD} 529160388Sthomas bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1 530149120Sphk bsdlabel ${MD}s1 531148422Sphk 532148422Sphk # Create first image 533212990Simp populate_slice /dev/${MD}s1a ${NANO_WORLDDIR} ${MNT} "s1a" 534194437Sn_hibma mount /dev/${MD}s1a ${MNT} 535175258Ssimon echo "Generating mtree..." 536194431Sn_hibma ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree 537194431Sn_hibma ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du 538148422Sphk umount ${MNT} 539148422Sphk 540171986Sphk if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then 541148422Sphk # Duplicate to second image (if present) 542175258Ssimon echo "Duplicating to second image..." 543194437Sn_hibma dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k 544170322Sphk mount /dev/${MD}s2a ${MNT} 545170322Sphk for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab 546170322Sphk do 547212169Simp sed -i "" "s=${NANO_DRIVE}s1=${NANO_DRIVE}s2=g" $f 548170322Sphk done 549170322Sphk umount ${MNT} 550212990Simp # Override the label from the first partition so we 551212990Simp # don't confuse glabel with duplicates. 552212938Simp if [ ! -z ${NANO_LABEL} ]; then 553212990Simp tunefs -L ${NANO_LABEL}"s2a" /dev/${MD}s2a 554212938Simp fi 555148422Sphk fi 556148422Sphk 557148422Sphk # Create Config slice 558212938Simp populate_cfg_slice /dev/${MD}s3 "${NANO_CFGDIR}" ${MNT} "s3" 559148422Sphk 560148422Sphk # Create Data slice, if any. 561194437Sn_hibma if [ $NANO_DATASIZE -ne 0 ] ; then 562212938Simp populate_data_slice /dev/${MD}s4 "${NANO_DATADIR}" ${MNT} "s4" 563148422Sphk fi 564148422Sphk 565175258Ssimon if [ "${NANO_MD_BACKING}" = "swap" ] ; then 566194440Sn_hibma echo "Writing out ${NANO_IMGNAME}..." 567175258Ssimon dd if=/dev/${MD} of=${IMG} bs=64k 568175258Ssimon fi 569175258Ssimon 570221852Simp if ${do_copyout_partition} ; then 571221851Simp echo "Writing out _.disk.image..." 572221851Simp dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k 573221851Simp fi 574148422Sphk mdconfig -d -u $MD 575197542Ssimon 576197542Ssimon trap - 1 2 15 EXIT 577197542Ssimon 578194431Sn_hibma ) > ${NANO_OBJ}/_.di 2>&1 579148422Sphk) 580148422Sphk 581205980Simp# i386 and amd64 are identical for disk images 582205980Simpcreate_amd64_diskimage ( ) ( 583205980Simp create_i386_diskimage 584205980Simp) 585205980Simp 586154466Sphklast_orders () ( 587154466Sphk # Redefine this function with any last orders you may have 588154466Sphk # after the build completed, for instance to copy the finished 589154466Sphk # image to a more convenient place: 590194431Sn_hibma # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk 591154466Sphk) 592154466Sphk 593148422Sphk####################################################################### 594148422Sphk# 595150366Sphk# Optional convenience functions. 596148422Sphk# 597148422Sphk####################################################################### 598148422Sphk 599149014Sphk####################################################################### 600150367Sphk# Common Flash device geometries 601150367Sphk# 602150367Sphk 603150367SphkFlashDevice () { 604150717Sphk if [ -d ${NANO_TOOLS} ] ; then 605150717Sphk . ${NANO_TOOLS}/FlashDevice.sub 606150717Sphk else 607150717Sphk . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub 608150717Sphk fi 609150367Sphk sub_FlashDevice $1 $2 610150367Sphk} 611150367Sphk 612183284Sbms####################################################################### 613183284Sbms# USB device geometries 614183284Sbms# 615183284Sbms# Usage: 616183284Sbms# UsbDevice Generic 1000 # a generic flash key sold as having 1GB 617183284Sbms# 618183284Sbms# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you. 619183284Sbms# 620183284Sbms# Note that the capacity of a flash key is usually advertised in MB or 621183284Sbms# GB, *not* MiB/GiB. As such, the precise number of cylinders available 622183284Sbms# for C/H/S geometry may vary depending on the actual flash geometry. 623183284Sbms# 624183284Sbms# The following generic device layouts are understood: 625183284Sbms# generic An alias for generic-hdd. 626183284Sbms# generic-hdd 255H 63S/T xxxxC with no MBR restrictions. 627183284Sbms# generic-fdd 64H 32S/T xxxxC with no MBR restrictions. 628183284Sbms# 629183284Sbms# The generic-hdd device is preferred for flash devices larger than 1GB. 630183284Sbms# 631150367Sphk 632183284SbmsUsbDevice () { 633183284Sbms a1=`echo $1 | tr '[:upper:]' '[:lower:]'` 634183284Sbms case $a1 in 635183284Sbms generic-fdd) 636183284Sbms NANO_HEADS=64 637183284Sbms NANO_SECTS=32 638183284Sbms NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 639183284Sbms ;; 640183284Sbms generic|generic-hdd) 641183284Sbms NANO_HEADS=255 642183284Sbms NANO_SECTS=63 643183284Sbms NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 644183284Sbms ;; 645183284Sbms *) 646183284Sbms echo "Unknown USB flash device" 647183284Sbms exit 2 648183284Sbms ;; 649183284Sbms esac 650183284Sbms} 651183284Sbms 652150367Sphk####################################################################### 653150366Sphk# Setup serial console 654150343Sphk 655150366Sphkcust_comconsole () ( 656150366Sphk # Enable getty on console 657181751Sphk sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys 658150366Sphk 659150366Sphk # Disable getty on syscons devices 660150366Sphk sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys 661150366Sphk 662150366Sphk # Tell loader to use serial console early. 663212938Simp echo " -h" > ${NANO_WORLDDIR}/boot.config 664150366Sphk) 665150366Sphk 666150366Sphk####################################################################### 667150366Sphk# Allow root login via ssh 668150366Sphk 669150366Sphkcust_allow_ssh_root () ( 670150366Sphk sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \ 671150366Sphk ${NANO_WORLDDIR}/etc/ssh/sshd_config 672150366Sphk) 673150366Sphk 674150366Sphk####################################################################### 675150366Sphk# Install the stuff under ./Files 676150366Sphk 677150366Sphkcust_install_files () ( 678150511Ssimon cd ${NANO_TOOLS}/Files 679221850Simp find . -print | grep -Ev '/(CVS|\.svn)' | cpio -Ldumpv ${NANO_WORLDDIR} 680150366Sphk) 681150366Sphk 682150366Sphk####################################################################### 683170607Sphk# Install packages from ${NANO_PACKAGE_DIR} 684170607Sphk 685170607Sphkcust_pkg () ( 686170607Sphk 687221856Simp # If the package directory doesn't exist, we're done. 688221856Simp if [ ! -d ${NANO_PACKAGE_DIR} ]; then 689221856Simp echo "DONE 0 packages" 690221856Simp return 0 691221856Simp fi 692221856Simp 693170607Sphk # Copy packages into chroot 694170607Sphk mkdir -p ${NANO_WORLDDIR}/Pkg 695186181Sphk ( 696186181Sphk cd ${NANO_PACKAGE_DIR} 697186181Sphk find ${NANO_PACKAGE_LIST} -print | 698221850Simp cpio -Ldumpv ${NANO_WORLDDIR}/Pkg 699186181Sphk ) 700170607Sphk 701170607Sphk # Count & report how many we have to install 702170607Sphk todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l` 703170607Sphk echo "=== TODO: $todo" 704170607Sphk ls ${NANO_WORLDDIR}/Pkg 705170607Sphk echo "===" 706170607Sphk while true 707170607Sphk do 708182690Sthomas # Record how many we have now 709170607Sphk have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l` 710170607Sphk 711171986Sphk # Attempt to install more packages 712171986Sphk # ...but no more than 200 at a time due to pkg_add's internal 713171986Sphk # limitations. 714171986Sphk chroot ${NANO_WORLDDIR} sh -c \ 715171986Sphk 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true 716170607Sphk 717170607Sphk # See what that got us 718170607Sphk now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l` 719170607Sphk echo "=== NOW $now" 720170607Sphk ls ${NANO_WORLDDIR}/var/db/pkg 721170607Sphk echo "===" 722170607Sphk 723170607Sphk 724170607Sphk if [ $now -eq $todo ] ; then 725170607Sphk echo "DONE $now packages" 726170607Sphk break 727170607Sphk elif [ $now -eq $have ] ; then 728170607Sphk echo "FAILED: Nothing happened on this pass" 729170607Sphk exit 2 730170607Sphk fi 731170607Sphk done 732170607Sphk rm -rf ${NANO_WORLDDIR}/Pkg 733170607Sphk) 734170607Sphk 735170607Sphk####################################################################### 736150366Sphk# Convenience function: 737209207Simp# Register all args as customize function. 738150366Sphk 739150343Sphkcustomize_cmd () { 740209207Simp NANO_CUSTOMIZE="$NANO_CUSTOMIZE $*" 741150343Sphk} 742150343Sphk 743150343Sphk####################################################################### 744181461Sphk# Convenience function: 745209207Simp# Register all args as late customize function to run just before 746181461Sphk# image creation. 747181461Sphk 748181461Sphklate_customize_cmd () { 749209207Simp NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $*" 750181461Sphk} 751181461Sphk 752181461Sphk####################################################################### 753150366Sphk# 754150366Sphk# All set up to go... 755150366Sphk# 756150366Sphk####################################################################### 757150366Sphk 758183314Ssimon# Progress Print 759183314Ssimon# Print $2 at level $1. 760183314Ssimonpprint() { 761183314Ssimon if [ "$1" -le $PPLEVEL ]; then 762194958Sn_hibma runtime=$(( `date +%s` - $NANO_STARTTIME )) 763194958Sn_hibma printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3 764183314Ssimon fi 765183314Ssimon} 766183314Ssimon 767156496Sphkusage () { 768156496Sphk ( 769221851Simp echo "Usage: $0 [-bfiknqvw] [-c config_file]" 770156496Sphk echo " -b suppress builds (both kernel and world)" 771221851Simp echo " -f suppress code slice extraction" 772183314Ssimon echo " -i suppress disk image build" 773156496Sphk echo " -k suppress buildkernel" 774184048Ssam echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc" 775207436Sphilip echo " -q make output more quiet" 776183314Ssimon echo " -v make output more verbose" 777156496Sphk echo " -w suppress buildworld" 778156496Sphk echo " -c specify config file" 779156496Sphk ) 1>&2 780156496Sphk exit 2 781156496Sphk} 782156496Sphk 783150366Sphk####################################################################### 784149014Sphk# Parse arguments 785148422Sphk 786184048Ssamdo_clean=true 787156496Sphkdo_kernel=true 788156496Sphkdo_world=true 789181460Sphkdo_image=true 790221851Simpdo_copyout_partition=true 791150366Sphk 792156496Sphkset +e 793221851Simpargs=`getopt bc:fhiknqvw $*` 794149014Sphkif [ $? -ne 0 ] ; then 795156496Sphk usage 796149014Sphk exit 2 797149014Sphkfi 798156496Sphkset -e 799149014Sphk 800149014Sphkset -- $args 801149014Sphkfor i 802149014Sphkdo 803149014Sphk case "$i" 804149014Sphk in 805150366Sphk -b) 806156496Sphk do_world=false 807156496Sphk do_kernel=false 808183314Ssimon shift 809150366Sphk ;; 810156496Sphk -k) 811156496Sphk do_kernel=false 812183314Ssimon shift 813156496Sphk ;; 814149014Sphk -c) 815149014Sphk . "$2" 816183314Ssimon shift 817183314Ssimon shift 818149014Sphk ;; 819221851Simp -f) 820221851Simp do_copyout_partition=false 821221851Simp shift 822221851Simp ;; 823150366Sphk -h) 824156496Sphk usage 825150366Sphk ;; 826181460Sphk -i) 827181460Sphk do_image=false 828183314Ssimon shift 829181481Ssam ;; 830184048Ssam -n) 831184048Ssam do_clean=false 832184048Ssam shift 833184048Ssam ;; 834183314Ssimon -q) 835183314Ssimon PPLEVEL=$(($PPLEVEL - 1)) 836183314Ssimon shift 837183314Ssimon ;; 838183314Ssimon -v) 839183314Ssimon PPLEVEL=$(($PPLEVEL + 1)) 840183314Ssimon shift 841183314Ssimon ;; 842156496Sphk -w) 843156496Sphk do_world=false 844183314Ssimon shift 845156496Sphk ;; 846149014Sphk --) 847183314Ssimon shift 848183314Ssimon break 849149014Sphk esac 850149014Sphkdone 851149014Sphk 852150366Sphkif [ $# -gt 0 ] ; then 853156496Sphk echo "$0: Extraneous arguments supplied" 854156496Sphk usage 855150366Sphkfi 856150366Sphk 857148422Sphk####################################################################### 858150366Sphk# Setup and Export Internal variables 859150366Sphk# 860194431Sn_hibmatest -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/ 861194431Sn_hibmatest -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ} 862194431Sn_hibmatest -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ} 863148422Sphk 864194431Sn_hibmaNANO_WORLDDIR=${NANO_OBJ}/_.w 865194431Sn_hibmaNANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build 866194431Sn_hibmaNANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install 867175258Ssimon 868150511Ssimonif [ -d ${NANO_TOOLS} ] ; then 869150511Ssimon true 870150511Ssimonelif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then 871150511Ssimon NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS} 872150511Ssimonelse 873150511Ssimon echo "NANO_TOOLS directory does not exist" 1>&2 874150511Ssimon exit 1 875150511Ssimonfi 876150511Ssimon 877184048Ssamif $do_clean ; then 878184048Ssam true 879184048Ssamelse 880184048Ssam NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN" 881184048Ssamfi 882184048Ssam 883212938Simp# Override user's NANO_DRIVE if they specified a NANO_LABEL 884212938Simpif [ ! -z "${NANO_LABEL}" ]; then 885212938Simp NANO_DRIVE=ufs/${NANO_LABEL} 886212938Simpfi 887212938Simp 888148422Sphkexport MAKEOBJDIRPREFIX 889148422Sphk 890148422Sphkexport NANO_ARCH 891149120Sphkexport NANO_CODESIZE 892148422Sphkexport NANO_CONFSIZE 893149073Sphkexport NANO_CUSTOMIZE 894148422Sphkexport NANO_DATASIZE 895149082Sphkexport NANO_DRIVE 896148422Sphkexport NANO_HEADS 897148648Sphkexport NANO_IMAGES 898183284Sbmsexport NANO_IMGNAME 899194431Sn_hibmaexport NANO_MAKE_CONF_BUILD 900194431Sn_hibmaexport NANO_MAKE_CONF_INSTALL 901149120Sphkexport NANO_MEDIASIZE 902148648Sphkexport NANO_NAME 903148648Sphkexport NANO_NEWFS 904148648Sphkexport NANO_OBJ 905148422Sphkexport NANO_PMAKE 906148648Sphkexport NANO_SECTS 907148422Sphkexport NANO_SRC 908150366Sphkexport NANO_TOOLS 909148648Sphkexport NANO_WORLDDIR 910168894Sadrianexport NANO_BOOT0CFG 911168894Sadrianexport NANO_BOOTLOADER 912212938Simpexport NANO_LABEL 913148422Sphk 914148422Sphk####################################################################### 915150366Sphk# And then it is as simple as that... 916148422Sphk 917194958Sn_hibma# File descriptor 3 is used for logging output, see pprint 918194958Sn_hibmaexec 3>&1 919194958Sn_hibma 920194958Sn_hibmaNANO_STARTTIME=`date +%s` 921183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} build starting" 922183314Ssimon 923156496Sphkif $do_world ; then 924184048Ssam if $do_clean ; then 925184048Ssam clean_build 926184048Ssam else 927184048Ssam pprint 2 "Using existing build tree (as instructed)" 928184048Ssam fi 929150366Sphk make_conf_build 930150366Sphk build_world 931156496Sphkelse 932183314Ssimon pprint 2 "Skipping buildworld (as instructed)" 933156496Sphkfi 934156496Sphk 935156496Sphkif $do_kernel ; then 936215070Simp if ! $do_world ; then 937215070Simp make_conf_build 938215070Simp fi 939150366Sphk build_kernel 940150366Sphkelse 941183314Ssimon pprint 2 "Skipping buildkernel (as instructed)" 942150366Sphkfi 943149120Sphk 944149120Sphkclean_world 945149082Sphkmake_conf_install 946148422Sphkinstall_world 947148422Sphkinstall_etc 948161341Ssimonsetup_nanobsd_etc 949148422Sphkinstall_kernel 950149120Sphk 951149082Sphkrun_customize 952150366Sphksetup_nanobsd 953149014Sphkprune_usr 954181461Sphkrun_late_customize 955181460Sphkif $do_image ; then 956181460Sphk create_${NANO_ARCH}_diskimage 957181460Sphkelse 958183314Ssimon pprint 2 "Skipping image build (as instructed)" 959181460Sphkfi 960154466Sphklast_orders 961149120Sphk 962183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} completed" 963