Initial sourcehut-inspired freebsd image build script

This commit is contained in:
fnux 2020-05-24 08:49:41 +02:00
parent 67c8cc94a4
commit 0d554866c8

View file

@ -0,0 +1,109 @@
#!/bin/sh
# This script generates FreeBSD images for OpenNebula, being heavily inspired
# from srht's FreeBSD build image definition. It assumes running on a FreeBSD host.
set -e
set -x
# XXX: Handle command-line arguments?
RELEASE=12.1-RELEASE
IMAGE_PATH=freebsd-$RELEASE-$(date +%+F).img.qcow2
IMAGE_SIZE=10G
# TODO: find the package definition and built ourself, publish in some RPM repository.
ONE_CONTEXT_RPM_URL="https://github.com/OpenNebula/addon-context-linux/releases/download/v5.10.0/one-context-5.10.0-1.el8.noarch.rpm"
ONE_CONTEXT_RPM_PATH=/root/one-context.rpm
cleanup() {
sync || true
umount /mnt/dev || true
umount /mnt || true
mdconfig -du md0 || true
}
trap cleanup EXIT
if [ "$(whoami)" != 'root' ]; then
echo "This script must be run as root." >&2
exit 1
fi
# Allocate and partition/format disk image.
truncate -s 6G disk.img
mdconfig -a -t vnode -f disk.img -u md0
gpart create -s gpt /dev/md0
gpart add -t freebsd-boot -l bootfs -b 40 -s 512K md0
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 md0
gpart add -t freebsd-ufs -l rootfs -b 1M -s 5G md0
newfs -U /dev/md0p2
# Mount allocated image.
mount /dev/md0p2 /mnt
mkdir -p /mnt/dev
mount -t devfs devfs /mnt/dev
# Download and extract base system.
dist_files="kernel.txz base.txz"
dist_dir="/usr/freebsd-dist/$arch/$release"
mkdir -p "$dist_dir"
for f in $dist_files
do
fetch -m -o "$dist_dir/$f" "$dist_base/$f"
tar -C /mnt -xJf "$dist_dir/$f"
done
# Configure new system.
echo "/dev/gpt/rootfs / ufs rw,noatime 1 1" >/mnt/etc/fstab
touch /mnt/firstboot
echo 'autoboot_delay="-1"' >>/mnt/boot/loader.conf
cat >>/mnt/etc/rc.conf <<EOF
ntpd_enable=YES
sshd_enable=YES
growfs_enable=YES
hostname="freebsd"
EOF
cp /etc/resolv.conf > /mnt/etc/resolv.conf
tzsetup -s -C /mnt UTC
cat >>/mnt/etc/ssh/sshd_config <<EOF
PermitRootLogin yes
PasswordAuthentication no
PermitEmptyPasswords no
EOF
mkdir -p /mnt/usr/local/etc/pkg/repos/
cat >/mnt/usr/local/etc/pkg/repos/FreeBSD.conf <<EOF
FreeBSD: {
url: pkg+http://pkg.FreeBSD.org/\$\{ABI\}/latest
enabled: yes
}
EOF
# freebsd-update is only supported for RELEASE
if [ "${release%-RELEASE}" != "$release" ]
then
env PAGER=true /usr/sbin/freebsd-update \
-b /mnt \
--currently-running "$release" \
--not-running-from-cron -F \
fetch install
fi
env ASSUME_ALWAYS_YES=YES pkg -c /mnt bootstrap -f
env ASSUME_ALWAYS_YES=YES pkg -c /mnt install bash
fetch -m -o "$dist_dir/ports.txz" "$ports_base/ports.txz"
tar -C /mnt -xJf "$dist_dir/ports.txz"
cleanup
trap : EXIT
mkdir -p "$arch"
qemu-img convert -f raw -O qcow2 disk.img "$arch"/root.img.qcow2
rm disk.img
# Filesystem will be enlarged by growfs(7) on next startup
qemu-img resize $IMAGE_PATH $IMAGE_SIZE