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