#! /bin/sh # Auto-install OpenBSD/amd64 6.6 to a QEMU guest machine. # # Inspired by: # # - https://man.openbsd.org/autoinstall # # - https://eradman.com/posts/autoinstall-openbsd.html # # - https://drewdevault.com/2018/09/10/Getting-started-with-qemu.html # # Copyright (c) 2020 Stefan Kreutz set -o errexit set -o xtrace # Fail early if any necessary tools are missing. for c in curl qemu-img qemu-system-x86_64 rsync signify ssh do command -v "$c" >/dev/null done # Read the public ssh key to be installed in the guest. ssh_pub_key="$( cat ~/.ssh/id_rsa.pub )" # Generate a response file for unattended installation. mkdir -p http cat << EOF > http/install.conf Change the default console to com0 = yes Which speed should com0 use = 115200 System hostname = openbsd-vm Password for root = ************* Public ssh key for root account = ${ssh_pub_key} Allow root ssh login = prohibit-password Setup a user = puffy Password for user = ************* Public ssh key for user = ${ssh_pub_key} What timezone are you in = UTC Location of sets = http HTTP Server = 10.0.2.2 Unable to connect using https. Use http instead = yes EOF # Download OpenBSD/amd64 6.6 distribution. [ -e openbsd-66-base.pub ] || curl --output openbsd-66-base.pub --silent https://ftp.openbsd.org/pub/OpenBSD/6.6/openbsd-66-base.pub ( [ -d http/pub/OpenBSD/6.6/amd64 ] && ( cd http/pub/OpenBSD/6.6/amd64 && signify -C -q -p ../../../../../openbsd-66-base.pub -x SHA256.sig ) ) || { mkdir -p http/pub/OpenBSD/6.6/amd64 rsync --recursive --delete --quiet rsync://ftp.halifax.rwth-aachen.de/openbsd/6.6/amd64/ http/pub/OpenBSD/6.6/amd64/ ( cd http/pub/OpenBSD/6.6/amd64 && signify -C -q -p ../../../../../openbsd-66-base.pub -x SHA256.sig ) } # TODO: Start HTTP server. # sudo python3 -m http.server --directory http 80 # Collect files to be served over TFTP. mkdir -p tftp cmp -s http/pub/OpenBSD/6.6/amd64/pxeboot tftp/pxeboot || cp -a http/pub/OpenBSD/6.6/amd64/pxeboot tftp/pxeboot cmp -s http/pub/OpenBSD/6.6/amd64/bsd.rd tftp/bsd.rd || cp -a http/pub/OpenBSD/6.6/amd64/bsd.rd tftp/bsd.rd rm -f tftp/auto_install ln -s pxeboot tftp/auto_install # Create boot configuration. mkdir -p tftp/etc cat << EOF > tftp/etc/boot.conf stty com0 115200 set tty com0 boot tftp:/bsd.rd EOF # Create copy-on-write disk image. [ -e vm.qcow2 ] || qemu-img create -f qcow2 vm.qcow2 16G # Auto-install and start guest machine. # # network = 10.0.2.0/24 # host = 10.0.2.2 # dns = 10.0.2.3 # guest = 10.0.2.15-31 qemu-system-x86_64 \ -enable-kvm \ -m 2G \ -device e1000,netdev=n1 \ -netdev user,id=n1,tftp=tftp,bootfile=auto_install,hostfwd=tcp::2222-:22 \ -drive file=vm.qcow2,media=disk,if=virtio \ -nographic # Connect with the following command: # # ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "Port 2222" root@127.0.0.1 # # Execute a command (add -T): # # ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "Port 2222" -T root@127.0.0.1 "uptime" # # Execute a heredoc: # # ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "Port 2222" -T root@127.0.0.1 << SSHEOF # set -o errexit # uptime # SSHEOF # # Copy a file: # # scp -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -o "Port 2222" -r notes.txt root@127.0.0.1: # # Stop guest machine with C-a x # # Show help with C-a h # TODO: Install a site-specific file set to apply the following patches. # # echo 'https://cdn.openbsd.org/pub/OpenBSD' > /etc/installurl # echo 'permit keepenv :wheel' > /etc/doas.conf # syspatch # shutdown -r now