]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/sysbuild/sysbuild.sh
MFC: r326864
[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                 if [ ! -d $d ] ; then
200                         echo "Missing port $d ($t)" 1>&2
201                         continue
202                 fi
203                 d=`cd /usr/ports && cd $d && /bin/pwd`
204                 if [ ! -f $d/Makefile ] ; then
205                         echo "Missing port $d" 1>&2
206                         continue
207                 fi
208                 if [ "x$t" != "x." ] ; then
209                         echo "\"$t\" -> \"$d\"" >> /tmp/_.plist.dot
210                 fi
211                 if grep -q "^$d\$" /tmp/_.plist ; then
212                         true
213                 elif grep -q "^$d\$" /tmp/_.plist.tdone ; then
214                         true
215                 else
216                         (
217                         cd $d
218                         l=""
219                         for a in `ports_make -V _UNIFIED_DEPENDS`
220                         do
221                                 x=`expr "$a" : '.*:\(.*\)'`
222                                 l="${l} ${x}"
223                         done
224                         ports_recurse $d $l
225                         )
226                         echo "$d" >> /tmp/_.plist
227                 fi
228         done
229         if [ "x$t" = "x." ] ; then
230                 echo '}' >> /tmp/_.plist.dot
231         fi
232 )
233
234 ports_build() (
235
236         ports_recurse . $PORTS_WE_WANT 
237
238         if [ "x${PKG_DIR}" != "x" ] ; then
239                 mkdir -p ${PKG_DIR}
240         fi
241
242         pd=`cd /usr/ports && /bin/pwd`
243         # Now build & install them
244         for p in `cat /tmp/_.plist`
245         do
246                 b=`echo $p | tr / _`
247                 t=`echo $p | sed "s,${pd},,"`
248                 pn=`cd $p && ports_make package-name`
249
250                 if [ "x`basename $p`" == "xpkg" ] ; then
251                         log_it "Very Special: $t ($pn)"
252
253                         (
254                         cd $p
255                         ports_make clean all install 
256                         ) > _.$b 2>&1 < /dev/null
257                         continue
258                 fi
259
260                 if pkg info $pn > /dev/null 2>&1 ; then
261                         log_it "Already installed: $t ($pn)"
262                         continue
263                 fi
264
265                 if [ "x${PKG_DIR}" != "x" -a -f ${PKG_DIR}/$pn.txz ] ; then
266                         if [ "x$use_pkg" = "x-p" ] ; then
267                                 log_it "Install $t ($pn)"
268                                 (
269                                 set +e
270                                 pkg add ${PKG_DIR}/$pn.txz || true
271                                 ) > _.$b 2>&1 < /dev/null
272                                 continue
273                         fi
274                 fi
275
276                 miss=`(cd $p ; ports_make missing) || true`
277
278                 if [ "x${miss}" != "x" ] ; then
279                         log_it "NB: MISSING for $p:" $miss
280                 fi
281
282                 log_it "build $pn ($p)"
283                 (
284                         set +e
285                         cd $p
286                         ports_make clean
287                         if ports_make install ; then
288                                 if [ "x${PKG_DIR}" != "x" ] ; then
289                                         ports_make package
290                                 fi
291                         else
292                                 log_it FAIL build $p
293                         fi
294                         ports_make clean
295
296                 ) > _.$b 2>&1 < /dev/null
297         done
298 )
299
300 ports_prefetch() (
301         (
302         set +x
303         ldir=$1
304         true > /${ldir}/_.prefetch
305         echo "Building /tmp/_.plist" >> /${ldir}/_.prefetch
306
307         ports_recurse . $PORTS_WE_WANT
308
309         echo "Completed /tmp/_.plist" >> /${ldir}/_.prefetch
310         # Now checksump/fetch them
311         for p in `cat /tmp/_.plist`
312         do
313                 b=`echo $p | tr / _`
314                 (
315                         cd $p
316                         if ports_make checksum ; then
317                                 rm -f /${ldir}/_.prefetch.$b
318                                 echo "OK $p" >> /${ldir}/_.prefetch
319                                 exit 0
320                         fi
321                         ports_make distclean
322                         ports_make checksum || true
323
324                         if ports_make checksum > /dev/null 2>&1 ; then
325                                 rm -f /${ldir}/_.prefetch.$b
326                                 echo "OK $p" >> /${ldir}/_.prefetch
327                         else
328                                 echo "BAD $p" >> /${ldir}/_.prefetch
329                         fi
330                 ) > /${ldir}/_.prefetch.$b 2>&1
331         done
332         echo "Done" >> /${ldir}/_.prefetch
333         ) 
334 )
335
336 #######################################################################
337
338 do_world=true
339 do_kernel=true
340 do_prefetch=false
341 use_pkg=""
342 c_arg=""
343
344 set +e
345 args=`getopt bc:hkpP:w $*`
346 if [ $? -ne 0 ] ; then
347         usage
348 fi
349 set -e
350
351 set -- $args
352 for i
353 do
354         case "$i"
355         in
356         -b)
357                 shift;
358                 do_world=false
359                 do_kernel=false
360                 ;;
361         -c)
362                 c_arg=$2
363                 if [ ! -f "$c_arg" ] ; then
364                         echo "Cannot read $c_arg" 1>&2
365                         usage
366                 fi
367                 . "$2"
368                 shift
369                 shift
370                 ;;
371         -h)
372                 usage
373                 ;;
374         -k)
375                 shift;
376                 do_kernel=false
377                 ;;
378         -p)
379                 shift;
380                 use_pkg="-p"
381                 ;;
382         -P)
383                 shift;
384                 do_prefetch=true
385                 distfile_cache=$1
386                 shift;
387                 ;;
388         -w)
389                 shift;
390                 do_world=false
391                 ;;
392         --)
393                 shift
394                 break;
395                 ;;
396         esac
397 done
398
399 #######################################################################
400
401 if [ "x$1" = "xchroot_script" ] ; then
402         set -e
403
404         shift
405
406         before_ports_chroot
407
408         ports_build
409
410         exit 0
411 fi
412
413 if [ "x$1" = "xfinal_chroot" ] ; then
414         final_chroot
415         exit 0
416 fi
417
418 if [ $# -gt 0 ] ; then
419         echo "$0: Extraneous arguments supplied"
420         usage
421 fi
422
423 #######################################################################
424
425 T0=`date +%s`
426 echo $T0 $T0 > /tmp/_sb_log
427
428 [ ! -d ${SBMNT} ] && mkdir -p ${SBMNT}
429
430 if $do_prefetch ; then
431         rm -rf /tmp/sysbuild/ports
432         mkdir -p /tmp/sysbuild/ports
433         ln -s ${distfile_cache} /tmp/sysbuild/ports/distfiles
434         export PORTS_OPTS=CD_MOUNTPTS=/tmp/sysbuild
435         ports_prefetch /tmp 
436         exit 0
437 fi
438
439 log_it Unmount everything
440 (
441         ( cleanup )
442         umount /freebsd/distfiles || true
443         umount ${SBMNT}/freebsd/distfiles || true
444         umount ${FREEBSD_PART} || true
445         umount ${SBMNT}/freebsd || true
446         umount ${SBMNT}/dev || true
447         umount ${SBMNT} || true
448         umount /dev/${TARGET_PART} || true
449 ) # > /dev/null 2>&1
450
451 log_it Prepare running image
452 mkdir -p /freebsd
453 mount ${FREEBSD_PART} /freebsd
454
455 #######################################################################
456
457 if [ ! -d /freebsd/${PORTS_PATH} ] ;  then
458         echo PORTS_PATH does not exist 1>&2
459         exit 1
460 fi
461
462 if [ ! -d /freebsd/${SRC_PATH} ] ;  then
463         echo SRC_PATH does not exist 1>&2
464         exit 1
465 fi
466
467 log_it TARGET_PART $TARGET_PART
468 sleep 5
469
470 rm -rf /usr/ports
471 ln -s /freebsd/${PORTS_PATH} /usr/ports
472
473 rm -rf /usr/src
474 ln -s /freebsd/${SRC_PATH} /usr/src
475
476 if $do_world ; then
477         if [ "x${OBJ_PATH}" != "x" ] ; then
478                 rm -rf /usr/obj
479                 (cd /freebsd && mkdir -p ${OBJ_PATH} && ln -s ${OBJ_PATH} /usr/obj)
480         else
481                 rm -rf /usr/obj
482                 mkdir -p /usr/obj
483         fi
484 fi
485
486 #######################################################################
487
488 for i in ${PORTS_WE_WANT}
489 do
490         (
491         cd /usr/ports
492         if [ ! -d $i ]  ; then
493                 echo "Port $i not found" 1>&2
494                 exit 2
495         fi
496         )
497 done
498
499 #export PORTS_WE_WANT
500 #export PORTS_OPTS
501
502 #######################################################################
503
504 log_it Prepare destination partition
505 newfs -t -E -O2 -U /dev/${TARGET_PART} > /dev/null
506 mount /dev/${TARGET_PART} ${SBMNT}
507 mkdir -p ${SBMNT}/dev
508 mount -t devfs devfs ${SBMNT}/dev
509
510 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
511         rm -rf /freebsd/${PORTS_PATH}/distfiles
512         ln -s /freebsd/distfiles /freebsd/${PORTS_PATH}/distfiles
513         mkdir -p /freebsd/distfiles
514         mount  ${REMOTEDISTFILES} /freebsd/distfiles
515 fi
516
517 log_it copy ports config files
518 (cd / ; find var/db/ports -print | cpio -dumpv ${SBMNT} > /dev/null 2>&1)
519
520 log_it "Start prefetch of ports distfiles"
521 ports_prefetch ${SBMNT} &
522
523 if $do_world ; then
524         (
525         cd /usr/src
526         log_it "Buildworld"
527         make ${JARG} -s buildworld ${SRCCONF} > ${SBMNT}/_.bw 2>&1
528         )
529 fi
530
531 if $do_kernel ; then
532         (
533         cd /usr/src
534         log_it "Buildkernel"
535         make ${JARG} -s buildkernel KERNCONF=$KERNCONF > ${SBMNT}/_.bk 2>&1
536         )
537 fi
538
539
540 log_it Installworld
541 (cd /usr/src && make ${JARG} installworld DESTDIR=${SBMNT} ${SRCCONF} ) \
542         > ${SBMNT}/_.iw 2>&1
543
544 log_it distribution
545 (cd /usr/src/etc && make -m /usr/src/share/mk distribution DESTDIR=${SBMNT} ${SRCCONF} ) \
546         > ${SBMNT}/_.dist 2>&1
547
548 log_it Installkernel
549 (cd /usr/src && make ${JARG} installkernel DESTDIR=${SBMNT} KERNCONF=$KERNCONF ) \
550         > ${SBMNT}/_.ik 2>&1
551
552 if [ "x${OBJ_PATH}" != "x" ] ; then
553         rmdir ${SBMNT}/usr/obj
554         ( cd /freebsd && mkdir -p ${OBJ_PATH} && ln -s ${OBJ_PATH} ${SBMNT}/usr/obj )
555 fi
556
557 log_it Wait for ports prefetch
558 log_it "(Tail ${SBMNT}/_.prefetch for progress)"
559 wait
560
561 log_it Move filesystems
562
563 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
564         umount /freebsd/distfiles
565 fi
566 umount ${FREEBSD_PART} || true
567 mkdir -p ${SBMNT}/freebsd
568 mount ${FREEBSD_PART} ${SBMNT}/freebsd
569 if [ "x${REMOTEDISTFILES}" != "x" ] ; then
570         mount  ${REMOTEDISTFILES} ${SBMNT}/freebsd/distfiles
571 fi
572
573 rm -rf ${SBMNT}/usr/ports || true
574 ln -s /freebsd/${PORTS_PATH} ${SBMNT}/usr/ports
575
576 rm -rf ${SBMNT}/usr/src || true
577 ln -s /freebsd/${SRC_PATH} ${SBMNT}/usr/src
578
579 log_it Build and install ports
580
581 # Make sure fetching will work in the chroot
582 if [ -f /etc/resolv.conf ] ; then
583         log_it copy resolv.conf
584         cp /etc/resolv.conf ${SBMNT}/etc
585         chflags schg ${SBMNT}/etc/resolv.conf
586 fi
587
588 if [ -f /etc/localtime ] ; then
589         log_it copy localtime
590         cp /etc/localtime ${SBMNT}/etc
591 fi
592
593 log_it ldconfig in chroot
594 chroot ${SBMNT} sh /etc/rc.d/ldconfig start
595
596 log_it before_ports
597
598         before_ports 
599 )
600
601 log_it fixing fstab
602 sed "/[         ]\/[    ]/s;^[^         ]*[     ];/dev/${TARGET_PART}   ;" \
603         /etc/fstab > ${SBMNT}/etc/fstab
604
605 log_it build ports
606
607 cp $0 ${SBMNT}/root
608 cp /tmp/_sb_log ${SBMNT}/tmp
609 b=`basename $0`
610 if [ "x$c_arg" != "x" ] ; then
611         cp $c_arg ${SBMNT}/root
612         chroot ${SBMNT} sh /root/$0 -c /root/`basename $c_arg` $use_pkg chroot_script 
613 else
614         chroot ${SBMNT} sh /root/$0 $use_pkg chroot_script
615 fi
616 cp ${SBMNT}/tmp/_sb_log /tmp
617
618 log_it create all mountpoints
619 grep -v '^[     ]*#' ${SBMNT}/etc/fstab | 
620 while read a b c
621 do
622         mkdir -p ${SBMNT}/$b
623 done
624
625 if [ "x$SERCONS" != "xfalse" ] ; then
626         log_it serial console
627         echo " -h" > ${SBMNT}/boot.config
628         sed -i "" -e /ttyd0/s/off/on/ ${SBMNT}/etc/ttys
629         sed -i "" -e /ttyu0/s/off/on/ ${SBMNT}/etc/ttys
630         sed -i "" -e '/^ttyv[0-8]/s/    on/     off/' ${SBMNT}/etc/ttys
631 fi
632
633 log_it move dist config files "(expect warnings)"
634 (
635         cd ${SBMNT}
636         mkdir root/configfiles_dist
637         find ${CONFIGFILES} -print | cpio -dumpv root/configfiles_dist
638 )
639
640 log_it copy live config files
641 (cd / && find ${CONFIGFILES} -print | cpio -dumpv ${SBMNT})
642
643 log_it final_root
644 ( final_root )
645 log_it final_chroot
646 cp /tmp/_sb_log ${SBMNT}/tmp
647 if [ "x$c_arg" != "x" ] ; then
648         chroot ${SBMNT} sh /root/$0 -c /root/`basename $c_arg` final_chroot
649 else
650         chroot ${SBMNT} sh /root/$0 final_chroot
651 fi
652 cp ${SBMNT}/tmp/_sb_log /tmp
653 log_it "Check these messages (if any):"
654 grep '^Stop' ${SBMNT}/_* || true
655 log_it DONE
656 echo "Now you probably want to:"
657 echo "    $GPART_SUGGESTION"
658 echo "    shutdown -r now"