3 # Copyright (c) 1994-2009 Poul-Henning Kamp.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
34 if [ `uname -m` = "i386" -o `uname -m` = "amd64" ] ; then
35 TARGET_PART=`df / | sed '
44 FREEBSD_PART=`sed -n \
46 -e '/[ ]\/freebsd[ ]/!d' \
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
58 GPART_SUGGESTION=unknown
62 # Relative to /freebsd
71 #SRCCONF="SRCCONF=/usr/src/src.conf"
75 ncpu=`sysctl -n kern.smp.cpus`
76 if [ $ncpu -gt 1 ] ; then
83 PKG_DIR=/usr/ports/packages/All
85 # Remotely mounted distfiles
86 # REMOTEDISTFILES=fs:/rdonly/distfiles
89 #FTP_PROXY=http://127.0.0.1:3128/
90 #HTTP_PROXY=http://127.0.0.1:3128/
91 #export FTP_PROXY HTTP_PROXY
96 PORTS_OPTS="BATCH=YES A4=yes"
104 SBMNT="/mnt.sysbuild"
112 before_ports_chroot() (
121 #######################################################################
122 # -P is a pretty neat way to clean junk out from your ports dist-files:
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
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"
133 #######################################################################
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"
148 #######################################################################
149 #######################################################################
151 if [ ! -f $0 ] ; then
152 echo "Must be able to access self ($0)" 1>&2
156 if grep -q 'Magic String: 0`0nQT40W%l,CX&' $0 ; then
159 echo "self ($0) does not contain magic string" 1>&2
163 #######################################################################
169 set `cat /tmp/_sb_log`
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"
178 #######################################################################
181 make $* WITH="${PORTS_WITH}" WITHOUT="${PORTS_WITHOUT}" ${PORTS_OPTS}
188 if [ "x$t" = "x." ] ; then
190 true > /tmp/_.plist.tdone
191 echo 'digraph {' > /tmp/_.plist.dot
193 if grep -q "^$t\$" /tmp/_.plist.tdone ; then
196 echo "$t" >> /tmp/_.plist.tdone
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
207 # echo "Flavored port $d ($t) (fl $fl) (bd $bd)" 1>&2
210 d=`cd /usr/ports && cd $d && /bin/pwd`
211 if [ ! -f $d/Makefile ] ; then
212 echo "Missing port (Makefile) $d" 1>&2
215 if [ "x$t" != "x." ] ; then
216 echo "\"$t\" -> \"$d\"" >> /tmp/_.plist.dot
218 if grep -q "^$d\$" /tmp/_.plist ; then
220 elif grep -q "^$d\$" /tmp/_.plist.tdone ; then
226 for a in `ports_make -V _UNIFIED_DEPENDS $fl`
228 x=`expr "$a" : '.*:\(.*\)'`
233 echo "$d" >> /tmp/_.plist
236 if [ "x$t" = "x." ] ; then
237 echo '}' >> /tmp/_.plist.dot
243 ports_recurse . $PORTS_WE_WANT
245 if [ "x${PKG_DIR}" != "x" ] ; then
249 pd=`cd /usr/ports && /bin/pwd`
250 # Now build & install them
251 for p in `cat /tmp/_.plist`
254 t=`echo $p | sed "s,${pd},,"`
255 pn=`cd $p && ports_make package-name`
257 if [ "x`basename $p`" == "xpkg" ] ; then
258 log_it "Very Special: $t ($pn)"
262 ports_make clean all install
263 ) > _.$b 2>&1 < /dev/null
267 if pkg info $pn > /dev/null 2>&1 ; then
268 log_it "Already installed: $t ($pn)"
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)"
277 pkg add ${PKG_DIR}/$pn.txz || true
278 ) > _.$b 2>&1 < /dev/null
283 miss=`(cd $p ; ports_make missing) || true`
285 if [ "x${miss}" != "x" ] ; then
286 log_it "NB: MISSING for $p:" $miss
289 log_it "build $pn ($p)"
294 if ports_make install ; then
295 if [ "x${PKG_DIR}" != "x" ] ; then
303 ) > _.$b 2>&1 < /dev/null
311 true > /${ldir}/_.prefetch
312 echo "Building /tmp/_.plist" >> /${ldir}/_.prefetch
314 ports_recurse . $PORTS_WE_WANT
316 echo "Completed /tmp/_.plist" >> /${ldir}/_.prefetch
317 # Now checksump/fetch them
318 for p in `cat /tmp/_.plist`
323 if ports_make checksum ; then
324 rm -f /${ldir}/_.prefetch.$b
325 echo "OK $p" >> /${ldir}/_.prefetch
329 ports_make checksum || true
331 if ports_make checksum > /dev/null 2>&1 ; then
332 rm -f /${ldir}/_.prefetch.$b
333 echo "OK $p" >> /${ldir}/_.prefetch
335 echo "BAD $p" >> /${ldir}/_.prefetch
337 ) > /${ldir}/_.prefetch.$b 2>&1
339 echo "Done" >> /${ldir}/_.prefetch
343 #######################################################################
352 args=`getopt bc:hkpP:w $*`
353 if [ $? -ne 0 ] ; then
370 if [ ! -f "$c_arg" ] ; then
371 echo "Cannot read $c_arg" 1>&2
406 #######################################################################
408 if [ "x$1" = "xchroot_script" ] ; then
420 if [ "x$1" = "xfinal_chroot" ] ; then
425 if [ $# -gt 0 ] ; then
426 echo "$0: Extraneous arguments supplied"
430 #######################################################################
433 echo $T0 $T0 > /tmp/_sb_log
435 [ ! -d ${SBMNT} ] && mkdir -p ${SBMNT}
437 if $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
446 log_it Unmount everything
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
458 log_it Prepare running image
460 mount ${FREEBSD_PART} /freebsd
462 #######################################################################
464 if [ ! -d /freebsd/${PORTS_PATH} ] ; then
465 echo PORTS_PATH does not exist 1>&2
469 if [ ! -d /freebsd/${SRC_PATH} ] ; then
470 echo SRC_PATH does not exist 1>&2
474 log_it TARGET_PART $TARGET_PART
478 ln -s /freebsd/${PORTS_PATH} /usr/ports
481 ln -s /freebsd/${SRC_PATH} /usr/src
484 if [ "x${OBJ_PATH}" != "x" ] ; then
486 (cd /freebsd && mkdir -p ${OBJ_PATH} && ln -s ${OBJ_PATH} /usr/obj)
493 #######################################################################
495 for i in ${PORTS_WE_WANT}
499 if [ ! -d $i ] ; then
500 echo "Port $i not found" 1>&2
506 #export PORTS_WE_WANT
509 #######################################################################
511 log_it Prepare destination partition
512 newfs -t -E -O2 -U /dev/${TARGET_PART} > /dev/null
513 mount /dev/${TARGET_PART} ${SBMNT}
514 mkdir -p ${SBMNT}/dev
515 mount -t devfs devfs ${SBMNT}/dev
517 if [ "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
524 log_it copy ports config files
525 (cd / ; find var/db/ports -print | cpio -dumpv ${SBMNT} > /dev/null 2>&1)
527 log_it "Start prefetch of ports distfiles"
528 ports_prefetch ${SBMNT} &
534 make ${JARG} -s buildworld ${SRCCONF} > ${SBMNT}/_.bw 2>&1
542 make ${JARG} -s buildkernel KERNCONF=$KERNCONF > ${SBMNT}/_.bk 2>&1
548 (cd /usr/src && make ${JARG} installworld DESTDIR=${SBMNT} ${SRCCONF} ) \
552 (cd /usr/src && make -m /usr/src/share/mk distribution DESTDIR=${SBMNT} ${SRCCONF} ) \
553 > ${SBMNT}/_.dist 2>&1
556 (cd /usr/src && make ${JARG} installkernel DESTDIR=${SBMNT} KERNCONF=$KERNCONF ) \
559 if [ "x${OBJ_PATH}" != "x" ] ; then
560 rmdir ${SBMNT}/usr/obj
561 ( cd /freebsd && mkdir -p ${OBJ_PATH} && ln -s ${OBJ_PATH} ${SBMNT}/usr/obj )
564 log_it Wait for ports prefetch
565 log_it "(Tail ${SBMNT}/_.prefetch for progress)"
568 log_it Move filesystems
570 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
571 umount /freebsd/distfiles
573 umount ${FREEBSD_PART} || true
574 mkdir -p ${SBMNT}/freebsd
575 mount ${FREEBSD_PART} ${SBMNT}/freebsd
576 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
577 mount ${REMOTEDISTFILES} ${SBMNT}/freebsd/distfiles
580 rm -rf ${SBMNT}/usr/ports || true
581 ln -s /freebsd/${PORTS_PATH} ${SBMNT}/usr/ports
583 rm -rf ${SBMNT}/usr/src || true
584 ln -s /freebsd/${SRC_PATH} ${SBMNT}/usr/src
586 log_it Build and install ports
588 # Make sure fetching will work in the chroot
589 if [ -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
595 if [ -f /etc/localtime ] ; then
596 log_it copy localtime
597 cp /etc/localtime ${SBMNT}/etc
600 log_it ldconfig in chroot
601 chroot ${SBMNT} sh /etc/rc.d/ldconfig start
609 sed "/[ ]\/[ ]/s;^[^ ]*[ ];/dev/${TARGET_PART} ;" \
610 /etc/fstab > ${SBMNT}/etc/fstab
615 cp /tmp/_sb_log ${SBMNT}/tmp
617 if [ "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
621 chroot ${SBMNT} sh /root/$0 $use_pkg chroot_script
623 cp ${SBMNT}/tmp/_sb_log /tmp
625 log_it create all mountpoints
626 grep -v '^[ ]*#' ${SBMNT}/etc/fstab |
632 if [ "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
640 log_it move dist config files "(expect warnings)"
643 mkdir root/configfiles_dist
644 find ${CONFIGFILES} -print | cpio -dumpv root/configfiles_dist
647 log_it copy live config files
648 (cd / && find ${CONFIGFILES} -print | cpio -dumpv ${SBMNT})
653 cp /tmp/_sb_log ${SBMNT}/tmp
654 if [ "x$c_arg" != "x" ] ; then
655 chroot ${SBMNT} sh /root/$0 -c /root/`basename $c_arg` final_chroot
657 chroot ${SBMNT} sh /root/$0 final_chroot
659 cp ${SBMNT}/tmp/_sb_log /tmp
660 log_it "Check these messages (if any):"
661 grep '^Stop' ${SBMNT}/_* || true
663 echo "Now you probably want to:"
664 echo " $GPART_SUGGESTION"
665 echo " shutdown -r now"