]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - release/picobsd/build/picobsd
This commit was generated by cvs2svn to compensate for changes in r169962,
[FreeBSD/FreeBSD.git] / release / picobsd / build / picobsd
1 #!/bin/sh -
2 #
3 # $FreeBSD$
4 # This file requires sysutils/makefs to run
5 #
6 # The new PicoBSD build script. Invoked as
7 #
8 # picobsd [options] floppy_type site_name
9 #
10 # Where floppy_type is a directory where the picobsd config info
11 # is held, and ${floppy_type}/floppy.tree.${site_name} contains
12 # optional site-specific configuration.
13 #
14 # For Options, see the bottom of the file where the processing is
15 # done. The picobsd(8) manpage might be of some help, but code and docs
16 # tend to lose sync over time...
17 #
18 # This script depends on the following files:
19 #
20 # in ${PICO_TREE} :
21 #   Makefile.conf       Makefile used to build the kernel
22 #   config              shell variables, sourced here.
23 #   mfs.mtree           mtree config file
24 #
25 #   floppy.tree/        files which go on the floppy
26 #   mfs_tree/           files which go onto the mfs
27 #
28 # in ${MY_TREE} :
29 #   PICOBSD             kernel config file
30 #   config              shell variables, sourced here.
31 #   crunch.conf         crunchgen configuration
32 #   floppy.tree.exclude files from floppy.tree/ which we do not need here.
33 #   floppy.tree/        local additions to the floppy.tree
34 #   floppy.tree.${site}/ same as above, site specific.
35 #   mfs_tree/           local additions to the mfs_free
36
37 #
38 #--- The main entry point is at the end.
39 #
40
41 # There are two set of initialization. The first one (set_defaults)
42 # is done on entry to the script, and is used to set default values
43 # for all variables which do not depend on floppy type and source tree.
44 #
45 # The second set is done after command line parsing, e.g.
46 # to resolve dependencies on the source tree.
47 #
48 # Naming:
49 # + variables that control operation (e.g. verbosity) and are generally
50 #   set from the command line have o_ ("option") as a name prefix
51 #
52 # + variables which contain pathnames and values that should not change
53 #   have c_ ("constant") as a name prefix
54 #
55 # + variables exported to Makefiles and subshells are CAPITAL
56 #
57 # + variables local to the script are lowercase, possibly with
58 #   an l_ ("local") prefix
59
60 # SRC points to your FreeBSD source tree.
61 # l_usrtree points to the /usr subdir for the source tree.
62 #     Normally /usr or ${SRC}/../usr
63 # l_objtree points to the obj tree. Normally ${l_usrtree}/obj-pico
64 # PICO_TREE is where standard picobsd stuff resides.
65 #     Normally ${SRC}/release/picobsd
66 # You can set SRC with --src <directory>
67 # It is not recommended to override the other variables.
68
69 # MY_TREE (set later) is where this floppy type resides.
70 # BUILDDIR is the build directory
71
72 # set some default values for variables.
73 # needs to be done as the first thing in the script.
74
75 # log something on stdout if verbose.
76 o_verbose=0     # this needs to be here!
77 log() {
78     if [ ${o_verbose} -gt 0 ] ; then
79         printf "\n*** %s\n" "$*"
80         if [ ${o_verbose}  -gt 1 ] ; then
81             read -p "=== Press enter to continue" foo
82         fi
83     fi
84 }
85
86 logverbose() {
87     local foo
88
89     printf "\n*** %s\n" "$*"
90     read -p "=== Press enter to continue" foo
91 }
92
93 set_defaults() {
94     # no way to use logging in this function, variable not set yet.
95
96     # EDITOR is the editor you use
97     # fd_size  floppy size in KB (default to 1440). You can use 1480,
98     #   1720, 2880, etc. but beware that only 1440 and 1480 will boot
99     #   from 1.44M floppy drives (1480 will not work on vmware).
100     EDITOR=${EDITOR:-vi}
101     fd_size=${fd_size:-1440}
102
103     o_all_in_mfs="yes"          # put all files in mfs so you can boot and run
104                                 # the image via diskless boot.
105     o_clean=""                  # do not clean
106     o_interactive=""            # default is interactive
107     o_verbose=0                 # verbose level, 0 is silent
108     o_tarv=""                   # tar verbose flag, "" or "v"
109     o_init_src=""               # non "" if we need to init libs and includes.
110     o_makeopts=${MAKEOPTS:--s}  # make options, be silent by default
111     o_no_devfs=yes              # we do not want devfs
112     o_do_modules=""             # do not build modules
113
114     SRC="/usr/src"              # default location for sources
115     c_startdir=`pwd`            # directory where we start
116                                 # used to lookup config and create BUILDDIR
117
118     # XXX 6.x/7.x have a single /boot/boot block, which is the concatenation
119     # of the old two. For the time being, we keep these, but this should
120     # be fixed at some point.
121
122     # blocks
123     c_boot1=/boot/boot1         # boot blocks (in case you want custom ones)
124     c_boot2=/boot/boot2
125
126     c_reply=${c_reply:-`mktemp "/tmp/reply.XXXXXXXXXX"`}
127                                 # file where User replies will be put
128     c_mnt=`mktemp -d "/tmp/picobsd.XXXXXXXXXX"`
129                                 # mountpoint used to build memory filesystems
130     c_fs=fs.PICOBSD             # filename used for the memory filesystem
131     c_img=picobsd.bin           # filename used for the picobsd image
132
133     # select the right memory disk name
134     case `uname -r` in
135         7.*|6.*|5.*)
136             l_label="bsdlabel"
137             ;;
138         *)
139             l_label="disklabel"
140             ;;
141     esac
142
143     set -e
144
145     trap fail 2
146     #trap fail 3
147     #trap fail 6
148     trap fail 15
149 }
150
151 create_includes_and_libraries2() {
152     local no
153     log "create_includes_and_libraries2() for ${SRC}"
154     if [ ${OSVERSION} -ge 600000 ] ; then
155         no="-DNO_CLEAN -DNO_PROFILE -DNO_GAMES -DNO_LIBC_R"
156     else
157         no="-DNOCLEAN -DNOPROFILE -DNOGAMES -DNOLIBC_R"
158     fi
159     MAKEOBJDIRPREFIX=${l_objtree}
160     export MAKEOBJDIRPREFIX
161     ( cd ${SRC};
162     # make -DNOCLEAN -DNOPROFILE -DNOGAMES -DNOLIBC_R -DPICOBSD buildworld
163     make _+_= $no toolchain
164     )
165 }
166
167 create_includes_and_libraries() {
168     local e i
169
170     log "create_includes_and_libraries() for ${SRC}"
171     # Optionally creates include directory and libraries.
172     mkdir -p ${l_usrtree}/include               # the include directory...
173     mkdir -p ${l_usrtree}/share/misc    # a few things go here
174     mkdir -p ${l_usrtree}/lib           # libraries
175     mkdir -p ${l_usrtree}/sbin          # some binaries
176     # override variables for ownershiip and destinations
177     # BINOWN:BINGRP are also used for include files
178     (cd ${SRC}; \
179         BINOWN=`id -un` BINGRP=`id -gn` \
180         DESTDIR=${l_usrtree}/.. \
181         make -m ${SRC}/share/mk includes ) || fail $? includes
182     # Pick up the correct headers for libraries.
183     CFLAGS="-nostdinc -I${l_usrtree}/include" ; export CFLAGS
184
185     (cd ${SRC}
186         # $e is the invocation of make with correct environment
187         # XXX check the NO* options below, maybe system dependent.
188         e="MAKEOBJDIRPREFIX=${l_objtree}/picobsd/libraries \
189             BINOWN=`id -un` BINGRP=`id -gn` \
190             DESTDIR=${l_usrtree}/.. \
191             make -m ${SRC}/share/mk \
192                 -DNOHTML -DNOINFO -DNOMAN -DNOSHARE -DNOFSCHG "
193         log "do a 'make obj' in a few places."
194         # This is very version-specific... The following works for 5.0
195         for i in lib secure/lib gnu/lib \
196                 gnu/usr.bin/perl usr.bin/lex usr.sbin/config ; do
197             (cd ${i}; eval $e obj)
198         done
199         log "now make the static libraries"
200         eval $e -DNOPROFILE -DNOPIC libraries
201         (cd ${SRC}/usr.sbin/config
202         eval $e         # build binary
203         eval $e install # install it
204         )
205     ) || fail $? "libraries"
206     log "Libraries done"
207 }
208
209 # set_type <type> looks in user or system directories for the floppy type
210 # specified as first argument, and sets variables according to the config.
211 # file. Also sets MY_TREE and BUILDDIR and SITE
212
213 set_type() {
214     local a i
215
216     log "set_type()"
217     THETYPE=$1
218     SITE=$2
219     a=$1
220     for i in ${c_startdir}/${a} ${PICO_TREE}/${a} ; do
221         log "set_type: checking $i"
222         if [ -d $i -a -f $i/PICOBSD -a -f $i/crunch.conf ] ; then
223             set -- `cat $i/PICOBSD | \
224             awk '/^#PicoBSD/ {print $2, $3, $4, $5, $6}'`
225             if [ "$1" != "" ]; then
226                 MFS_SIZE=$1 ; init_name=$2
227                 mfs_inodes=$3 ; fd_inodes=$4
228                 name=`(cd $i ; pwd) `
229                 name=`basename $name`
230                 MY_TREE=$i
231                 BUILDDIR=${c_startdir}/build_dir-${name}
232                 log "Matching file $name in $i"
233                 return ;
234             fi
235         fi
236     done
237     echo "Type $a NOT FOUND"
238 }
239
240 clean_tree() {
241     log "clean_tree()"
242     if [ "${name}" = "" ] ; then
243         echo "---> Wrong floppy type"
244         exit 3
245     fi
246     rm -rf ${BUILDDIR}
247 }
248
249 # prepare a message to be printed in the dialog menus.
250 set_msgs() {            # OK
251     log "set_msgs()"
252
253     MSG1="Type: ${THETYPE} name $name"
254
255     MSG="PicoBSD build -- Current parameters:\n\n\t1.  ${MSG1}\n\
256 \t2.  MFS size: ${MFS_SIZE} kB\n\
257 \t3.  Site-info: ${SITE}\n\t4.  Full-path: ${MY_TREE}\n"
258 }
259
260
261 # Main build procedure.
262 build_image() {
263     log "build_image() <${name}>"
264     [ "${name}" != "" ] || fail $? bad_type
265     clear
266     set_msgs
267     printf "${MSG}---> We'll use the sources living in ${SRC}\n\n"
268
269     # read config variables from a global and then a type-specific file
270     # basically STAND_LINKS and MY_DEVS, but can also override other
271     # variables.
272     # 
273     . ${PICO_TREE}/build/config
274     if [ -f ${MY_TREE}/config ] ; then
275         . ${MY_TREE}/config
276     fi
277
278     # location of the object directory
279     PICO_OBJ=${l_objtree}/picobsd/${THETYPE}
280     log "PICO_OBJ is ${PICO_OBJ}"
281
282     if [ ${OSVERSION} -ge 500035 ] ; then
283         MAKEOBJDIRPREFIX=${l_objtree}
284         export MAKEOBJDIRPREFIX
285         log `cd ${SRC}; make -f Makefile.inc1 -V WMAKEENV`
286         eval export `cd ${SRC}; make -f Makefile.inc1 -V WMAKEENV`
287     fi
288     # create build directory and subtree
289     mkdir -p ${BUILDDIR}/crunch
290     # remove any old stuff
291     rm -f ${BUILDDIR}/kernel.gz ${BUILDDIR}/${c_fs}
292     # invoke commands to build a kernel
293     do_kernel
294     # fill a subdirectory with things that go into the floppy
295     # (mostly /etc and similar stuff)
296     populate_floppy_fs
297     # populate it and produce a file with the MFS image
298     populate_mfs_tree           # things which go into mfs
299     # create, mount and fill a filesystem with floppy image
300     fill_floppy_image # copies everything into the floppy
301 }
302
303 build_package() {
304     local z msg
305
306     log "build_package()"
307     touch build.status
308     echo "##############################################" >>build.status
309     echo "## `date` ">>build.status
310     echo "##############################################" >>build.status
311     for z in bridge dial router net isp ; do
312         set_type ${z}
313         echo "---------------------------------------------">>build.status
314         echo "Building TYPE=${z}, SIZE=${MFS_SIZE}" >>build.status
315         msg="(ok)"      # error message
316         build_image || msg="** FAILED! **"
317         echo "  ${msg}">>build.status
318         # where do i put things ?
319         # clean_tree
320     done
321     exit 0
322 }
323
324 # Set build parameters interactively
325
326 main_dialog() {
327   local ans i l
328
329   log "main_dialog()"
330   while [ true ] ; do
331     set_msgs
332     rm ${c_reply}
333     dialog --menu "PicoBSD build menu -- (29 sep 2001)" 19 70 12 \
334         N "--> READY, build it <---" \
335         T "${MSG1}" \
336         K "edit Kernel config file" \
337         E "Edit crunch.conf file" \
338         S "MFS Size: ${MFS_SIZE}kB" \
339         I "Init type: ${init_name}" \
340         F "Floppy size: ${fd_size}kB" \
341         M "MFS bytes per inode: ${mfs_inodes}" \
342         U "UFS bytes per inode: ${fd_inodes}" \
343         $ "Site-info: ${SITE}" \
344         Q "Quit" \
345         2> ${c_reply}
346     ans=`cat ${c_reply}`
347     rm ${c_reply}
348     case ${ans} in
349     T)
350         l=""
351         for i in ${c_startdir} ${c_startdir}/* ${PICO_TREE}/* ; do
352             if [ -d $i -a -f $i/PICOBSD -a -f $i/crunch.conf ]; then
353                 l="$l `basename $i` `basename $i`"
354             fi
355         done
356         log $l
357         { dialog --menu "Setup the type of configuration" 12 70 5 $l \
358                 2> ${c_reply} && set_type "`cat ${c_reply}`" ${SITE} ; } || true
359         ;;
360     I)
361         { dialog --menu "Choose your init(8) program" \
362         10 70 2 init "Standard init (requires getty)" \
363         oinit "small init from TinyWare" 2> ${c_reply} \
364                 && init_name=`cat ${c_reply}` ; } || true
365         ;;
366
367     K) ${EDITOR} ${MY_TREE}/PICOBSD ;;
368
369     E) ${EDITOR} ${MY_TREE}/crunch.conf ;;
370
371     S)
372         { dialog --title "MFS Size setup" --inputbox \
373 "MFS size depends on what you need to put on the MFS image. Typically \
374 ranges between 820kB (for very small bridge/router images) to \
375 as much as 2500kB kB for a densely packed image. \
376 Keep in mind that this memory is \
377 totally lost to other programs. Usually you want to keep \
378 this as small as possible. " 10 70 2> ${c_reply} \
379         && MFS_SIZE=`cat ${c_reply}` ; } || true
380         ;;
381
382     \$)
383         { dialog --title "Site info setup" --inputbox \
384         "Please enter the full path to the directory \
385         containing site-specific setup. \
386         This directory tree must contain files that replace \
387         standard ones in floppy.tree/ and mfs.tree/ . " \
388         10 70 2> ${c_reply} && SITE=`cat ${c_reply}` ; } || true
389         ;;
390
391     F)
392         { dialog --menu "Set floppy size" 15 70 4 \
393             1440 "1.44MB" 1720 "1.72MB" 2880 "2.88MB" 4096 "4MB" \
394                  2> ${c_reply} && fd_size=`cat ${c_reply}` ; } || true
395         ;;
396
397     M)
398         { dialog --title "MFS bytes per inode:" --inputbox \
399         "Enter MFS bytes per inode (typically 4096..65536). \
400         A larger value means fewer inodes but more space on MFS" \
401         10 70 2> ${c_reply} && mfs_inodes=`cat ${c_reply}`  ; } || true
402         ;;
403
404     U)
405         { dialog --title "Floppy bytes per inode:" --inputbox \
406         "Enter floppy bytes per inode (typically 3072..65536). \
407         A larger value means fewer inodes but more space on the floppy." \
408         10 70 2> ${c_reply} && fd_inodes=`cat ${c_reply}` ; } || true
409         ;;
410
411     N) break 2
412         ;;
413
414     Q) exit 0 ;;
415
416     *) echo "\aUnknown option \"${ans}\". Try again."
417         sleep 2
418         clear
419         ;;
420     esac
421   done
422 }
423
424 # Call the build procedure
425 # Install image
426 do_install() {
427     log "do_install()"
428
429     if [ "${o_interactive}" = "NO" ] ; then
430         echo "+++ Build completed +++"
431         cat .build.reply || true
432         return
433     fi
434     dialog --title "Build ${THETYPE} completed" --inputbox \
435 "\nThe build process was completed successfuly.\n\
436 `cat .build.reply` \n\n\
437 Now we are going to install the image on the floppy.\n\
438 Please insert a blank floppy in /dev/fd0.\\n
439 WARNING: the contents of the floppy will be permanently erased!\n\
440 \n\
441 Your options:\n\
442         * ^C or [Cancel] to abort,\n\
443         * Enter to install ${c_img},\n\
444 " 20 80 2> ${c_reply}
445     if [ "$?" = "0" ]; then
446         echo "Writing ${c_img}..."
447         dd if=${BUILDDIR}/${c_img} of=/dev/fd0.${fd_size}
448     else
449         echo "Ok, the image is in ${c_img}"
450     fi
451     echo "Done."
452 }
453
454
455 #-------------------------------------------------------------------
456
457 # invoke the Makefile to compile the kernel.
458 do_kernel() {           # OK
459     log "do_kernel() Preparing kernel \"$name\" in $MY_TREE"
460     (cd $MY_TREE; export name SRC BUILDDIR # used in this makefile ;
461         # export CONFIG
462         if [ "${o_do_modules}" = "yes" ] ; then
463                 MODULES=""
464                 export MODULES
465         fi
466         make -m ${SRC}/share/mk -v -f ${PICO_TREE}/build/Makefile.conf ) || \
467         fail $? missing_kernel
468 }
469
470 # Populate the variable part of the floppy filesystem. Must be done before
471 # the MFS because its content might need to be copied there as well.
472 #
473 # This involves fetching files from three subtrees, in this order:
474 #
475 #  1. a standard one, from which type-specific files are excluded;
476 #  2. a type-specific one;
477 #  3. a site-specific one.
478 #
479 # Files are first copied to a local tree and then compressed.
480
481 populate_floppy_fs() {          # OK
482     local dst excl srcdir
483
484     log "populate_floppy_fs()"
485     dst=${BUILDDIR}/floppy.tree
486     log "pwd=`pwd` Populating floppy filesystem..."
487
488     # clean relics from old compilations.
489     rm -rf ${dst} || true
490     mkdir ${dst}
491
492     excl=${MY_TREE}/floppy.tree.exclude
493     if [ -f ${excl} ] ; then
494         excl="--exclude-from ${excl}"
495         log "Files excluded from generic tree: `echo;cat ${excl}`"
496     else
497         excl=""
498     fi
499     (cd ${PICO_TREE}/floppy.tree ; tar -cf - --exclude CVS ${excl} . ) | \
500                 (cd ${dst} ; tar x${o_tarv}f - )
501     log "Copied from generic floppy-tree `echo; ls -laR ${dst}`"
502
503     srcdir=${MY_TREE}/floppy.tree
504     if [ -d ${srcdir} ] ; then
505         log "update with type-specific files:"
506         (cd ${srcdir} ; tar -cf - --exclude CVS . ) | \
507             (cd ${dst} ; tar x${o_tarv}f - )
508         log "Copied from type floppy-tree `echo; ls -laR ${dst}`"
509     else
510         log "No type-specific floppy-tree"
511     fi
512     if [ -d ${srcdir}.${SITE} ] ; then
513         log "Update with site-specific (${SITE}) files:"
514         (cd ${srcdir}.${SITE} ; tar -cf - --exclude CVS . ) | \
515             (cd ${dst} ; tar x${o_tarv}f - )
516         log "Copied from site floppy-tree `echo; ls -laR ${dst}`"
517     else
518         log "No site-specific floppy-tree"
519     fi
520
521     # gzip returns an error if it fails to compress some file
522     (cd $dst ; gzip -9 etc/*
523             log "Compressed files in etc/ `echo; ls -l etc`"
524     ) || true
525 }
526
527 # Populate the memory filesystem with binaries and non-variable
528 # configuration files.
529 # First do an mtree pass, then create directory links and device entries,
530 # then run crunchgen etc. to build the binary and create links.
531 # Then copy the specific/generic mfs_tree.
532 # Finally, if required, make a copy of the floppy.tree onto /fd
533
534 populate_mfs_tree() {
535     local a dst
536
537     log "populate_mfs_tree()"
538     dst=${BUILDDIR}/mfs.tree
539     # clean relics from old compilations.
540     rm -rf ${dst} || true
541     mkdir ${dst}
542
543     log "pwd=`pwd`, Populating MFS tree..."
544
545     # use type-specific mfs.mtree, default to generic one.
546     a=${MY_TREE}/mfs.mtree
547     [ -f ${a} ] || a=${PICO_TREE}/build/mfs.mtree
548     log "Running mtree using $a..."
549     mtree -deU -f $a -p ${dst} > /dev/null || fail $? mtree
550
551     # XXX create links
552     for i in ${STAND_LINKS}; do
553         ln -s /stand ${dst}/$i
554     done
555     ln -s /dev/null ${dst}/var/run/log
556     ln -s /etc/termcap ${dst}/usr/share/misc/termcap
557
558
559     (
560     cd ${BUILDDIR}/crunch
561     log "Making and installing crunch1 from `pwd` src ${SRC}..."
562     a=${BUILDDIR}/crunch1.conf
563     ( export BUILDDIR SRC MY_TREE PICO_OBJ ;
564         make -m ${SRC}/share/mk \
565                 -v -f ${PICO_TREE}/build/Makefile.conf ${BUILDDIR}/crunch.mk )
566     log "Libs are ${LIBS} "
567     export SRC # used by crunch.mk
568     # export LIBS CFLAGS
569     log "Now make -f crunch.mk"
570     make -m ${SRC}/share/mk ${o_makeopts} -f ${BUILDDIR}/crunch.mk
571     strip --remove-section=.note --remove-section=.comment crunch1
572     mv crunch1 ${dst}/stand/crunch
573     chmod 555 ${dst}/stand/crunch
574     log "Making links for binaries..."
575     for i in `crunchgen -l $a` ; do
576         ln ${dst}/stand/crunch ${dst}/stand/${i};
577     done
578     # rm $a # do not remove!
579     ) || fail $? crunch
580
581     if [ -f ${dst}/stand/sshd ] ; then
582         log "Setting up host key for sshd:"
583         if [ -f ${BUILDDIR}/floppy.tree/etc/ssh_host_key.gz ] ; then
584             log "Using existing host key"
585         else
586             log "Generating new host key" 
587             ssh-keygen -t rsa1 -f ${BUILDDIR}/floppy.tree/etc/ssh_host_key \
588                  -N "" -C "root@picobsd"
589             gzip -9 ${BUILDDIR}/floppy.tree/etc/ssh_host_key* || true
590         fi
591     fi
592
593     log "Copy generic and site-specific MFS tree..."
594     for MFS_TREE in ${PICO_TREE}/mfs_tree ${MY_TREE}/mfs_tree ; do
595         if [ -d ${MFS_TREE} ] ; then
596             log "Copy ${MFS_TREE} ..."
597             (cd ${MFS_TREE} ; tar -cf - --exclude CVS . ) | \
598                     (cd ${dst} ; tar x${o_tarv}f - )
599         fi
600     done
601
602     if [ "${o_all_in_mfs}" = "yes" ]; then
603         log "Copy generic floppy_tree into MFS..."
604         # this may fail in case the floppy is empty
605         cp -Rp ${BUILDDIR}/floppy.tree/* ${dst}/fd || true
606     fi
607
608     if [ "${o_no_devfs}" != "" ] ; then
609         # create device entries using MAKEDEV
610         (cd ${dst}/dev
611         ln -s ${SRC}/etc/MAKEDEV ; chmod 555 MAKEDEV
612         # log `pwd`
613         sh ./MAKEDEV ${MY_DEVS}
614         rm MAKEDEV
615         )
616     fi
617     if [ "`id -u`" = "0" ] ; then
618         log "Fixing permissions"
619         (cd ${dst}; chown -R root . )
620     fi
621
622     if [ -n "${import_files}" ] ; then
623         log "importing ${import_files} into mfs"
624         # We do it in a chroot environment on the target so
625         # symlinks are followed correctly.
626         cp `which tar` ${dst}/my_copy_of_tar
627         (cd ${l_usrtree}/.. ; tar cf - ${import_files} ) | \
628             (chroot ${dst} /my_copy_of_tar xf - )
629         rm ${dst}/my_copy_of_tar
630     fi
631
632     (cd ${BUILDDIR}
633         # override the owner
634         echo "/set uid=0 gid=0" > mtree.out
635         mtree -c -p ${dst} -k "" >> mtree.out
636         log "mtre.out at ${BUILDDIR}/mtree.out"
637         makefs -t ffs -o bsize=4096 -o fsize=512 \
638                 -s ${MFS_SIZE}k -f 100 -F mtree.out ${c_fs} ${dst}
639         ls -l ${c_fs} )
640     log "done mfs image"
641 }
642
643 final_cleanup() {
644     log "final_cleanup()"
645     rm -rf ${c_mnt} ${c_reply} 2> /dev/null || true
646     rm -f ${c_reply}
647 }
648
649 # fail errno errcode
650 # This function is used to trap errors and print msgs
651 #
652 fail() {
653     local errno errocode where
654
655     errno=$1
656     errcode=$2
657     where=$3
658     echo "---> fail: Error <${errno}> error code <${errcode}> in <${where}>"
659     case ${errcode} in
660     mtree)
661         echo "Error while making hierarchy in ${c_mnt}"
662         ;;
663     crunch)
664         echo "Error while building ${name}."
665         ;;
666     missing_kernel)
667         echo "Error: you must build PICOBSD${suffix} kernel first"
668         ;;
669     includes)
670         echo "Error: failed while making includes"
671         ;;
672     libraries)
673         echo "Error: failed while making libraries"
674         ;;
675     bad_type)
676         echo "Error: unknown floppy type ${name}"
677         ;;
678     no_space)
679         echo "Error: no space left on device (${where})"
680         ;;
681     no_mfs)
682         echo "Error: while writing MFS into the kernel."
683         ;;
684     "")
685         echo "User break"
686         errcode="userbreak"
687         ;;
688     *)
689         echo "unknown error, maybe user break: $errno $errcode"
690         ;;
691     esac
692     echo "---> Aborting $0"
693     # try to cleanup the vnode.
694     final_cleanup
695     exit 2
696 }
697
698 fill_floppy_image() {
699     local blocks dst mfs_start mfs_end mfs_size img_size
700
701     log "fill_floppy_image()"
702     dst=${c_mnt}        # where to create the image
703
704     log "Preparing ${fd_size}kB floppy filesystem..."
705
706     # correct blocks according to size.
707     blocks=${fd_size};
708     if [ "${blocks}" = "1720" ]; then
709         blocks=1722
710     elif [ "${blocks}" = "1480" ]; then
711         blocks=1476
712     fi
713
714     log "Labeling floppy image"
715     log "patch ${c_boot2} to boot /kernel right away"
716     b2=${BUILDDIR}/boot2 # modified boot2
717     cp -f ${c_boot2} ${b2}
718     chmod 0644 ${b2}
719
720     set `strings -at d ${b2} | grep "/boot/loader"`
721     echo -e "/kernel\0\0\0\0\0" | \
722         dd of=${b2} obs=$1 oseek=1 conv=notrunc 2>/dev/null
723     chmod 0444 ${b2}
724
725     dst=${BUILDDIR}/image.tree
726     rm -rf ${dst}
727     mkdir -p ${dst}
728     (
729     cd ${BUILDDIR}
730     set 0 0 # reset variables
731     # $1 takes the offset of the MFS filesystem
732     set `strings -at d kernel | grep "MFS Filesystem goes here"`
733     mfs_start=$1
734     set 0 0 # reset variables
735     set `strings -at d kernel | grep "MFS Filesystem had better"`
736     mfs_end=$1
737     mfs_size="$((${mfs_end} - ${mfs_start}))"
738     set -- `ls -l ${c_fs}`; imgsize="$5"
739     if [ ${mfs_start} -gt 0 -a ${mfs_size} -ge ${imgsize} ] ; then
740         mfs_ofs=$((${mfs_start} + 8192))
741         log "Preload kernel with file ${c_fs} at ${mfs_ofs}"
742         dd if=${c_fs} ibs=8192 iseek=1 of=kernel obs=${mfs_ofs} \
743             oseek=1 conv=notrunc 2> /dev/null
744     else
745         log "not loading mfs, size ${mfs_size} img ${imgsize}"
746     fi
747     log "Compress with kgzip and copy to floppy image"
748     kgzip -o kernel.gz kernel
749     cp -p kernel.gz ${dst}/kernel || fail $? no_space "copying kernel"
750
751     log "Now transfer floppy tree if not already in MFS image"
752     # now transfer the floppy tree. If it is already in mfs, dont bother.
753     if [ "${o_all_in_mfs}" != "yes" ] ; then
754         cp -Rp floppy.tree/* ${dst} || \
755                 fail $? no_space "copying floppy tree"
756     fi
757     )
758     (cd ${BUILDDIR}
759     makefs -t ffs -o bsize=4096 -o fsize=512 \
760         -s ${blocks}k -f 50 ${c_img} ${dst}
761     # ${l_label} -f `pwd`/${c_img}
762     ${l_label} -w -f `pwd`/${c_img} auto # write in a label
763     # copy partition c: into a: with some sed magic
764     ${l_label} -f `pwd`/${c_img} | sed -e '/  c:/{p;s/c:/a:/;}' | \
765         ${l_label} -R -f `pwd`/${c_img} /dev/stdin
766     ${l_label} -f `pwd`/${c_img}
767     ls -l ${c_img}
768         logverbose "after disklabel"
769          )
770     # dump the primary and secondary boot
771     # XXX primary is 512 bytes
772     dd if=${c_boot1} of=${BUILDDIR}/${c_img} conv=notrunc 2>/dev/null
773     # XXX secondary starts after the 0x114 = dec 276 bytes of the label
774     # so we skip 276 from the source, and 276+512=788 from dst
775     # the old style blocks used 512 and 1024 respectively
776
777     dd if=${b2} iseek=1 ibs=276 2> /dev/null | \
778         dd of=${BUILDDIR}/${c_img} oseek=1 obs=788 conv=notrunc 2>/dev/null
779     logverbose "done floppy image"
780     # XXX (log "Fixing permissions"; cd ${dst}; chown -R root *)
781     rm -rf ${BUILDDIR}/floppy.tree || true # cleanup
782     # df -ik ${dst} | colrm 70 > .build.reply
783     rm -rf ${dst}
784     rm ${BUILDDIR}/kernel.gz ${BUILDDIR}/${c_fs}
785 }
786
787 # This function creates variables which depend on the source tree in use:
788 # SRC, l_usrtree, l_objtree
789 # Optionally creates libraries, includes and the like (for cross compiles,
790 # needs to be done once).
791
792 set_build_parameters() {
793     log "set_build_parameters() SRC is ${SRC}"
794     if [ "${SRC}" = "/usr/src" ] ; then
795         l_usrtree=${USR:-/usr}
796     else
797         l_usrtree=${USR:-${SRC}/../usr}
798     fi
799     l_objtree=${l_usrtree}/obj-pico
800     PICO_TREE=${PICO_TREE:-${SRC}/release/picobsd}
801     set `grep "#define[\t ]__FreeBSD_version" ${SRC}/sys/sys/param.h`
802     OSVERSION=$3
803     log "OSVERSION is ${OSVERSION}"
804     if [ "${o_init_src}" != "" ] ; then
805         if [ ${OSVERSION} -lt 500035 ] ; then
806             create_includes_and_libraries
807         else
808             create_includes_and_libraries2
809         fi
810     fi
811     if [ ${OSVERSION} -lt 500035 ] ; then
812         # Create the right LIBS and CFLAGS for further builds.
813         # and build the config program
814         LIBS="-L${l_usrtree}/lib"
815         CFLAGS="-nostdinc -I${l_usrtree}/include"
816         export LIBS CFLAGS
817         CONFIG=${l_usrtree}/sbin/config
818         export CONFIG
819     fi
820 }
821
822 #-------------------------------------------------------------------
823 # Main entry of the script. Initialize variables, parse command line
824 # arguments.
825
826 set_defaults
827 while [ true ]; do
828     case $1 in
829     --src)      # set the source path instead of /usr/src
830         SRC=`(cd $2; pwd)`
831         shift
832         ;;
833     --init)
834         o_init_src="YES"
835         ;;
836
837     --floppy_size)
838         fd_size=$2
839         shift
840         ;;
841
842     --all_in_mfs)
843         o_all_in_mfs="yes"
844         ;;
845
846     --no_all_in_mfs)
847         o_all_in_mfs=""
848         ;;
849
850     --modules)  # also build kernel modules
851         o_do_modules="yes"
852         ;;
853     -n)
854         o_interactive="NO"
855         ;;
856
857     -clear|-clean|-c) # clean
858         o_clean="YES"
859         o_interactive="NO"
860         ;;
861
862     -v) # need -v -v to wait for user input
863         o_verbose=$((${o_verbose}+1))   # verbose level
864         o_tarv="v"                      # tar verbose flag
865         o_makeopts="-d l" # be verbose
866         ;;
867     *)
868         break ;
869         ;;
870
871     esac
872     shift
873 done
874 set_build_parameters    # things that depend on ${SRC}
875 set_type $1 $2          # type and site, respectively
876
877 # If $1="package", it creates a neat set of floppies
878
879 if [ "$1" = "package" ] ; then
880     build_package
881 fi
882 if [ "${o_interactive}" != "NO" ] ; then
883     main_dialog
884 fi
885 if [ "${o_clean}" = "YES" ] ; then
886     clean_tree
887 else
888     build_image
889     do_install
890 fi
891 final_cleanup
892 exit 0