#!/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=13.0-RELEASE ARCH=amd64 IMAGE_PATH=freebsd-$RELEASE-$(date -I).img.qcow2 IMAGE_SIZE=10G DIST_BASE="https://download.freebsd.org/ftp/releases/$ARCH/$RELEASE" ONE_CONTEXT_PKG_URL="https://github.com/OpenNebula/addon-context-linux/releases/download/v6.2.0/one-context-6.2.0_1.txz" 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 env ASSUME_ALWAYS_YES=YES pkg install -y qemu-tools # Allocate and partition/format disk image. disk=$(mktemp) truncate -s 6G $disk mdconfig -a -t vnode -f $disk -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 sysrc -f /mnt/boot/loader.conf autoboot_delay=-1 sysrc -f /mnt/etc/rc.conf ntpd_enable=YES sshd_enable=YES growfs_enable=YES hostname=freebsd cp /etc/resolv.conf /mnt/etc/resolv.conf tzsetup -s -C /mnt UTC cat >>/mnt/etc/ssh/sshd_config </mnt/usr/local/etc/pkg/repos/FreeBSD.conf # 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 rm -rf /mnt/var/db/freebsd-update/* fi env ASSUME_ALWAYS_YES=YES pkg -c /mnt bootstrap -f fetch -m -o /mnt/one-context.txz "$ONE_CONTEXT_PKG_URL" # OpenNebula has dependencies, but these are not included in the package for some reason # https://github.com/OpenNebula/addon-context-linux/blob/40efc929487b2955e6f32643853a5cdc93c548da/targets.sh#L25 # It would be useful to see if there is an alternative to OpenNebula without so many dependencies, # so we can run on FreeBSD base, and avoid breaking OpenNebula when the admin removes a dependency. env ASSUME_ALWAYS_YES=YES pkg -c /mnt install sudo bash curl base64 ruby open-vm-tools-nox11 gawk virt-what one-context.txz env ASSUME_ALWAYS_YES=YES pkg -c /mnt clean --all rm /mnt/one-context.txz trap : EXIT cleanup mkdir -p "$ARCH" qemu-img convert -f raw -O qcow2 "$disk" "$IMAGE_PATH" rm "$disk" # Filesystem will be enlarged by growfs(7) on next startup qemu-img resize "$IMAGE_PATH" "$IMAGE_SIZE"