From 62b4a30deb39ea618eaf3e20a29234ecffea6359 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kai=20L=C3=BCke?= <kai@kinvolk.io>
Date: Tue, 15 Jun 2021 11:43:23 +0200
Subject: [PATCH 1/3] bootstrap: recreate node when PXE boot is forced with
 plain QEMU

In the QEMU mode where the prepare.sh script is directly used (i.e.,
not the Racker QEMU IPMI simulator environment), the reprovisioning
with PXE could be only forced when the VM got deleted manually before.
Automatically delete any existing VM before creating it.

Fixes https://github.com/kinvolk/racker/issues/42
---
 bootstrap/prepare.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bootstrap/prepare.sh b/bootstrap/prepare.sh
index 8d7a02b..a1814cc 100755
--- a/bootstrap/prepare.sh
+++ b/bootstrap/prepare.sh
@@ -630,7 +630,7 @@ EOF
     tee -a "${variable_file}" >/dev/null <<-EOF
 	kernel_console = []
 	install_pre_reboot_cmds = ""
-	pxe_commands = "sudo virt-install --name \$domain --network=bridge:${INTERNAL_BRIDGE_NAME},mac=\$mac  --network=bridge:${EXTERNAL_BRIDGE_NAME} --memory=${VM_MEMORY} --vcpus=1 --disk pool=default,size=${VM_DISK} --os-type=linux --os-variant=generic --noautoconsole --events on_poweroff=preserve --boot=hd,network"
+	pxe_commands = "sudo virsh destroy \$domain || true; sudo virsh undefine \$domain || true; sudo virsh pool-refresh default || true; sudo virsh vol-delete --pool default \$domain.qcow2 || true; sudo virt-install --name \$domain --network=bridge:${INTERNAL_BRIDGE_NAME},mac=\$mac  --network=bridge:${EXTERNAL_BRIDGE_NAME} --memory=${VM_MEMORY} --vcpus=1 --disk pool=default,size=${VM_DISK} --os-type=linux --os-variant=generic --noautoconsole --events on_poweroff=preserve --boot=hd,network"
 EOF
   else
     # The first ipmitool raw command is used to disable the 60 secs timeout that clears the boot flag

From a4ac4dabbc8c46135faa4a6f3f38449d58faa833 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kai=20L=C3=BCke?= <kai@kinvolk.io>
Date: Tue, 15 Jun 2021 11:58:44 +0200
Subject: [PATCH 2/3] bootstrap: fix case where no node file is used with QEMU

The NODES variable is undefined with plain QEMU usage.
---
 bootstrap/prepare.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/bootstrap/prepare.sh b/bootstrap/prepare.sh
index a1814cc..1319fe3 100755
--- a/bootstrap/prepare.sh
+++ b/bootstrap/prepare.sh
@@ -523,7 +523,11 @@ function gen_cluster_vars() {
     name="controller"
   fi
   for mac in ${MAC_ADDRESS_LIST[*]}; do
-    node_color="$(echo "$NODES" | grep ${mac} | cut -d , -f 4 | xargs)"
+    if [ -n "$USE_QEMU" ]; then
+      node_color=""
+    else
+      node_color="$(echo "$NODES" | grep ${mac} | cut -d , -f 4 | xargs)"
+    fi
     ip_address="$(calc_ip_addr $mac)"
     if [ "$type" = "lokomotive" ]; then
       id="${CLUSTER_NAME}-${name}-${count}.${KUBERNETES_DOMAIN_NAME}"

From 6df3ac4334362a4e3d9b5c9f5925c2c78ba62e2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kai=20L=C3=BCke?= <kai@kinvolk.io>
Date: Tue, 15 Jun 2021 12:20:55 +0200
Subject: [PATCH 3/3] README: document how to use the standalone QEMU dev
 environment

Lokomotive Baremetal development requires a larger local setup
consisting of a DHCP bridge for PXE with Matchbox and dnsmasq.
The prepare script has a mode to work with local VMs (not the QEMU
IPMI simulator) and it was not documented yet.
---
 README.md | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/README.md b/README.md
index fc48803..7a95ec0 100644
--- a/README.md
+++ b/README.md
@@ -82,6 +82,34 @@ To upgrade to the latest version which may have breaking changes, run `racker up
 
 Both will update the `lokoctl` and `terraform` binaries, too.
 
+### Lokomotive Baremetal Development Environment
+
+The part of Racker which creates the Lokomotive configuration can be run stand-alone to set up libvirt QEMU instances on your laptop.
+This is different from the [IPMI QEMU simulator environment](racker-sim/) which is preferred as it fully utilizes Racker.
+However, for quick development of Racker/Lokomotive this is how to run it:
+
+```
+cd /var/tmp/
+mkdir mycluster # "prepare.sh create" must run in an empty folder with just the controller_macs/worker_macs files
+cd mycluster
+echo 0c:42:a1:11:11:11 > controller_macs
+echo 0c:42:a1:11:11:22 > worker_macs
+# compile the right Lokomotive branch used in Racker (see installer/conf.yaml)
+sudo rm -r /opt/racker/terraform/
+sudo mkdir -p /opt/racker/terraform
+sudo cp -r /home/$USER/kinvolk/lokomotive/assets/terraform-modules/matchbox-flatcar/* /opt/racker/terraform
+PATH="$PATH:/home/$USER/kinvolk/lokomotive" /home/$USER/kinvolk/racker/bootstrap/prepare.sh create
+[…]
+PATH="$PATH:/home/$USER/kinvolk/lokomotive" lokoctl cluster apply # or any other things you want to do
+[…]
+# later destroy it again:
+PATH="$PATH:/home/$USER/kinvolk/lokomotive" /home/$USER/kinvolk/racker/bootstrap/prepare.sh destroy
+```
+
+It will create two bridges, one for the internal PXE and one for the network with Internet access (using NAT).
+Matchbox and dnsmasq are started as containers (when using Podman matchbox is a user container and dnsmasq a root container).
+The `/opt/racker-state/` folder gets populated with the Flatcar image and the Matchbox configuration.
+
 ## Code of Conduct
 
 Please refer to the Kinvolk [Code of Conduct](https://github.com/kinvolk/contribution/blob/master/CODE_OF_CONDUCT.md).