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"
101 SBMNT="/mnt.sysbuild"
109 before_ports_chroot() (
118 #######################################################################
119 # -P is a pretty neat way to clean junk out from your ports dist-files:
121 # mkdir /freebsd/ports/distfiles.old
122 # mv /freebsd/ports/distfiles/* /freebsd/ports/distfiles.old
123 # sh sysbuild.sh -c $yourconfig -P /freebsd/ports/distfiles.old
124 # rm -rf /freebsd/ports/distfiles.old
126 # Unfortunately bsd.ports.mk does not attempt to use a hard-link so
127 # while this runs you need diskspace for both your old and your "new"
130 #######################################################################
134 echo "Usage: $0 [-b/-k/-w] [-c config_file]"
135 echo " -b suppress builds (both kernel and world)"
136 echo " -k suppress buildkernel"
137 echo " -w suppress buildworld"
138 echo " -p used cached packages"
139 echo " -P <dir> prefetch ports"
140 echo " -c specify config file"
145 #######################################################################
146 #######################################################################
148 if [ ! -f $0 ] ; then
149 echo "Must be able to access self ($0)" 1>&2
153 if grep -q 'Magic String: 0`0nQT40W%l,CX&' $0 ; then
156 echo "self ($0) does not contain magic string" 1>&2
160 #######################################################################
166 set `cat /tmp/_sb_log`
168 echo "$1 $TX" > /tmp/_sb_log
169 DT=`expr $TX - $1 || true`
170 DL=`expr $TX - $2 || true`
171 echo -n "### `date +%H:%M:%S`"
172 printf " ### %5d ### %5d ### %s\n" $DT $DL "$a"
175 #######################################################################
182 if [ "x$t" = "x." ] ; then
184 true > /tmp/_.plist.tdone
185 echo 'digraph {' > /tmp/_.plist.dot
187 if grep -q "^$t\$" /tmp/_.plist.tdone ; then
190 echo "$t" >> /tmp/_.plist.tdone
193 if [ ! -d $d ] ; then
194 echo "Missing port $d" 1>&2
197 d=`cd /usr/ports && cd $d && /bin/pwd`
198 if [ ! -f $d/Makefile ] ; then
199 echo "Missing port $d" 1>&2
202 if [ "x$t" != "x." ] ; then
203 echo "\"$t\" -> \"$d\"" >> /tmp/_.plist.dot
205 if grep -q "^$d\$" /tmp/_.plist ; then
207 elif grep -q "^$d\$" /tmp/_.plist.tdone ; then
213 for a in `make -V _UNIFIED_DEPENDS ${PORTS_OPTS}`
215 x=`expr "$a" : '.*:\(.*\)'`
219 # -> _UNIFIED_DEPENDS
220 #ports_recurse $d `make -V _DEPEND_DIRS ${PORTS_OPTS}`
221 #ports_recurse $d `make all-depends-list`
223 echo "$d" >> /tmp/_.plist
226 if [ "x$t" = "x." ] ; then
227 echo '}' >> /tmp/_.plist.dot
233 ports_recurse . $PORTS_WE_WANT
235 if [ "x${PKG_DIR}" != "x" ] ; then
239 pd=`cd /usr/ports && /bin/pwd`
240 # Now build & install them
241 for p in `cat /tmp/_.plist`
244 t=`echo $p | sed "s,${pd},,"`
245 pn=`cd $p && make package-name ${PORTS_OPTS}`
247 if [ "x`basename $p`" == "xpkg" ] ; then
248 log_it "Very Special: $t ($pn)"
252 make clean all install ${PORTS_OPTS}
253 ) > _.$b 2>&1 < /dev/null
257 if pkg info $pn > /dev/null 2>&1 ; then
258 log_it "Already installed: $t ($pn)"
262 if [ "x${PKG_DIR}" != "x" -a -f ${PKG_DIR}/$pn.txz ] ; then
263 if [ "x$use_pkg" = "x-p" ] ; then
264 log_it "Install $t ($pn)"
267 pkg add ${PKG_DIR}/$pn.txz || true
268 ) > _.$b 2>&1 < /dev/null
273 miss=`(cd $p ; make missing ${PORTS_OPTS}) || true`
275 if [ "x${miss}" != "x" ] ; then
276 log_it "NB: MISSING for $p:" $miss
279 log_it "build $pn ($p)"
283 make clean ${PORTS_OPTS}
284 if make install ${PORTS_OPTS} ; then
285 if [ "x${PKG_DIR}" != "x" ] ; then
286 make package ${PORTS_OPTS}
291 make clean ${PORTS_OPTS}
292 ) > _.$b 2>&1 < /dev/null
300 true > /${ldir}/_.prefetch
301 echo "Building /tmp/_.plist" >> /${ldir}/_.prefetch
303 ports_recurse . $PORTS_WE_WANT
305 echo "Completed /tmp/_.plist" >> /${ldir}/_.prefetch
306 # Now checksump/fetch them
307 for p in `cat /tmp/_.plist`
312 if make checksum $PORTS_OPTS ; then
313 rm -f /${ldir}/_.prefetch.$b
314 echo "OK $p" >> /${ldir}/_.prefetch
318 make checksum $PORTS_OPTS || true
320 if make checksum $PORTS_OPTS > /dev/null 2>&1 ; then
321 rm -f /${ldir}/_.prefetch.$b
322 echo "OK $p" >> /${ldir}/_.prefetch
324 echo "BAD $p" >> /${ldir}/_.prefetch
326 ) > /${ldir}/_.prefetch.$b 2>&1
328 echo "Done" >> /${ldir}/_.prefetch
332 #######################################################################
341 args=`getopt bc:hkpP:w $*`
342 if [ $? -ne 0 ] ; then
359 if [ ! -f "$c_arg" ] ; then
360 echo "Cannot read $c_arg" 1>&2
395 #######################################################################
397 if [ "x$1" = "xchroot_script" ] ; then
409 if [ "x$1" = "xfinal_chroot" ] ; then
414 if [ $# -gt 0 ] ; then
415 echo "$0: Extraneous arguments supplied"
419 #######################################################################
422 echo $T0 $T0 > /tmp/_sb_log
424 [ ! -d ${SBMNT} ] && mkdir -p ${SBMNT}
426 if $do_prefetch ; then
427 rm -rf /tmp/sysbuild/ports
428 mkdir -p /tmp/sysbuild/ports
429 ln -s ${distfile_cache} /tmp/sysbuild/ports/distfiles
430 export PORTS_OPTS=CD_MOUNTPTS=/tmp/sysbuild
435 log_it Unmount everything
438 umount /freebsd/distfiles || true
439 umount ${SBMNT}/freebsd/distfiles || true
440 umount ${FREEBSD_PART} || true
441 umount ${SBMNT}/freebsd || true
442 umount ${SBMNT}/dev || true
443 umount ${SBMNT} || true
444 umount /dev/${TARGET_PART} || true
447 log_it Prepare running image
449 mount ${FREEBSD_PART} /freebsd
451 #######################################################################
453 if [ ! -d /freebsd/${PORTS_PATH} ] ; then
454 echo PORTS_PATH does not exist 1>&2
458 if [ ! -d /freebsd/${SRC_PATH} ] ; then
459 echo SRC_PATH does not exist 1>&2
463 log_it TARGET_PART $TARGET_PART
467 ln -s /freebsd/${PORTS_PATH} /usr/ports
470 ln -s /freebsd/${SRC_PATH} /usr/src
473 if [ "x${OBJ_PATH}" != "x" ] ; then
475 mkdir -p /freebsd/${OBJ_PATH}
476 ln -s /freebsd/${OBJ_PATH} /usr/obj
483 #######################################################################
485 for i in ${PORTS_WE_WANT}
489 if [ ! -d $i ] ; then
490 echo "Port $i not found" 1>&2
499 #######################################################################
501 log_it Prepare destination partition
502 newfs -t -E -O2 -U /dev/${TARGET_PART} > /dev/null
503 mount /dev/${TARGET_PART} ${SBMNT}
504 mkdir -p ${SBMNT}/dev
505 mount -t devfs devfs ${SBMNT}/dev
507 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
508 rm -rf /freebsd/${PORTS_PATH}/distfiles
509 ln -s /freebsd/distfiles /freebsd/${PORTS_PATH}/distfiles
510 mkdir -p /freebsd/distfiles
511 mount ${REMOTEDISTFILES} /freebsd/distfiles
514 log_it copy ports config files
515 (cd / ; find var/db/ports -print | cpio -dumpv ${SBMNT} > /dev/null 2>&1)
517 log_it "Start prefetch of ports distfiles"
518 ports_prefetch ${SBMNT} &
524 make ${JARG} -s buildworld ${SRCCONF} > ${SBMNT}/_.bw 2>&1
532 make ${JARG} -s buildkernel KERNCONF=$KERNCONF > ${SBMNT}/_.bk 2>&1
538 (cd /usr/src && make ${JARG} installworld DESTDIR=${SBMNT} ${SRCCONF} ) \
542 (cd /usr/src/etc && make -m /usr/src/share/mk distribution DESTDIR=${SBMNT} ${SRCCONF} ) \
543 > ${SBMNT}/_.dist 2>&1
546 (cd /usr/src && make ${JARG} installkernel DESTDIR=${SBMNT} KERNCONF=$KERNCONF ) \
549 if [ "x${OBJ_PATH}" != "x" ] ; then
550 rmdir ${SBMNT}/usr/obj
551 ln -s /freebsd/${OBJ_PATH} ${SBMNT}/usr/obj
554 log_it Wait for ports prefetch
555 log_it "(Tail ${SBMNT}/_.prefetch for progress)"
558 log_it Move filesystems
560 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
561 umount /freebsd/distfiles
563 umount ${FREEBSD_PART} || true
564 mkdir -p ${SBMNT}/freebsd
565 mount ${FREEBSD_PART} ${SBMNT}/freebsd
566 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
567 mount ${REMOTEDISTFILES} ${SBMNT}/freebsd/distfiles
570 rm -rf ${SBMNT}/usr/ports || true
571 ln -s /freebsd/${PORTS_PATH} ${SBMNT}/usr/ports
573 rm -rf ${SBMNT}/usr/src || true
574 ln -s /freebsd/${SRC_PATH} ${SBMNT}/usr/src
576 log_it Build and install ports
578 # Make sure fetching will work in the chroot
579 if [ -f /etc/resolv.conf ] ; then
580 log_it copy resolv.conf
581 cp /etc/resolv.conf ${SBMNT}/etc
582 chflags schg ${SBMNT}/etc/resolv.conf
585 if [ -f /etc/localtime ] ; then
586 log_it copy localtime
587 cp /etc/localtime ${SBMNT}/etc
590 log_it ldconfig in chroot
591 chroot ${SBMNT} sh /etc/rc.d/ldconfig start
599 sed "/[ ]\/[ ]/s;^[^ ]*[ ];/dev/${TARGET_PART} ;" \
600 /etc/fstab > ${SBMNT}/etc/fstab
605 cp /tmp/_sb_log ${SBMNT}/tmp
607 if [ "x$c_arg" != "x" ] ; then
608 cp $c_arg ${SBMNT}/root
609 chroot ${SBMNT} sh /root/$0 -c /root/`basename $c_arg` $use_pkg chroot_script
611 chroot ${SBMNT} sh /root/$0 $use_pkg chroot_script
613 cp ${SBMNT}/tmp/_sb_log /tmp
615 log_it create all mountpoints
616 grep -v '^[ ]*#' ${SBMNT}/etc/fstab |
622 if [ "x$SERCONS" != "xfalse" ] ; then
623 log_it serial console
624 echo " -h" > ${SBMNT}/boot.config
625 sed -i "" -e /ttyd0/s/off/on/ ${SBMNT}/etc/ttys
626 sed -i "" -e /ttyu0/s/off/on/ ${SBMNT}/etc/ttys
627 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' ${SBMNT}/etc/ttys
630 log_it move dist config files "(expect warnings)"
633 mkdir root/configfiles_dist
634 find ${CONFIGFILES} -print | cpio -dumpv root/configfiles_dist
637 log_it copy live config files
638 (cd / && find ${CONFIGFILES} -print | cpio -dumpv ${SBMNT})
643 cp /tmp/_sb_log ${SBMNT}/tmp
644 if [ "x$c_arg" != "x" ] ; then
645 chroot ${SBMNT} sh /root/$0 -c /root/`basename $c_arg` final_chroot
647 chroot ${SBMNT} sh /root/$0 final_chroot
649 cp ${SBMNT}/tmp/_sb_log /tmp
650 log_it "Check these messages (if any):"
651 grep '^Stop' ${SBMNT}/_* || true
653 echo "Now you probably want to:"
654 echo " $GPART_SUGGESTION"
655 echo " shutdown -r now"