File:  [ELWIX - Embedded LightWeight unIX -] / elwix / config / rc.subr
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

#
# ELWIX project build helper subroutines
#
# (C) AITNET ltd - Sofia/Bulgaria <office@aitnet.org>
#    by Michael Pounov <misho@elwix.org>
#
# $Id: rc.subr,v 1.12 2021/07/16 18:35:50 misho Exp $
#

### Dont edit this file !!!

Die() {
	echo -n "EXIT: " >&2; echo "$@" >&2
	exit 1
}

CheckVer()
{
	[ -z "$1" -o ! -r "$1" ] && return 1

	cat $1 | awk '($1 == "ELWIX") { split($3, arr, ":"); printf("%s", arr[2]); }'
	return 0
}

SnitVer()
{
	install -d ${TOPDIR}

	V=$(CheckVer $1)

	if [ $? -ne 0 ]; then
# file not found
		return 1
	elif [ X"$V" != X"${VERSION}" ]; then
# different ELWIX version
		return 2
	fi

# already present file with same version
	return 0
}

Create_etc_motd()
{
	ELWIX_VER="ELWIX-${VERSION}_$2_${TARGET_ARCH}";
	ELWIX_HDR_MOTD="${ELWIX_VER} :: Build date ${BUILD_DATE}";

	awk -v fo="${1:-motd}" 'BEGIN { print "'"${ELWIX_HDR_MOTD}"'" > fo; } { print $0 >> fo; }' \
		   ${CFGDIR}/elwix_signature.txt;
}

# $1 = fs/elwix
# $2 = config/etc/default
MakeFS_var()
{
	cd $1

	install -d altroot;

	install -d altroot/pkg;
	install -d altroot/pkg/db;
	install -d altroot/pkg/keys;
	install -d altroot/pkg/keys/revoked;
	install -d altroot/pkg/keys/trusted;

	install -d altroot/var;
	install -d altroot/var/account;
	install -d altroot/var/at;
	install -d altroot/var/at/jobs;
	install -d altroot/var/at/spool;
	install -d -g 63 -m 0770 altroot/var/authpf;
	install -d altroot/var/backups;
	install -d -m 0750 altroot/var/cron;
	install -d -m 0700 altroot/var/cron/tabs;
	install -d altroot/var/db;
	install -d altroot/var/db/pkg;
	install -d -m 0555 altroot/var/empty;
	install -d altroot/var/log;
	install -d -o 80 -g 80 -m 0700 altroot/var/log/lighttpd;
	install -d -o 101 -g 101 altroot/var/log/quagga;
	install -d -o 883 -g 883 altroot/var/mqtt;
	install -d altroot/var/frm;
	install -d altroot/var/run;
	install -d -o 80 -g 80 altroot/var/run/lighttpd;
	install -d -o 101 -g 101 altroot/var/run/quagga;
	install -d altroot/var/spool;
	install -d -o 66 -g 68 -m 0775 altroot/var/spool/lock;
	install -d -o 80 -g 80 -m 0775 altroot/var/spool/lighttpd;
	install -d -o 80 -g 80 altroot/var/spool/lighttpd/sockets;
	install -d -g 1 altroot/var/spool/lpd;
	install -d -g 1 altroot/var/spool/output;
	install -d -g 1 altroot/var/spool/output/lpd;
	install -d -o 59 -g 59 altroot/var/unbound;

	ln -fs /tmp altroot/var/tmp;

	touch altroot/var/run/utmp

	touch altroot/var/account/acct

	touch altroot/var/log/elwix
	touch altroot/var/log/lastlog
	touch altroot/var/log/wifi.log
	touch altroot/var/log/utx.log

	touch altroot/var/log/auth.log
	touch altroot/var/log/cron
	touch altroot/var/log/console.log
	touch altroot/var/log/debug.log
	touch altroot/var/log/init.log
	touch altroot/var/log/lpd-errs
	touch altroot/var/log/maillog
	touch altroot/var/log/messages
	touch altroot/var/log/security
	touch altroot/var/log/xferlog
	touch altroot/var/log/ppp.log
	chgrp 69 altroot/var/log/ppp.log

#	install -v $2/resolv.safe altroot/var/run/resolv.conf
	install -v ${WORLD}/usr/share/keys/pkg/trusted/* altroot/pkg/keys/trusted

	cd -
}

# $1 = fs/elwix
# $2 = config/etc/default
# $3 = name
MakeFS_etc()
{
	InstallDir $2 $1/etc

	chown -R root:wheel $1/etc;

	case "$3" in
		uboot)
			;;
		*)
#			chmod 0600 $1/etc/opieaccess;
#			chmod 0600 $1/etc/opiekeys;
			;;
	esac

	chmod 0600 $1/etc/master.passwd;
	chmod 0600 $1/etc/spwd.db;
	chmod 0600 $1/etc/ssh/ssh_host_dsa_key;
	chmod 0600 $1/etc/ssh/ssh_host_key;
	chmod 0600 $1/etc/ssh/ssh_host_rsa_key;
	chmod 0600 $1/etc/ssh/ssh_host_ecdsa_key;
	chmod 0600 $1/etc/ssh/ssh_host_ed25519_key;

#	chmod 0440 $1/etc/sudoers;

	mkdir -p $1/etc/mqtt;
	chgrp -R 883 $1/etc/mqtt;
	chown -R 883 $1/etc/mqtt;

	ln -sf /etc/rc.s $1/etc/rc.S

	ln -sf /usr/share/zoneinfo/${TIMEZONE} $1/etc/localtime;
	ln -sf /usr/share/misc/termcap $1/etc/termcap;

#	ln -sf /var/run/resolv.conf $1/etc/resolv.conf;

#	ln -sf /elwix/pkg/etc/mk.conf $1/etc/mk.conf;

	echo "${TARGET_ARCH} $3" >$1/etc/platform;

	Create_etc_motd $1/etc/motd $3;

	CleanCVS $1
	CleanCVS $1/etc
}

# $1 = fs/elwix
MakeFS_home()
{
	install -d $1/root/.ssh
	[ -r ${CFGDIR}/root.skel/ssh/authorized_keys2 ] && \
		install -v ${CFGDIR}/root.skel/ssh/authorized_keys2 $1/root/.ssh;

	install -v ${CFGDIR}/root.skel/cshrc $1/root/.cshrc;
	install -v ${CFGDIR}/root.skel/login $1/root/.login;
	install -v ${CFGDIR}/root.skel/logout $1/root/.logout;
	install -v ${CFGDIR}/root.skel/profile $1/root/.profile;
	install -v ${CFGDIR}/root.skel/vimrc $1/root/.vimrc;
}

# $1 = fs
MakeFS_boot()
{
	InstallDir ${WORLD}/boot $1/boot

	install -m 644 ${CFGDIR}/boot/boot.config $1;

	case ${TARGET} in
		i386|amd64)
			install -m 444 ${CFGDIR}/boot/logo-elwix.4th $1/boot;
			install -m 444 ${CFGDIR}/boot/brand-elwix.4th $1/boot;
			install -m 444 ${CFGDIR}/boot/menu.4th $1/boot;
			install -m 444 ${CFGDIR}/boot/logo-elwix.lua $1/boot/lua;
			install -m 444 ${CFGDIR}/boot/brand-elwix.lua $1/boot/lua;
			;;
		*)
			;;
	esac

	install -m 444 ${KERNDEVHINTS} $1/boot/device.hints;

	if [ -r ${CFGDIR}/boot/loader_${TARGET_ARCH}.conf ]; then
		install -m 644 ${CFGDIR}/boot/loader_${TARGET_ARCH}.conf $1/boot/loader.conf;
		install -m 644 ${CFGDIR}/boot/loader.conf.local $1/boot/loader.conf.local;
	fi;
}

# $1 = ELWIX install list config
# $2 = From World dir
# $3 = To Dest dir
InstallList()
{
	local TMPFILE=$(mktemp -q /tmp/instlist.XXXXXX)
	if [ $? -ne 0 ]; then
		echo "Error:: Can't create temp file, exiting..."
		exit 1
	fi

	sed -nE -e 's/^ *([^ #]+) */\1/p' < $1 | tr ':' '\n' >$TMPFILE
	cd $2
	for i in $(cat $TMPFILE); do
		if [ ! -f $i ]; then
			rm -f $TMPFILE
			echo "Error:: File not found $i"
			exit 1
		fi
		if [ -x $i ]; then
			echo "Strip '$i'"
			${CEXEC} chflags -f noschg $i
			${CEXEC} strip -s $i
		fi
	done
	cd -
	tar -C $2 -cvf - -T $TMPFILE | tar -C $3 -x -f -
	rm -f $TMPFILE
}

# $1 = From dir
# $2 = To dir
InstallDir()
{
	cd $1
	pax -rw -pe -X . $2
	cd -
}

# $* = Program arguments for install tool
InstallProg()
{
	install -c $*
}

# $1 = file_pattern
# $2 = From dir
# $3 = To dir
InstallFiles()
{
	if [ -d "$2" ]; then
		cd $2
		find . -type f -name "$1" | 
			tar -cvf - -T - | tar -C $3 -xf -
		cd -
	fi
}

# $1 = From dir
# $2 = To dir
InstallLibs()
{
	${CEXEC} find -X $2 ! -name "*.ko" -and -type f | \
		xargs readelf -d 2>&- | grep "NEEDED" | sort | uniq | \
		awk '($2 == "(NEEDED)") { print substr($5, 2, length($5) - 2); }' \
		2>/dev/null >${DEST}/elwix_mklibs.tmp
	for i in $(cat ${DEST}/elwix_mklibs.tmp); do
		echo $i
		${CEXEC} find $1 -type f -name $i -exec install -v {} $LIBS \\\;
		${CEXEC} chflags -f noschg $LIBS/$i
		${CEXEC} strip -s $LIBS/$i
	done
#	${CEXEC} find -X $2 ! -name "*.ko" -and -type f -and -perm +111 | \
#		xargs ldd -f "%p\n" 2> /dev/null | sort | uniq | \
#		sed 's|^/||' >${DEST}/elwix_mklibs.conf
#	InstallList ${DEST}/elwix_mklibs.conf $1 $2
}

# $1 = Directory for clean
CleanCVS()
{
	find $1 -type d -name CVS -exec rm -rf {} \; >/dev/null 2>&1
	find $1 -type f -name .#\* -exec rm -f {} \; >/dev/null 2>&1
}

# $1 = file_pattern
# $2 = From dir
StripFiles()
{
	if [ -d "$2" ]; then
		local TMPFILE=$(mktemp -q /tmp/instlist.XXXXXX)
		if [ $? -ne 0 ]; then
			echo "Error:: Can't create temp file, exiting..."
			exit 1
		fi

		cd $2
		find . -type f -name "$1" >$TMPFILE
		for i in $(cat $TMPFILE); do
			if [ -x $i ]; then
				echo "Strip '$i'"
				${CEXEC} chflags -f noschg $i
				${CEXEC} strip -s $i
			fi
		done
		cd -
		rm -f $TMPFILE
	fi
}

# $1 = file_pattern
# $2 = From dir
# $3 = To dir
InstallLinks()
{
	if [ -d "$2" ]; then
		cd $2
		find . -type l -name "$1" | 
			tar -cvf - -T - | tar -C $3 -xf -
		cd -
	fi
}

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