+set -e; . /usr/local/lib/bash-bear; set +e
+
+this_file="$(readlink -f -- "${BASH_SOURCE[0]}")"
+readonly this_file this_dir="${this_file%/*}"
+cd "$this_dir"
+
+PATH="$PATH:$PWD"
+
+e() { echo "$*"; "$@"; }
+
+
+usage() {
+ cat <<EOF
+# Usage: ${0##*/} [OPTIONS]
+Setup fai or arch pxe (depending on $0 name)
+then start a virtual machine to test the config
+
+note: useful for debugging:
+apt install guestmount
+guestmount -a /var/lib/libvirt/images/demohost1 -i --ro /mnt/1
+# when done:
+guestunmount /mnt/1
+
+todo: make it so this can run this on a network we dont control, the 2
+ways I know which could work are either running in libvirt's the nated
+network, and altering the dnsmasq options for the dnsmasq that runs in
+that, or giving the vm a static ip and for resolving faiserver, and then
+for resolving "faiserver", either setup some static resolution in the
+vm, or give it the host machine's ip as a dns server, or in general
+change references of faiserver to faiserver.b8.nz (I like this idea
+because it helps in other cases too).
+
+Note, sometimes shutting down the existing demohost vm
+fails. Just run again if that happens.
+
+-d When doing pxe with -p, don't do dhcp setup. Good for when we
+ aren't on Ian's home network.
+-n Create new qcow2(s) for vm. Good for testing partitioning
+ script, to ensure a blank disk.
+-p Use pxe instead of autodiscover iso with fai.
+-c Use normal fai-cd iso is instead of autodiscover iso.
+-r Do not boot after install is complete
+-h|--help Print help and exit.
+
+Note: Uses GNU getopt options parsing style
+EOF
+ exit $1
+}
+
+orig_args=("$@")
+new_disk=false
+pxe=false
+iso=autodiscover.iso
+temp=$(getopt -l help dnpcrh "$@") || usage 1
+eval set -- "$temp"
+while true; do
+ case $1 in
+ -d) dhcp_arg=-d; shift ;;
+ -n) new_disk=true; shift ;;
+ -p) pxe=true; shift ;;
+ -c) iso=netinst.iso; shift ;;
+ -r) reboot_arg=--noreboot; shift ;;
+ -h|--help) usage ;;
+ --) shift; break ;;
+ *) echo "$0: Internal error!" ; exit 1 ;;
+ esac
+done
+
+
+# change this to test different disk counts.
+disk_count=1
+disk_count=3
+
+
+rm -f /tmp/fai-revm-did-pxe
+
+if ip l show br0 &>/dev/null; then
+ net_arg="-w bridge=br0,mac=52:54:00:9c:ef:ad"
+else
+ # if this computer has ethernet, we could setup a br0 like so:
+ # cat <<'EOF'
+ # fai-rvm error: no bridge detected. add one to interfaces like this:
+ # iface eth0 inet manual
+ # iface br0 inet dhcp
+ # bridge_ports eth0
+ # bridge_stp off
+ # bridge_maxwait 0
+ # EOF
+
+ # if we only have wifi, cant use eth0
+ net_arg="-w network=default,mac=52:54:00:9c:ef:ad"
+
+
+ # make the dynamic range not conflict with static ip.
+ sub="range start='192.168.122.100' end='192.168.122.254'"
+ if ! virsh net-dumpxml default | grep -F "$sub" &>/dev/null; then
+ virsh net-update default delete ip-dhcp-range '<range start="192.168.122.2" end="192.168.122.254"/>' --live --config
+ virsh net-update default add ip-dhcp-range "<range $sub/>" --live --config
+ fi
+ sub="name='demohost' ip='192.168.122.84'"
+ if ! virsh net-dumpxml default | grep -F "$sub" &>/dev/null; then
+ virsh net-update default add ip-dhcp-host "<host mac='52:54:00:9c:ef:ad' $sub />" --live --config;
+ fi
+fi
+
+if [[ $this_dir == /a/bin/* ]]; then
+ # Copy our script elsewhere so we can develop it
+ # and save it at the same time it's running
+ rm -rf /tmp/faifreeze
+ cp -ar /a/bin/fai /tmp/faifreeze
+ exec /tmp/faifreeze/${BASH_SOURCE##*/} "${orig_args[@]}"
+fi
+
+
+is_arch_revm() {
+ [[ ${0##*/} == arch-revm ]]
+}
+
+err-cleanup() {
+ echo "doing cleanup"
+ if [[ -e /tmp/fai-revm-did-pxe ]]; then
+ e ./pxe-server $dhcp_arg
+ fi
+ ./faiserver-disable
+}
+
+boot_arg=--pxe
+if is_arch_revm; then
+ e ./pxe-server $dhcp_arg demohost arch
+ touch /tmp/fai-revm-did-pxe
+ sleep 2
+ # via osinfo-query os. guessing arch is closest to latest fedora.
+ variant=fedora22
+else
+ if $pxe; then
+ e ./pxe-server $dhcp_arg demohost fai
+ touch /tmp/fai-revm-did-pxe
+ sleep 2
+ else
+ killall fai-monitor &>/dev/null ||:
+ fai-monitor &
+ if [[ ! $BASEFILE_DIR ]]; then
+ BASEFILE_DIR=/tmp
+ fi
+ isopath=$BASEFILE_DIR/$iso
+ isosrc=$BASEFILE_DIR/BOOKWORM64.tar.zst
+ if [[ ! -e $isopath || $(stat -c %Y $isopath) -lt $(stat -c %Y $isosrc) ]]; then
+ e fai-cd -g $(readlink -f grub.cfg.${iso%%.*}) -f -A $isopath
+ fi
+ boot_arg="--cdrom $isopath"
+ e fai-redep
+ /a/exe/cedit -s /srv/fai/nfsroot/root/.ssh/authorized_keys <~/.ssh/demo.pub
+ e myfai-chboot default
+ fi
+ # I don't think these variants actually make a diff for us, but I
+ # use the appropriate one when trying a new distro just in case.
+ #variant=ubuntu14.04
+ #variant=ubuntu16.04
+ #variant=debian8
+ variant=ubuntu20.04
+fi
+
+name=demohost
+
+e virsh destroy $name ||:
+sleep 1
+e virsh destroy $name ||:
+e virsh undefine $name ||:
+sleep 1
+
+## begin virtual disk creation ##
+disk_arg=()
+for ((i=1; i <= disk_count; i++)); do
+ f=/var/lib/libvirt/images/${name}$i
+ disk_arg+=("--disk path=$f")
+ if $new_disk || [[ ! -e $f ]]; then
+ rm -f $f
+ # https://btrfs.wiki.kernel.org/index.php/FAQ
+ touch $f
+ chattr +C $f
+ e qemu-img create -o preallocation=metadata -f qcow2 $f 50G
+ fi
+done
+## end virtual disk creation ##
+
+if [[ $SSH_CLIENT ]]; then
+ console_arg=--noautoconsole
+fi
+
+# docker makes forward default to drop, which blocks the vm pxe on flidas. easiest solution:
+e iptables -P FORWARD ACCEPT
+
+# --cpu host: this causes mkfs.btrfs to fail with a stack trace which began
+# something like:
+# init_module+0x108/0x1000 [raid6_pq]
+#
+
+cpus=1
+if (( $(nproc) > 2 )); then
+ cpus=2
+fi
+
+e systemctl start libvirtd
+if [[ ! $SSH_CLIENT ]]; then
+ graphics_arg="--graphics spice,listen=0.0.0.0"
+fi
+
+e virt-install --rng /dev/urandom --os-variant $variant -n $name $boot_arg -r 2048 --vcpus $cpus \
+ ${disk_arg[*]} $net_arg $reboot_arg \
+ $graphics_arg $console_arg
+
+
+sleep 90
+while ! timeout -s 9 10 ssh -oBatchMode=yes root@$name true; do
+ e sleep 5