From f9433ad477443fe6fd3add144b1febe0a916358c Mon Sep 17 00:00:00 2001 From: vandomej Date: Fri, 3 Oct 2025 09:58:04 -0700 Subject: [PATCH] Defining libvirt network definition --- control-plane/control-plane-net.xml | 17 ++ control-plane/main.tf | 100 ++------- control-plane/vagrant/Vagrantfile | 29 --- control-plane/vagrant/setup.sh | 2 - .../vm-setup/create-control-plane-vms.sh | 202 ------------------ 5 files changed, 37 insertions(+), 313 deletions(-) create mode 100644 control-plane/control-plane-net.xml delete mode 100644 control-plane/vagrant/Vagrantfile delete mode 100755 control-plane/vagrant/setup.sh delete mode 100755 control-plane/vm-setup/create-control-plane-vms.sh diff --git a/control-plane/control-plane-net.xml b/control-plane/control-plane-net.xml new file mode 100644 index 0000000..338d3e0 --- /dev/null +++ b/control-plane/control-plane-net.xml @@ -0,0 +1,17 @@ + + my-talos-net + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/control-plane/main.tf b/control-plane/main.tf index ccde7f9..0eb125a 100644 --- a/control-plane/main.tf +++ b/control-plane/main.tf @@ -1,5 +1,19 @@ terraform { required_version = ">= 1.0" + required_providers { + talos = { + source = "siderolabs/talos" + version = "~> 0.9" + } + libvirt = { + source = "dmacvicar/libvirt" + version = "~> 0.7" + } + } +} + +provider "libvirt" { + uri = "qemu:///system" } # Load variables from a .tfvars file @@ -27,85 +41,11 @@ variable "talos_version" { default = "v1.11.1" } -resource "null_resource" "talos_cluster" { - triggers = { - cluster_name = var.cluster_name - memory_mb = var.memory_mb - vcpu_count = var.vcpu_count - talos_version = var.talos_version - config_hash = sha1(join("", [ - var.cluster_name, - tostring(var.memory_mb), - tostring(var.vcpu_count), - var.talos_version, - filesha1("${path.module}/cleanup.sh") # Recreate if cleanup script changes - ])) +resource "libvirt_network" "talos_bridge" { + name = "my-talos-net" + autostart = true + + xml { + file = "control-plane-net.xml" } - - # Download Talos kernel and initramfs - provisioner "local-exec" { - command = < "$script_path" << EOF -#!/bin/bash -OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES qemu-system-aarch64 \\ - -name "$vm_name" \\ - -machine virt,highmem=off \\ - -accel hvf \\ - -cpu host \\ - -smp "$CPUS" \\ - -m "${MEMORY}M" \\ - -drive file="$disk_path",if=virtio,format=qcow2 \\ - -kernel "$kernel_path" \\ - -initrd "$initramfs_path" \\ - -append "talos.platform=metal console=tty0 console=ttyS0" \\ - -netdev user,id=net0 \\ - -device virtio-net-pci,netdev=net0,mac=$mac_address \\ - -nographic \\ - -serial mon:stdio -EOF - - chmod +x "$script_path" - echo "$script_path" -} - -# Create systemd service file (optional) -create_service_file() { - local vm_path=$1 - local vm_name=$2 - local script_path=$3 - - local service_path="$vm_path/$vm_name.service" - - cat > "$service_path" << EOF -[Unit] -Description=Talos VM - $vm_name -After=network.target - -[Service] -Type=simple -ExecStart=$script_path -WorkingDirectory=$vm_path -Restart=always -User=$USER - -[Install] -WantedBy=multi-user.target -EOF - - echo "$service_path" -} - -# Main execution -main() { - log_info "Creating Talos VMs with QEMU..." - - if ! command -v qemu-system-aarch64 &> /dev/null; then - log_error "QEMU is not installed. Install with: brew install qemu" - exit 1 - fi - - log_info "Creating controlplane VM..." - local controlplane_path=$(create_vm_dir "$CONTROLPLANE_NAME") - local controlplane_disk=$(create_disk_image "$controlplane_path" "$CONTROLPLANE_NAME") - local controlplane_mac=$(generate_mac "$CONTROLPLANE_NAME") - local kernel_path=$(download_kernel_file) - local initramfs_path=$(download_initramfs_file) - local controlplane_script=$(create_vm_script "$controlplane_path" "$CONTROLPLANE_NAME" "$controlplane_mac" "$kernel_path" "$initramfs_path" "$controlplane_disk") - create_service_file "$controlplane_path" "$CONTROLPLANE_NAME" "$controlplane_script" - - local manage_script="$VM_DIR/manage-vm.sh" - cat > "$manage_script" << 'EOF' -#!/bin/bash -VM_DIR="$(cd "$(dirname "$0")" && pwd)" - -start_vm() { - echo "Starting Talos VM..." - "$VM_DIR/talos-controlplane/start.sh" & -} - -stop_vm() { - echo "Stopping Talos VM..." - pkill -f "qemu-system-aarch64.*talos-controlplane" -} - -case "$1" in - start) - start_vm - ;; - stop) - stop_vm - ;; - status) - pgrep -f "qemu-system-aarch64.*talos-controlplane" > /dev/null && echo "VM is running" || echo "VM is stopped" - ;; - *) - echo "Usage: $0 {start|stop|status}" - exit 1 - ;; -esac -EOF - chmod +x "$manage_script" - - log_info "VM creation complete!" - log_info "VM files located at: $VM_DIR" - log_info "" - log_info "To start VM: $manage_script start" - log_info "To stop VM: $manage_script stop" - log_info "" - log_info "Controlplane MAC: $controlplane_mac" -} - -main "$@" \ No newline at end of file