]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/sysbuild/sysbuild.sh
MFH
[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 PKG_DIR=/usr/ports/packages/All
84
85 # Remotely mounted distfiles
86 # REMOTEDISTFILES=fs:/rdonly/distfiles
87
88 # Proxy
89 #FTP_PROXY=http://127.0.0.1:3128/
90 #HTTP_PROXY=http://127.0.0.1:3128/
91 #export FTP_PROXY HTTP_PROXY
92
93 PORTS_WE_WANT='
94 '
95
96 PORTS_OPTS="BATCH=YES A4=yes"
97
98 PORTS_WITHOUT=""
99 PORTS_WITH=""
100
101 CONFIGFILES='
102 '
103
104 SBMNT="/mnt.sysbuild"
105
106 cleanup() (
107 )
108
109 before_ports() (
110 )
111
112 before_ports_chroot() (
113 )
114
115 final_root() (
116 )
117
118 final_chroot() (
119 )
120
121 #######################################################################
122 # -P is a pretty neat way to clean junk out from your ports dist-files:
123 #
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
128 #
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"
131 # distfiles.
132 #
133 #######################################################################
134
135 usage () {
136         (
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"
144         ) 1>&2
145         exit 2
146 }
147
148 #######################################################################
149 #######################################################################
150
151 if [ ! -f $0 ] ; then
152         echo "Must be able to access self ($0)" 1>&2
153         exit 1
154 fi
155
156 if grep -q 'Magic String: 0`0nQT40W%l,CX&' $0 ; then
157         true
158 else
159         echo "self ($0) does not contain magic string" 1>&2
160         exit 1
161 fi
162
163 #######################################################################
164
165 set -e
166
167 log_it() (
168         a="$*"
169         set `cat /tmp/_sb_log`
170         TX=`date +%s`
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"
176 )
177
178 #######################################################################
179
180 ports_make() {
181         make $* WITH="${PORTS_WITH}" WITHOUT="${PORTS_WITHOUT}" ${PORTS_OPTS}
182 }
183
184 ports_recurse() (
185         cd /usr/ports
186         t=$1
187         shift
188         if [ "x$t" = "x." ] ; then
189                 true > /tmp/_.plist
190                 true > /tmp/_.plist.tdone
191                 echo 'digraph {' > /tmp/_.plist.dot
192         fi
193         if grep -q "^$t\$" /tmp/_.plist.tdone ; then
194                 return
195         fi
196         echo "$t" >> /tmp/_.plist.tdone
197         for d
198         do
199                 fl=""
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
205                                 continue
206                         fi
207                         # echo "Flavored port $d ($t) (fl $fl) (bd $bd)" 1>&2
208                         d=$bd
209                 fi
210                 d=`cd /usr/ports && cd $d && /bin/pwd`
211                 if [ ! -f $d/Makefile ] ; then
212                         echo "Missing port (Makefile) $d" 1>&2
213                         continue
214                 fi
215                 if [ "x$t" != "x." ] ; then
216                         echo "\"$t\" -> \"$d\"" >> /tmp/_.plist.dot
217                 fi
218                 if grep -q "^$d\$" /tmp/_.plist ; then
219                         true
220                 elif grep -q "^$d\$" /tmp/_.plist.tdone ; then
221                         true
222                 else
223                         (
224                         cd $d
225                         l=""
226                         for a in `ports_make -V _UNIFIED_DEPENDS $fl`
227                         do
228                                 x=`expr "$a" : '.*:\(.*\)'`
229                                 l="${l} ${x}"
230                         done
231                         ports_recurse $d $l
232                         )
233                         echo "$d" >> /tmp/_.plist
234                 fi
235         done
236         if [ "x$t" = "x." ] ; then
237                 echo '}' >> /tmp/_.plist.dot
238         fi
239 )
240
241 ports_build() (
242
243         ports_recurse . $PORTS_WE_WANT 
244
245         if [ "x${PKG_DIR}" != "x" ] ; then
246                 mkdir -p ${PKG_DIR}
247         fi
248
249         pd=`cd /usr/ports && /bin/pwd`
250         # Now build & install them
251         for p in `cat /tmp/_.plist`
252         do
253                 b=`echo $p | tr / _`
254                 t=`echo $p | sed "s,${pd},,"`
255                 pn=`cd $p && ports_make package-name`
256
257                 if [ "x`basename $p`" == "xpkg" ] ; then
258                         log_it "Very Special: $t ($pn)"
259
260                         (
261                         cd $p
262                         ports_make clean all install 
263                         ) > _.$b 2>&1 < /dev/null
264                         continue
265                 fi
266
267                 if pkg info $pn > /dev/null 2>&1 ; then
268                         log_it "Already installed: $t ($pn)"
269                         continue
270                 fi
271
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)"
275                                 (
276                                 set +e
277                                 pkg add ${PKG_DIR}/$pn.txz || true
278                                 ) > _.$b 2>&1 < /dev/null
279                                 continue
280                         fi
281                 fi
282
283                 miss=`(cd $p ; ports_make missing) || true`
284
285                 if [ "x${miss}" != "x" ] ; then
286                         log_it "NB: MISSING for $p:" $miss
287                 fi
288
289                 log_it "build $pn ($p)"
290                 (
291                         set +e
292                         cd $p
293                         ports_make clean
294                         if ports_make install ; then
295                                 if [ "x${PKG_DIR}" != "x" ] ; then
296                                         ports_make package
297                                 fi
298                         else
299                                 log_it FAIL build $p
300                         fi
301                         ports_make clean
302
303                 ) > _.$b 2>&1 < /dev/null
304         done
305 )
306
307 ports_prefetch() (
308         (
309         set +x
310         ldir=$1
311         true > /${ldir}/_.prefetch
312         echo "Building /tmp/_.plist" >> /${ldir}/_.prefetch
313
314         ports_recurse . $PORTS_WE_WANT
315
316         echo "Completed /tmp/_.plist" >> /${ldir}/_.prefetch
317         # Now checksump/fetch them
318         for p in `cat /tmp/_.plist`
319         do
320                 b=`echo $p | tr / _`
321                 (
322                         cd $p
323                         if ports_make checksum ; then
324                                 rm -f /${ldir}/_.prefetch.$b
325                                 echo "OK $p" >> /${ldir}/_.prefetch
326                                 exit 0
327                         fi
328                         ports_make distclean
329                         ports_make checksum || true
330
331                         if ports_make checksum > /dev/null 2>&1 ; then
332                                 rm -f /${ldir}/_.prefetch.$b
333                                 echo "OK $p" >> /${ldir}/_.prefetch
334                         else
335                                 echo "BAD $p" >> /${ldir}/_.prefetch
336                         fi
337                 ) > /${ldir}/_.prefetch.$b 2>&1
338         done
339         echo "Done" >> /${ldir}/_.prefetch
340         ) 
341 )
342
343 #######################################################################
344
345 do_world=true
346 do_kernel=true
347 do_prefetch=false
348 use_pkg=""
349 c_arg=""
350
351 set +e
352 args=`getopt bc:hkpP:w $*`
353 if [ $? -ne 0 ] ; then
354         usage
355 fi
356 set -e
357
358 set -- $args
359 for i
360 do
361         case "$i"
362         in
363         -b)
364                 shift;
365                 do_world=false
366                 do_kernel=false
367                 ;;
368         -c)
369                 c_arg=$2
370                 if [ ! -f "$c_arg" ] ; then
371                         echo "Cannot read $c_arg" 1>&2
372                         usage
373                 fi
374                 . "$2"
375                 shift
376                 shift
377                 ;;
378         -h)
379                 usage
380                 ;;
381         -k)
382                 shift;
383                 do_kernel=false
384                 ;;
385         -p)
386                 shift;
387                 use_pkg="-p"
388                 ;;
389         -P)
390                 shift;
391                 do_prefetch=true
392                 distfile_cache=$1
393                 shift;
394                 ;;
395         -w)
396                 shift;
397                 do_world=false
398                 ;;
399         --)
400                 shift
401                 break;
402                 ;;
403         esac
404 done
405
406 #######################################################################
407
408 if [ "x$1" = "xchroot_script" ] ; then
409         set -e
410
411         shift
412
413         before_ports_chroot
414
415         ports_build
416
417         exit 0
418 fi
419
420 if [ "x$1" = "xfinal_chroot" ] ; then
421         final_chroot
422         exit 0
423 fi
424
425 if [ $# -gt 0 ] ; then
426         echo "$0: Extraneous arguments supplied"
427         usage
428 fi
429
430 #######################################################################
431
432 T0=`date +%s`
433 echo $T0 $T0 > /tmp/_sb_log
434
435 [ ! -d ${SBMNT} ] && mkdir -p ${SBMNT}
436
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
442         ports_prefetch /tmp 
443         exit 0
444 fi
445
446 log_it Unmount everything
447 (
448         ( cleanup )
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
456 ) # > /dev/null 2>&1
457
458 log_it Prepare running image
459 mkdir -p /freebsd
460 mount ${FREEBSD_PART} /freebsd
461
462 #######################################################################
463
464 if [ ! -d /freebsd/${PORTS_PATH} ] ;  then
465         echo PORTS_PATH does not exist 1>&2
466         exit 1
467 fi
468
469 if [ ! -d /freebsd/${SRC_PATH} ] ;  then
470         echo SRC_PATH does not exist 1>&2
471         exit 1
472 fi
473
474 log_it TARGET_PART $TARGET_PART
475 sleep 5
476
477 rm -rf /usr/ports
478 ln -s /freebsd/${PORTS_PATH} /usr/ports
479
480 rm -rf /usr/src
481 ln -s /freebsd/${SRC_PATH} /usr/src
482
483 if $do_world ; then
484         if [ "x${OBJ_PATH}" != "x" ] ; then
485                 rm -rf /usr/obj
486                 (cd /freebsd && mkdir -p ${OBJ_PATH} && ln -s ${OBJ_PATH} /usr/obj)
487         else
488                 rm -rf /usr/obj
489                 mkdir -p /usr/obj
490         fi
491 fi
492
493 #######################################################################
494
495 for i in ${PORTS_WE_WANT}
496 do
497         (
498         cd /usr/ports
499         if [ ! -d $i ]  ; then
500                 echo "Port $i not found" 1>&2
501                 exit 2
502         fi
503         )
504 done
505
506 #export PORTS_WE_WANT
507 #export PORTS_OPTS
508
509 #######################################################################
510
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
516
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
522 fi
523
524 log_it copy ports config files
525 (cd / ; find var/db/ports -print | cpio -dumpv ${SBMNT} > /dev/null 2>&1)
526
527 log_it "Start prefetch of ports distfiles"
528 ports_prefetch ${SBMNT} &
529
530 if $do_world ; then
531         (
532         cd /usr/src
533         log_it "Buildworld"
534         make ${JARG} -s buildworld ${SRCCONF} > ${SBMNT}/_.bw 2>&1
535         )
536 fi
537
538 if $do_kernel ; then
539         (
540         cd /usr/src
541         log_it "Buildkernel"
542         make ${JARG} -s buildkernel KERNCONF=$KERNCONF > ${SBMNT}/_.bk 2>&1
543         )
544 fi
545
546
547 log_it Installworld
548 (cd /usr/src && make ${JARG} installworld DESTDIR=${SBMNT} ${SRCCONF} ) \
549         > ${SBMNT}/_.iw 2>&1
550
551 log_it distribution
552 (cd /usr/src && make -m /usr/src/share/mk distribution DESTDIR=${SBMNT} ${SRCCONF} ) \
553         > ${SBMNT}/_.dist 2>&1
554
555 log_it Installkernel
556 (cd /usr/src && make ${JARG} installkernel DESTDIR=${SBMNT} KERNCONF=$KERNCONF ) \
557         > ${SBMNT}/_.ik 2>&1
558
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 )
562 fi
563
564 log_it Wait for ports prefetch
565 log_it "(Tail ${SBMNT}/_.prefetch for progress)"
566 wait
567
568 log_it Move filesystems
569
570 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
571         umount /freebsd/distfiles
572 fi
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
578 fi
579
580 rm -rf ${SBMNT}/usr/ports || true
581 ln -s /freebsd/${PORTS_PATH} ${SBMNT}/usr/ports
582
583 rm -rf ${SBMNT}/usr/src || true
584 ln -s /freebsd/${SRC_PATH} ${SBMNT}/usr/src
585
586 log_it Build and install ports
587
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
593 fi
594
595 if [ -f /etc/localtime ] ; then
596         log_it copy localtime
597         cp /etc/localtime ${SBMNT}/etc
598 fi
599
600 log_it ldconfig in chroot
601 chroot ${SBMNT} sh /etc/rc.d/ldconfig start
602
603 log_it before_ports
604
605         before_ports 
606 )
607
608 log_it fixing fstab
609 sed "/[         ]\/[    ]/s;^[^         ]*[     ];/dev/${TARGET_PART}   ;" \
610         /etc/fstab > ${SBMNT}/etc/fstab
611
612 log_it build ports
613
614 cp $0 ${SBMNT}/root
615 cp /tmp/_sb_log ${SBMNT}/tmp
616 b=`basename $0`
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 
620 else
621         chroot ${SBMNT} sh /root/$0 $use_pkg chroot_script
622 fi
623 cp ${SBMNT}/tmp/_sb_log /tmp
624
625 log_it create all mountpoints
626 grep -v '^[     ]*#' ${SBMNT}/etc/fstab | 
627 while read a b c
628 do
629         mkdir -p ${SBMNT}/$b
630 done
631
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
638 fi
639
640 log_it move dist config files "(expect warnings)"
641 (
642         cd ${SBMNT}
643         mkdir root/configfiles_dist
644         find ${CONFIGFILES} -print | cpio -dumpv root/configfiles_dist
645 )
646
647 log_it copy live config files
648 (cd / && find ${CONFIGFILES} -print | cpio -dumpv ${SBMNT})
649
650 log_it final_root
651 ( final_root )
652 log_it final_chroot
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
656 else
657         chroot ${SBMNT} sh /root/$0 final_chroot
658 fi
659 cp ${SBMNT}/tmp/_sb_log /tmp
660 log_it "Check these messages (if any):"
661 grep '^Stop' ${SBMNT}/_* || true
662 log_it DONE
663 echo "Now you probably want to:"
664 echo "    $GPART_SUGGESTION"
665 echo "    shutdown -r now"