From b23c70e9fce3f78a81f8cb580915902aa3573619 Mon Sep 17 00:00:00 2001
From: Mohamed Chiheb Ben Jemaa <mc.benjemaa@gmail.com>
Date: Wed, 23 Oct 2024 13:36:35 +0200
Subject: [PATCH] Add scsi controller and write docs

---
 .../ignition/proxmox-bootstrap-pass-auth.json | 46 -------------------
 images/capi/packer/proxmox/README.md          | 37 ++++++++++++++-
 images/capi/packer/proxmox/flatcar.json       |  2 +-
 images/capi/packer/proxmox/packer.json.tmpl   |  4 +-
 4 files changed, 39 insertions(+), 50 deletions(-)
 delete mode 100644 images/capi/packer/files/flatcar/ignition/proxmox-bootstrap-pass-auth.json

diff --git a/images/capi/packer/files/flatcar/ignition/proxmox-bootstrap-pass-auth.json b/images/capi/packer/files/flatcar/ignition/proxmox-bootstrap-pass-auth.json
deleted file mode 100644
index 8e09b18a1c..0000000000
--- a/images/capi/packer/files/flatcar/ignition/proxmox-bootstrap-pass-auth.json
+++ /dev/null
@@ -1,46 +0,0 @@
-{
-  "ignition": {
-    "config": {},
-    "security": {
-      "tls": {}
-    },
-    "timeouts": {},
-    "version": "2.3.0"
-  },
-  "networkd": {},
-  "passwd": {
-    "users": [
-      {
-        "groups": [
-          "wheel",
-          "sudo",
-          "docker"
-        ],
-        "name": "builder",
-        "passwordHash": "BUILDERPASSWORDHASH"
-      }
-    ]
-  },
-  "storage": {},
-  "systemd": {
-    "units": [
-      {
-        "enable": true,
-        "name": "docker.service"
-      },
-      {
-        "mask": true,
-        "name": "update-engine.service"
-      },
-      {
-        "mask": true,
-        "name": "locksmithd.service"
-      },
-      {
-        "name": "flatcar-openstack-hostname.service",
-        "enabled": false,
-        "mask": true
-      }
-    ]
-  }
-}
diff --git a/images/capi/packer/proxmox/README.md b/images/capi/packer/proxmox/README.md
index 96f45bb15d..fb6141b4a2 100644
--- a/images/capi/packer/proxmox/README.md
+++ b/images/capi/packer/proxmox/README.md
@@ -1,11 +1,12 @@
+## Custom Kubernetes version
+
 To build an image using a specific version of Kubernetes use the "PACKER_FLAGS" env var like in the example below:
 
 ```
 PACKER_FLAGS="--var 'kubernetes_rpm_version=1.28.3' --var 'kubernetes_semver=v1.28.3' --var 'kubernetes_series=v1.28' --var 'kubernetes_deb_version=1.28.3-1.1'" make build-proxmox-ubuntu-2204
 ```
 
-
-# ISO files
+## ISO files
 
 To use existing ISO files, set the `ISO_FILE` environment variable to the path of the ISO file.
 For example, to use a local ISO file, set the `ISO_FILE` environment variable like this:
@@ -13,3 +14,35 @@ For example, to use a local ISO file, set the `ISO_FILE` environment variable li
 ```
 export ISO_FILE="local:iso/ubuntu-24.04.1-live-server-amd64.iso"
 ```
+
+## Flatcar for Proxmox
+
+Currently, Proxmox doesn't support ignition and it's currently in-development.
+* https://github.com/coreos/fedora-coreos-tracker/issues/1652
+* https://github.com/flatcar/scripts/pull/1783
+
+But we do a trick to make it working on Proxmox, until the support is already released.
+
+We use OEM_ID `nutanix` which is an openstack provider that loads ignition from device with label `config-2`:
+https://github.com/coreos/ignition/blob/main/internal/providers/nutanix/nutanix.go#L51
+
+Therefore, we build an image with `OEM_ID=nutanix` so that we can provide an ISO that contain the ignition file in `/openstack/latest/user_data`
+https://github.com/coreos/ignition/blob/main/internal/providers/nutanix/nutanix.go#L40C29-L40C56
+
+**To build a Proxmox template for flatcar**
+
+```shell
+export PROXMOX_URL="https://example.net:8006/api2/json"
+export PROXMOX_USERNAME='root@pam!proxmox'
+export PROXMOX_TOKEN="xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx"
+export PROXMOX_NODE="pve1"
+export PROXMOX_ISO_POOL="local"
+export PROXMOX_BRIDGE="vmbr1"
+export PROXMOX_STORAGE_POOL="ceph_pool"
+
+## flatcar version
+export FLATCAR_VERSION=4081.1.0
+export FLATCAR_CHANNEL=beta
+
+export OEM_ID=nutanix # make sure to choose OEM_ID=nutanix
+```
diff --git a/images/capi/packer/proxmox/flatcar.json b/images/capi/packer/proxmox/flatcar.json
index 4f91f16ac8..64bd2df5ff 100644
--- a/images/capi/packer/proxmox/flatcar.json
+++ b/images/capi/packer/proxmox/flatcar.json
@@ -1,7 +1,7 @@
 {
   "ansible_extra_vars": "ansible_python_interpreter=/opt/bin/python oem_id={{user `oem_id`}}",
   "boot_command_prefix": "sudo systemctl mask sshd.socket --now<enter><wait>curl -sLo /tmp/ignition.json ",
-  "boot_command_suffix": "/proxmox-bootstrap-pass-auth.json<enter>sed -i \"s|BUILDERPASSWORDHASH|$(mkpasswd -5 {{user `ssh_password`}})|\" /tmp/ignition.json<enter>sudo flatcar-install -d /dev/sda -C {{user `channel_name`}} -V {{user `release_version`}} -i /tmp/ignition.json && sudo reboot<enter>",
+  "boot_command_suffix": "/bootstrap-pass-auth.json<enter>sed -i \"s|BUILDERPASSWORDHASH|$(mkpasswd -5 {{user `ssh_password`}})|\" /tmp/ignition.json<enter>sudo flatcar-install -d /dev/sda -C {{user `channel_name`}} -V {{user `release_version`}} -i /tmp/ignition.json && sudo reboot<enter>",
   "boot_media_path": "http://{{ .HTTPIP }}:{{ .HTTPPort }}",
   "boot_wait": "180s",
   "build_name": "flatcar-{{env `FLATCAR_CHANNEL`}}-{{env `FLATCAR_VERSION`}}",
diff --git a/images/capi/packer/proxmox/packer.json.tmpl b/images/capi/packer/proxmox/packer.json.tmpl
index ee6661416f..be037a95d8 100644
--- a/images/capi/packer/proxmox/packer.json.tmpl
+++ b/images/capi/packer/proxmox/packer.json.tmpl
@@ -19,6 +19,7 @@
           "type": "scsi"
         }
       ],
+      "scsi_controller": "{{user `scsi_controller`}}",
       "http_directory": "{{user `http_directory`}}",
       "insecure_skip_tls_verify": true,
       "iso_checksum": "{{user `iso_checksum_type`}}:{{user `iso_checksum`}}",
@@ -215,6 +216,7 @@
     "token": "{{env `PROXMOX_TOKEN`}}",
     "username": "{{env `PROXMOX_USERNAME`}}",
     "vlan_tag": "{{env `PROXMOX_VLAN`}}",
-    "vmid": ""
+    "vmid": "",
+    "scsi_controller": "virtio-scsi-pci"
   }
 }