Initialize the vms on host hypervisor

This commit is contained in:
Sho Ishida 2026-07-14 04:14:01 +00:00
parent e8ecb76a6b
commit 47514edb34
3 changed files with 288 additions and 0 deletions

41
terraform/.terraform.lock.hcl generated Normal file
View File

@ -0,0 +1,41 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/dmacvicar/libvirt" {
version = "0.7.6"
constraints = "0.7.6"
hashes = [
"h1:mmbm4vTyC/DCGO4Ed/vbp5AKvy1gmVn/94fzB9VmR08=",
"zh:0bde54f6f658b20b620b875daf106b5b25b1bae4d15408d6c5f06d58360e254d",
"zh:0c97c6930015918b8a34b6d7a2b0c3d17a649c226fcd1874fcba5bbbc0f35972",
"zh:1bdd7aa0011c5f024a09a124836ee9bc8e71b05a6ece810c61824275fd3f695f",
"zh:2b0cc7c794e4caf395d84ffff0b380d17e4b3219a4696264271bfe5059450efe",
"zh:2f8633f7fe07f76c188836ed6f93321ec5fbf5c004bc7699e1741d9b21ed5f37",
"zh:5bf47eed286ce55ed10a5cf657de49a34ab21cc8677c56fef3aab69cdde41a27",
"zh:7dca790fc5fd1d42bc4bc7170be003a7093602026d0f95c8aab84ad551fdf2a4",
"zh:80476b68bc84e3d661d1390025f83879b88f9cdc836de9751af09bd5716089cb",
"zh:82f3e2f3f50176cd6041c8ba36e295cbda1b289ef52ab75b5eceb0f921f64f7b",
"zh:a179b165f3b9bb9a67ebbbf9d73157ded33f02d476b2f58906389dca03b653c9",
"zh:acae54a5d0616f22b3180ddd8e8aad39af664e604394fdacf1f7b337bca2d5b4",
"zh:da4406a2428a9a7e98272c032cb93431c3919253af2fe9934b532d26c0deab09",
"zh:f63dbd8e579ab5268d01ffab4503b8a8e736b70d1a04e4f271559ba8dd133dcd",
"zh:f85c1d9e51a94ecde137435c9d6b0fb7be590437ea8a725334d1577eebbc550c",
]
}
provider "registry.terraform.io/hashicorp/template" {
version = "2.2.0"
hashes = [
"h1:94qn780bi1qjrbC3uQtjJh3Wkfwd5+tTtJHOb7KTg9w=",
"zh:01702196f0a0492ec07917db7aaa595843d8f171dc195f4c988d2ffca2a06386",
"zh:09aae3da826ba3d7df69efeb25d146a1de0d03e951d35019a0f80e4f58c89b53",
"zh:09ba83c0625b6fe0a954da6fbd0c355ac0b7f07f86c91a2a97849140fea49603",
"zh:0e3a6c8e16f17f19010accd0844187d524580d9fdb0731f675ffcf4afba03d16",
"zh:45f2c594b6f2f34ea663704cc72048b212fe7d16fb4cfd959365fa997228a776",
"zh:77ea3e5a0446784d77114b5e851c970a3dde1e08fa6de38210b8385d7605d451",
"zh:8a154388f3708e3df5a69122a23bdfaf760a523788a5081976b3d5616f7d30ae",
"zh:992843002f2db5a11e626b3fc23dc0c87ad3729b3b3cff08e32ffb3df97edbde",
"zh:ad906f4cebd3ec5e43d5cd6dc8f4c5c9cc3b33d2243c89c5fc18f97f7277b51d",
"zh:c979425ddb256511137ecd093e23283234da0154b7fa8b21c2687182d9aea8b2",
]
}

233
terraform/main.tf Normal file
View File

@ -0,0 +1,233 @@
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" "ubuntu2404_image" {
name = "ubuntu2404-base.qcow2"
pool = libvirt_pool.vm_storage_pool.name
source = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
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" "palworld_disk" {
name = "palworld-disk.qcow2"
pool = libvirt_pool.vm_storage_pool.name
base_volume_id = libvirt_volume.ubuntu2404_image.id
size = 32212254720
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
}
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
}
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
}
resource "libvirt_cloudinit_disk" "palworld_init" {
name = "palworld-init.iso"
pool = libvirt_pool.vm_storage_pool.name
user_data = data.template_file.user_data.rendered
}
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
}
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" "palworld_vm" {
name = "palworld"
memory = "12288"
vcpu = 4
cpu { mode = "host-passthrough" }
cloudinit = libvirt_cloudinit_disk.palworld_init.id
network_interface {
bridge = "br0"
mac = "52:54:00:ee:ef:64"
}
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
disk { volume_id = libvirt_volume.palworld_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:ee: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
}
}

View File

@ -0,0 +1,14 @@
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