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