nanobsd.sh revision 194958
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 194958 2009-06-25 13:15:20Z n_hibma $ 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 78149073Sphk# Customize commands. 79149073SphkNANO_CUSTOMIZE="" 80149073Sphk 81181461Sphk# Late customize commands. 82181461SphkNANO_LATE_CUSTOMIZE="" 83181461Sphk 84148648Sphk# Newfs paramters to use 85148648SphkNANO_NEWFS="-b 4096 -f 512 -i 8192 -O1 -U" 86148648Sphk 87149082Sphk# The drive name of the media at runtime 88149082SphkNANO_DRIVE=ad0 89149082Sphk 90148648Sphk# Target media size in 512 bytes sectors 91183284SbmsNANO_MEDIASIZE=1200000 92148648Sphk 93148648Sphk# Number of code images on media (1 or 2) 94148422SphkNANO_IMAGES=2 95148648Sphk 96171986Sphk# 0 -> Leave second image all zeroes so it compresses better. 97171986Sphk# 1 -> Initialize second image with a copy of the first 98171986SphkNANO_INIT_IMG2=1 99171986Sphk 100149535Ssimon# Size of code file system in 512 bytes sectors 101149120Sphk# If zero, size will be as large as possible. 102149120SphkNANO_CODESIZE=0 103149120Sphk 104149120Sphk# Size of configuration file system in 512 bytes sectors 105149120Sphk# Cannot be zero. 106148422SphkNANO_CONFSIZE=2048 107148648Sphk 108148648Sphk# Size of data file system in 512 bytes sectors 109149120Sphk# If zero: no partition configured. 110149120Sphk# If negative: max size possible 111149120SphkNANO_DATASIZE=0 112148422Sphk 113154466Sphk# Size of the /etc ramdisk in 512 bytes sectors 114154466SphkNANO_RAM_ETCSIZE=10240 115154466Sphk 116154466Sphk# Size of the /tmp+/var ramdisk in 512 bytes sectors 117154466SphkNANO_RAM_TMPVARSIZE=10240 118154466Sphk 119148422Sphk# Media geometry, only relevant if bios doesn't understand LBA. 120170607SphkNANO_SECTS=63 121148422SphkNANO_HEADS=16 122148422Sphk 123168894Sadrian# boot0 flags/options and configuration 124168894SadrianNANO_BOOT0CFG="-o packet -s 1 -m 3" 125168894SadrianNANO_BOOTLOADER="boot/boot0sio" 126168894Sadrian 127175258Ssimon# Backing type of md(4) device 128175258Ssimon# Can be "file" or "swap" 129175258SsimonNANO_MD_BACKING="file" 130175258Ssimon 131183314Ssimon# Progress Print level 132183314SsimonPPLEVEL=3 133183314Ssimon 134148422Sphk####################################################################### 135148648Sphk# Not a variable at this time 136148648Sphk 137148648SphkNANO_ARCH=i386 138148648Sphk 139148648Sphk####################################################################### 140150366Sphk# 141150366Sphk# The functions which do the real work. 142150366Sphk# Can be overridden from the config file(s) 143150366Sphk# 144150366Sphk####################################################################### 145148422Sphk 146149120Sphkclean_build ( ) ( 147183314Ssimon pprint 2 "Clean and create object directory (${MAKEOBJDIRPREFIX})" 148148422Sphk 149194431Sn_hibma if ! rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then 150148422Sphk chflags -R noschg ${MAKEOBJDIRPREFIX} 151194431Sn_hibma rm -r ${MAKEOBJDIRPREFIX} 152148422Sphk fi 153148422Sphk mkdir -p ${MAKEOBJDIRPREFIX} 154150343Sphk printenv > ${MAKEOBJDIRPREFIX}/_.env 155148422Sphk) 156148422Sphk 157148422Sphkmake_conf_build ( ) ( 158194431Sn_hibma pprint 2 "Construct build make.conf ($NANO_MAKE_CONF_BUILD)" 159148422Sphk 160194431Sn_hibma echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_BUILD} 161194431Sn_hibma echo "${CONF_BUILD}" >> ${NANO_MAKE_CONF_BUILD} 162148422Sphk) 163148422Sphk 164148422Sphkbuild_world ( ) ( 165183314Ssimon pprint 2 "run buildworld" 166183314Ssimon pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bw" 167148422Sphk 168148422Sphk cd ${NANO_SRC} 169183284Sbms env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} \ 170194431Sn_hibma __MAKE_CONF=${NANO_MAKE_CONF_BUILD} buildworld \ 171148422Sphk > ${MAKEOBJDIRPREFIX}/_.bw 2>&1 172148422Sphk) 173148422Sphk 174148422Sphkbuild_kernel ( ) ( 175183314Ssimon pprint 2 "build kernel ($NANO_KERNEL)" 176183314Ssimon pprint 3 "log: ${MAKEOBJDIRPREFIX}/_.bk" 177148422Sphk 178148422Sphk if [ -f ${NANO_KERNEL} ] ; then 179148422Sphk cp ${NANO_KERNEL} ${NANO_SRC}/sys/${NANO_ARCH}/conf 180148422Sphk fi 181148422Sphk 182181083Ssam (cd ${NANO_SRC}; 183181083Ssam # unset these just in case to avoid compiler complaints 184181083Ssam # when cross-building 185181083Ssam unset TARGET_CPUTYPE 186181083Ssam unset TARGET_BIG_ENDIAN 187183284Sbms env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} buildkernel \ 188194431Sn_hibma __MAKE_CONF=${NANO_MAKE_CONF_BUILD} KERNCONF=`basename ${NANO_KERNEL}` \ 189194440Sn_hibma > ${MAKEOBJDIRPREFIX}/_.bk 2>&1 190181083Ssam ) 191148422Sphk) 192148422Sphk 193148422Sphkclean_world ( ) ( 194194431Sn_hibma if [ "${NANO_OBJ}" != "${MAKEOBJDIRPREFIX}" ]; then 195194431Sn_hibma pprint 2 "Clean and create object directory (${NANO_OBJ})" 196194431Sn_hibma if ! rm -rf ${NANO_OBJ} > /dev/null 2>&1 ; then 197194431Sn_hibma chflags -R noschg ${NANO_OBJ} 198194431Sn_hibma rm -r ${NANO_OBJ} 199194431Sn_hibma fi 200194431Sn_hibma mkdir -p ${NANO_OBJ} ${NANO_WORLDDIR} 201194431Sn_hibma printenv > ${NANO_OBJ}/_.env 202148422Sphk else 203194431Sn_hibma pprint 2 "Clean and create world directory (${NANO_WORLDDIR})" 204194431Sn_hibma if ! rm -rf ${NANO_WORLDDIR}/ > /dev/null 2>&1 ; then 205194431Sn_hibma chflags -R noschg ${NANO_WORLDDIR} 206194431Sn_hibma rm -rf ${NANO_WORLDDIR} 207194431Sn_hibma fi 208194431Sn_hibma mkdir -p ${NANO_WORLDDIR} 209148422Sphk fi 210148422Sphk) 211148422Sphk 212148422Sphkmake_conf_install ( ) ( 213194431Sn_hibma pprint 2 "Construct install make.conf ($NANO_MAKE_CONF_INSTALL)" 214148422Sphk 215194431Sn_hibma echo "${CONF_WORLD}" > ${NANO_MAKE_CONF_INSTALL} 216194431Sn_hibma echo "${CONF_INSTALL}" >> ${NANO_MAKE_CONF_INSTALL} 217148422Sphk) 218148422Sphk 219148422Sphkinstall_world ( ) ( 220183314Ssimon pprint 2 "installworld" 221194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.iw" 222148422Sphk 223148422Sphk cd ${NANO_SRC} 224183284Sbms env TARGET_ARCH=${NANO_ARCH} \ 225194431Sn_hibma ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} installworld \ 226148422Sphk DESTDIR=${NANO_WORLDDIR} \ 227194431Sn_hibma > ${NANO_OBJ}/_.iw 2>&1 228148422Sphk chflags -R noschg ${NANO_WORLDDIR} 229148422Sphk) 230148422Sphk 231148422Sphkinstall_etc ( ) ( 232148422Sphk 233183314Ssimon pprint 2 "install /etc" 234194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.etc" 235148422Sphk 236160783Sthomas cd ${NANO_SRC} 237183284Sbms env TARGET_ARCH=${NANO_ARCH} \ 238194431Sn_hibma ${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} distribution \ 239148422Sphk DESTDIR=${NANO_WORLDDIR} \ 240194431Sn_hibma > ${NANO_OBJ}/_.etc 2>&1 241148422Sphk) 242148422Sphk 243148422Sphkinstall_kernel ( ) ( 244183314Ssimon pprint 2 "install kernel" 245194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.ik" 246148422Sphk 247148422Sphk cd ${NANO_SRC} 248183284Sbms env TARGET_ARCH=${NANO_ARCH} ${NANO_PMAKE} installkernel \ 249148422Sphk DESTDIR=${NANO_WORLDDIR} \ 250194431Sn_hibma __MAKE_CONF=${NANO_MAKE_CONF_INSTALL} KERNCONF=`basename ${NANO_KERNEL}` \ 251194431Sn_hibma > ${NANO_OBJ}/_.ik 2>&1 252148422Sphk) 253148422Sphk 254150366Sphkrun_customize() ( 255150366Sphk 256183314Ssimon pprint 2 "run customize scripts" 257150366Sphk for c in $NANO_CUSTOMIZE 258150366Sphk do 259183314Ssimon pprint 2 "customize \"$c\"" 260194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.cust.$c" 261183314Ssimon pprint 4 "`type $c`" 262194431Sn_hibma ( $c ) > ${NANO_OBJ}/_.cust.$c 2>&1 263150366Sphk done 264150366Sphk) 265150366Sphk 266181461Sphkrun_late_customize() ( 267181461Sphk 268183314Ssimon pprint 2 "run late customize scripts" 269181461Sphk for c in $NANO_LATE_CUSTOMIZE 270181461Sphk do 271183314Ssimon pprint 2 "late customize \"$c\"" 272194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.late_cust.$c" 273183314Ssimon pprint 4 "`type $c`" 274194431Sn_hibma ( $c ) > ${NANO_OBJ}/_.late_cust.$c 2>&1 275181461Sphk done 276181461Sphk) 277181461Sphk 278150366Sphksetup_nanobsd ( ) ( 279183314Ssimon pprint 2 "configure nanobsd setup" 280194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.dl" 281148422Sphk 282149103Sphk ( 283148422Sphk cd ${NANO_WORLDDIR} 284148422Sphk 285170607Sphk # Move /usr/local/etc to /etc/local so that the /cfg stuff 286170607Sphk # can stomp on it. Otherwise packages like ipsec-tools which 287170607Sphk # have hardcoded paths under ${prefix}/etc are not tweakable. 288170607Sphk if [ -d usr/local/etc ] ; then 289170607Sphk ( 290183727Sn_hibma mkdir -p etc/local 291170607Sphk cd usr/local/etc 292170607Sphk find . -print | cpio -dumpl ../../../etc/local 293170607Sphk cd .. 294170607Sphk rm -rf etc 295170607Sphk ln -s ../../etc/local etc 296170607Sphk ) 297170607Sphk fi 298170607Sphk 299149082Sphk for d in var etc 300149082Sphk do 301149082Sphk # link /$d under /conf 302149082Sphk # we use hard links so we have them both places. 303149082Sphk # the files in /$d will be hidden by the mount. 304149082Sphk # XXX: configure /$d ramdisk size 305149082Sphk mkdir -p conf/base/$d conf/default/$d 306149082Sphk find $d -print | cpio -dumpl conf/base/ 307149082Sphk done 308148422Sphk 309154466Sphk echo "$NANO_RAM_ETCSIZE" > conf/base/etc/md_size 310154466Sphk echo "$NANO_RAM_TMPVARSIZE" > conf/base/var/md_size 311154466Sphk 312148422Sphk # pick up config files from the special partition 313149082Sphk echo "mount -o ro /dev/${NANO_DRIVE}s3" > conf/default/etc/remount 314148422Sphk 315149082Sphk # Put /tmp on the /var ramdisk (could be symlink already) 316149082Sphk rmdir tmp || true 317149082Sphk rm tmp || true 318148422Sphk ln -s var/tmp tmp 319149082Sphk 320194431Sn_hibma ) > ${NANO_OBJ}/_.dl 2>&1 321148422Sphk) 322148422Sphk 323161341Ssimonsetup_nanobsd_etc ( ) ( 324183314Ssimon pprint 2 "configure nanobsd /etc" 325161341Ssimon 326161341Ssimon ( 327161341Ssimon cd ${NANO_WORLDDIR} 328161341Ssimon 329161341Ssimon # create diskless marker file 330161341Ssimon touch etc/diskless 331161341Ssimon 332170324Sphk # Make root filesystem R/O by default 333170324Sphk echo "root_rw_mount=NO" >> etc/defaults/rc.conf 334170324Sphk 335161341Ssimon # save config file for scripts 336161341Ssimon echo "NANO_DRIVE=${NANO_DRIVE}" > etc/nanobsd.conf 337161341Ssimon 338161341Ssimon echo "/dev/${NANO_DRIVE}s1a / ufs ro 1 1" > etc/fstab 339161341Ssimon echo "/dev/${NANO_DRIVE}s3 /cfg ufs rw,noauto 2 2" >> etc/fstab 340161341Ssimon mkdir -p cfg 341161341Ssimon ) 342161341Ssimon) 343161341Ssimon 344149014Sphkprune_usr() ( 345149014Sphk 346149014Sphk # Remove all empty directories in /usr 347149014Sphk find ${NANO_WORLDDIR}/usr -type d -depth -print | 348149014Sphk while read d 349149014Sphk do 350149014Sphk rmdir $d > /dev/null 2>&1 || true 351149014Sphk done 352149014Sphk) 353149014Sphk 354148422Sphkcreate_i386_diskimage ( ) ( 355183314Ssimon pprint 2 "build diskimage" 356194431Sn_hibma pprint 3 "log: ${NANO_OBJ}/_.di" 357148422Sphk 358149103Sphk ( 359149120Sphk echo $NANO_MEDIASIZE $NANO_IMAGES \ 360149120Sphk $NANO_SECTS $NANO_HEADS \ 361149120Sphk $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE | 362148422Sphk awk ' 363150717Sphk { 364149120Sphk printf "# %s\n", $0 365149120Sphk 366148422Sphk # size of cylinder in sectors 367149120Sphk cs = $3 * $4 368148422Sphk 369148422Sphk # number of full cylinders on media 370148422Sphk cyl = int ($1 / cs) 371148422Sphk 372149120Sphk # output fdisk geometry spec, truncate cyls to 1023 373148422Sphk if (cyl <= 1023) 374149120Sphk print "g c" cyl " h" $4 " s" $3 375148422Sphk else 376149120Sphk print "g c" 1023 " h" $4 " s" $3 377148422Sphk 378149120Sphk if ($7 > 0) { 379149120Sphk # size of data partition in full cylinders 380149120Sphk dsl = int (($7 + cs - 1) / cs) 381149120Sphk } else { 382149120Sphk dsl = 0; 383149120Sphk } 384148422Sphk 385148422Sphk # size of config partition in full cylinders 386149120Sphk csl = int (($6 + cs - 1) / cs) 387148422Sphk 388150717Sphk if ($5 == 0) { 389149120Sphk # size of image partition(s) in full cylinders 390149120Sphk isl = int ((cyl - dsl - csl) / $2) 391149120Sphk } else { 392149120Sphk isl = int (($5 + cs - 1) / cs) 393149120Sphk } 394148422Sphk 395148422Sphk # First image partition start at second track 396149120Sphk print "p 1 165 " $3, isl * cs - $3 397148422Sphk c = isl * cs; 398148422Sphk 399148422Sphk # Second image partition (if any) also starts offset one 400148422Sphk # track to keep them identical. 401148422Sphk if ($2 > 1) { 402149120Sphk print "p 2 165 " $3 + c, isl * cs - $3 403148422Sphk c += isl * cs; 404148422Sphk } 405148422Sphk 406148422Sphk # Config partition starts at cylinder boundary. 407148422Sphk print "p 3 165 " c, csl * cs 408148422Sphk c += csl * cs 409148422Sphk 410148422Sphk # Data partition (if any) starts at cylinder boundary. 411149120Sphk if ($7 > 0) { 412148422Sphk print "p 4 165 " c, dsl * cs 413174936Ssimon } else if ($7 < 0 && $1 > c) { 414174936Ssimon print "p 4 165 " c, $1 - c 415150717Sphk } else if ($1 < c) { 416150717Sphk print "Disk space overcommitted by", \ 417150717Sphk c - $1, "sectors" > "/dev/stderr" 418150717Sphk exit 2 419148422Sphk } 420183279Sbms 421183279Sbms # Force slice 1 to be marked active. This is necessary 422183279Sbms # for booting the image from a USB device to work. 423183279Sbms print "a 1" 424150717Sphk } 425194431Sn_hibma ' > ${NANO_OBJ}/_.fdisk 426148422Sphk 427183284Sbms IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME} 428194431Sn_hibma MNT=${NANO_OBJ}/_.mnt 429148422Sphk mkdir -p ${MNT} 430148422Sphk 431175258Ssimon if [ "${NANO_MD_BACKING}" = "swap" ] ; then 432175258Ssimon MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \ 433175258Ssimon -y ${NANO_HEADS}` 434175258Ssimon else 435175258Ssimon echo "Creating md backing file..." 436175258Ssimon dd if=/dev/zero of=${IMG} bs=${NANO_SECTS}b \ 437194437Sn_hibma count=`expr ${NANO_MEDIASIZE} / ${NANO_SECTS}` 438175258Ssimon MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \ 439175258Ssimon -y ${NANO_HEADS}` 440175258Ssimon fi 441148422Sphk 442149014Sphk trap "df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT 443148422Sphk 444194431Sn_hibma fdisk -i -f ${NANO_OBJ}/_.fdisk ${MD} 445149120Sphk fdisk ${MD} 446148422Sphk # XXX: params 447148422Sphk # XXX: pick up cached boot* files, they may not be in image anymore. 448168894Sadrian boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD} 449160388Sthomas bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1 450149120Sphk bsdlabel ${MD}s1 451148422Sphk 452148422Sphk # Create first image 453148648Sphk newfs ${NANO_NEWFS} /dev/${MD}s1a 454194437Sn_hibma mount /dev/${MD}s1a ${MNT} 455149014Sphk df -i ${MNT} 456175258Ssimon echo "Copying worlddir..." 457148422Sphk ( cd ${NANO_WORLDDIR} && find . -print | cpio -dump ${MNT} ) 458148422Sphk df -i ${MNT} 459175258Ssimon echo "Generating mtree..." 460194431Sn_hibma ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree 461194431Sn_hibma ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du 462148422Sphk umount ${MNT} 463148422Sphk 464171986Sphk if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then 465148422Sphk # Duplicate to second image (if present) 466175258Ssimon echo "Duplicating to second image..." 467194437Sn_hibma dd if=/dev/${MD}s1 of=/dev/${MD}s2 bs=64k 468170322Sphk mount /dev/${MD}s2a ${MNT} 469170322Sphk for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab 470170322Sphk do 471170322Sphk sed -i "" "s/${NANO_DRIVE}s1/${NANO_DRIVE}s2/g" $f 472170322Sphk done 473170322Sphk umount ${MNT} 474148422Sphk fi 475148422Sphk 476148422Sphk # Create Config slice 477148648Sphk newfs ${NANO_NEWFS} /dev/${MD}s3 478148422Sphk # XXX: fill from where ? 479148422Sphk 480148422Sphk # Create Data slice, if any. 481194437Sn_hibma if [ $NANO_DATASIZE -ne 0 ] ; then 482148648Sphk newfs ${NANO_NEWFS} /dev/${MD}s4 483148422Sphk # XXX: fill from where ? 484148422Sphk fi 485148422Sphk 486175258Ssimon if [ "${NANO_MD_BACKING}" = "swap" ] ; then 487194440Sn_hibma echo "Writing out ${NANO_IMGNAME}..." 488175258Ssimon dd if=/dev/${MD} of=${IMG} bs=64k 489175258Ssimon fi 490175258Ssimon 491175258Ssimon echo "Writing out _.disk.image..." 492194437Sn_hibma dd if=/dev/${MD}s1 of=${NANO_DISKIMGDIR}/_.disk.image bs=64k 493148422Sphk mdconfig -d -u $MD 494194431Sn_hibma ) > ${NANO_OBJ}/_.di 2>&1 495148422Sphk) 496148422Sphk 497154466Sphklast_orders () ( 498154466Sphk # Redefine this function with any last orders you may have 499154466Sphk # after the build completed, for instance to copy the finished 500154466Sphk # image to a more convenient place: 501194431Sn_hibma # cp ${NANO_DISKIMGDIR}/_.disk.image /home/ftp/pub/nanobsd.disk 502154466Sphk) 503154466Sphk 504148422Sphk####################################################################### 505148422Sphk# 506150366Sphk# Optional convenience functions. 507148422Sphk# 508148422Sphk####################################################################### 509148422Sphk 510149014Sphk####################################################################### 511150367Sphk# Common Flash device geometries 512150367Sphk# 513150367Sphk 514150367SphkFlashDevice () { 515150717Sphk if [ -d ${NANO_TOOLS} ] ; then 516150717Sphk . ${NANO_TOOLS}/FlashDevice.sub 517150717Sphk else 518150717Sphk . ${NANO_SRC}/${NANO_TOOLS}/FlashDevice.sub 519150717Sphk fi 520150367Sphk sub_FlashDevice $1 $2 521150367Sphk} 522150367Sphk 523183284Sbms####################################################################### 524183284Sbms# USB device geometries 525183284Sbms# 526183284Sbms# Usage: 527183284Sbms# UsbDevice Generic 1000 # a generic flash key sold as having 1GB 528183284Sbms# 529183284Sbms# This function will set NANO_MEDIASIZE, NANO_HEADS and NANO_SECTS for you. 530183284Sbms# 531183284Sbms# Note that the capacity of a flash key is usually advertised in MB or 532183284Sbms# GB, *not* MiB/GiB. As such, the precise number of cylinders available 533183284Sbms# for C/H/S geometry may vary depending on the actual flash geometry. 534183284Sbms# 535183284Sbms# The following generic device layouts are understood: 536183284Sbms# generic An alias for generic-hdd. 537183284Sbms# generic-hdd 255H 63S/T xxxxC with no MBR restrictions. 538183284Sbms# generic-fdd 64H 32S/T xxxxC with no MBR restrictions. 539183284Sbms# 540183284Sbms# The generic-hdd device is preferred for flash devices larger than 1GB. 541183284Sbms# 542150367Sphk 543183284SbmsUsbDevice () { 544183284Sbms a1=`echo $1 | tr '[:upper:]' '[:lower:]'` 545183284Sbms case $a1 in 546183284Sbms generic-fdd) 547183284Sbms NANO_HEADS=64 548183284Sbms NANO_SECTS=32 549183284Sbms NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 550183284Sbms ;; 551183284Sbms generic|generic-hdd) 552183284Sbms NANO_HEADS=255 553183284Sbms NANO_SECTS=63 554183284Sbms NANO_MEDIASIZE=$(( $2 * 1000 * 1000 / 512 )) 555183284Sbms ;; 556183284Sbms *) 557183284Sbms echo "Unknown USB flash device" 558183284Sbms exit 2 559183284Sbms ;; 560183284Sbms esac 561183284Sbms} 562183284Sbms 563150367Sphk####################################################################### 564150366Sphk# Setup serial console 565150343Sphk 566150366Sphkcust_comconsole () ( 567150366Sphk # Enable getty on console 568181751Sphk sed -i "" -e /tty[du]0/s/off/on/ ${NANO_WORLDDIR}/etc/ttys 569150366Sphk 570150366Sphk # Disable getty on syscons devices 571150366Sphk sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${NANO_WORLDDIR}/etc/ttys 572150366Sphk 573150366Sphk # Tell loader to use serial console early. 574150366Sphk echo " -h" > ${NANO_WORLDDIR}/boot.config 575150366Sphk) 576150366Sphk 577150366Sphk####################################################################### 578150366Sphk# Allow root login via ssh 579150366Sphk 580150366Sphkcust_allow_ssh_root () ( 581150366Sphk sed -i "" -e '/PermitRootLogin/s/.*/PermitRootLogin yes/' \ 582150366Sphk ${NANO_WORLDDIR}/etc/ssh/sshd_config 583150366Sphk) 584150366Sphk 585150366Sphk####################################################################### 586150366Sphk# Install the stuff under ./Files 587150366Sphk 588150366Sphkcust_install_files () ( 589150511Ssimon cd ${NANO_TOOLS}/Files 590183315Ssimon find . -print | grep -Ev '/(CVS|\.svn)' | cpio -dumpv ${NANO_WORLDDIR} 591150366Sphk) 592150366Sphk 593150366Sphk####################################################################### 594170607Sphk# Install packages from ${NANO_PACKAGE_DIR} 595170607Sphk 596170607Sphkcust_pkg () ( 597170607Sphk 598170607Sphk # Copy packages into chroot 599170607Sphk mkdir -p ${NANO_WORLDDIR}/Pkg 600186181Sphk ( 601186181Sphk cd ${NANO_PACKAGE_DIR} 602186181Sphk find ${NANO_PACKAGE_LIST} -print | 603186181Sphk cpio -dumpv ${NANO_WORLDDIR}/Pkg 604186181Sphk ) 605170607Sphk 606170607Sphk # Count & report how many we have to install 607170607Sphk todo=`ls ${NANO_WORLDDIR}/Pkg | wc -l` 608170607Sphk echo "=== TODO: $todo" 609170607Sphk ls ${NANO_WORLDDIR}/Pkg 610170607Sphk echo "===" 611170607Sphk while true 612170607Sphk do 613182690Sthomas # Record how many we have now 614170607Sphk have=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l` 615170607Sphk 616171986Sphk # Attempt to install more packages 617171986Sphk # ...but no more than 200 at a time due to pkg_add's internal 618171986Sphk # limitations. 619171986Sphk chroot ${NANO_WORLDDIR} sh -c \ 620171986Sphk 'ls Pkg/*tbz | xargs -n 200 pkg_add -F' || true 621170607Sphk 622170607Sphk # See what that got us 623170607Sphk now=`ls ${NANO_WORLDDIR}/var/db/pkg | wc -l` 624170607Sphk echo "=== NOW $now" 625170607Sphk ls ${NANO_WORLDDIR}/var/db/pkg 626170607Sphk echo "===" 627170607Sphk 628170607Sphk 629170607Sphk if [ $now -eq $todo ] ; then 630170607Sphk echo "DONE $now packages" 631170607Sphk break 632170607Sphk elif [ $now -eq $have ] ; then 633170607Sphk echo "FAILED: Nothing happened on this pass" 634170607Sphk exit 2 635170607Sphk fi 636170607Sphk done 637170607Sphk rm -rf ${NANO_WORLDDIR}/Pkg 638170607Sphk) 639170607Sphk 640170607Sphk####################################################################### 641150366Sphk# Convenience function: 642150366Sphk# Register $1 as customize function. 643150366Sphk 644150343Sphkcustomize_cmd () { 645150343Sphk NANO_CUSTOMIZE="$NANO_CUSTOMIZE $1" 646150343Sphk} 647150343Sphk 648150343Sphk####################################################################### 649181461Sphk# Convenience function: 650181461Sphk# Register $1 as late customize function to run just before 651181461Sphk# image creation. 652181461Sphk 653181461Sphklate_customize_cmd () { 654181461Sphk NANO_LATE_CUSTOMIZE="$NANO_LATE_CUSTOMIZE $1" 655181461Sphk} 656181461Sphk 657181461Sphk####################################################################### 658150366Sphk# 659150366Sphk# All set up to go... 660150366Sphk# 661150366Sphk####################################################################### 662150366Sphk 663183314Ssimon# Progress Print 664183314Ssimon# Print $2 at level $1. 665183314Ssimonpprint() { 666183314Ssimon if [ "$1" -le $PPLEVEL ]; then 667194958Sn_hibma runtime=$(( `date +%s` - $NANO_STARTTIME )) 668194958Sn_hibma printf "%s %.${1}s %s\n" "`date -u -r $runtime +%H:%M:%S`" "#####" "$2" 1>&3 669183314Ssimon fi 670183314Ssimon} 671183314Ssimon 672156496Sphkusage () { 673156496Sphk ( 674183314Ssimon echo "Usage: $0 [-bikqvw] [-c config_file]" 675156496Sphk echo " -b suppress builds (both kernel and world)" 676183314Ssimon echo " -i suppress disk image build" 677156496Sphk echo " -k suppress buildkernel" 678184048Ssam echo " -n add -DNO_CLEAN to buildworld, buildkernel, etc" 679183314Ssimon echo " -q make output more quite" 680183314Ssimon echo " -v make output more verbose" 681156496Sphk echo " -w suppress buildworld" 682156496Sphk echo " -c specify config file" 683156496Sphk ) 1>&2 684156496Sphk exit 2 685156496Sphk} 686156496Sphk 687150366Sphk####################################################################### 688149014Sphk# Parse arguments 689148422Sphk 690184048Ssamdo_clean=true 691156496Sphkdo_kernel=true 692156496Sphkdo_world=true 693181460Sphkdo_image=true 694150366Sphk 695156496Sphkset +e 696184048Ssamargs=`getopt bc:hiknqvw $*` 697149014Sphkif [ $? -ne 0 ] ; then 698156496Sphk usage 699149014Sphk exit 2 700149014Sphkfi 701156496Sphkset -e 702149014Sphk 703149014Sphkset -- $args 704149014Sphkfor i 705149014Sphkdo 706149014Sphk case "$i" 707149014Sphk in 708150366Sphk -b) 709156496Sphk do_world=false 710156496Sphk do_kernel=false 711183314Ssimon shift 712150366Sphk ;; 713156496Sphk -k) 714156496Sphk do_kernel=false 715183314Ssimon shift 716156496Sphk ;; 717149014Sphk -c) 718149014Sphk . "$2" 719183314Ssimon shift 720183314Ssimon shift 721149014Sphk ;; 722150366Sphk -h) 723156496Sphk usage 724150366Sphk ;; 725181460Sphk -i) 726181460Sphk do_image=false 727183314Ssimon shift 728181481Ssam ;; 729184048Ssam -n) 730184048Ssam do_clean=false 731184048Ssam shift 732184048Ssam ;; 733183314Ssimon -q) 734183314Ssimon PPLEVEL=$(($PPLEVEL - 1)) 735183314Ssimon shift 736183314Ssimon ;; 737183314Ssimon -v) 738183314Ssimon PPLEVEL=$(($PPLEVEL + 1)) 739183314Ssimon shift 740183314Ssimon ;; 741156496Sphk -w) 742156496Sphk do_world=false 743183314Ssimon shift 744156496Sphk ;; 745149014Sphk --) 746183314Ssimon shift 747183314Ssimon break 748149014Sphk esac 749149014Sphkdone 750149014Sphk 751150366Sphkif [ $# -gt 0 ] ; then 752156496Sphk echo "$0: Extraneous arguments supplied" 753156496Sphk usage 754150366Sphkfi 755150366Sphk 756148422Sphk####################################################################### 757150366Sphk# Setup and Export Internal variables 758150366Sphk# 759194431Sn_hibmatest -n "${NANO_OBJ}" || NANO_OBJ=/usr/obj/nanobsd.${NANO_NAME}/ 760194431Sn_hibmatest -n "${MAKEOBJDIRPREFIX}" || MAKEOBJDIRPREFIX=${NANO_OBJ} 761194431Sn_hibmatest -n "${NANO_DISKIMGDIR}" || NANO_DISKIMGDIR=${NANO_OBJ} 762148422Sphk 763194431Sn_hibmaNANO_WORLDDIR=${NANO_OBJ}/_.w 764194431Sn_hibmaNANO_MAKE_CONF_BUILD=${MAKEOBJDIRPREFIX}/make.conf.build 765194431Sn_hibmaNANO_MAKE_CONF_INSTALL=${NANO_OBJ}/make.conf.install 766175258Ssimon 767150511Ssimonif [ -d ${NANO_TOOLS} ] ; then 768150511Ssimon true 769150511Ssimonelif [ -d ${NANO_SRC}/${NANO_TOOLS} ] ; then 770150511Ssimon NANO_TOOLS=${NANO_SRC}/${NANO_TOOLS} 771150511Ssimonelse 772150511Ssimon echo "NANO_TOOLS directory does not exist" 1>&2 773150511Ssimon exit 1 774150511Ssimonfi 775150511Ssimon 776184048Ssamif $do_clean ; then 777184048Ssam true 778184048Ssamelse 779184048Ssam NANO_PMAKE="${NANO_PMAKE} -DNO_CLEAN" 780184048Ssamfi 781184048Ssam 782148422Sphkexport MAKEOBJDIRPREFIX 783148422Sphk 784148422Sphkexport NANO_ARCH 785149120Sphkexport NANO_CODESIZE 786148422Sphkexport NANO_CONFSIZE 787149073Sphkexport NANO_CUSTOMIZE 788148422Sphkexport NANO_DATASIZE 789149082Sphkexport NANO_DRIVE 790148422Sphkexport NANO_HEADS 791148648Sphkexport NANO_IMAGES 792183284Sbmsexport NANO_IMGNAME 793194431Sn_hibmaexport NANO_MAKE_CONF_BUILD 794194431Sn_hibmaexport NANO_MAKE_CONF_INSTALL 795149120Sphkexport NANO_MEDIASIZE 796148648Sphkexport NANO_NAME 797148648Sphkexport NANO_NEWFS 798148648Sphkexport NANO_OBJ 799148422Sphkexport NANO_PMAKE 800148648Sphkexport NANO_SECTS 801148422Sphkexport NANO_SRC 802150366Sphkexport NANO_TOOLS 803148648Sphkexport NANO_WORLDDIR 804168894Sadrianexport NANO_BOOT0CFG 805168894Sadrianexport NANO_BOOTLOADER 806148422Sphk 807148422Sphk####################################################################### 808150366Sphk# And then it is as simple as that... 809148422Sphk 810194958Sn_hibma# File descriptor 3 is used for logging output, see pprint 811194958Sn_hibmaexec 3>&1 812194958Sn_hibma 813194958Sn_hibmaNANO_STARTTIME=`date +%s` 814183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} build starting" 815183314Ssimon 816156496Sphkif $do_world ; then 817184048Ssam if $do_clean ; then 818184048Ssam clean_build 819184048Ssam else 820184048Ssam pprint 2 "Using existing build tree (as instructed)" 821184048Ssam fi 822150366Sphk make_conf_build 823150366Sphk build_world 824156496Sphkelse 825183314Ssimon pprint 2 "Skipping buildworld (as instructed)" 826156496Sphkfi 827156496Sphk 828156496Sphkif $do_kernel ; then 829150366Sphk build_kernel 830150366Sphkelse 831183314Ssimon pprint 2 "Skipping buildkernel (as instructed)" 832150366Sphkfi 833149120Sphk 834149120Sphkclean_world 835149082Sphkmake_conf_install 836148422Sphkinstall_world 837148422Sphkinstall_etc 838161341Ssimonsetup_nanobsd_etc 839148422Sphkinstall_kernel 840149120Sphk 841149082Sphkrun_customize 842150366Sphksetup_nanobsd 843149014Sphkprune_usr 844181461Sphkrun_late_customize 845181460Sphkif $do_image ; then 846181460Sphk create_${NANO_ARCH}_diskimage 847181460Sphkelse 848183314Ssimon pprint 2 "Skipping image build (as instructed)" 849181460Sphkfi 850154466Sphklast_orders 851149120Sphk 852183314Ssimonpprint 1 "NanoBSD image ${NANO_NAME} completed" 853