Skip to content

Instantly share code, notes, and snippets.

@tomalexander
Created Dec 29, 2020
Embed
What would you like to do?
#!/usr/bin/env sh
#
# Build a revision of FreeBSD
set -eu
REVISION="stable/12"
KERNCONF="GENERIC"
NETIF="em0"
JOBS=2
STATUS_FILE="/root/build_status"
JAIL_DIR="/jail"
SELF_PATH="$(readlink -f "$0")"
cd /usr/src
if [ -e "$STATUS_FILE" ]; then
current_status=$(cat $STATUS_FILE)
else
current_status=1
fi
next_status=$((current_status + 1))
if [ "$current_status" = 1 ]; then
echo "Starting a clean build"
cat > /etc/devfs.rules <<EOF
[tajail=11]
add include \$devfsrules_hide_all
add include \$devfsrules_unhide_basic
add include \$devfsrules_unhide_login
add path 'bpf*' unhide
EOF
cat > /boot/loader.conf <<EOF
net.fibs=2
EOF
cat > /etc/sysctl.conf <<EOF
net.add_addr_allfibs=1
EOF
cp /usr/share/examples/jails/jib /usr/local/bin/jib
chmod +x /usr/local/bin/jib
cat > /etc/jail.conf <<EOF
test {
host.hostname = "test.localhost";
path = "/jail";
exec.clean;
exec.system_user = "root";
exec.jail_user = "root";
exec.fib = 1;
vnet;
vnet.interface = "e0b_test";
exec.prestart += "jib addm test $NETIF";
exec.poststop += "jib destroy test";
exec.prestart += "echo 'search localdomain\\nnameserver 1.1.1.1' > \$path/etc/resolv.conf";
exec.start += "/bin/sh /etc/rc";
exec.stop = "/bin/sh /etc/rc.shutdown jail";
exec.consolelog = "/var/log/jail.log";
mount.devfs;
devfs_ruleset = 11;
allow.set_hostname = 1;
allow.sysvipc = 1;
}
EOF
if [ ! -d /usr/src/.git ]; then
/usr/local/bin/git clone 'https://git.FreeBSD.org/src.git' /usr/src
fi
cat > /usr/local/etc/rc.d/buildsetup <<EOF
#!/bin/sh
#
# PROVIDE: buildsetup
# REQUIRE: NETWORKING DAEMON
# KEYWORD: shutdown
. /etc/rc.subr
name="buildsetup"
rcvar="\${name}_enable"
load_rc_config \${name}
: \${buildsetup_enable:="NO"}
: \${buildsetup_script:="/usr/local/bin/build.sh"}
pidfile="/var/run/\${name}.pid"
command="/bin/sh"
command_args="-c \\"\${buildsetup_script} 2>&1 | tee -a /var/log/build\\""
required_files="\${buildsetup_script}"
run_rc_command "\$1"
EOF
chmod +x /usr/local/etc/rc.d/buildsetup
cat > /etc/rc.conf.d/buildsetup <<EOF
buildsetup_enable="YES"
buildsetup_script="$SELF_PATH"
EOF
echo "Rebooting"
echo "$next_status" > $STATUS_FILE
reboot
elif [ "$current_status" = 2 ]; then
cd /usr/src
/usr/local/bin/git reset --hard HEAD
/usr/local/bin/git checkout "$REVISION"
make -j "$JOBS" clean
make -j "$JOBS" buildworld
make -j "$JOBS" kernel KERNCONF="$KERNCONF"
echo "Rebooting"
echo "$next_status" > $STATUS_FILE
reboot
elif [ "$current_status" = 3 ]; then
cd /usr/src
make installworld
echo "Rebooting"
echo "$next_status" > $STATUS_FILE
reboot
elif [ "$current_status" = 4 ]; then
cd /usr/src
make installworld DESTDIR="$JAIL_DIR"
make distribution DESTDIR="$JAIL_DIR"
cat > "$JAIL_DIR"/etc/rc.conf <<EOF
ifconfig_e0b_test="inet 192.168.16.2 netmask 255.255.255.0"
defaultrouter="192.168.16.1"
EOF
rm /usr/local/etc/rc.d/buildsetup /etc/rc.conf.d/buildsetup
echo "$next_status" > $STATUS_FILE
current_status=$(cat $STATUS_FILE)
fi
if [ "$current_status" = 5 ]; then
echo "Your machine and jail have been updated, you can now start the jail with:"
echo " service jail onestart test"
echo ""
echo "To start over:"
echo " - Update the revision in this script"
echo " - Delete the contents of $JAIL_DIR"
echo " - Delete $STATUS_FILE"
fi
@tomalexander

This comment has been minimized.

Copy link
Owner Author

@tomalexander tomalexander commented Dec 29, 2020

Screenshot of debugger you get when running this on FreeBSD 13 with the GENERIC kernel
VirtualBox_13_29_12_2020_17_29_23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment