xref: /freebsd/tools/tools/nanobsd/dhcpd/common (revision d0b2dbfa)
1
2#-
3# Copyright (c) 2014 M. Warner Losh <imp@FreeBSD.org>
4# Copyright (c) 2010 iXsystems, Inc.
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 iXsystems, Inc 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#
28# This file is heavily derived from both Sam Leffler's Avilia config,
29# as well as the BSDRP project's config file.  Neither of these have
30# an explicit copyright/license statement, but are implicitly BSDL. This
31# example has been taken from the FreeNAS project (an early version) and
32# simplified to meet the needs of the example.
33#
34
35# NB: You want the other file
36
37NANO_PMAKE="make -j $(sysctl -n hw.ncpu)"
38
39NANO_CFG_BASE=$(pwd)
40NANO_CFG_BASE=${NANO_CFG_BASE%/dhcpd}
41NANO_SRC=$(pwd)
42NANO_SRC=${NANO_SRC%/tools/tools/nanobsd/dhcpd}
43NANO_OBJ=${NANO_SRC}/../nanobsd-builds/dhcpd/obj
44# Where cust_pkg() finds packages to install
45#XXX: Is this the right place?
46NANO_PORTS=$(realpath ${NANO_SRC}/../ports)
47#NANO_PORTS=/usr/ports
48NANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg
49NANO_DATADIR=${NANO_OBJ}/_.data
50NANO_DATASIZE=40960
51NANO_INIT_IMG2=0
52unset MAKEOBJDIRPREFIX
53
54# this to go into nanobsd.sh
55NANO_PORTS=${NANO_PORTS:-/usr/ports}
56
57customize_cmd cust_allow_ssh_root
58
59add_etc_make_conf()
60{
61	touch ${NANO_WORLDDIR}/etc/make.conf
62}
63customize_cmd add_etc_make_conf
64
65clean_usr_local()
66{
67	LOCAL_DIR=${NANO_WORLDDIR}/usr/local
68	pprint 2 "Clean and create world directory (${LOCAL_DIR})"
69	if rm -rf ${LOCAL_DIR}/ > /dev/null 2>&1 ; then
70		true
71	else
72		chflags -R noschg ${LOCAL_DIR}/
73		rm -rf ${LOCAL_DIR}/
74	fi
75	for f in bin etc lib libdata libexec sbin share; do
76		mkdir -p ${LOCAL_DIR}/$f
77	done
78}
79customize_cmd clean_usr_local
80
81cust_install_machine_files()
82{
83	echo "cd ${NANO_CFG_BASE}/Files"
84	cd ${NANO_CFG_BASE}/Files
85	find . -print | grep -Ev '/(CVS|\.git|\.hg|\.svn)' | cpio -dumpv ${NANO_WORLDDIR}
86}
87customize_cmd cust_install_files
88customize_cmd cust_install_machine_files
89
90buildenv()
91{
92	cd ${NANO_SRC}
93	env __MAKE_CONF=${NANO_MAKE_CONF_BUILD} DESTDIR=${NANO_WORLDDIR} make buildenv
94}
95
96NANO_MAKEFS="makefs -B big \
97	-o bsize=4096,fsize=512,density=8192,optimization=space"
98export NANO_MAKEFS
99
100CONF_BUILD="
101WITHOUT_ACPI=true
102WITHOUT_AUDIT=true
103WITHOUT_BLUETOOTH=true
104WITHOUT_CALENDAR=true
105WITHOUT_DICT=true
106WITHOUT_EXAMPLES=true
107WITHOUT_GAMES=true
108WITHOUT_HTML=true
109WITHOUT_IPFILTER=true
110WITHOUT_LLVM_COV=true
111WITHOUT_LOCALES=true
112WITHOUT_LPR=true
113WITHOUT_MAN=true
114WITHOUT_NETCAT=true
115WITHOUT_NIS=true
116WITHOUT_NLS=true
117WITHOUT_NS_CACHING=true
118WITHOUT_PROFILE=true
119WITHOUT_SENDMAIL=true
120WITHOUT_SHAREDOCS=true
121WITHOUT_SYSCONS=true
122WITHOUT_LIB32=true
123"
124CONF_INSTALL="$CONF_BUILD
125INSTALL_NODEBUG=t
126NOPORTDOCS=t
127NO_INSTALL_MANPAGES=t
128"
129# The following would help...
130# WITHOUT_TOOLCHAIN=true		can't build ports
131# WITHOUT_INSTALLLIB=true		libgcc.a
132PKG_ONLY_MAKE_CONF="
133WITHOUT_TOOLCHAIN=true
134WITHOUT_INSTALLLIB=true
135"
136
137NANO_PACKAGE_ONLY=1
138
139# install a package from a pre-built binary
140do_add_pkg ()
141{
142	# Need to create ${NANO_OBJ}/ports in this add_pkg_${port} function
143	set -x
144	mkdir -p ${NANO_OBJ}/ports/distfiles
145	mkdir -p ${NANO_OBJ}/ports/packages
146	mkdir -p ${NANO_WORLDDIR}/usr/ports/packages
147	mkdir -p ${NANO_WORLDDIR}/usr/ports/distfiles
148	mount -t nullfs -o noatime ${NANO_OBJ}/ports/packages \
149	    ${NANO_WORLDDIR}/usr/ports/packages
150	mount -t nullfs -o noatime ${NANO_OBJ}/ports/distfiles \
151	    ${NANO_WORLDDIR}/usr/ports/distfiles
152	CR env ASSUME_ALWAYS_YES=YES SIGNATURE_TYPE=none /usr/sbin/pkg add /usr/ports/packages/All/$1.txz
153	umount ${NANO_WORLDDIR}/usr/ports/distfiles
154	umount ${NANO_WORLDDIR}/usr/ports/packages
155	rmdir ${NANO_WORLDDIR}/usr/ports/packages
156	rmdir ${NANO_WORLDDIR}/usr/ports/distfiles
157	rmdir ${NANO_WORLDDIR}/usr/ports
158	set +x
159}
160
161# Build a port (with the side effect of creating a package)
162do_add_port ()
163{
164	local port_path
165	port_path=$1
166	shift
167	set -x
168	# Need to create ${NANO_OBJ}/ports in this add_port_${port} function
169	mkdir -p ${NANO_OBJ}/ports/distfiles
170	mkdir -p ${NANO_OBJ}/ports/packages
171	mkdir -p ${NANO_PORTS}/packages
172	mkdir -p ${NANO_PORTS}/distfiles
173	mkdir -p ${NANO_WORLDDIR}/usr/src
174	mkdir -p ${NANO_WORLDDIR}/usr/ports
175      	mount -t nullfs -o noatime ${NANO_SRC} ${NANO_WORLDDIR}/usr/src
176	mount -t nullfs -o noatime ${NANO_PORTS} ${NANO_WORLDDIR}/usr/ports
177	mount -t nullfs -o noatime ${NANO_OBJ}/ports/packages \
178	    ${NANO_WORLDDIR}/usr/ports/packages
179	mount -t nullfs -o noatime ${NANO_OBJ}/ports/distfiles \
180	    ${NANO_WORLDDIR}/usr/ports/distfiles
181	mkdir -p ${NANO_WORLDDIR}/dev
182	mount -t devfs devfs ${NANO_WORLDDIR}/dev
183	mkdir -p ${NANO_WORLDDIR}/usr/workdir
184	cp /etc/resolv.conf ${NANO_WORLDDIR}/etc/resolv.conf
185	# OK, a little inefficient, but likely not enough to worry about.
186	CR ldconfig /lib /usr/lib /usr/local/lib
187	CR ldconfig -R
188	CR ldconfig -r
189# Improvement: Don't know why package-recursive don't works here
190	CR "env UNAME_p=${NANO_ARCH} TARGET=${NANO_ARCH} \
191	    TARGET_ARCH=${NANO_ARCH} PORTSDIR=${NANO_PORTS} make \
192	    __MAKE_CONF=${NANO_MAKE_CONF_BUILD} \
193	    WRKDIRPREFIX=/usr/workdir -C /usr/ports/$port_path \
194	    package-recursive BATCH=yes $* clean FORCE_PKG_REGISTER=t"
195	rm ${NANO_WORLDDIR}/etc/resolv.conf
196	rm -rf ${NANO_WORLDDIR}/usr/obj
197	rm -rf ${NANO_WORLDDIR}/usr/workdir
198	umount ${NANO_WORLDDIR}/dev
199	umount ${NANO_WORLDDIR}/usr/ports/packages
200	umount ${NANO_WORLDDIR}/usr/ports/distfiles
201	umount ${NANO_WORLDDIR}/usr/ports
202	umount ${NANO_WORLDDIR}/usr/src
203	set +x
204}
205
206# Need to check if this function works with cross-compiling architecture!!!!
207# Recursive complex fonction: Generate one function for each ports
208add_port () {
209    local port_path=$1
210    local port=`echo $1 | sed -e 's/\//_/'`
211    shift
212    # Check if package already exist
213    # Need to:
214    # 1. check ARCH of this package!
215    # 2. Add a trap
216    cd ${NANO_PORTS}/${port_path}
217    PKG_NAME=`env PORTSDIR=${NANO_PORTS} make __MAKE_CONF=${NANO_MAKE_CONF_BUILD} package-name`
218    if [ -f ${NANO_OBJ}/ports/packages/All/${PKG_NAME}.txz ]; then
219	# Pkg file found: Generate add_pkg_NAME function
220	eval "
221	    add_pkg_${port} () {
222	        do_add_pkg ${PKG_NAME}
223	    }
224            customize_cmd add_pkg_${port}
225            "
226    else
227	# No pkg file: Generate add_port_NAME function
228        eval "
229            add_port_${port} () {
230	        do_add_port ${port_path} $*
231	    }
232	    customize_cmd add_port_${port}
233	"
234	NANO_PACKAGE_ONLY=0
235    fi
236}
237
238die()
239{
240	echo "$*"
241	exit 1
242}
243
244# Automatically include the packaging port here so it is always first so it
245# builds the port and adds the package so we can add other packages.
246add_port ports-mgmt/pkg
247
248rp=$(realpath ${NANO_OBJ}/)
249__a=`mount | grep ${rp} | awk '{print length($3), $3;}' | sort -rn | awk '{$1=""; print;}'`
250if [ -n "$__a" ]; then
251    echo "unmounting $__a"
252    umount $__a
253fi
254
255NANO_BOOTLOADER="boot/boot0"
256