ungleich-tools/opennebula-images/openbsd-build-opennebula-image.sh

145 lines
3.8 KiB
Bash
Raw Normal View History

#!/bin/sh
# This script generates OpenBSD images for OpenNebula, being inspired from
# srht's OpenBSD build image definition. It assumes running on an OpenBSD host.
set -e
set -x
# XXX: Handle command-line arguments?
RELEASE=7.4
ARCH=amd64
IMAGE_PATH="$(pwd)/openbsd-$RELEASE-$(date +"%Y-%m-%d").img"
IMAGE_SIZE=10G
VIRTUAL_DEVICE=vnd0
# Setup working directory.
workdir="$(mktemp -d)"
cd "${workdir:?}"
cleanup() {
# The order here is important.
umount /mnt/dev/pts 2>/dev/null || true
umount /mnt/dev/shm 2>/dev/null || true
umount /mnt/dev 2>/dev/null || true
umount /mnt/proc 2>/dev/null || true
umount /mnt/run 2>/dev/null || true
umount /mnt/sys 2>/dev/null || true
umount /mnt/boot 2>/dev/null || true
umount /mnt 2>/dev/null || true
vnconfig -u "$VIRTUAL_DEVICE"
rm -r "${workdir:?}"
}
# Create base image.
vmctl create -s "$IMAGE_SIZE" "$IMAGE_PATH"
vnconfig "$VIRTUAL_DEVICE" "$IMAGE_PATH"
# Don't forget to cleanup, even if the script crash.
trap cleanup EXIT
# Format disk, mount /mnt.
fdisk -iy "${VIRTUAL_DEVICE}"
cat > "${workdir}/partitions" <<EOF
/ 1G-* 100%
EOF
disklabel -w -A -T "${workdir:?}/partitions" "${VIRTUAL_DEVICE}"
# Fetch base system.
short_version="$(echo "$RELEASE" | tr -d .)"
openbsd_sets="base comp xbase xshare"
openbsd_kernel="bsd.mp"
openbsd_installurl="${openbsd_installurl:-"https://cdn.openbsd.org/pub/OpenBSD"}"
mirror_dir="${RELEASE}"
openbsd_public_key="openbsd-${short_version}-base.pub"
set_files=""
for s in $openbsd_sets; do
set_files="$set_files ${s}${short_version}.tgz"
done
for f in $set_files $openbsd_kernel SHA256.sig SHA256
do
test -f "$f" || ftp "${openbsd_installurl}/${mirror_dir}/${ARCH}/${f}"
done
signify -Cp /etc/signify/"$openbsd_public_key" \
-x SHA256.sig $set_files $openbsd_kernel
# Install base system.
for f in $set_files
do
tar -zxphf "$f" -C /mnt
done
tar -zxphf /mnt/var/sysmerge/etc.tgz -C /mnt
tar -zxphf /mnt/var/sysmerge/xetc.tgz -C /mnt
cat > /mnt/etc/fstab <<EOF
/dev/sd0a / ffs rw,wxallowed 1 1
EOF
cd /mnt/dev
sh MAKEDEV all
cd "$workdir"
# Configure base system.
cat >>/mnt/etc/ssh/sshd_config <<EOF
PermitRootLogin yes
EOF
echo "openbsd" > /mnt/etc/myname
echo "nameserver 2606:4700:4700::1111" >> /mnt/etc/resolv.conf
echo "nameserver 1.1.1.1" > /mnt/etc/resolv.conf
echo "127.0.0.1 localhost.localdomain localhost" > /mnt/etc/hosts
echo "::1 localhost.localdomain localhost" >> /mnt/etc/hosts
echo "$openbsd_installurl" > /mnt/etc/installurl
ln -sf /usr/share/zoneinfo/UTC /mnt/etc/localtime
cat >>/mnt/etc/rc.conf.local <<EOF
dhcpleased_flags=NO
pf=NO
pflogd_flags=NO
resolvd_flags=NO
slaacd_flags=NO
sndiod_flags=NO
EOF
# Install kernel
cp "$openbsd_kernel" /mnt/bsd
cp SHA256.sig /mnt/var/db/installed.SHA256.sig
sha256 /mnt/bsd | (umask 077; sed 's,/mnt,,' >/mnt/var/db/kernel.SHA256)
rm -rf /mnt/usr/share/relink/kernel
mkdir -m 700 /mnt/usr/share/relink/kernel
tar -C /mnt/usr/share/relink/kernel -xzf /mnt/usr/share/relink/kernel.tgz GENERIC.MP
rm -f /mnt/usr/share/relink/kernel.tgz
echo "Relinking kernel"
chroot /mnt /bin/ksh -e -c "cd /usr/share/relink/kernel/GENERIC.MP; make newbsd; make newinstall" > /dev/null
# Update and install utilities.
chroot /mnt /usr/sbin/pkg_add $pkg_add_params -u
chroot /mnt /usr/sbin/pkg_add $pkg_add_params bash cloud-agent
echo '!/usr/local/libexec/cloud-agent "\$if"' > /mnt/etc/hostname.vio0
# Remove useless kernel object files. This saves about 300MB of space in the final image
rm -rf /mnt/usr/share/relink/kernel/GENERIC.MP/
# Disable boot wait. Saves 5 seconds
echo "boot" > /mnt/etc/boot.conf
# Dump root filesystem in OS image.
makefs "/dev/${VIRTUAL_DEVICE}a" /mnt
growfs -y "/dev/${VIRTUAL_DEVICE}a"
fsck -y "/dev/${VIRTUAL_DEVICE}a"
sync
# Setup bootloader.
mount /dev/vnd0a /mnt
installboot -vr /mnt ${VIRTUAL_DEVICE:?}
umount /mnt
# Convert raw image to qcow.
vmctl create -i "$IMAGE_PATH" "$IMAGE_PATH.qcow2"