3 # Copyright (c) 2015 M. Warner Losh <imp@FreeBSD.org>
4 # Copyright (c) 2010-2011 iXsystems, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
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.
35 # NB: You want the other file as the command line arg :)
38 # o mkimg setting active partition
39 # o mkimg starting at arbitrary offset (needed for A10, et al)
40 # o mtools still needed because we have no makefs -t msdos
41 # o nanobsd doesn't record changes to WORLDTEMP in customization
42 # scripts yet, so we have kludge to pick up all files
43 # o easy way for pkg to grab files from other repos and put that
45 # o Need to have some way to create / resize the s4 slice to do ping
46 # pong bouncing between s3 and s4 for an arbitrary image. we can resize
48 # o hints in the uboot ports for how to create the image
51 # o documentation for how to run the qemu images
52 # o msdos mtools fallback
53 # o special boot for !x86 !arm platforms
54 # o qemu image for arm
55 # o qemu image for aarch64
56 # o qemu image for armv6/armv7
57 # o easy support for different image / vm formats
58 # o need to promote much of this to nanobsd.sh and friends
59 # o uses old kludge to build image w/o ownership being right
60 # for many files. Should move to mtree-dedup.awk.
61 # o support for EFI images
62 # o support for GPT partitioning
65 # o common tooling for creating images for odd-ball platforms
66 # o support for boot loaders other than uboot in the image
67 # or via special instructions
68 # o no pony support; sadly, you cannot have a pony
71 if [ -z $NANO_NAME ]; then
72 echo "NANO_NAME not defined. Use foo.cfg instead."
75 NANO_SLICE_FAT_SIZE=32m
76 NANO_SLICE_CFG_SIZE=32m
78 NANO_BOOT2CFG="-h -S115200"
81 NANO_RAM_TMPVARSIZE=8192
84 NANO_PMAKE="make -j $(sysctl -n hw.ncpu)"
87 NANO_CFG_BASE=$(realpath ${NANO_CFG_BASE}/..)
88 NANO_SRC=$(realpath ${NANO_CFG_BASE}/../../..)
90 if [ -z ${NANO_CPUTYPE} ]; then
91 NANO_OBJ=${NANO_SRC}/../embedded/obj
93 # Alas, I can't set OBJTREE to ${MACHINE}.${MACHINE_ARCH}.${CPUTYPE}
94 # so this will have to do until I can.
95 NANO_OBJ=${NANO_SRC}/../embedded/obj.${NANO_CPUTYPE}
97 NANO_LOG=${NANO_OBJ}/../${NANO_NAME}
98 NANO_DISKIMGDIR=${NANO_OBJ}/../images
99 NANO_WORLDDIR=${NANO_LOG}/_.w
102 unset MAKEOBJDIRPREFIX
105 NANO_OBJ=$(realpath ${NANO_OBJ})
107 NANO_LOG=$(realpath ${NANO_LOG})
108 mkdir -p ${NANO_WORLDDIR}
109 NANO_WORLDDIR=$(realpath ${NANO_WORLDDIR})
110 mkdir -p ${NANO_DISKIMGDIR}
111 NANO_DISKIMGDIR=$(realpath ${NANO_DISKIMGDIR})
113 NANO_FAT_DIR=${NANO_LOG}/_.fat
115 customize_cmd cust_allow_ssh_root
117 add_etc_make_conf ( ) (
118 touch ${NANO_WORLDDIR}/etc/make.conf
120 customize_cmd add_etc_make_conf
122 cust_install_machine_files ( ) (
123 echo "cd ${NANO_CFG_BASE}/Files"
124 cd ${NANO_CFG_BASE}/Files
125 find . -print | grep -Ev '/(CVS|\.svn|\.hg|\.git)' | cpio -dumpv ${NANO_WORLDDIR}
127 customize_cmd cust_install_files
128 customize_cmd cust_install_machine_files
131 LOCAL_XTOOL_DIRS=usr.bin/mkimg
134 WITHOUT_BLUETOOTH=true
135 WITHOUT_CALENDAR=true
137 WITHOUT_EXAMPLES=true
140 WITHOUT_IPFILTER=true
141 WITHOUT_LLVM_COV=true
148 WITHOUT_NS_CACHING=true
150 WITHOUT_SENDMAIL=true
151 WITHOUT_SHAREDOCS=true
155 WITHOUT_DEBUG_FILES=t
156 WITHOUT_KERNEL_SYMBOLS=t
158 CONF_INSTALL="$CONF_BUILD
161 NO_INSTALL_MANPAGES=t
163 # The following would help...
164 # WITHOUT_TOOLCHAIN=true can't build ports
165 # WITHOUT_INSTALLLIB=true libgcc.a
169 WITHOUT_TOOLCHAIN=true
170 WITHOUT_INSTALLLIB=true
175 # Creates images for all the formats that use MBR / GPT
176 # split later if the #ifdef soup gets too bad.
177 create_diskimage_gpt ( ) (
178 pprint 2 "build diskimage gpt ${NANO_NAME}"
180 create_diskimage_mbr $*
183 create_diskimage_mbr ( ) (
187 [ -z ${NANO_DISKIMAGE_FORMAT} ] || fmt=".${NANO_DISKIMAGE_FORMAT}"
189 pprint 2 "build diskimage ${NANO_NAME}"
190 pprint 3 "log: ${NANO_LOG}/_.di"
191 pprint 3 "image in: ${NANO_DISKIMGDIR}/_.disk.image.${NANO_NAME}${fmt}"
194 local extra i sz fmt fmtarg bootmbr bootbsd skiparg
196 # Tell mtools not to be too picky
197 export MTOOLS_SKIP_CHECK=1
199 [ -z ${NANO_DISKIMAGE_FORMAT} ] || fmtarg="-f ${NANO_DISKIMAGE_FORMAT}"
200 [ -z ${NANO_DISKIMAGE_FORMAT} ] || fmt=".${NANO_DISKIMAGE_FORMAT}"
201 bootmbr=${NANO_BOOT_MBR:+-b ${NANO_BOOT_MBR}}
202 bootbsd=${NANO_BOOT_BSD:+-b ${NANO_BOOT_BSD}}
203 skiparg=${NANO_MBR_FIRST_SKIP:+-S ${NANO_MBR_FIRST_SKIP}}
205 for i in s1 s2 s3 s4 p1 p2 p3 p4 p5 empty; do
206 rm -fr ${NANO_LOG}/_.${i}*
209 # Populate the FAT partition, if needed
210 if [ -n "${NANO_SLICE_FAT}" ]; then
211 echo Creating MSDOS partition for kernel
212 newfs_msdos -C ${NANO_SLICE_FAT_SIZE} -F 16 -L ${NANO_NAME} \
213 ${NANO_LOG}/_.${NANO_SLICE_FAT}
214 if [ -d ${NANO_FAT_DIR} ]; then
215 # Need to copy files from ${NANO_FATDIR} with mtools, or use
216 # makefs -t msdos once that's supported
217 mcopy -s -i ${NANO_LOG}/_.${NANO_SLICE_FAT} ${NANO_FAT_DIR}/* ::
221 # Populate the Powerpc boot image, if needed
222 if [ "${NANO_LAYOUT}" = powerpc64-ibm ]; then
223 dd if=${NANO_WORLDDIR}/boot/boot1.elf of=${NANO_LOG}/_.s1 bs=800k count=1 conv=sync
226 # Populate the / partition, and place it into a slice with a
228 [ -z ${NANO_NOPRIV_BUILD} ] || extra="-F ${NANO_METALOG}"
229 sz=${NANO_SLICE_ROOT_SIZE:+-s ${NANO_SLICE_ROOT_SIZE}}
230 eval "${NANO_MAKEFS_UFS}" ${extra} $sz "${NANO_LOG}/_.${NANO_ROOT}" \
232 case ${NANO_DISK_SCHEME} in
234 mkimg -s bsd ${bootbsd} -p freebsd-ufs:=${NANO_LOG}/_.${NANO_ROOT} \
235 -o ${NANO_LOG}/_.${NANO_SLICE_ROOT}
236 eval $NANO_SLICE_CFG=freebsd
237 eval $NANO_SLICE_ROOT=freebsd
240 eval $NANO_SLICE_CFG=freebsd-ufs
241 eval $NANO_SLICE_ROOT=freebsd-ufs
245 # Populate the /cfg partition, empty if none given
246 if [ -z "${NANO_CFGDIR}" ]; then
247 echo "Faking cfg dir, it's empty"
248 NANO_CFGDIR=${NANO_LOG}/_.empty
249 mkdir -p ${NANO_CFGDIR}
252 eval "${NANO_MAKEFS_UFS}" -s ${NANO_SLICE_CFG_SIZE} \
253 "${NANO_LOG}/_.${NANO_SLICE_CFG}" "${NANO_CFGDIR}"
255 # data slice not supported since we need the part for FAT for
258 # Now shuffle all the slices together into the proper layout
259 if [ -n "$NANO_SLICE_FAT" ]; then
260 eval $NANO_SLICE_FAT=fat16b
263 out=${NANO_DISKIMGDIR}/_.disk.image.${NANO_NAME}${fmt}
264 # below depends on https://reviews.freebsd.org/D4403 not yet in the tree
265 # but there's problems: it marks all partitions as active, so you have to
266 # boot off partition 3 or 2 by hand if you're playing around with this WIP
267 case ${NANO_LAYOUT} in
269 mkimg -a 3 ${skiparg} ${fmtarg} ${bootmbr} -s mbr -p ${s1}:=${NANO_LOG}/_.s1 \
270 -p ${s2}:=${NANO_LOG}/_.s2 \
271 -p ${s3}:=${NANO_LOG}/_.s3 \
275 # s3 is cfg, s1 is /, s2 is other / (s2 is created in first boot script)
276 mkimg -a 1 ${fmtarg} ${bootmbr} -s mbr -p ${s1}:=${NANO_LOG}/_.s1 \
278 -p ${s3}:=${NANO_LOG}/_.s3 \
284 # s1 is boot, s2 is cfg, s3 is /, not sure how to make that
285 # boot (marked as active) with mkimg yet
286 mkimg -a 2 ${fmtarg} ${bootmbr} -s mbr \
287 -p efi:=${NANO_WORLDDIR}/boot/efiboot.img \
288 -p ${s2}:=${NANO_LOG}/_.s2 \
289 -p ${s3}:=${NANO_LOG}/_.s3 \
293 # p1 is boot for uefi, p2 is boot for gpt, p3 is cfg, p4 is /
294 # and p5 is alt-root (after resize)
295 mkimg -a 2 ${fmtarg} ${bootmbr} -s gpt \
296 -p efi:=${NANO_WORLDDIR}/boot/efiboot.img \
297 -p freebsd-boot:=${NANO_WORLDDIR}/boot/gptboot \
298 -p ${p3}:=${NANO_LOG}/_.p3 \
299 -p ${p4}:=${NANO_LOG}/_.p4 \
303 # A lie to make the boot loader work, it boots the first BSD partition
304 # it finds, regardless of the active flag.
306 # boot image is on a special partition, ala std-embedded, but that
307 # partition isn't FAT with special files, but a copy of the boot
309 mkimg -a 1 ${fmtarg} -s mbr -p prepboot:=${NANO_LOG}/_.s1 \
310 -p ${s2}:=${NANO_LOG}/_.s2 \
311 -p ${s3}:=${NANO_LOG}/_.s3a \
317 ) > ${NANO_LOG}/_.di 2>&1
325 # Automatically include the packaging port here so it is always first so it
326 # builds the port and adds the package so we can add other packages.
327 #XXX Doesn't work for cross build, so punting until I can integreate qemu-static
328 #XXX or poudriere, both of which require priv'd execution. Or qemu-system,
329 #XXX which is super, super slow.
330 #add_port ports-mgmt/pkg
331 #add_port security/sudo
334 rp=$(realpath ${NANO_OBJ}/)
335 __a=`mount | grep ${rp} | awk '{print length($3), $3;}' | sort -rn | awk '{$1=""; print;}'`
336 if [ -n "$__a" ]; then
337 echo "unmounting $__a"
341 if [ "$DEBUG" = 1 ]; then
351 CONF_INSTALL="$CONF_BUILD
354 CONF_INSTALL="$CONF_INSTALL
358 if [ "${NANO_PACKAGE_ONLY}" -eq 1 ]; then
359 CONF_INSTALL="${CONF_INSTALL}
360 ${PKG_ONLY_MAKE_CONF}
362 echo "Automatically building a thin image with packages"
364 echo "Automatically building a * * F A T * * image so we can build ports"
367 VARS="MASTER_SITE_BACKUP MASTER_SITE_OVERRIDE PACKAGEROOT PACKAGESITE"
370 val=$(eval echo "\$$var")
371 if [ -n "$val" ]; then
372 CONF_INSTALL="${CONF_INSTALL}
377 typical_embedded ( ) (
378 # Need to create rc.conf before we copy over /etc to /conf/base/etc
379 # so now's a good time.
381 local rc=${NANO_WORLDDIR}/etc/rc.conf
383 echo "hostname=nanobsd-${NANO_NAME}" > $rc
384 echo "growfs_enable=YES" >> $rc
385 echo "growfs_type=nanobsd-pingpong" >> $rc
386 echo "ntpdate_enable=YES" >> $rc
387 echo "ifconfig_DEFAULT=DHCP" >> $rc
388 echo "ntpdate_hosts='0.freebsd.pool.ntp.org 1.freebsd.pool.ntp.org'" >> $rc
389 # Make sure that firstboot scripts run so growfs works.
390 # Note: still some issues remvoing this XXX
391 touch ${NANO_WORLDDIR}/firstboot
393 customize_cmd typical_embedded
396 chdir ${NANO_WORLDDIR}
400 mkdir -p pkg/tmp # Needed for pkg bootstrap
401 mkdir -p usr/local/etc # Will get moved to local/etc
403 echo 'PKG_DBDIR = "/pkg/db"'
404 echo 'PKG_CACHEDIR = "/pkg/cache"'
405 echo 'DEFAULT_ALWAYS_YES = "yes"'
406 echo 'ASSUME_ALWAYS_YES = "yes"'
407 ) >> usr/local/etc/pkg.conf
408 [ -z ${NANO_NOPRIV_BUILD} ] || (
409 echo "./pkg type=dir uname=root gname=wheel mode=0755"
410 echo "./pkg/cache type=dir uname=root gname=wheel mode=0755"
411 echo "./pkg/db type=dir uname=root gname=wheel mode=0755"
412 echo "./pkg/tmp type=dir uname=root gname=wheel mode=0755"
415 customize_cmd fix_pkg
418 VERSION_FILE=${NANO_WORLDDIR}/etc/version
419 if [ "${SVNREVISION}" = "${REVISION}" ]; then
420 echo "${NANO_NAME}" > "${VERSION_FILE}"
422 echo "${NANO_NAME} (${SVNREVISION})" > "${VERSION_FILE}"
425 customize_cmd save_build
427 shrink_md_fbsize ( ) (
428 # We have a lot of little files on our memory disks. Let's decrease
429 # the block and frag size to fit more little files on them (this
430 # halves our space requirement by ~50% on /etc and /var on 8.x --
431 # and gives us more back on 9.x as the default block and frag size
432 # are 4 times larger).
433 sed -i '' -e 's,-S -i 4096,-S -i 4096 -b 4096 -f 512,' \
434 ${NANO_WORLDDIR}/etc/rc.initdiskless
436 customize_cmd shrink_md_fbsize
438 customize_cmd cust_comconsole
441 local d=/usr/local/share/u-boot/${NANO_BOOT_PKG}
442 local f=${NANO_FAT_DIR}
444 # For now, just copy all the files. However, for iMX6 and Allwinner,
445 # we'll need to put a special boot block at a fixed location
446 # on the disk as well.
452 # We have to touch the saveenv file
455 # Now we need to copy over dtb files from the build.
456 cp ${NANO_WORLDDIR}/boot/dtb/*.dtb .
459 if [ -n "$NANO_BOOT_PKG" ]; then
460 d=/usr/local/share/u-boot/${NANO_BOOT_PKG}
461 if [ ! -d ${d} ]; then
462 echo ${NANO_BOOT_PKG} not installed. Sadly, it must be.
465 if [ ! -x /usr/local/bin/mcopy ]; then
466 echo mtools not installed. Sadly, we gotta have it.
469 customize_cmd dos_boot_part
473 # not quite ready to tweak these in nopriv build
474 if [ -z ${NANO_NOPRIV_BUILD} ]; then
475 # Last second tweaks -- generally not needed
476 chown -R root:wheel ${NANO_WORLDDIR}/root
477 chmod 0755 ${NANO_WORLDDIR}/root/*
478 chmod 0755 ${NANO_WORLDDIR}/*
479 chown -R root:wheel ${NANO_WORLDDIR}/etc
480 chown -R root:wheel ${NANO_WORLDDIR}/boot
481 chown root:wheel ${NANO_WORLDDIR}/
482 chown root:wheel ${NANO_WORLDDIR}/usr
485 late_customize_cmd product_custom
488 # Convenience routines to bring up many settings for standard environments
492 # For each architecture, we have a standard set of settings to the extent
493 # it makes sense. For architectures that don't have a standard environment
494 # cfg files need to either define a lot of settings, provide their own disk
495 # imaging, or set which of the variants we support. No subshells, since we
496 # set global variables
516 # Default values, if not overridden in .cfg file
517 : ${NANO_KERNEL:=GENERIC}
518 : ${NANO_DRIVE:=ada0}
519 : ${NANO_LAYOUT:=std-x86}
520 : ${NANO_BOOT_MBR:=${NANO_WORLDDIR}/boot/boot0sio}
521 : ${NANO_BOOT_BSD:=${NANO_WORLDDIR}/boot/boot}
529 NANO_LAYOUT=powerpc64-ibm
534 # QEMU settings for the standard environments
538 NANO_DISKIMAGE_FORMAT=qcow2
541 # other standard environments will go here
543 eval std_${NANO_ARCH}
545 # Slice 1: FAT for ROM loading bootstrap
546 # Slice 2: Config partition
547 # Slice 3: FreeBSD partition (sized exactly)
548 # Slice 4: FreeBSD partition (empty)
549 # on first boot, we resize slice 3 & 4 to be 1/2 of what's
550 # left over on the SD card after slice 1 and 2 are taken
551 # off the top. We also resize the 'a' partition on first boot
552 # to the size of the partition for the ping/pong upgrade.
553 # This feature needs support in the rc.d bootup script.
555 # Ideally, we'd not put BSD labels on the MBR disks.
556 # However, we can't boot off raw MBR disks. First,
557 # boot2 defaults to 'a' partition, and freaks out
558 # unless you tell it to use 'c'. But even if we
559 # hack that, then /boot/loader wants to load off
560 # of 'c' partition. If you fix that, then we'll
561 # try to mount root, but sanity checks prevent
562 # slices from working.
565 : ${NANO_ENDIAN:=little} # make -V something to figure it out?
566 : ${NANO_LAYOUT:=std-embedded}
567 : ${NANO_MAKEFS_UFS:=makefs -t ffs -B ${NANO_ENDIAN}}
568 : ${NANO_DISK_SCHEME:=mbr} # No others really supported ATM (well, gpt)
569 case ${NANO_LAYOUT} in
574 NANO_SLICE_ALTROOT=s4
575 NANO_ROOT=${NANO_SLICE_ROOT}a
576 NANO_ALTROOT=${NANO_SLICE_ALTROOT}a
581 NANO_SLICE_ALTROOT=s2
582 NANO_ROOT=${NANO_SLICE_ROOT}a
583 NANO_ALTROOT=${NANO_SLICE_ALTROOT}a
586 NANO_SLICE_PPCBOOT=s1
589 NANO_SLICE_ALTROOT=s4
590 NANO_ROOT=${NANO_SLICE_ROOT}a
591 NANO_ALTROOT=${NANO_SLICE_ALTROOT}a
601 NANO_SLICE_ALTROOT=s4
602 NANO_ROOT=${NANO_SLICE_ROOT}
603 NANO_ALTROOT=${NANO_SLICE_ALTROOT}
611 NANO_SLICE_ALTROOT=p5
612 NANO_ROOT=${NANO_SLICE_ROOT}
613 NANO_ALTROOT=${NANO_SLICE_ALTROOT}
616 echo Unknown Layout ${NANO_LAYOUT}
622 NANO_SLICE_DATA= # Not included
624 # These don't make any sense to this strategy, so stub them out.
625 calculate_partitioning ( ) (
628 # These don't make any sense to this strategy, so stub them out.
629 create_code_slice ( ) (
632 # Each major disk scheme has its own routine. Generally
633 # this is for mbr, gpt, etc. These are generally are widely
634 # shared, but some specialized formats won't be shared.
635 create_diskimage ( ) (
636 eval create_diskimage_${NANO_DISK_SCHEME}
639 # Set the path to the same path we use for buldworld to use latest mkimg
640 NANO_TARGET=$(cd ${NANO_SRC}; ${NANO_MAKE} TARGET_ARCH=${NANO_ARCH} -V _TARGET)
641 NANO_TMPPATH=$(cd ${NANO_SRC}; ${NANO_MAKE} MK_AUTO_OBJ=no TARGET=${NANO_TARGET} TARGET_ARCH=${NANO_ARCH} -f Makefile.inc1 buildenv -V TMPPATH)
642 export PATH="${NANO_TMPPATH}:${PATH}"