From f6888134b840fe47aed464b5f5c4e4baf8aba71f Mon Sep 17 00:00:00 2001 From: Sho Ishida Date: Thu, 16 Jul 2026 06:24:13 +0000 Subject: [PATCH] Deploy initial virtual machine instances --- terraform/main.tf | 209 +++++++++++++++++++++ terraform/templates/cloud_init.cfg | 15 ++ terraform/templates/network_config.cfg.tpl | 14 ++ 3 files changed, 238 insertions(+) create mode 100644 terraform/main.tf create mode 100644 terraform/templates/cloud_init.cfg create mode 100644 terraform/templates/network_config.cfg.tpl diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000..aeb2443 --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,209 @@ +terraform { + required_version = ">=1.5.0" + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = "0.7.6" + } + } +} + +provider "libvirt" { + uri = "qemu+ssh://sho@172.30.1.200/system" +} + +resource "libvirt_pool" "vm_storage_pool" { + name = "vm_pool" + type = "dir" + path = "/var/lib/libvirt/images/vm_pool" +} + +resource "libvirt_volume" "almalinux10_image" { + name = "almalinux10-base.qcow2" + pool = libvirt_pool.vm_storage_pool.name + source = "https://repo.almalinux.org/almalinux/10/cloud/x86_64/images/AlmaLinux-10-GenericCloud-latest.x86_64.qcow2" + format = "qcow2" +} + +resource "libvirt_volume" "debian12_image" { + name = "debian12-base.qcow2" + pool = libvirt_pool.vm_storage_pool.name + source = "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2" + format = "qcow2" +} + +resource "libvirt_volume" "freeipa_disk" { + name = "freeipa-disk.qcow2" + pool = libvirt_pool.vm_storage_pool.name + base_volume_id = libvirt_volume.almalinux10_image.id + size = 42949672960 + format = "qcow2" +} + +resource "libvirt_volume" "portfolio_disk" { + name = "portfolio-disk.qcow2" + pool = libvirt_pool.vm_storage_pool.name + base_volume_id = libvirt_volume.debian12_image.id + size = 10737418240 + format = "qcow2" +} + +resource "libvirt_volume" "minecraft_disk" { + name = "minecraft-disk.qcow2" + pool = libvirt_pool.vm_storage_pool.name + base_volume_id = libvirt_volume.debian12_image.id + size = 21474836480 + format = "qcow2" +} + +resource "libvirt_volume" "navidrome_disk" { + name = "navidrome-disk.qcow2" + pool = libvirt_pool.vm_storage_pool.name + base_volume_id = libvirt_volume.debian12_image.id + size = 16106127360 + format = "qcow2" +} + +data "template_file" "user_data" { + template = file("${path.module}/templates/cloud_init.cfg") + vars = { + admin_user = "sho" + ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILM/84tpkx+yYsA8Zr5or1xuELOGMl0JEP576SyUc9eC sho@bazzite" + } +} + +resource "libvirt_cloudinit_disk" "freeipa_init" { + name = "freeipa-init.iso" + pool = libvirt_pool.vm_storage_pool.name + user_data = data.template_file.user_data.rendered + network_config = templatefile("${path.module}/templates/network_config.cfg.tpl", { + ip_address = "172.30.1.85" + gateway_ip = "172.30.1.254" + dns_ip = "172.30.1.85" + }) +} + +resource "libvirt_cloudinit_disk" "portfolio_init" { + name = "portfolio-init.iso" + pool = libvirt_pool.vm_storage_pool.name + user_data = data.template_file.user_data.rendered + network_config = templatefile("${path.module}/templates/network_config.cfg.tpl", { + ip_address = "172.30.1.93" + gateway_ip = "172.30.1.254" + dns_ip = "172.30.1.85" + }) +} + +resource "libvirt_cloudinit_disk" "minecraft_init" { + name = "minecraft-init.iso" + pool = libvirt_pool.vm_storage_pool.name + user_data = data.template_file.user_data.rendered + network_config = templatefile("${path.module}/templates/network_config.cfg.tpl", { + ip_address = "172.30.1.91" + gateway_ip = "172.30.1.254" + dns_ip = "172.30.1.85" + }) +} + +resource "libvirt_cloudinit_disk" "navidrome_init" { + name = "navidrome-init.iso" + pool = libvirt_pool.vm_storage_pool.name + user_data = data.template_file.user_data.rendered + network_config = templatefile("${path.module}/templates/network_config.cfg.tpl", { + ip_address = "172.30.1.92" + gateway_ip = "172.30.1.254" + dns_ip = "172.30.1.85" + }) +} + +resource "libvirt_domain" "freeipa_vm" { + name = "freeipa" + memory = "3072" + vcpu = 2 + cpu { mode = "host-passthrough" } + cloudinit = libvirt_cloudinit_disk.freeipa_init.id + network_interface { + bridge = "br0" + mac = "52:54:00:ee:ef:61" + } + console { + type = "pty" + target_port = "0" + target_type = "serial" + } + disk { volume_id = libvirt_volume.freeipa_disk.id } + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} + +resource "libvirt_domain" "portfolio_vm" { + name = "portfolio" + memory = "1024" + vcpu = 1 + cpu { mode = "host-passthrough" } + cloudinit = libvirt_cloudinit_disk.portfolio_init.id + network_interface { + bridge = "br0" + mac = "52:54:00:ee:ef:62" + } + console { + type = "pty" + target_port = "0" + target_type = "serial" + } + disk { volume_id = libvirt_volume.portfolio_disk.id } + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} + +resource "libvirt_domain" "minecraft_vm" { + name = "minecraft" + memory = "6144" + vcpu = 2 + cpu { mode = "host-passthrough" } + cloudinit = libvirt_cloudinit_disk.minecraft_init.id + network_interface { + bridge = "br0" + mac = "52:54:00:ee:ef:63" + } + console { + type = "pty" + target_port = "0" + target_type = "serial" + } + disk { volume_id = libvirt_volume.minecraft_disk.id } + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} + +resource "libvirt_domain" "navidrome_vm" { + name = "navidrome" + memory = "1024" + vcpu = 1 + cpu { mode = "host-passthrough" } + cloudinit = libvirt_cloudinit_disk.navidrome_init.id + network_interface { + bridge = "br0" + mac = "52:54:00:ef:65" + } + console { + type = "pty" + target_port = "0" + target_type = "serial" + } + disk { volume_id = libvirt_volume.navidrome_disk.id } + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} diff --git a/terraform/templates/cloud_init.cfg b/terraform/templates/cloud_init.cfg new file mode 100644 index 0000000..10f6d2d --- /dev/null +++ b/terraform/templates/cloud_init.cfg @@ -0,0 +1,15 @@ +#cloud-config +package_update: true +package_upgrade: false + +users: + - name: ${admin_user} + groups: wheel, systemd-journal + sudo: ["ALL=(ALL) NOPASSWD:ALL"] + shell: /bin/bash + ssh_authorized_keys: + - ${ssh_key} + +runcmd: + - sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config + - systemctl restart sshd diff --git a/terraform/templates/network_config.cfg.tpl b/terraform/templates/network_config.cfg.tpl new file mode 100644 index 0000000..d9257d0 --- /dev/null +++ b/terraform/templates/network_config.cfg.tpl @@ -0,0 +1,14 @@ +#cloud-config +version: 2 +ethernets: + eth0: + dhcp4: no + addresses: + - ${ip_address}/24 + routes: + - to: default + via: ${gateway_ip} + nameservers: + addresses: + - ${dns_ip} + - 1.1.1.1