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" ] ; then
35 TARGET_PART=`df / | sed '
44 # Where our build-bits are to be found
45 FREEBSD_PART=`echo $TARGET_PART | sed 's/s[12]a/s3/'`
46 elif [ `uname -m` = "amd64" ] ; then
47 TARGET_PART=`df / | sed '
56 # Where our build-bits are to be found
57 FREEBSD_PART=`echo $TARGET_PART | sed 's/s[12]a/s3/'`
63 # Relative to /freebsd
72 #SRCCONF="SRCCONF=/usr/src/src.conf"
76 ncpu=`sysctl -n kern.smp.cpus`
77 if [ $ncpu -gt 1 ] ; then
84 # Remotely mounted distfiles
85 # REMOTEDISTFILES=fs:/rdonly/distfiles
88 #FTP_PROXY=http://127.0.0.1:3128/
89 #HTTP_PROXY=http://127.0.0.1:3128/
90 #export FTP_PROXY HTTP_PROXY
95 PORTS_OPTS="BATCH=YES MAKE_IDEA=YES A4=yes"
106 before_ports_chroot() (
115 #######################################################################
116 #######################################################################
120 echo "Usage: $0 [-b/-k/-w] [-c config_file]"
121 echo " -b suppress builds (both kernel and world)"
122 echo " -k suppress buildkernel"
123 echo " -w suppress buildworld"
124 echo " -p used cached packages"
125 echo " -c specify config file"
130 #######################################################################
131 #######################################################################
133 if [ ! -f $0 ] ; then
134 echo "Must be able to access self ($0)" 1>&2
138 if grep -q 'Magic String: 0`0nQT40W%l,CX&' $0 ; then
141 echo "self ($0) does not contain magic string" 1>&2
145 #######################################################################
152 set `cat /tmp/_sb_log`
154 echo "$1 $TX" > /tmp/_sb_log
155 DT=`expr $TX - $1 || true`
156 DL=`expr $TX - $2 || true`
157 echo -n "### `date +%H:%M:%S`"
158 printf " ### %5d ### %5d ### %s\n" $DT $DL "$a"
161 #######################################################################
168 if [ "x$t" = "x." ] ; then
170 true > /tmp/_.plist.tdone
171 echo 'digraph {' > /tmp/_.plist.dot
173 if grep -q "^$t\$" /tmp/_.plist.tdone ; then
176 echo "$t" >> /tmp/_.plist.tdone
179 if [ ! -d $d ] ; then
180 echo "Missing port $d" 1>&2
183 if [ ! -f $d/Makefile ] ; then
184 echo "Missing port $d" 1>&2
187 if [ "x$t" != "x." ] ; then
188 echo "\"$t\" -> \"$d\"" >> /tmp/_.plist.dot
190 if grep -q "^$d\$" /tmp/_.plist ; then
192 elif grep -q "^$d\$" /tmp/_.plist.tdone ; then
197 ports_recurse $d `make -V _DEPEND_DIRS ${PORTS_OPTS}`
199 echo "$d" >> /tmp/_.plist
202 if [ "x$t" = "x." ] ; then
203 echo '}' >> /tmp/_.plist.dot
210 ports_recurse . $PORTS_WE_WANT
212 # Now build & install them
213 for p in `cat /tmp/_.plist`
215 t=`echo $p | sed 's,/usr/ports/,,'`
216 pn=`cd $p && make package-name`
217 if [ "x${PKG_DIR}" != "x" -a -f ${PKG_DIR}/$pn.tbz ] ; then
218 if [ "x$use_pkg" = "x-p" ] ; then
219 log_it "install $p from ${PKG_DIR}/$pn.tbz"
220 pkg_add ${PKG_DIR}/$pn.tbz
224 if [ -z "$i" ] ; then
232 make clean ${PORTS_OPTS}
233 if make install ${PORTS_OPTS} ; then
234 if [ "x${PKG_DIR}" != "x" ] ; then
235 make package ${PORTS_OPTS}
243 ) > _.$b 2>&1 < /dev/null
252 true > /mnt/_.prefetch
253 echo "Building /tmp/_.plist" >> /mnt/_.prefetch
255 ports_recurse . $PORTS_WE_WANT
257 echo "Completed /tmp/_.plist" >> /mnt/_.prefetch
258 # Now checksump/fetch them
259 for p in `cat /tmp/_.plist`
264 if make checksum $PORTS_OPTS ; then
265 rm -f /mnt/_.prefetch.$b
266 echo "OK $p" >> /mnt/_.prefetch
270 make checksum $PORTS_OPTS || true
272 if make checksum $PORTS_OPTS > /dev/null 2>&1 ; then
273 rm -f /mnt/_.prefetch.$b
274 echo "OK $p" >> /mnt/_.prefetch
276 echo "BAD $p" >> /mnt/_.prefetch
278 ) > /mnt/_.prefetch.$b 2>&1
283 #######################################################################
291 args=`getopt bc:hkpw $*`
292 if [ $? -ne 0 ] ; then
309 if [ ! -f "$c_arg" ] ; then
310 echo "Cannot read $c_arg" 1>&2
339 #######################################################################
341 if [ "x$1" = "xchroot_script" ] ; then
354 if [ "x$1" = "xfinal_chroot" ] ; then
359 if [ $# -gt 0 ] ; then
360 echo "$0: Extraneous arguments supplied"
364 #######################################################################
367 echo $T0 $T0 > /tmp/_sb_log
369 log_it Unmount everything
372 umount /freebsd/distfiles || true
373 umount /mnt/freebsd/distfiles || true
374 umount /dev/${FREEBSD_PART} || true
375 umount /mnt/freebsd || true
376 umount /mnt/dev || true
378 umount /dev/${TARGET_PART} || true
381 log_it Prepare running image
383 mount /dev/${FREEBSD_PART} /freebsd
385 #######################################################################
387 if [ ! -d /freebsd/${PORTS_PATH} ] ; then
388 echo PORTS_PATH does not exist 1>&2
392 if [ ! -d /freebsd/${SRC_PATH} ] ; then
393 echo SRC_PATH does not exist 1>&2
397 log_it TARGET_PART $TARGET_PART
401 ln -s /freebsd/${PORTS_PATH} /usr/ports
404 ln -s /freebsd/${SRC_PATH} /usr/src
407 if [ "x${OBJ_PATH}" != "x" ] ; then
409 mkdir -p /freebsd/${OBJ_PATH}
410 ln -s /freebsd/${OBJ_PATH} /usr/obj
417 #######################################################################
419 for i in ${PORTS_WE_WANT}
421 if [ ! -d $i ] ; then
422 echo "Port $i not found" 1>&2
430 #######################################################################
432 log_it Prepare destination partition
433 newfs -O2 -U /dev/${TARGET_PART} > /dev/null
434 mount /dev/${TARGET_PART} /mnt
436 mount -t devfs devfs /mnt/dev
438 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
439 rm -rf /freebsd/${PORTS_PATH}/distfiles
440 ln -s /freebsd/distfiles /freebsd/${PORTS_PATH}/distfiles
441 mkdir -p /freebsd/distfiles
442 mount ${REMOTEDISTFILES} /freebsd/distfiles
445 log_it copy ports config files
446 (cd / ; find var/db/ports -print | cpio -dumpv /mnt > /dev/null 2>&1)
448 log_it "Start prefetch of ports distfiles"
455 make ${JARG} -s buildworld ${SRCCONF} > /mnt/_.bw 2>&1
463 make ${JARG} -s buildkernel KERNCONF=$KERNCONF > /mnt/_.bk 2>&1
469 (cd /usr/src && make ${JARG} installworld DESTDIR=/mnt ${SRCCONF} ) \
473 (cd /usr/src/etc && make -m /usr/src/share/mk distribution DESTDIR=/mnt ${SRCCONF} ) \
477 (cd /usr/src && make ${JARG} installkernel DESTDIR=/mnt KERNCONF=$KERNCONF ) \
480 if [ "x${OBJ_PATH}" != "x" ] ; then
482 ln -s /freebsd/${OBJ_PATH} /mnt/usr/obj
485 log_it Wait for ports prefetch
486 log_it "(Tail /mnt/_.prefetch for progress)"
489 log_it Move filesystems
491 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
492 umount /freebsd/distfiles
494 umount /dev/${FREEBSD_PART} || true
495 mkdir -p /mnt/freebsd
496 mount /dev/${FREEBSD_PART} /mnt/freebsd
497 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
498 mount ${REMOTEDISTFILES} /mnt/freebsd/distfiles
501 rm -rf /mnt/usr/ports || true
502 ln -s /freebsd/${PORTS_PATH} /mnt/usr/ports
504 rm -rf /mnt/usr/src || true
505 ln -s /freebsd/${SRC_PATH} /mnt/usr/src
507 log_it Build and install ports
509 # Make sure fetching will work in the chroot
510 if [ -f /etc/resolv.conf ] ; then
511 log_it copy resolv.conf
512 cp /etc/resolv.conf /mnt/etc
513 chflags schg /mnt/etc/resolv.conf
516 if [ -f /etc/localtime ] ; then
517 log_it copy localtime
518 cp /etc/localtime /mnt/etc
521 log_it ldconfig in chroot
522 chroot /mnt sh /etc/rc.d/ldconfig start
532 cp /tmp/_sb_log /mnt/tmp
534 if [ "x$c_arg" != "x" ] ; then
536 chroot /mnt sh /root/$0 -c /root/`basename $c_arg` $use_pkg chroot_script
538 chroot /mnt sh /root/$0 $use_pkg chroot_script
540 cp /mnt/tmp/_sb_log /tmp
543 sed "/[ ]\/[ ]/s;^[^ ]*[ ];/dev/${TARGET_PART} ;" \
544 /etc/fstab > /mnt/etc/fstab
546 log_it create all mountpoints
547 grep -v '^[ ]*#' /mnt/etc/fstab |
553 if [ "x$SERCONS" != "xfalse" ] ; then
554 log_it serial console
555 echo " -h" > /mnt/boot.config
556 sed -i "" -e /ttyd0/s/off/on/ /mnt/etc/ttys
557 sed -i "" -e /ttyu0/s/off/on/ /mnt/etc/ttys
558 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' /mnt/etc/ttys
561 log_it move config files
564 mkdir root/configfiles_dist
565 find ${CONFIGFILES} -print | cpio -dumpv root/configfiles_dist
568 (cd / && find ${CONFIGFILES} -print | cpio -dumpv /mnt)
573 cp /tmp/_sb_log /mnt/tmp
574 if [ "x$c_arg" != "x" ] ; then
575 chroot /mnt sh /root/$0 -c /root/`basename $c_arg` final_chroot
577 chroot /mnt sh /root/$0 final_chroot
579 cp /mnt/tmp/_sb_log /tmp
580 log_it "Check these messages (if any):"
581 grep '^Stop' /mnt/_* || true