Compare commits

...

7 Commits

Author SHA1 Message Date
Lukas Greve
8078d9c041 cloud_init.yaml insecure example 2025-09-04 11:34:09 +02:00
Lukas Greve
4409d31ae9 unnecessary files 2025-09-04 11:33:55 +02:00
Lukas Greve
2433e08f90 added some explanations 2025-09-04 11:32:42 +02:00
Lukas Greve
07ed85724b first example of a deployment, a simple ubuntu-based BIOS virtual machine with the i440fx virtual chipset 2025-09-04 11:32:20 +02:00
Lukas Greve
7e470c0bc4 cloud_init.yaml, intented to be shared 2025-09-04 11:31:28 +02:00
Lukas Greve
ac81cb65ce shared modules to be used across deployments 2025-09-04 11:30:46 +02:00
Lukas Greve
96e8bd7588 first simple example 2025-09-04 11:30:25 +02:00
14 changed files with 292 additions and 21 deletions

View File

@@ -4,6 +4,26 @@ This repository contains Terraform recipes to deploy various modern virtual mach
By modern, it is meant virtual machines that leverage the use of modern desktop-oriented technologies, like UEFI firmware and recent virtual motherboard chipset (i.e. Phyllome OS itself), by staying as close as possible as domain definitions maintained [here](https://git.phyllo.me/roots/xml-definition-for-domains).
## Organization
The folder *multiple* contains two subfolders, one with shared modules and the other with the various target deployment environments.
The idea is to reuse modules across multiple virtual machines and operating systems.
```
./multiple:
environments shared_modules
./multiple/environments:
cloud_init.yaml ubuntu-cloud-server-2404-bios
./multiple/environments/ubuntu-cloud-server-2404-bios:
ubuntu-cloud-server-2404-bios.tf
./multiple/shared_modules:
cloud-init.tf domain.tf network.tf outputs.tf pool.tf provider.tf variables.tf volume.tf
```
## Requirements
- [QEMU](https://www.qemu.org/)
@@ -17,7 +37,7 @@ Your Linux x86_64-based machine has at least 4 GB of available memory and 2 CPUs
## How to use it
- Clone this repository
- Go to folder *ubuntu-cloud-server-2404-bios*
- Go to folder *example*
- Execute the following commands, which will download and install the required Terraform provider if not already present
```
@@ -221,6 +241,8 @@ libvirt_pool.ubuntu2: Destruction complete after 0s
Destroy complete! Resources: 4 destroyed.
```
## Explanations
Let's take a look inside the *ubuntu-cloud-server-2404-bios* folder, which contains two files, *ubuntu-cloud-server-2404-bios.tf* and *cloud_init.cfg*

18
example/cloud_init.yaml Normal file
View File

@@ -0,0 +1,18 @@
#cloud-config
# vim: syntax=yaml
# examples:
# https://cloudinit.readthedocs.io/en/latest/topics/examples.html
---
ssh_pwauth: true
disable_root: false
chpasswd:
list: |
root:password
expire: false
users:
- name: ubuntu
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
home: /home/ubuntu
shell: /bin/bash
lock_passwd: false

View File

@@ -23,23 +23,18 @@ resource "libvirt_pool" "ubuntu-bios" {
resource "libvirt_volume" "ubuntu-bios" {
name = "ubuntu-bios-${count.index}"
pool = libvirt_pool.ubuntu-bios.name
source = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
source = "/var/lib/libvirt/images/noble-server-cloudimg-amd64.img"
format = "qcow2"
count = 2
}
data "template_file" "user_data" {
template = file("${path.module}/cloud_init.cfg")
count = 1
}
resource "libvirt_cloudinit_disk" "commoninit" {
name = "commoninit.iso"
user_data = data.template_file.user_data.rendered
pool = libvirt_pool.ubuntu-bios.name
user_data = templatefile("${path.module}/cloud_init.yaml", {})
}
resource "libvirt_domain" "domain" {
count = 2
count = 1
name = "ubuntu-cloud-server-2404-${count.index}"
memory = "4092"
vcpu = 2
@@ -73,3 +68,4 @@ resource "libvirt_domain" "domain" {
}
}

View File

@@ -0,0 +1,18 @@
#cloud-config
# vim: syntax=yaml
# examples:
# https://cloudinit.readthedocs.io/en/latest/topics/examples.html
---
ssh_pwauth: true
disable_root: false
chpasswd:
list: |
root:password
expire: false
users:
- name: ubuntu
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
home: /home/ubuntu
shell: /bin/bash
lock_passwd: false

View File

@@ -0,0 +1,20 @@
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.8.3"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
module "shared_modules" {
source = "../../shared_modules"
vm_name = "ubuntu-cloud-server-2404-bios"
image_location = "/var/lib/libvirt/images/noble-server-cloudimg-amd64.img"
}

View File

@@ -0,0 +1,6 @@
resource "libvirt_cloudinit_disk" "commoninit" {
name = var.cloudinit_filename
user_data = templatefile("${path.module}/../environments/cloud_init.yaml", {})
pool = var.pool_name
depends_on = [libvirt_pool.tf_tmp_storage]
}

View File

@@ -0,0 +1,33 @@
resource "libvirt_domain" "domain" {
count = var.instance_count
name = "${var.vm_name}-${count.index}"
memory = var.memory
vcpu = var.vcpu
cloudinit = libvirt_cloudinit_disk.commoninit.id
cpu {
mode = "host-model"
}
disk {
volume_id = element(libvirt_volume.vm_disk.*.id, count.index)
}
console {
type = "pty"
target_port = "0"
target_type = "virtio"
}
video {
type = "virtio"
}
tpm {
backend_type = "emulator"
backend_version = "2.0"
}
network_interface {
network_name = var.network_name
}
}

View File

@@ -0,0 +1,11 @@
resource "libvirt_network" "tf_libvirt_tmp_network" {
name = var.network_name
mode = var.network_mode
domain = var.network_domain
addresses = var.network_addresses
dns {
enabled = var.dns_enabled
local_only = var.dns_local_only
}
}

View File

@@ -0,0 +1,19 @@
output "pool_name" {
value = libvirt_pool.tf_tmp_storage.name
}
output "network_name" {
value = libvirt_network.tf_libvirt_tmp_network.name
}
output "network_addresses" {
value = libvirt_network.tf_libvirt_tmp_network.addresses
}
output "cloudinit_disk_id" {
value = libvirt_cloudinit_disk.commoninit.id
}
output "cloudinit_disk_name" {
value = libvirt_cloudinit_disk.commoninit.name
}

View File

@@ -0,0 +1,7 @@
resource "libvirt_pool" "tf_tmp_storage" {
name = var.pool_name
type = "dir"
target {
path = var.pool_path
}
}

View File

@@ -0,0 +1,9 @@
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.8.3"
}
}
}

View File

@@ -0,0 +1,113 @@
variable "libvirt_uri" {
description = "URI for libvirt connection"
type = string
default = "qemu:///system"
}
variable "pool_name" {
description = "Name of the storage pool"
type = string
default = "tf_tmp_pool"
}
variable "pool_path" {
description = "Path for the storage pool"
type = string
default = "/tmp/tf_tmp_storage"
}
variable "instance_count" {
description = "Number of instances to create"
type = number
default = 1
}
variable "vm_name" {
description = "Name prefix for VMs"
type = string
}
variable "image_location" {
description = "Location of the OS image"
type = string
default = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
}
# In order to avoid refetching the cloud ISO each time, it could could be set to a local directory, like : "/var/lib/libvirt/images/noble-server-cloudimg-amd64.img"
# variable "cloudinit_user_data" {
# description = "User data for cloud-init"
# type = string
# default = <<EOF
# #cloud-config
# hostname: ubuntu-cloud-server
# fqdn: ubuntu-cloud-server.tf.local
# EOF
# }
# variable "cloud_init_file" {
# description = "Name of cloud-init config file"
# type = optional(string)
# }
variable "cloudinit_filename" {
description = "Name of the cloud-init ISO file"
type = string
default = "commoninit.iso"
}
variable "user_data" {
description = "User data for cloud-init"
type = string
default = "data.template_file.user_data.rendered"
}
variable "memory" {
description = "Memory allocation in MB"
type = number
default = 2048
validation {
condition = var.memory >= 512
error_message = "Memory must be at least 512MB."
}
}
variable "vcpu" {
description = "Number of virtual CPUs"
type = number
default = 1
}
variable "network_name" {
description = "Name of the network"
type = string
default = "tf"
}
variable "network_mode" {
description = "Network mode (nat, none, route, open, bridge)"
type = string
default = "nat"
}
variable "network_domain" {
description = "Domain name for the network"
type = string
default = "tf.local"
}
variable "network_addresses" {
description = "List of network addresses"
type = list(string)
default = ["10.17.3.0/24", "2001:db8:ca2:2::1/64"]
}
variable "dns_enabled" {
description = "Enable DNS for the network"
type = bool
default = true
}
variable "dns_local_only" {
description = "DNS requests only resolved by virtual network's DNS server"
type = bool
default = false
}

View File

@@ -0,0 +1,9 @@
resource "libvirt_volume" "vm_disk" {
count = var.instance_count
name = "${var.vm_name}-${count.index}"
pool = var.pool_name
source = var.image_location
format = "qcow2"
depends_on = [libvirt_pool.tf_tmp_storage]
}

View File

@@ -1,10 +0,0 @@
ssh_pwauth: true
chpasswd:
list: |
root:phyllome
expire: False
disable_root: false
runcmd:
- sed -i '/PermitRootLogin/s/.*/PermitRootLogin yes/' /etc/ssh/sshd_config
- systemctl restart sshd