Files
ilkerandClaude Opus 5.5 876eeea4b7 baremetal: wipe a released machine's disks before it is given again
Releasing a cluster now cleans each machine instead of clearing its port:
power off, into a cleaning VLAN (vlan_base+255) served only by
hpcod-clean on the KVM host, a network boot into a wipe image built from
that host's kernel and busybox, which discards each disk, zeroes and reads
back its ends (or all of it), and reports with a one-time token. The port
is freed only on a complete report of that cleaning with every disk
verified; otherwise the machine is powered off and held in the cleaning
VLAN, which discovery shows, and the release fails. hpcod baremetal clean
retries one. Setup installs and builds it on the KVM host (step clean);
the uplink always carries the cleaning VLAN.

Found on the way: the image first loaded every module it carries, which
brought up scsi_debug and zeroed its fake disk; drivers are now loaded by
modalias for the hardware present. A failed cleaning first left the
machine running; it is now powered off.

Checked on the simulated rack, each machine now with a local disk: data
written to two machines' disks read back as zero over all of them after
hpcod selftest -compute 2, whose release, with both cleanings, took 48 s;
a cleaning with the service stopped left the machine off and held, and a
retry freed it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 20:51:45 +01:00

155 lines
5.0 KiB
Terraform

# One cluster on a bare-metal site: a head on the site's head host, and
# physical machines for its compute nodes.
#
# The head is built exactly as a libvirt site builds one, by the same
# module, with two differences: its fabric NIC is on the head host's
# VLAN-aware bridge rather than on a network of its own, and no compute
# guests are made. The machines already exist. What this adds is putting
# their switch ports and the head's in the cluster's VLAN, and powering the
# machines on to network boot from the head once it is ready.
#
# There is no Terraform provider for a switch port or a Redfish BMC, so
# those are terraform_data resources whose provisioners call `hpcod
# baremetal`. Each call reads back what it changed before it exits, since
# Terraform takes exit 0 as done and never looks again.
terraform {
required_version = ">= 1.6"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.9.9"
}
cloudinit = {
source = "hashicorp/cloudinit"
version = "~> 2.3"
}
}
}
provider "libvirt" {
uri = var.libvirt_uri
}
locals {
vlan = var.vlan_base + var.cluster_id
bm = "${var.hpcod} baremetal -c ${var.sites_file} -site ${var.site_id}"
unknown = [for h in var.compute_hosts : h if !contains(keys(var.machines), h)]
}
module "tenant" {
source = "../../modules/libvirt"
cluster_id = var.cluster_id
tenant_id = var.tenant_id
tenant_name = var.tenant_name
head = var.head
compute = var.compute
mgmt_ip_cidr = var.mgmt_ip_cidr
head_host = var.head_host
compute_hosts = var.compute_hosts
devices = var.devices
apps = var.apps
control_plane_cert = var.control_plane_cert
started = var.started
pool = var.pool
head_base_path = var.head_base_path
mgmt_bridge = var.mgmt_bridge
mgmt_gateway = var.mgmt_gateway
nameservers = var.nameservers
mac_prefix = var.mac_prefix
ci_user = var.ci_user
ssh_public_keys = var.ssh_public_keys
fabric_bridge = var.fabric_bridge
compute_vms = false
# Placement named each node's machine; the head registers each by the
# MAC that machine boots with.
compute_macs = [for h in var.compute_hosts : var.machines[h].mac if contains(keys(var.machines), h)]
}
# The head's port: its fabric NIC is a tap on the head host's bridge.
resource "terraform_data" "head_port" {
input = {
bm = local.bm
mac = lower(module.tenant.head.fabric_mac)
vlan = local.vlan
}
provisioner "local-exec" {
command = "${self.input.bm} port -mac ${self.input.mac} -vlan ${self.input.vlan}"
}
provisioner "local-exec" {
when = destroy
command = "${self.input.bm} port -mac ${self.input.mac} -clear"
}
}
# Each machine's port, from the moment the cluster exists: a machine is in
# its cluster's VLAN before it is ever powered on. Taking it down is not
# clearing the port but cleaning the machine: off, into the cleaning VLAN,
# its disks wiped and read back, and only then a free port. A cleaning
# that fails fails the destroy, and the machine stays held in the cleaning
# VLAN - and the cluster unreleased - until someone looks.
resource "terraform_data" "machine_port" {
count = var.compute.count
input = {
bm = local.bm
machine = var.compute_hosts[count.index]
vlan = local.vlan
}
provisioner "local-exec" {
command = "${self.input.bm} port -machine ${self.input.machine} -vlan ${self.input.vlan}"
}
provisioner "local-exec" {
when = destroy
command = "${self.input.bm} clean -machine ${self.input.machine} -timeout 30m"
}
lifecycle {
precondition {
condition = length(var.compute_hosts) == var.compute.count && length(local.unknown) == 0
error_message = "every compute node needs a machine from the machines file; compute_hosts is ${jsonencode(var.compute_hosts)}${length(local.unknown) > 0 ? ", and ${join(", ", local.unknown)} is not in it" : ""}."
}
}
}
# Power, as started says: the machines network boot from the head, so they
# stay off until Warewulf on it is ready - the same switch the guests'
# running flag is on a libvirt site. Destroyed before the ports, so a
# machine is off before it leaves the VLAN.
resource "terraform_data" "power" {
count = var.started ? var.compute.count : 0
input = {
bm = local.bm
machine = var.compute_hosts[count.index]
vlan = local.vlan
}
provisioner "local-exec" {
command = "${self.input.bm} power -machine ${self.input.machine} -on -vlan ${self.input.vlan}"
}
provisioner "local-exec" {
when = destroy
command = "${self.input.bm} power -machine ${self.input.machine} -off"
}
depends_on = [terraform_data.machine_port, terraform_data.head_port, module.tenant]
}
output "tenant" {
value = {
bridge = module.tenant.bridge
subnet = module.tenant.subnet
head = module.tenant.head
compute = module.tenant.compute
vlan = local.vlan
}
}
output "ansible_inventory" {
value = module.tenant.ansible_inventory
}