git: 7acb53817ffc - main - release: Add code to make FreeBSD/Firecracker

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Mon, 10 Jul 2023 22:19:53 UTC
The branch main has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=7acb53817ffcfcbe328f08a60f8087e6b88fa098

commit 7acb53817ffcfcbe328f08a60f8087e6b88fa098
Author:     Colin Percival <cperciva@FreeBSD.org>
AuthorDate: 2023-07-10 06:16:31 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2023-07-10 22:19:33 +0000

    release: Add code to make FreeBSD/Firecracker
    
    This just stages the kernel and builds a stripped-down rootfs for use
    with the Firecracker VMM.  At some point in the future the release
    engineering team might start publishing these, but initially it's
    just here to simplify FreeBSD/Firecracker development and testing.
    
    Note that the rootfs generated:
    * Uses an IP address of 10.0.0.2 with a gateway of 10.0.0.1,
    * Has sshd enabled,
    * Has user "freebsd" with password "freebsd" and a root password
      of "root", and
    * Is 1 GB in size (but has growfs enabled).
    
    All of those are subject to change without notice; anyone intending to
    use FreeBSD/Firecracker in anything remotely resembling a production
    environment should talk to cperciva first.
    
    Reviewed by:    gjb
    Sponsored by:   https://www.patreon.com/cperciva
    Differential Revision:  https://reviews.freebsd.org/D40956
---
 release/Makefile.firecracker | 58 ++++++++++++++++++++++++++++++++++++++++++++
 release/Makefile.vm          |  1 +
 2 files changed, 59 insertions(+)

diff --git a/release/Makefile.firecracker b/release/Makefile.firecracker
new file mode 100644
index 000000000000..bb54db7da5db
--- /dev/null
+++ b/release/Makefile.firecracker
@@ -0,0 +1,58 @@
+#
+# Makefile for creating FreeBSD/Firecracker artifacts
+#
+
+CLEANDIRS+=	${TARGET}/firecracker-kern ${TARGET}/firecracker-world
+
+# Bits related to hardware which won't exist in a VM.
+WITHOUT_VM_ENOENT=WITHOUT_APM=YES WITHOUT_BLUETOOTH=YES WITHOUT_CXGBETOOL=YES \
+    WITHOUT_FLOPPY=YES WITHOUT_GPIO=YES WITHOUT_MLX5TOOL=YES WITHOUT_USB=YES \
+    WITHOUT_USB_GADGET_EXAMPLES=YES WITHOUT_VT=YES WITHOUT_WIRELESS=YES
+# Bits related to software which doesn't exist in Firecracker specifically.
+WITHOUT_FC_ENOENT=WITHOUT_ACPI=YES WITHOUT_BOOT=YES WITHOUT_BHYVE=YES \
+    WITHOUT_EFI=YES WITHOUT_FDT=YES WITHOUT_HYPERV=YES \
+    WITHOUT_LEGACY_CONSOLE=YES WITHOUT_SYSCONS=YES
+# Bits which take up a lot of space and probably won't be wanted inside a
+# Firecracker VM.
+WITHOUT_FC_FEATURES=WITHOUT_DEBUG_FILES=YES WITHOUT_INCLUDES=YES \
+    WITHOUT_INSTALLLIB=YES WITHOUT_TESTS=YES WITHOUT_TOOLCHAIN=YES
+# All the excluded bits
+WITHOUTS=${WITHOUT_VM_ENOENT} ${WITHOUT_FC_ENOENT} ${WITHOUT_FC_FEATURES}
+
+firecracker:	firecracker-freebsd-kern.bin firecracker-freebsd-rootfs.bin
+
+FCKDIR=	${.OBJDIR}/${TARGET}/firecracker-kern
+firecracker-freebsd-kern.bin:
+.if !defined(DESTDIR) || !exists(${DESTDIR})
+	@echo "--------------------------------------------------------------"
+	@echo ">>> DESTDIR must point to destination for Firecracker binaries"
+	@echo "--------------------------------------------------------------"
+	@false
+.endif
+	mkdir -p ${FCKDIR}
+	${MAKE} -C ${WORLDDIR} DESTDIR=${FCKDIR} \
+	    KERNCONF=FIRECRACKER TARGET=${TARGET} installkernel
+	cp ${FCKDIR}/boot/kernel/kernel ${DESTDIR}/freebsd-kern.bin
+
+FCWDIR=	${.OBJDIR}/${TARGET}/firecracker-world
+firecracker-freebsd-rootfs.bin:
+	mkdir -p ${FCWDIR}
+	${MAKE} -C ${WORLDDIR} DESTDIR=${FCWDIR} \
+	    ${WITHOUTS} TARGET=${TARGET} installworld distribution distrib-dirs
+	echo '/dev/ufs/rootfs / ufs rw 1 1' > ${FCWDIR}/etc/fstab
+	echo 'hostname="freebsd"' >> ${FCWDIR}/etc/rc.conf
+	echo 'ifconfig_vtnet0="inet 10.0.0.2 netmask 255.255.0.0"' >> ${FCWDIR}/etc/rc.conf
+	echo 'defaultrouter="10.0.0.1"' >> ${FCWDIR}/etc/rc.conf
+	echo 'sshd_enable="YES"' >> ${FCWDIR}/etc/rc.conf
+	echo 'sshd_rsa_enable="NO"' >> ${FCWDIR}/etc/rc.conf
+	echo 'growfs_enable="YES"' >> ${FCWDIR}/etc/rc.conf
+	echo 'nameserver 8.8.8.8' >> ${FCWDIR}/etc/resolv.conf
+	sed -i '' -e '/periodic/s/^/#/' ${FCWDIR}/etc/crontab
+	pw -R ${FCWDIR} groupadd freebsd -g 1001
+	mkdir -p ${FCWDIR}/home/freebsd
+	pw -R ${FCWDIR} useradd freebsd -m -M 0755 -w yes -n freebsd \
+	    -u 1001 -g 1001 -G 0 -c "FreeBSD User" -d /home/freebsd -s /bin/sh
+	pw -R ${FCWDIR} usermod root -w yes
+	touch ${FCWDIR}/firstboot
+	makefs -s 1g -o label=rootfs -o version=2 -o softupdates=1 \
+	    ${DESTDIR}/freebsd-rootfs.bin ${FCWDIR}
diff --git a/release/Makefile.vm b/release/Makefile.vm
index 81f46a23b2ac..ce86fabdfe5e 100644
--- a/release/Makefile.vm
+++ b/release/Makefile.vm
@@ -181,6 +181,7 @@ cloudware-release:
 
 .include "${.CURDIR}/Makefile.azure"
 .include "${.CURDIR}/Makefile.ec2"
+.include "${.CURDIR}/Makefile.firecracker"
 .include "${.CURDIR}/Makefile.gce"
 .include "${.CURDIR}/Makefile.vagrant"
 .include "${.CURDIR}/Makefile.inc1"