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/'`
51 # Relative to /freebsd
60 #SRCCONF="SRCCONF=/usr/src/src.conf"
64 ncpu=`sysctl -n kern.smp.cpus`
65 if [ $ncpu -gt 1 ] ; then
72 # Remotely mounted distfiles
73 # REMOTEDISTFILES=fs:/rdonly/distfiles
76 #FTP_PROXY=http://127.0.0.1:3128/
77 #HTTP_PROXY=http://127.0.0.1:3128/
78 #export FTP_PROXY HTTP_PROXY
83 PORTS_OPTS="BATCH=YES MAKE_IDEA=YES A4=yes"
94 before_ports_chroot() (
103 #######################################################################
104 #######################################################################
108 echo "Usage: $0 [-b/-k/-w] [-c config_file]"
109 echo " -b suppress builds (both kernel and world)"
110 echo " -k suppress buildkernel"
111 echo " -w suppress buildworld"
112 echo " -p used cached packages"
113 echo " -c specify config file"
118 #######################################################################
119 #######################################################################
121 if [ ! -f $0 ] ; then
122 echo "Must be able to access self ($0)" 1>&2
126 if grep -q 'Magic String: 0`0nQT40W%l,CX&' $0 ; then
129 echo "self ($0) does not contain magic string" 1>&2
133 #######################################################################
140 set `cat /tmp/_sb_log`
142 echo "$1 $TX" > /tmp/_sb_log
143 DT=`expr $TX - $1 || true`
144 DL=`expr $TX - $2 || true`
145 echo -n "### `date +%H:%M:%S`"
146 printf " ### %5d ### %5d ### %s\n" $DT $DL "$a"
149 #######################################################################
156 if [ ! -d $d ] ; then
157 echo "Missing port $d" 1>&2
160 if grep -q "^$d\$" /tmp/_.plist ; then
165 ports_recurse `make -V _DEPEND_DIRS`
167 echo $d >> /tmp/_.plist
176 ports_recurse $PORTS_WE_WANT
178 # Now build & install them
179 for p in `cat /tmp/_.plist`
181 t=`echo $p | sed 's,/usr/ports/,,'`
182 pn=`cd $p && make package-name`
183 if [ "x${PKG_DIR}" != "x" -a -f ${PKG_DIR}/$pn.tbz ] ; then
184 if [ "x$use_pkg" = "x-p" ] ; then
185 log_it "install $p from ${PKG_DIR}/$pn.tbz"
186 pkg_add ${PKG_DIR}/$pn.tbz
190 if [ -z "$i" ] ; then
199 if make install ${PORTS_OPTS} ; then
200 if [ "x${PKG_DIR}" != "x" ] ; then
201 make package ${PORTS_OPTS}
208 ) > _.$b 2>&1 < /dev/null
218 ports_recurse $PORTS_WE_WANT
220 # Now checksump/fetch them
221 for p in `cat /tmp/_.plist`
226 if make checksum $PORTS_OPTS ; then
230 make checksum $PORTS_OPTS || true
232 ) > /mnt/_.prefetch.$b 2>&1
237 #######################################################################
245 args=`getopt bc:hkpw $*`
246 if [ $? -ne 0 ] ; then
263 if [ ! -f "$c_arg" ] ; then
264 echo "Cannot read $c_arg" 1>&2
293 #######################################################################
295 if [ "x$1" = "xchroot_script" ] ; then
308 if [ "x$1" = "xfinal_chroot" ] ; then
313 if [ $# -gt 0 ] ; then
314 echo "$0: Extraneous arguments supplied"
318 #######################################################################
321 echo $T0 $T0 > /tmp/_sb_log
323 log_it Unmount everything
326 umount /freebsd/distfiles || true
327 umount /mnt/freebsd/distfiles || true
328 umount /dev/${FREEBSD_PART} || true
329 umount /mnt/freebsd || true
330 umount /mnt/dev || true
332 umount /dev/${TARGET_PART} || true
335 log_it Prepare running image
337 mount /dev/${FREEBSD_PART} /freebsd
339 #######################################################################
341 if [ ! -d /freebsd/${PORTS_PATH} ] ; then
342 echo PORTS_PATH does not exist 1>&2
346 if [ ! -d /freebsd/${SRC_PATH} ] ; then
347 echo SRC_PATH does not exist 1>&2
351 log_it TARGET_PART $TARGET_PART
355 ln -s /freebsd/${PORTS_PATH} /usr/ports
358 ln -s /freebsd/${SRC_PATH} /usr/src
361 if [ "x${OBJ_PATH}" != "x" ] ; then
363 mkdir -p /freebsd/${OBJ_PATH}
364 ln -s /freebsd/${OBJ_PATH} /usr/obj
371 #######################################################################
373 for i in ${PORTS_WE_WANT}
375 if [ ! -d $i ] ; then
376 echo "Port $i not found" 1>&2
384 #######################################################################
386 log_it Prepare destination partition
387 newfs -O2 -U /dev/${TARGET_PART} > /dev/null
388 mount /dev/${TARGET_PART} /mnt
390 mount -t devfs devfs /mnt/dev
392 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
393 rm -rf /freebsd/${PORTS_PATH}/distfiles
394 ln -s /freebsd/distfiles /freebsd/${PORTS_PATH}/distfiles
395 mkdir -p /freebsd/distfiles
396 mount ${REMOTEDISTFILES} /freebsd/distfiles
399 log_it "Start prefetch of ports distfiles"
406 make ${JARG} -s buildworld ${SRCCONF} > /mnt/_.bw 2>&1
414 make ${JARG} -s buildkernel KERNCONF=$KERNCONF > /mnt/_.bk 2>&1
420 (cd /usr/src && make ${JARG} installworld DESTDIR=/mnt ${SRCCONF} ) \
424 (cd /usr/src/etc && make -m /usr/src/share/mk distribution DESTDIR=/mnt ${SRCCONF} ) \
428 (cd /usr/src && make ${JARG} installkernel DESTDIR=/mnt KERNCONF=$KERNCONF ) \
431 if [ "x${OBJ_PATH}" != "x" ] ; then
433 ln -s /freebsd/${OBJ_PATH} /mnt/usr/obj
436 log_it Wait for ports prefetch
439 log_it Move filesystems
441 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
442 umount /freebsd/distfiles
444 umount /dev/${FREEBSD_PART} || true
445 mkdir -p /mnt/freebsd
446 mount /dev/${FREEBSD_PART} /mnt/freebsd
447 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
448 mount ${REMOTEDISTFILES} /mnt/freebsd/distfiles
451 rm -rf /mnt/usr/ports || true
452 ln -s /freebsd/${PORTS_PATH} /mnt/usr/ports
454 rm -rf /mnt/usr/src || true
455 ln -s /freebsd/${SRC_PATH} /mnt/usr/src
457 log_it Build and install ports
459 # Make sure fetching will work in the chroot
460 if [ -f /etc/resolv.conf ] ; then
461 log_it copy resolv.conf
462 cp /etc/resolv.conf /mnt/etc
463 chflags schg /mnt/etc/resolv.conf
466 if [ -f /etc/localtime ] ; then
467 log_it copy localtime
468 cp /etc/localtime /mnt/etc
471 log_it copy ports config files
472 (cd / ; find var/db/ports -print | cpio -dumpv /mnt )
474 log_it ldconfig in chroot
475 chroot /mnt sh /etc/rc.d/ldconfig start
485 cp /tmp/_sb_log /mnt/tmp
487 if [ "x$c_arg" != "x" ] ; then
489 chroot /mnt sh /root/$0 -c /root/`basename $c_arg` $use_pkg chroot_script
491 chroot /mnt sh /root/$0 $use_pkg chroot_script
493 cp /mnt/tmp/_sb_log /tmp
496 sed "/[ ]\/[ ]/s;^[^ ]*[ ];/dev/${TARGET_PART} ;" \
497 /etc/fstab > /mnt/etc/fstab
499 log_it create all mountpoints
500 grep -v '^[ ]*#' /mnt/etc/fstab |
506 if [ "x$SERCONS" != "xfalse" ] ; then
507 log_it serial console
508 echo " -h" > /mnt/boot.config
509 sed -i "" -e /ttyd0/s/off/on/ /mnt/etc/ttys
510 sed -i "" -e /ttyu0/s/off/on/ /mnt/etc/ttys
511 sed -i "" -e '/^ttyv[0-8]/s/ on/ off/' /mnt/etc/ttys
514 log_it move config files
517 mkdir root/configfiles_dist
518 find ${CONFIGFILES} -print | cpio -dumpv root/configfiles_dist
521 (cd / && find ${CONFIGFILES} -print | cpio -dumpv /mnt)
526 cp /tmp/_sb_log /mnt/tmp
527 if [ "x$c_arg" != "x" ] ; then
528 chroot /mnt sh /root/$0 -c /root/`basename $c_arg` final_chroot
530 chroot /mnt sh /root/$0 final_chroot
532 cp /mnt/tmp/_sb_log /tmp
533 log_it "Check these messages (if any):"
534 grep '^Stop' /mnt/_* || true