primer commit
This commit is contained in:
commit
864b3e4008
105 changed files with 3185 additions and 0 deletions
15
opentofu/.editorconfig
Normal file
15
opentofu/.editorconfig
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# EditorConfig is awesome: https://EditorConfig.org
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_size = 4
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.rst]
|
||||
indent_size = 3
|
||||
|
||||
[GNUmakefile]
|
||||
indent_style = tab
|
||||
13
opentofu/.gitignore
vendored
Normal file
13
opentofu/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# Terraform
|
||||
.terraform/
|
||||
*.tfstate
|
||||
*.tfstate.*
|
||||
*.tfvars
|
||||
.terraform.lock.hcl
|
||||
|
||||
# Local development
|
||||
*.log
|
||||
*.backup
|
||||
*.swp
|
||||
*.swo
|
||||
*.DS_Store
|
||||
130
opentofu/README.rst
Normal file
130
opentofu/README.rst
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
==========================================
|
||||
Despliegue Básico de OpenTofu Libvirt CS10
|
||||
==========================================
|
||||
|
||||
Este proyecto proporciona una configuración básica de OpenTofu para desplegar máquinas virtuales utilizando el proveedor Libvirt.
|
||||
|
||||
Prerrequisitos
|
||||
==============
|
||||
Esto hay que hacerlo en tu estación de trabajo, como `root`.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# Añadir usuario a grupos libvirt, kvm, qemu (reemplaza <tu_nombre_de_usuario>)
|
||||
usermod -a -G libvirt,kvm,qemu <tu_nombre_de_usuario>
|
||||
|
||||
# Sal y vuelve a entrar en tu sesión o reinicia
|
||||
|
||||
# Fedora
|
||||
## Instalar libvirt-nss
|
||||
dnf install libvirt-nss
|
||||
|
||||
## Habilitar libvirt-nss
|
||||
authselect enable-feature with-libvirt-nss
|
||||
|
||||
# Reinicia tu sistema.
|
||||
|
||||
Adicionalmente, tienes que tener una imagen base de CentOS Stream 10 en: ``$HOME/VirtualMachines/cs10-base.qcow2``. Debe ser una
|
||||
instalación mínima y con la instalación sin LVM; con la partición de root como la del índice más alto.
|
||||
|
||||
Por ejemplo, tus particiones deben lucir así:
|
||||
|
||||
::
|
||||
|
||||
# lsblk
|
||||
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
|
||||
vda 253:0 0 100G 0 disk
|
||||
├─vda1 253:1 0 600M 0 part /boot/efi
|
||||
├─vda2 253:2 0 1G 0 part /boot
|
||||
├─vda3 253:3 0 512M 0 part [SWAP]
|
||||
└─vda4 253:4 0 2.9G 0 part /
|
||||
|
||||
|
||||
.. important::
|
||||
No olvides instalar cloud-init y cloud-utils-growpart en tu imagen base de CentOS Stream 10.
|
||||
|
||||
Actualiza tu imagen
|
||||
-------------------
|
||||
Un pequeño truco para actualizar tu imagen base de CentOS Stream 10:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
virt-customize -a $HOME/VirtualMachines/cs10.qcow2 --smp=4 --memsize=8192 --update
|
||||
|
||||
Eso la actualiza ahí mismo, utilizando 4 CPUs y 8192 MiB de RAM para que sea más rápido. Es muy útil para mantener al día tu imagen
|
||||
base.
|
||||
|
||||
Despliegue
|
||||
==========
|
||||
Estos comandos se ejecutan en el directorio raíz del proyecto.
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# Inicializar OpenTofu
|
||||
tofu init
|
||||
|
||||
# Revisar el plan
|
||||
tofu plan
|
||||
|
||||
# Aplicar configuración
|
||||
tofu apply
|
||||
|
||||
# Aplicar configuración (sin confirmación)
|
||||
tofu apply -auto-approve
|
||||
|
||||
# Destruir recursos
|
||||
tofu destroy
|
||||
|
||||
# Destruir recursos (sin confirmación)
|
||||
tofu destroy -auto-approve
|
||||
|
||||
Personalización de la inyección de llaves SSH
|
||||
=============================================
|
||||
Para usar tu propia llave SSH en las VMs:
|
||||
|
||||
Copia tu llave pública de SSH (ej. `id_rsa.pub`) a `ssh/millave.pub` y, luego, modifica `templates/cloudinit.cfg.tftpl`:
|
||||
|
||||
.. code-block:: yaml
|
||||
|
||||
ssh_authorized_keys:
|
||||
- ${file("ssh/millave.pub")}
|
||||
|
||||
Aumento del Número de máquinas virtuales
|
||||
========================================
|
||||
Para crear múltiples "guests", ajusta la variable `guest_count`:
|
||||
|
||||
Modifica `variables.tf`:
|
||||
|
||||
.. code-block:: hcl
|
||||
|
||||
variable "guest_count" {
|
||||
description = "Número de máquinas virtuales a crear"
|
||||
type = number
|
||||
default = 1 # Cambia este valor
|
||||
}
|
||||
|
||||
O en `terraform.tfvars`:
|
||||
|
||||
.. code-block:: hcl
|
||||
|
||||
guest_count = 3
|
||||
|
||||
O por línea de comandos:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
tofu apply -var="guest_count=3"
|
||||
|
||||
Problemática
|
||||
============
|
||||
|
||||
El CD-ROM tiene que usar SATA cuando usamos UEFI
|
||||
------------------------------------------------
|
||||
Por cuestiones de diseño del plugin, hay que aplicar un parche con XSLT al XML del CD-ROM en donde se adhiere el ISO de cloud-init.
|
||||
El problema es que no hay manera de definir ese CD-ROM para que use el bus SATA, así que usamos la táctica descrita en este reporte
|
||||
para hacerlo: https://github.com/dmacvicar/terraform-provider-libvirt/issues/1018#issuecomment-1886550277
|
||||
|
||||
Con este parche, es posible usar cloud-init para las operaciones que necesitamos.
|
||||
|
||||
.. note::
|
||||
Una alternativa a esto sería olvidarnos de cloud-init y usar scripts solamente.
|
||||
85
opentofu/cs10-base.tf
Normal file
85
opentofu/cs10-base.tf
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
resource "libvirt_volume" "cs10-base-system" {
|
||||
count = var.guest_count
|
||||
name = "${var.guest_base_name}-${format("%02d", count.index)}.${var.domain}-system.qcow2"
|
||||
base_volume_id = libvirt_volume.cs10-base.id
|
||||
pool = var.pool_name
|
||||
size = var.disk_size
|
||||
}
|
||||
|
||||
resource "libvirt_domain" "cs10-base" {
|
||||
count = var.guest_count
|
||||
name = "${var.guest_base_name}-${format("%02d", count.index)}.${var.domain}"
|
||||
memory = var.guest_memory
|
||||
vcpu = var.guest_vcpu
|
||||
qemu_agent = true
|
||||
autostart = true
|
||||
firmware = "/usr/share/edk2/ovmf/OVMF_CODE_4M.qcow2"
|
||||
|
||||
cpu {
|
||||
mode = "host-passthrough"
|
||||
}
|
||||
|
||||
cloudinit = libvirt_cloudinit_disk.cs10-cloudinit.id
|
||||
|
||||
network_interface {
|
||||
network_name = "default"
|
||||
wait_for_lease = true
|
||||
}
|
||||
|
||||
disk {
|
||||
volume_id = libvirt_volume.cs10-base-system[count.index].id
|
||||
}
|
||||
|
||||
console {
|
||||
type = "pty"
|
||||
target_type = "serial"
|
||||
target_port = "0"
|
||||
}
|
||||
|
||||
console {
|
||||
type = "pty"
|
||||
target_type = "virtio"
|
||||
target_port = "1"
|
||||
}
|
||||
|
||||
graphics {
|
||||
type = "spice"
|
||||
listen_type = "address"
|
||||
autoport = true
|
||||
}
|
||||
|
||||
video {
|
||||
type = "virtio"
|
||||
}
|
||||
|
||||
provisioner "file" {
|
||||
source = "scripts"
|
||||
destination = "/tmp"
|
||||
|
||||
connection {
|
||||
type = "ssh"
|
||||
host = self.name
|
||||
timeout = "1m"
|
||||
}
|
||||
}
|
||||
|
||||
provisioner "remote-exec" {
|
||||
inline = [
|
||||
"bash /tmp/scripts/set-hostname.bash ${self.name}",
|
||||
"bash /tmp/scripts/dnf-fastest_mirrors.bash",
|
||||
"bash /tmp/scripts/install-tools.bash",
|
||||
"bash /tmp/scripts/upgrade.bash"
|
||||
]
|
||||
|
||||
connection {
|
||||
type = "ssh"
|
||||
user = "root"
|
||||
host = self.name
|
||||
timeout = "5m"
|
||||
}
|
||||
}
|
||||
|
||||
xml {
|
||||
xslt = file("xslt/ide-to-sata.xslt")
|
||||
}
|
||||
}
|
||||
27
opentofu/main.tf
Normal file
27
opentofu/main.tf
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
terraform {
|
||||
required_providers {
|
||||
libvirt = {
|
||||
source = "dmacvicar/libvirt"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "libvirt" {
|
||||
uri = "qemu:///system"
|
||||
}
|
||||
|
||||
resource "libvirt_cloudinit_disk" "cs10-cloudinit" {
|
||||
name = "cs10-cloudinit.iso"
|
||||
pool = var.pool_name
|
||||
user_data = templatefile("templates/cloudinit.cfg.tftpl", {
|
||||
root_password = var.root_password
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
resource "libvirt_volume" "cs10-base" {
|
||||
name = "cs10-base.qcow2"
|
||||
pool = var.pool_name
|
||||
source = var.base_image_path
|
||||
format = "qcow2"
|
||||
}
|
||||
10
opentofu/scripts/dnf-conf.bash
Normal file
10
opentofu/scripts/dnf-conf.bash
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/bash
|
||||
|
||||
cat << EOF >> /etc/dnf/dnf.conf
|
||||
|
||||
# get me the fastest mirrors!
|
||||
max_parallel_downloads = 10
|
||||
minrate=1M
|
||||
timeout=10
|
||||
|
||||
EOF
|
||||
10
opentofu/scripts/dnf-fastest_mirrors.bash
Normal file
10
opentofu/scripts/dnf-fastest_mirrors.bash
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/bash
|
||||
|
||||
cat << EOF >> /etc/dnf/dnf.conf
|
||||
|
||||
# get me the fastest mirrors!
|
||||
max_parallel_downloads = 10
|
||||
minrate=1M
|
||||
timeout=10
|
||||
|
||||
EOF
|
||||
4
opentofu/scripts/install-tools.bash
Normal file
4
opentofu/scripts/install-tools.bash
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#!/usr/bin/bash
|
||||
|
||||
dnf -y install epel-release
|
||||
dnf -y install bash-completion htop iftop iotop rsync tmux tmux-powerline vim-enhanced vim-powerline wireshark-cli
|
||||
3
opentofu/scripts/set-hostname.bash
Normal file
3
opentofu/scripts/set-hostname.bash
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/bash
|
||||
|
||||
hostnamectl set-hostname $1
|
||||
12
opentofu/scripts/upgrade.bash
Normal file
12
opentofu/scripts/upgrade.bash
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/bash
|
||||
|
||||
# upgrade all
|
||||
dnf -y upgrade
|
||||
dnf -y distro-sync --allowerasing
|
||||
dnf -y autoremove
|
||||
|
||||
# selinux relabel
|
||||
fixfiles onboot
|
||||
|
||||
# reboot
|
||||
reboot
|
||||
1
opentofu/ssh/renich.pub
Normal file
1
opentofu/ssh/renich.pub
Normal file
|
|
@ -0,0 +1 @@
|
|||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILWo3RJ88qk1RS+P6b8U+rFJ1GpIxKvWW7AGrgiCx8dK renich@desktop
|
||||
29
opentofu/templates/cloudinit.cfg.tftpl
Normal file
29
opentofu/templates/cloudinit.cfg.tftpl
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#cloud-config
|
||||
|
||||
# ssh
|
||||
disable_root: false
|
||||
allow_public_ssh_keys: true
|
||||
ssh_authorized_keys:
|
||||
- ${file("ssh/renich.pub")}
|
||||
|
||||
users:
|
||||
- name: root
|
||||
lock_passwd: false
|
||||
ssh_authorized_keys:
|
||||
- ${file("ssh/renich.pub")}
|
||||
- name: renich
|
||||
lock_passwd: true
|
||||
ssh_authorized_keys:
|
||||
- ${file("ssh/renich.pub")}
|
||||
|
||||
chpasswd:
|
||||
expire: False
|
||||
users:
|
||||
- name: root
|
||||
password: "${root_password}"
|
||||
type: text
|
||||
|
||||
growpart:
|
||||
devices: ['/']
|
||||
ignore_growroot_disabled: true
|
||||
mode: auto
|
||||
54
opentofu/variables.tf
Normal file
54
opentofu/variables.tf
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
variable "root_password" {
|
||||
description = "Contraseña de root para las máquinas virtuales."
|
||||
type = string
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
variable "domain" {
|
||||
description = "Dominio de red para las máquinas virtuales."
|
||||
type = string
|
||||
sensitive = false
|
||||
}
|
||||
|
||||
variable "guest_count" {
|
||||
description = "Número de máquinas virtuales a crear"
|
||||
type = number
|
||||
default = 1
|
||||
}
|
||||
|
||||
variable "guest_base_name" {
|
||||
description = "Nombre base para las máquinas virtuales invitadas."
|
||||
type = string
|
||||
default = "cs10-base"
|
||||
}
|
||||
|
||||
variable "base_image_path" {
|
||||
description = "Ruta al archivo de la imagen base de la máquina virtual."
|
||||
type = string
|
||||
default = "/home/renich/VirtualMachines/cs10.qcow2"
|
||||
}
|
||||
|
||||
variable "guest_memory" {
|
||||
description = "Cantidad de memoria RAM para cada invitado en MB."
|
||||
type = number
|
||||
default = 4096
|
||||
}
|
||||
|
||||
variable "guest_vcpu" {
|
||||
description = "Número de vCPUs para cada invitado."
|
||||
type = number
|
||||
default = 2
|
||||
}
|
||||
|
||||
variable "disk_size" {
|
||||
description = "Tamaño del disco del sistema para cada invitado en bytes."
|
||||
type = number
|
||||
default = 107374182400 # 100 GB
|
||||
}
|
||||
|
||||
variable "pool_name" {
|
||||
description = "Nombre del pool de almacenamiento de Libvirt."
|
||||
type = string
|
||||
default = "fast"
|
||||
}
|
||||
|
||||
21
opentofu/xslt/ide-to-sata.xslt
Normal file
21
opentofu/xslt/ide-to-sata.xslt
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
<xsl:output omit-xml-declaration="yes" indent="yes"/>
|
||||
<xsl:strip-space elements="*"/>
|
||||
|
||||
<xsl:template match="node()|@*">
|
||||
<xsl:copy>
|
||||
<xsl:apply-templates select="node()|@*"/>
|
||||
</xsl:copy>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template match="disk[serial='cloudinit']">
|
||||
<xsl:copy>
|
||||
<xsl:apply-templates select="@*"/>
|
||||
<xsl:copy-of select="driver"/>
|
||||
<source file="{source/@file}"/>
|
||||
<target dev="sda" bus="sata"/>
|
||||
<xsl:copy-of select="readonly"/>
|
||||
<address type="drive" controller="0" bus="0" target="0" unit="0"/>
|
||||
</xsl:copy>
|
||||
</xsl:template>
|
||||
</xsl:stylesheet>
|
||||
Loading…
Reference in a new issue