1#!/bin/sh 2# 3# Copyright (c) 1994-2009 Poul-Henning Kamp. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# 27# $FreeBSD$ 28# 29 30set -e 31 32exec < /dev/null 33 34if [ `uname -m` = "i386" -o `uname -m` = "amd64" ] ; then 35 TARGET_PART=`df / | sed ' 36 1d 37 s/[ ].*// 38 s,/dev/,, 39 s,s1a,s3a, 40 s,s2a,s1a, 41 s,s3a,s2a, 42 '` 43 44 FREEBSD_PART=`sed -n \ 45 -e 's/#.*//' \ 46 -e '/[ ]\/freebsd[ ]/!d' \ 47 -e 's/[ ].*//p' \ 48 /etc/fstab` 49 50 # Calculate a suggested gpart command 51 TARGET_DISK=`expr ${TARGET_PART} : '\(.*\)s[12]a$' || true` 52 TARGET_SLICE=`expr ${TARGET_PART} : '.*s\([12]\)a$' || true` 53 GPART_SUGGESTION="gpart set -a active -i $TARGET_SLICE /dev/$TARGET_DISK" 54 unset TARGET_DISK TARGET_SLICE 55else 56 TARGET_PART=unknown 57 FREEBSD_PART=unknown 58 GPART_SUGGESTION=unknown 59fi 60 61 62# Relative to /freebsd 63PORTS_PATH=ports 64SRC_PATH=src 65# OBJ_PATH=obj 66 67# Name of kernel 68KERNCONF=GENERIC 69 70# srcconf 71#SRCCONF="SRCCONF=/usr/src/src.conf" 72 73# -j arg to make(1) 74 75ncpu=`sysctl -n kern.smp.cpus` 76if [ $ncpu -gt 1 ] ; then 77 JARG="-j $ncpu" 78fi 79 80# serial console ? 81SERCONS=false 82 83PKG_DIR=/usr/ports/packages/All 84 85# Remotely mounted distfiles 86# REMOTEDISTFILES=fs:/rdonly/distfiles 87 88# Proxy 89#FTP_PROXY=http://127.0.0.1:3128/ 90#HTTP_PROXY=http://127.0.0.1:3128/ 91#export FTP_PROXY HTTP_PROXY 92 93PORTS_WE_WANT=' 94' 95 96PORTS_OPTS="BATCH=YES A4=yes" 97 98PORTS_WITHOUT="" 99PORTS_WITH="" 100 101CONFIGFILES=' 102' 103 104SBMNT="/mnt.sysbuild" 105 106cleanup() ( 107) 108 109before_ports() ( 110) 111 112before_ports_chroot() ( 113) 114 115final_root() ( 116) 117 118final_chroot() ( 119) 120 121####################################################################### 122# -P is a pretty neat way to clean junk out from your ports dist-files: 123# 124# mkdir /freebsd/ports/distfiles.old 125# mv /freebsd/ports/distfiles/* /freebsd/ports/distfiles.old 126# sh sysbuild.sh -c $yourconfig -P /freebsd/ports/distfiles.old 127# rm -rf /freebsd/ports/distfiles.old 128# 129# Unfortunately bsd.ports.mk does not attempt to use a hard-link so 130# while this runs you need diskspace for both your old and your "new" 131# distfiles. 132# 133####################################################################### 134 135usage () { 136 ( 137 echo "Usage: $0 [-b/-k/-w] [-c config_file]" 138 echo " -b suppress builds (both kernel and world)" 139 echo " -k suppress buildkernel" 140 echo " -w suppress buildworld" 141 echo " -p used cached packages" 142 echo " -P <dir> prefetch ports" 143 echo " -c specify config file" 144 ) 1>&2 145 exit 2 146} 147 148####################################################################### 149####################################################################### 150 151if [ ! -f $0 ] ; then 152 echo "Must be able to access self ($0)" 1>&2 153 exit 1 154fi 155 156if grep -q 'Magic String: 0`0nQT40W%l,CX&' $0 ; then 157 true 158else 159 echo "self ($0) does not contain magic string" 1>&2 160 exit 1 161fi 162 163####################################################################### 164 165set -e 166 167log_it() ( 168 a="$*" 169 set `cat /tmp/_sb_log` 170 TX=`date +%s` 171 echo "$1 $TX" > /tmp/_sb_log 172 DT=`expr $TX - $1 || true` 173 DL=`expr $TX - $2 || true` 174 echo -n "### `date +%H:%M:%S`" 175 printf " ### %5d ### %5d ### %s\n" $DT $DL "$a" 176) 177 178####################################################################### 179 180ports_make() { 181 make $* WITH="${PORTS_WITH}" WITHOUT="${PORTS_WITHOUT}" ${PORTS_OPTS} 182} 183 184ports_recurse() ( 185 cd /usr/ports 186 t=$1 187 shift 188 if [ "x$t" = "x." ] ; then 189 true > /tmp/_.plist 190 true > /tmp/_.plist.tdone 191 echo 'digraph {' > /tmp/_.plist.dot 192 fi 193 if grep -q "^$t\$" /tmp/_.plist.tdone ; then 194 return 195 fi 196 echo "$t" >> /tmp/_.plist.tdone 197 for d 198 do 199 fl="" 200 if [ ! -d $d ] ; then 201 fl=FLAVOR=`expr $d : '.*@\(.*\)'` 202 bd=`expr $d : '\(.*\)@.*'` 203 if [ ! -d $bd ] ; then 204 echo "Missing port $d ($t) (fl $fl) (bd $bd)" 1>&2 205 continue 206 fi 207 # echo "Flavored port $d ($t) (fl $fl) (bd $bd)" 1>&2 208 d=$bd 209 fi 210 d=`cd /usr/ports && cd $d && /bin/pwd` 211 if [ ! -f $d/Makefile ] ; then 212 echo "Missing port (Makefile) $d" 1>&2 213 continue 214 fi 215 if [ "x$t" != "x." ] ; then 216 echo "\"$t\" -> \"$d\"" >> /tmp/_.plist.dot 217 fi 218 if grep -q "^$d\$" /tmp/_.plist ; then 219 true 220 elif grep -q "^$d\$" /tmp/_.plist.tdone ; then 221 true 222 else 223 ( 224 cd $d 225 l="" 226 for a in `ports_make -V _UNIFIED_DEPENDS $fl` 227 do 228 x=`expr "$a" : '.*:\(.*\)'` 229 l="${l} ${x}" 230 done 231 ports_recurse $d $l 232 ) 233 echo "$d" >> /tmp/_.plist 234 fi 235 done 236 if [ "x$t" = "x." ] ; then 237 echo '}' >> /tmp/_.plist.dot 238 fi 239) 240 241ports_build() ( 242 243 ports_recurse . $PORTS_WE_WANT 244 245 if [ "x${PKG_DIR}" != "x" ] ; then 246 mkdir -p ${PKG_DIR} 247 fi 248 249 pd=`cd /usr/ports && /bin/pwd` 250 # Now build & install them 251 for p in `cat /tmp/_.plist` 252 do 253 b=`echo $p | tr / _` 254 t=`echo $p | sed "s,${pd},,"` 255 pn=`cd $p && ports_make package-name` 256 257 if [ "x`basename $p`" == "xpkg" ] ; then 258 log_it "Very Special: $t ($pn)" 259 260 ( 261 cd $p 262 ports_make clean all install 263 ) > _.$b 2>&1 < /dev/null 264 continue 265 fi 266 267 if pkg info $pn > /dev/null 2>&1 ; then 268 log_it "Already installed: $t ($pn)" 269 continue 270 fi 271 272 if [ "x${PKG_DIR}" != "x" -a -f ${PKG_DIR}/$pn.txz ] ; then 273 if [ "x$use_pkg" = "x-p" ] ; then 274 log_it "Install $t ($pn)" 275 ( 276 set +e 277 pkg add ${PKG_DIR}/$pn.txz || true 278 ) > _.$b 2>&1 < /dev/null 279 continue 280 fi 281 fi 282 283 miss=`(cd $p ; ports_make missing) || true` 284 285 if [ "x${miss}" != "x" ] ; then 286 log_it "NB: MISSING for $p:" $miss 287 fi 288 289 log_it "build $pn ($p)" 290 ( 291 set +e 292 cd $p 293 ports_make clean 294 if ports_make install ; then 295 if [ "x${PKG_DIR}" != "x" ] ; then 296 ports_make package 297 fi 298 else 299 log_it FAIL build $p 300 fi 301 ports_make clean 302 303 ) > _.$b 2>&1 < /dev/null 304 done 305) 306 307ports_prefetch() ( 308 ( 309 set +x 310 ldir=$1 311 true > /${ldir}/_.prefetch 312 echo "Building /tmp/_.plist" >> /${ldir}/_.prefetch 313 314 ports_recurse . $PORTS_WE_WANT 315 316 echo "Completed /tmp/_.plist" >> /${ldir}/_.prefetch 317 # Now checksump/fetch them 318 for p in `cat /tmp/_.plist` 319 do 320 b=`echo $p | tr / _` 321 ( 322 cd $p 323 if ports_make checksum ; then 324 rm -f /${ldir}/_.prefetch.$b 325 echo "OK $p" >> /${ldir}/_.prefetch 326 exit 0 327 fi 328 ports_make distclean 329 ports_make checksum || true 330 331 if ports_make checksum > /dev/null 2>&1 ; then 332 rm -f /${ldir}/_.prefetch.$b 333 echo "OK $p" >> /${ldir}/_.prefetch 334 else 335 echo "BAD $p" >> /${ldir}/_.prefetch 336 fi 337 ) > /${ldir}/_.prefetch.$b 2>&1 338 done 339 echo "Done" >> /${ldir}/_.prefetch 340 ) 341) 342 343####################################################################### 344 345do_world=true 346do_kernel=true 347do_prefetch=false 348use_pkg="" 349c_arg="" 350 351set +e 352args=`getopt bc:hkpP:w $*` 353if [ $? -ne 0 ] ; then 354 usage 355fi 356set -e 357 358set -- $args 359for i 360do 361 case "$i" 362 in 363 -b) 364 shift; 365 do_world=false 366 do_kernel=false 367 ;; 368 -c) 369 c_arg=$2 370 if [ ! -f "$c_arg" ] ; then 371 echo "Cannot read $c_arg" 1>&2 372 usage 373 fi 374 . "$2" 375 shift 376 shift 377 ;; 378 -h) 379 usage 380 ;; 381 -k) 382 shift; 383 do_kernel=false 384 ;; 385 -p) 386 shift; 387 use_pkg="-p" 388 ;; 389 -P) 390 shift; 391 do_prefetch=true 392 distfile_cache=$1 393 shift; 394 ;; 395 -w) 396 shift; 397 do_world=false 398 ;; 399 --) 400 shift 401 break; 402 ;; 403 esac 404done 405 406####################################################################### 407 408if [ "x$1" = "xchroot_script" ] ; then 409 set -e 410 411 shift 412 413 before_ports_chroot 414 415 ports_build 416 417 exit 0 418fi 419 420if [ "x$1" = "xfinal_chroot" ] ; then 421 final_chroot 422 exit 0 423fi 424 425if [ $# -gt 0 ] ; then 426 echo "$0: Extraneous arguments supplied" 427 usage 428fi 429 430####################################################################### 431 432T0=`date +%s` 433echo $T0 $T0 > /tmp/_sb_log 434 435[ ! -d ${SBMNT} ] && mkdir -p ${SBMNT} 436 437if $do_prefetch ; then 438 rm -rf /tmp/sysbuild/ports 439 mkdir -p /tmp/sysbuild/ports 440 ln -s ${distfile_cache} /tmp/sysbuild/ports/distfiles 441 export PORTS_OPTS=CD_MOUNTPTS=/tmp/sysbuild 442 ports_prefetch /tmp 443 exit 0 444fi 445 446log_it Unmount everything 447( 448 ( cleanup ) 449 umount /freebsd/distfiles || true 450 umount ${SBMNT}/freebsd/distfiles || true 451 umount ${FREEBSD_PART} || true 452 umount ${SBMNT}/freebsd || true 453 umount ${SBMNT}/dev || true 454 umount ${SBMNT} || true 455 umount /dev/${TARGET_PART} || true 456) # > /dev/null 2>&1 457 458log_it Prepare running image 459mkdir -p /freebsd 460mount ${FREEBSD_PART} /freebsd 461 462####################################################################### 463 464if [ ! -d /freebsd/${PORTS_PATH} ] ; then 465 echo PORTS_PATH does not exist 1>&2 466 exit 1 467fi 468 469if [ ! -d /freebsd/${SRC_PATH} ] ; then 470 echo SRC_PATH does not exist 1>&2 471 exit 1 472fi 473 474log_it TARGET_PART $TARGET_PART 475sleep 5 476 477rm -rf /usr/ports 478ln -s /freebsd/${PORTS_PATH} /usr/ports 479 480rm -rf /usr/src 481ln -s /freebsd/${SRC_PATH} /usr/src 482 483if $do_world ; then 484 if [ "x${OBJ_PATH}" != "x" ] ; then 485 rm -rf /usr/obj 486 (cd /freebsd && mkdir -p ${OBJ_PATH} && ln -s ${OBJ_PATH} /usr/obj) 487 else 488 rm -rf /usr/obj 489 mkdir -p /usr/obj 490 fi 491fi 492 493####################################################################### 494 495for i in ${PORTS_WE_WANT} 496do 497 ( 498 cd /usr/ports 499 if [ ! -d $i ] ; then 500 echo "Port $i not found" 1>&2 501 exit 2 502 fi 503 ) 504done 505 506#export PORTS_WE_WANT 507#export PORTS_OPTS 508 509####################################################################### 510 511log_it Prepare destination partition 512newfs -t -E -O2 -U /dev/${TARGET_PART} > /dev/null 513mount /dev/${TARGET_PART} ${SBMNT} 514mkdir -p ${SBMNT}/dev 515mount -t devfs devfs ${SBMNT}/dev 516 517if [ "x${REMOTEDISTFILES}" != "x" ] ; then 518 rm -rf /freebsd/${PORTS_PATH}/distfiles 519 ln -s /freebsd/distfiles /freebsd/${PORTS_PATH}/distfiles 520 mkdir -p /freebsd/distfiles 521 mount ${REMOTEDISTFILES} /freebsd/distfiles 522fi 523 524log_it copy ports config files 525(cd / ; find var/db/ports -print | cpio -dumpv ${SBMNT} > /dev/null 2>&1) 526 527log_it "Start prefetch of ports distfiles" 528ports_prefetch ${SBMNT} & 529 530if $do_world ; then 531 ( 532 cd /usr/src 533 log_it "Buildworld" 534 make ${JARG} -s buildworld ${SRCCONF} > ${SBMNT}/_.bw 2>&1 535 ) 536fi 537 538if $do_kernel ; then 539 ( 540 cd /usr/src 541 log_it "Buildkernel" 542 make ${JARG} -s buildkernel KERNCONF=$KERNCONF > ${SBMNT}/_.bk 2>&1 543 ) 544fi 545 546 547log_it Installworld 548(cd /usr/src && make ${JARG} installworld DESTDIR=${SBMNT} ${SRCCONF} ) \ 549 > ${SBMNT}/_.iw 2>&1 550 551log_it distribution 552(cd /usr/src && make -m /usr/src/share/mk distribution DESTDIR=${SBMNT} ${SRCCONF} ) \ 553 > ${SBMNT}/_.dist 2>&1 554 555log_it Installkernel 556(cd /usr/src && make ${JARG} installkernel DESTDIR=${SBMNT} KERNCONF=$KERNCONF ) \ 557 > ${SBMNT}/_.ik 2>&1 558 559if [ "x${OBJ_PATH}" != "x" ] ; then 560 rmdir ${SBMNT}/usr/obj 561 ( cd /freebsd && mkdir -p ${OBJ_PATH} && ln -s ${OBJ_PATH} ${SBMNT}/usr/obj ) 562fi 563 564log_it Wait for ports prefetch 565log_it "(Tail ${SBMNT}/_.prefetch for progress)" 566wait 567 568log_it Move filesystems 569 570if [ "x${REMOTEDISTFILES}" != "x" ] ; then 571 umount /freebsd/distfiles 572fi 573umount ${FREEBSD_PART} || true 574mkdir -p ${SBMNT}/freebsd 575mount ${FREEBSD_PART} ${SBMNT}/freebsd 576if [ "x${REMOTEDISTFILES}" != "x" ] ; then 577 mount ${REMOTEDISTFILES} ${SBMNT}/freebsd/distfiles 578fi 579 580rm -rf ${SBMNT}/usr/ports || true 581ln -s /freebsd/${PORTS_PATH} ${SBMNT}/usr/ports 582 583rm -rf ${SBMNT}/usr/src || true 584ln -s /freebsd/${SRC_PATH} ${SBMNT}/usr/src 585 586log_it Build and install ports 587 588# Make sure fetching will work in the chroot 589if [ -f /etc/resolv.conf ] ; then 590 log_it copy resolv.conf 591 cp /etc/resolv.conf ${SBMNT}/etc 592 chflags schg ${SBMNT}/etc/resolv.conf 593fi 594 595if [ -f /etc/localtime ] ; then 596 log_it copy localtime 597 cp /etc/localtime ${SBMNT}/etc 598fi 599 600log_it ldconfig in chroot 601chroot ${SBMNT} sh /etc/rc.d/ldconfig start 602 603log_it before_ports 604( 605 before_ports 606) 607 608log_it fixing fstab 609sed "/[ ]\/[ ]/s;^[^ ]*[ ];/dev/${TARGET_PART} ;" \ 610 /etc/fstab > ${SBMNT}/etc/fstab 611 612log_it build ports 613 614cp $0 ${SBMNT}/root 615cp /tmp/_sb_log ${SBMNT}/tmp 616b=`basename $0` 617if [ "x$c_arg" != "x" ] ; then 618 cp $c_arg ${SBMNT}/root 619 chroot ${SBMNT} sh /root/$0 -c /root/`basename $c_arg` $use_pkg chroot_script 620else 621 chroot ${SBMNT} sh /root/$0 $use_pkg chroot_script 622fi 623cp ${SBMNT}/tmp/_sb_log /tmp 624 625log_it create all mountpoints 626grep -v '^[ ]*#' ${SBMNT}/etc/fstab | 627while read a b c 628do 629 mkdir -p ${SBMNT}/$b 630done 631 632if [ "x$SERCONS" != "xfalse" ] ; then 633 log_it serial console 634 echo " -h" > ${SBMNT}/boot.config 635 sed -i "" -e /ttyd0/s/off/on/ ${SBMNT}/etc/ttys 636 sed -i "" -e /ttyu0/s/off/on/ ${SBMNT}/etc/ttys 637 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${SBMNT}/etc/ttys 638fi 639 640log_it move dist config files "(expect warnings)" 641( 642 cd ${SBMNT} 643 mkdir root/configfiles_dist 644 find ${CONFIGFILES} -print | cpio -dumpv root/configfiles_dist 645) 646 647log_it copy live config files 648(cd / && find ${CONFIGFILES} -print | cpio -dumpv ${SBMNT}) 649 650log_it final_root 651( final_root ) 652log_it final_chroot 653cp /tmp/_sb_log ${SBMNT}/tmp 654if [ "x$c_arg" != "x" ] ; then 655 chroot ${SBMNT} sh /root/$0 -c /root/`basename $c_arg` final_chroot 656else 657 chroot ${SBMNT} sh /root/$0 final_chroot 658fi 659cp ${SBMNT}/tmp/_sb_log /tmp 660log_it "Check these messages (if any):" 661grep '^Stop' ${SBMNT}/_* || true 662log_it DONE 663echo "Now you probably want to:" 664echo " $GPART_SUGGESTION" 665echo " shutdown -r now" 666