File:  [ELWIX - Embedded LightWeight unIX -] / elwix / build_rootfs.sh
Revision 1.12: download - view: text, annotated - select for diffs - revision graph
Fri Jul 16 18:35:50 2021 UTC (2 years, 9 months ago) by misho
Branches: MAIN
CVS tags: elwix2_8, HEAD, ELWIX2_7
ver 2.7

#!/bin/sh
#
# ELWIX build script for rootfs firmware
#
# (C) AITNET ltd - Sofia/Bulgaria <office@aitnet.org>
#    by Michael Pounov <misho@elwix.org>
#
# $Id: build_rootfs.sh,v 1.12 2021/07/16 18:35:50 misho Exp $
#

[ -r ./config/rc.elwix ] && . ./config/rc.elwix
if [ X"${ELWIX_IS_CONFIGURED}" != X"YES" ]; then
	echo
	echo "ELWIX project is not configured !?!"
	echo
	exit 1
fi

if [ ! -d ${BSDSRC} ]; then
	echo
	echo "ELWIX project is not configured - missing BSD sources !?!"
	echo
	exit 1
fi

if [ "${USER}" != "root" ]; then
	echo
	echo "ELWIX project is not configured - RootFS must build under root !?!"
	echo
	exit 1
fi

if [ ! -x "${OBJDUMP}" ]; then
	echo
	echo "ELWIX project is not configured - executable objdump not found !"
	echo
	exit 1
fi

KRNNAME="kernel_rootfs"
NEWKERN="${REL}/${KRNNAME}"
MKF="Makefile.elwix"
MKH="Makefile.inc"

# Set TARGET_CPUTYPE only for world compiling. Don't use this in kernel build!
X_TARGET_CPUTYPE="TARGET_CPUTYPE=${TARGET_CPUTYPE}"

clean_elwix()
{
	echo "+++ Clean elwix binary from ELWIX World ... $BSDOBJ/elwix"
	rm -rf $BSDOBJ/elwix

	echo "+++ Clean ELWIX RootFS firmware ... $ROOTFS_MKDIR"
	rm -rf $ROOTFS_MKDIR
	mkdir $ROOTFS_MKDIR

	echo "+++ Clean RootFS directory ... $ROOTFS"
	chflags -R noschg $ROOTFS
	rm -rf $ROOTFS
	mkdir $ROOTFS

	echo "+++ Clean Release directory ..."
	rm -rf $REL/$ROOTFS_IMG ${NEWKERN}*
}

prepare_elwix()
{
	echo "+++ Prepair ELWIX RootFS firmware ... $ROOTFS_MKDIR"
	cd $ROOTFS_MKDIR
	cat >$MKH <<_EOF
# Auto-generated Makefile include for crungegen
# !!! Dont edit by hand !!!
#

LDFLAGS += -L${WORLD}/lib -L${WORLD}/usr/lib
CFLAGS += -I${BSDSRC}/lib/libifconfig -I${BSDOBJ}${BSDSRC}/${TARGET}.${TARGET_ARCH}/lib/libifconfig
_EOF
	cat >$ROOTFS_CONF <<_EOF
srcdirs ${BSDSRC}/tools/tools ${BSDSRC}/tools/tools/ath
srcdirs ${BSDSRC}/contrib/ncurses/progs ${BSDSRC}/lib/libifconfig
srcdirs ${BSDSRC}/bin ${BSDSRC}/sbin ${BSDSRC}/libexec ${BSDSRC}/cddl/sbin ${BSDSRC}/sbin/ggate
srcdirs ${BSDSRC}/usr.bin ${BSDSRC}/usr.sbin ${BSDSRC}/usr.sbin/wpa
srcdirs ${BSDSRC}/secure/usr.bin ${BSDSRC}/secure/usr.sbin ${BSDSRC}/secure/libexec

#buildopts TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} ${X_TARGET_CPUTYPE} __MAKE_CONF=${MAKECONF}
buildopts -DNO_PAM -DNO_KERBEROS WITHOUT_PAM_SUPPORT=yes WITHOUT_KERBEROS_SUPPORT=yes WITHOUT_KERBEROS=yes
buildopts -DMK_CASPER=no WITHOUT_CASPER=yes MK_TESTS=no UPDATE_DEPENDFILE=yes _RECURSING_CRUNCH=1
_EOF
	cat $CFGDIR/$ROOTFS_CONF >>$ROOTFS_CONF
	cat >>$ROOTFS_CONF <<_EOF
special ping6 srcdir ${BSDSRC}/sbin/ping
special tput srcdir ${BSDSRC}/contrib/ncurses/progs
special tset srcdir ${BSDSRC}/contrib/ncurses/progs

special ifconfig lib ${BSDOBJ}${BSDSRC}/${TARGET}.${TARGET_ARCH}/lib/libifconfig/libifconfig.a
_EOF
	echo "+++ Prepared RootFS config ... $CFGDIR/$ROOTFS_CONF"

	install -d ${BSDOBJ}/elwix
 	env MAKEOBJDIRPREFIX=${BSDOBJ}/elwix ${CEXEC} crunchgen -f \
		-h $ROOTFS_MKDIR/$MKH -m $MKF \
		-c elwix.c -e elwix $ROOTFS_CONF
	if [ $? -ne 0 ]; then
		exit 1
	fi
#	cat >>$MKF << _EOF
#
#.for _tool in ${CRUNCH_BUILDTOOLS}
#build-tools-${_tool}:
#	${_+_}cd ${.CURDIR}/../../${_tool}; \
#	    if [ "${MK_AUTO_OBJ}" = "no" ]; then \
#	        ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} obj; \
#	    fi; \
#	    ${CRUNCHENV} MAKEOBJDIRPREFIX=${CRUNCHOBJS} ${MAKE} build-tools
#
#build-tools: build-tools-${_tool}
#	/usr/bin ${CFLAGS} ${LDFLAGS} ${.IMPSRC} ${LDLIBS} -o ${.TARGET}
#
#.endfor
#_EOF
	cd -
}

build_elwix()
{
	echo "+++ Build ELWIX RootFS firmware ... $ROOTFS_MKDIR"
	cd $ROOTFS_MKDIR
	${CBUILD} -f $MKF \
		TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
		${X_TARGET_CPUTYPE} __MAKE_CONF=${MAKECONF}
	if [ $? -ne 0 ]; then
		exit 1
	fi
	cd -
}

build_rootdir()
{
	echo "+++ Build RootFS directory ... $ROOTFS"
	cd $ROOTFS

	install -d bin;
	install -d dev;
	install -d elwix;
	install -d etc;
	install -d libexec;
	install -d mnt;
	install -d -m 0555 proc;
	install -d rescue;
	install -d sbin;
	install -d -m 1777 tmp;
	install -d usr;
	install -d usr/bin;
	install -d usr/lib;
	install -d usr/libexec;
	install -d usr/sbin;
	install -d var;
	install -d var/sys;

	ln -fs /usr/lib lib;

	ln -fs /elwix/boot boot;
	ln -fs /elwix/exports exports;
	ln -fs /elwix/home home;
	ln -fs /elwix/root root;
	ln -fs /elwix/tftpboot tftpboot;

	cd -
}

install_rootfs()
{
	BIN="${ROOTFS}/bin"
	SBIN="${ROOTFS}/sbin"
	UBIN="${ROOTFS}/usr/bin"
	LIBEXEC="${ROOTFS}/usr/libexec"
	ETC="${ROOTFS}/etc"
	RESCUE="${ROOTFS}/rescue"

	PRG="/sbin/elwix"
	LNPRG="${SBIN}/elwix"

	echo "+++ Install ELWIX into RootFS directory ... $ROOTFS"
	install $ROOTFS_MKDIR/elwix $SBIN

	InstallList ${ELWIX_ROOTFS} ${WORLD} ${ROOTFS}

	InstallDir ${ROOTFS_ETC} $ETC

#	ln -s /usr/sbin/nologin $SBIN

	ln -s $PRG $BIN/test
	ln -s $PRG $BIN/\[
	ln -s $PRG $BIN/cat
	ln -s $PRG $BIN/chflags
	ln -s $PRG $BIN/chio
	ln -s $PRG $BIN/chmod
	ln -s $PRG $BIN/cp
	ln -s $PRG $RESCUE/cp
	ln -s $PRG $BIN/csh
	ln -s $PRG $BIN/-csh
	ln -s $PRG $RESCUE/csh
	ln -s $PRG $BIN/tcsh
	ln -s $PRG $BIN/-tcsh
	ln -s $PRG $BIN/date
	ln -s $PRG $BIN/dd
	ln -s $PRG $BIN/df
	ln -s $PRG $BIN/domainname
	ln -s $PRG $BIN/echo
	ln -s $PRG $BIN/expr
	ln -s $PRG $BIN/hostname
	ln -s $PRG $BIN/kenv
	ln -s $PRG $BIN/kill
	ln -s $PRG $BIN/ln
	ln -s $PRG $BIN/link
	ln -s $PRG $BIN/ls
	ln -s $PRG $BIN/mkdir
	ln -s $PRG $BIN/mv
	ln -s $PRG $BIN/pax
	ln -s $PRG $BIN/pkill
	ln -s $PRG $BIN/pgrep
	ln -s $PRG $BIN/sh
	ln -s $PRG $BIN/-sh
	ln -s $PRG $RESCUE/sh
	ln -s $PRG $BIN/ps
	ln -s $PRG $BIN/pwait
	ln -s $PRG $BIN/pwd
	ln -s $PRG $BIN/realpath
	ln -s $PRG $BIN/rm
	ln -s $PRG $RESCUE/rm
	ln -s $PRG $BIN/unlink
#	ln -s $PRG $BIN/rmdir
	ln -s $PRG $BIN/sleep
	ln -s $PRG $BIN/stty
	ln -s $PRG $BIN/sync
#	ln -s $PRG $BIN/uuidgen

	ln -s $PRG $BIN/true
	ln -s $PRG $BIN/false
	ln -s $PRG $BIN/basename
	ln -s $PRG $BIN/tail
	ln -s $PRG $BIN/nc
	ln -s $PRG $BIN/w
	ln -s $PRG $BIN/head
	ln -s $PRG $BIN/uname
	ln -s $PRG $BIN/tset
	ln -s $PRG $BIN/vmstat
	ln -s $PRG $BIN/systat
	ln -s $PRG $BIN/tput
	ln -s $PRG $BIN/grep
	ln -s $PRG $BIN/awk
	ln -s $PRG $BIN/less
	ln -s $PRG $BIN/gzip
	ln -s $PRG $BIN/cap_mkdb
	ln -s $PRG $BIN/sed
	ln -s $PRG $BIN/vi
	ln -s $PRG $RESCUE/vi
	ln -s $PRG $BIN/du
	ln -s $PRG $BIN/getopt
	ln -s $PRG $BIN/logger
	ln -s $PRG $BIN/nice
	ln -s $PRG $BIN/nohup
	ln -s $PRG $BIN/seq
	ln -s $PRG $BIN/sockstat
	ln -s $PRG $BIN/stat
	ln -s $PRG $BIN/tee
	ln -s $PRG $BIN/tftp
	ln -s $PRG $BIN/touch
	ln -s $PRG $BIN/wc
	ln -s $PRG $BIN/xargs
	ln -s $PRG $BIN/printenv
	ln -s $PRG $BIN/egrep
	ln -s $PRG $BIN/fgrep
	ln -s $PRG $BIN/tar
	ln -s $PRG $BIN/cpio
	ln -s $PRG $BIN/nawk
	ln -s $PRG $BIN/gunzip
	ln -s $PRG $BIN/gzcat
	ln -s $PRG $BIN/zcat
	ln -s $PRG $BIN/more
	ln -s $PRG $BIN/reset
	ln -s $PRG $BIN/readlink
	ln -s $PRG $BIN/uptime
	ln -s $PRG $BIN/bzip2
	ln -s $PRG $BIN/bunzip2
	ln -s $PRG $BIN/bzcat

	ln -s $PRG $SBIN/bsdlabel
	ln -s $PRG $SBIN/disklabel
#	ln -s $PRG $SBIN/camcontrol
#	ln -s $PRG $SBIN/comcontrol
#	ln -s $PRG $SBIN/conscontrol
	ln -s $PRG $SBIN/devfs
	ln -s $PRG $SBIN/dhclient
	ln -s $PRG $SBIN/dmesg
	ln -s $PRG $SBIN/dump
	ln -s $PRG $SBIN/rdump
	ln -s $PRG $SBIN/dumpon
	ln -s $PRG $SBIN/fdisk
	ln -s $PRG $SBIN/fsck
	ln -s $PRG $RESCUE/fsck
	ln -s $PRG $SBIN/fsck_ffs
	ln -s $PRG $SBIN/fsck_ufs
	ln -s $PRG $SBIN/fsck_4.2bsd
	ln -s $PRG $SBIN/fsck_msdosfs
	ln -s $PRG $SBIN/etherswitchcfg
#	ln -s $PRG $SBIN/-geom
#	ln -s $PRG $SBIN/glabel
#	ln -s $PRG $SBIN/gpart
#	ln -s $PRG $SBIN/graid
#	ln -s $PRG $SBIN/growfs
#	ln -s $PRG $SBIN/hastctl
#	ln -s $PRG $SBIN/hastd
	ln -s $PRG $SBIN/ifconfig
	ln -s $PRG $RESCUE/ifconfig
	ln -s $PRG $SBIN/init
	ln -s $PRG $SBIN/ipfw
	ln -s $PRG $SBIN/iscontrol
	ln -s $PRG $SBIN/kldconfig
	ln -s $PRG $SBIN/kldload
	ln -s $PRG $SBIN/kldstat
	ln -s $PRG $SBIN/kldunload
	ln -s $PRG $SBIN/ldconfig
	ln -s $PRG $SBIN/md5
	ln -s $PRG $SBIN/sha1
	ln -s $PRG $SBIN/sha256
	ln -s $PRG $SBIN/rmd160
	ln -s $PRG $SBIN/mdconfig
	ln -s $PRG $RESCUE/mdconfig
	ln -s $PRG $SBIN/mdmfs
	ln -s $PRG $SBIN/mount_mfs
	ln -s $PRG $SBIN/mknod
#	ln -s $PRG $SBIN/mksnap_ffs
	ln -s $PRG $SBIN/mount
	ln -s $PRG $RESCUE/mount
#	ln -s $PRG $SBIN/mount_cd9660
	ln -s $PRG $SBIN/mount_msdosfs
	ln -s $PRG $SBIN/mount_nfs
#	ln -s $PRG $SBIN/mount_ntfs
	ln -s $PRG $SBIN/mount_nullfs
#	ln -s $PRG $SBIN/mount_udf
	ln -s $PRG $SBIN/mount_unionfs
	ln -s $PRG $SBIN/newfs
	ln -s $PRG $RESCUE/newfs
	ln -s $PRG $SBIN/newfs_msdos
#	ln -s $PRG $SBIN/nfsiod
#	ln -s $PRG $SBIN/nos-tun
#	ln -s $PRG $SBIN/nvmecontrol
	ln -s $PRG $SBIN/pfctl
	ln -s $PRG $SBIN/pflogd
	ln -s $PRG $SBIN/quotacheck
	ln -s $PRG $SBIN/reboot
	ln -s $PRG $RESCUE/reboot
	ln -s $PRG $SBIN/halt
	ln -s $PRG $RESCUE/halt
	ln -s $PRG $SBIN/fastboot
	ln -s $PRG $SBIN/fasthalt
#	ln -s $PRG $SBIN/recoverdisk
	ln -s $PRG $SBIN/restore
	ln -s $PRG $SBIN/rrestore
	ln -s $PRG $SBIN/route
	ln -s $PRG $SBIN/rtsol
	ln -s $PRG $SBIN/shutdown
#	ln -s $PRG $SBIN/savecore
#	ln -s $PRG $SBIN/swapon
#	ln -s $PRG $SBIN/swapoff
#	ln -s $PRG $SBIN/swapctl
	ln -s $PRG $SBIN/sysctl
	ln -s $PRG $SBIN/tunefs
	ln -s $PRG $SBIN/umount
	ln -s $PRG $SBIN/zfs
	ln -s $PRG $SBIN/zpool

	ln -s $PRG $SBIN/ping
	ln -s $PRG $SBIN/ping6
	ln -s $PRG $SBIN/traceroute
	ln -s $PRG $SBIN/traceroute6

	ln -s $PRG $SBIN/chown
	ln -s $PRG $SBIN/pwd_mkdb
	ln -s $PRG $SBIN/arp
	ln -s $PRG $SBIN/gpioctl
	ln -s $PRG $SBIN/hostapd
	ln -s $PRG $SBIN/hostapd_cli
	ln -s $PRG $SBIN/wpa_supplicant
	ln -s $PRG $SBIN/wpa_cli
	ln -s $PRG $SBIN/wpa_passphrase
	ln -s $PRG $SBIN/wake
	ln -s $PRG $SBIN/watchdog
	ln -s $PRG $SBIN/watchdogd
#	ln -s $PRG $SBIN/inetd
	ln -s $PRG $SBIN/wlandebug
	ln -s $PRG $SBIN/chroot
	ln -s $PRG $SBIN/i2c
	ln -s $PRG $SBIN/iostat
#	ln -s $PRG $SBIN/jail
#	ln -s $PRG $SBIN/jexec
#	ln -s $PRG $SBIN/jls
	ln -s $PRG $SBIN/ndp
	ln -s $PRG $SBIN/newsyslog
	ln -s $PRG $SBIN/nologin
	ln -s $PRG $SBIN/pstat
	ln -s $PRG $SBIN/rtadvctl
	ln -s $PRG $SBIN/rtadvd
	ln -s $PRG $SBIN/rtsold
	ln -s $PRG $SBIN/syslogd
	ln -s $PRG $SBIN/setfib
	ln -s $PRG $SBIN/swapinfo
	ln -s $PRG $SBIN/chgrp

	ln -s $PRG $UBIN/login
	ln -s $PRG $UBIN/passwd

	ln -s $PRG $LIBEXEC/getty
#	ln -s $PRG $LIBEXEC/tftpd

	strip -s $LNPRG

	chown -hR root:wheel $ROOTFS

	echo "+++ ELWIX clean CVS tags ..."
	CleanCVS $ETC
	echo
}

build_image()
{
	echo "+++ Build ELWIX RootFS image ... $ROOTFS --> $REL/$ROOTFS_IMG"
	makefs -s ${ROOTFS_SIZE} -o minfree=0 $REL/$ROOTFS_IMG $ROOTFS || exit 1
	echo "+++ Completed ELWIX RootFS image ... $ROOTFS --> $REL/$ROOTFS_IMG"
}

install_image()
{
	echo "+++ Install ELWIX RootFS Image into kernel ... $REL/$ROOTFS_IMG"
	echo "exec> stat -f '%z' $REL/$ROOTFS_IMG"
	mfs_size=`stat -f '%z' $REL/$ROOTFS_IMG 2>/dev/null`
	[ -z ${mfs_size} ] && echo "Can't determine MFS image size" && exit 1

	echo "exec> ${OBJDUMP} -h $KERNEL_ROOTFS 2> /dev/null | grep \" oldmfs \""
	sec_info=`${OBJDUMP} -h $KERNEL_ROOTFS 2> /dev/null | grep " oldmfs "`
	[ -z "${sec_info}" ] && echo "Can't locate mfs section within kernel" && exit 1
	echo "'-> Object dump { ${sec_info} }"

	sec_size=`echo ${sec_info} | awk '{printf("%d", "0x" $3)}' 2> /dev/null`
	sec_start=`echo ${sec_info} | awk '{printf("%d", "0x" $6)}' 2> /dev/null`
	echo "'-> Start offset from $sec_start and rootfs size $sec_size byte"
	[ ${sec_size} -lt ${mfs_size} ] && echo "MFS image too large" && exit 1

	cp $KERNEL_ROOTFS $NEWKERN
	dd if=$REL/$ROOTFS_IMG ibs=8192 of=$NEWKERN obs=${sec_start} oseek=1 conv=notrunc \
		2>/dev/null && echo "'-> Completed MFS image embedded into kernel"

###	START=`strings -td $KERNEL_ROOTFS | grep "MFS Filesystem" | cut -d ' ' -f 1 | head -1`
###	STOP=`strings -td $KERNEL_ROOTFS | grep "MFS Filesystem" | cut -d ' ' -f 1 | tail -1`
###	STOP=$(($STOP + 1))
###	echo "'-> Start offset from $START to $STOP byte"

###	head -c $START $KERNEL_ROOTFS >$NEWKERN
###	cat $REL/$ROOTFS_IMG >>$NEWKERN
###	tail -c +$STOP $KERNEL_ROOTFS >>$NEWKERN

	echo "'-> Compress kernel"
	cd $REL
	gzip -9 $KRNNAME
	cd -

	echo "+++ Completed ELWIX RootFS kernel ... $NEWKERN.gz"
}

### main()
if SnitVer ${WORLD}/elwix.ver; then
	clean_elwix
	prepare_elwix || exit 1
	build_elwix || exit 1

	build_rootdir || exit 1
	install_rootfs || exit 1

	build_image || exit 1
	install_image || exit 1
else
	echo "$? Skipped ELWIX RootFS build ..."
fi

echo "+++ Complete build ELWIX RootFS"

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>