#!/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" < /mnt/etc/fstab <>/mnt/etc/ssh/sshd_config < /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 </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"