hate-no-d/.forgejo/scripts/freebsd-build.sh

135 lines
4.1 KiB
Bash

#!/bin/sh
# Build hate-no-d inside a FreeBSD VM and copy the binary and the static
# library back.
#
# freebsd-build.sh <output-file> [source-dir]
#
# Needs qemu-system-x86, cloud-image-utils, openssh-client, xz and curl, plus
# /dev/kvm (act_runner: options: "--device /dev/kvm"); under TCG the boot takes
# many minutes. The guest runs with -snapshot, so nothing persists.
set -eu
VERSION="${FREEBSD_VERSION:-15.1}"
ARCH=amd64
BASE="https://download.freebsd.org/releases/VM-IMAGES/${VERSION}-RELEASE/${ARCH}/Latest"
IMAGE="FreeBSD-${VERSION}-RELEASE-${ARCH}-BASIC-CLOUDINIT-ufs.qcow2.xz"
SSH_PORT="${FREEBSD_SSH_PORT:-2222}"
if [ $# -lt 1 ]; then
echo "usage: $0 <output-file> [source-dir]" >&2
exit 2
fi
OUT_DIR=$(dirname "$1")
mkdir -p "$OUT_DIR"
OUT=$(cd "$OUT_DIR" && pwd)/$(basename "$1")
SRC=$(cd "${2:-.}" && pwd)
WORK="${FBSD_WORK:-${TMPDIR:-/tmp}/fbsd-ci}"
log() { echo "freebsd-build: $*"; }
die() { echo "freebsd-build: $*" >&2; exit 1; }
[ -r /dev/kvm ] && [ -w /dev/kvm ] || die "/dev/kvm is not usable; the runner must expose it (container options: --device /dev/kvm)"
mkdir -p "$WORK"
cd "$WORK"
# --- guest credentials -------------------------------------------------------
[ -f id_ci ] || ssh-keygen -t ed25519 -N '' -f id_ci -q
chmod 600 id_ci
PUB=$(cat id_ci.pub)
cat > user-data <<EOF
#cloud-config
hostname: fbsd-ci
disable_root: false
ssh_pwauth: false
users:
- name: root
ssh_authorized_keys:
- $PUB
runcmd:
- sed -i '' -e 's/^#*PermitRootLogin.*/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
- sysrc sshd_enable=YES
- service sshd restart
EOF
cat > meta-data <<'EOF'
instance-id: fbsd-ci-1
local-hostname: fbsd-ci
EOF
# --- disk image --------------------------------------------------------------
if [ ! -f disk.qcow2 ]; then
[ -f "$IMAGE" ] || { log "downloading $IMAGE"; curl -fsSL -o "$IMAGE" "$BASE/$IMAGE"; }
log "decompressing"
xz -dc -T0 "$IMAGE" > disk.qcow2
fi
log "building cloud-init seed"
cloud-localds seed.iso user-data meta-data
# --- boot --------------------------------------------------------------------
SSH_OPTS="-i $WORK/id_ci -p $SSH_PORT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o BatchMode=yes -o ConnectTimeout=3"
SCP_OPTS="-i $WORK/id_ci -P $SSH_PORT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o BatchMode=yes"
rm -f console.log qemu.pid
qemu_pid=""
cleanup() {
if [ -n "$qemu_pid" ] && kill -0 "$qemu_pid" 2>/dev/null; then
kill "$qemu_pid" 2>/dev/null || true
wait "$qemu_pid" 2>/dev/null || true
fi
}
trap cleanup EXIT INT TERM
log "booting FreeBSD $VERSION"
qemu-system-x86_64 \
-M q35 -cpu host -smp 2 -m 2048 \
-enable-kvm \
-drive file=disk.qcow2,if=virtio,format=qcow2,snapshot=on \
-drive file=seed.iso,if=virtio,media=cdrom,readonly=on \
-netdev "user,id=n0,hostfwd=tcp:127.0.0.1:${SSH_PORT}-:22" \
-device virtio-net-pci,netdev=n0 \
-display none -serial file:console.log \
-daemonize -pidfile qemu.pid
qemu_pid=$(cat qemu.pid)
ssh_vm() { ssh $SSH_OPTS "root@127.0.0.1" "$@"; }
i=0
until ssh_vm true 2>/dev/null; do
i=$((i + 1))
if [ "$i" -gt 90 ]; then
tail -40 console.log >&2 || true
die "guest did not become reachable over SSH"
fi
sleep 2
done
log "guest up after ~$((i * 2))s: $(ssh_vm 'uname -sr')"
# --- build -------------------------------------------------------------------
log "installing gmake in guest"
ssh_vm 'set -e; env ASSUME_ALWAYS_YES=yes pkg install -y gmake >/dev/null 2>&1; gmake --version | head -1'
log "transferring source"
tar czf "$WORK/src.tar.gz" -C "$SRC" \
--exclude=.git --exclude=.build --exclude=hate-no-d \
--exclude='*.o' --exclude='*.a' --exclude='*.so' .
scp $SCP_OPTS "$WORK/src.tar.gz" "root@127.0.0.1:/root/src.tar.gz"
# FreeBSD has no gcc: CC must be on the command line to reach the sub-make.
log "building"
ssh_vm 'set -e
rm -rf /root/src && mkdir -p /root/src
tar xzf /root/src.tar.gz -C /root/src
cd /root/src
gmake CC=cc clean >/dev/null 2>&1 || true
gmake CC=cc all'
log "fetching binary"
mkdir -p "$(dirname "$OUT")"
scp $SCP_OPTS "root@127.0.0.1:/root/src/hate-no-d" "$OUT"
log "built $OUT ($(wc -c < "$OUT") bytes)"