8 changed files with 217 additions and 0 deletions
@ -0,0 +1,51 @@ |
|||||
|
# Lab of Centos Machines |
||||
|
|
||||
|
Fetch the latest CentOS Stream 8 cloud image. |
||||
|
|
||||
|
```sh |
||||
|
sudo curl -Lo /var/lib/libvirt/images/centos-stream-8.qcow2 http://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-GenericCloud-8-20201217.0.x86_64.qcow2 |
||||
|
``` |
||||
|
|
||||
|
Define a new network with VLANs. |
||||
|
|
||||
|
```xml |
||||
|
<network> |
||||
|
<name>lab</name> |
||||
|
<forward mode="bridge" /> |
||||
|
<bridge name="lab" /> |
||||
|
<virtualport type='openvswitch'> |
||||
|
</virtualport> |
||||
|
<portgroup name='lab7' default='yes'> |
||||
|
</portgroup> |
||||
|
<portgroup name='lab8'> |
||||
|
<vlan> |
||||
|
<tag id='8'/> |
||||
|
</vlan> |
||||
|
</portgroup> |
||||
|
<portgroup name='lab16'> |
||||
|
<vlan> |
||||
|
<tag id='16'/> |
||||
|
</vlan> |
||||
|
</portgroup> |
||||
|
</network> |
||||
|
``` |
||||
|
|
||||
|
Then, deploy the lab. |
||||
|
|
||||
|
```sh |
||||
|
export LIBVIRT_DEFAULT_URI=qemu:///system |
||||
|
terraform init |
||||
|
terraform apply |
||||
|
``` |
||||
|
|
||||
|
Destroy the lab. |
||||
|
|
||||
|
```sh |
||||
|
terraform destroy |
||||
|
``` |
||||
|
|
||||
|
Edit patch.xml and change the target portgroup to "lab8". |
||||
|
|
||||
|
```sh |
||||
|
terraform apply -var centos_mac_format=02:01:08:00:08:%02x |
||||
|
``` |
||||
@ -0,0 +1,53 @@ |
|||||
|
|
||||
|
resource "libvirt_cloudinit_disk" "centos_cloudinit" { |
||||
|
name = "centos-cloudinit.iso" |
||||
|
user_data = file("${path.module}/templates/cloud-init.cfg") |
||||
|
network_config = file("${path.module}/templates/network-config.cfg") |
||||
|
pool = var.pool_name |
||||
|
} |
||||
|
|
||||
|
resource "libvirt_volume" "centos_disk" { |
||||
|
name = "${format(var.centos_hostname_format, count.index + 1)}.${var.volume_format}" |
||||
|
count = var.centos_machine_count |
||||
|
format = var.volume_format |
||||
|
pool = var.pool_name |
||||
|
base_volume_name = "${var.centos_image}.${var.volume_format}" |
||||
|
} |
||||
|
|
||||
|
resource "libvirt_domain" "centos_machine" { |
||||
|
count = var.centos_machine_count |
||||
|
name = format(var.centos_hostname_format, count.index + 1) |
||||
|
vcpu = "1" |
||||
|
memory = "1024" |
||||
|
cloudinit = libvirt_cloudinit_disk.centos_cloudinit.id |
||||
|
autostart = false |
||||
|
qemu_agent = true |
||||
|
|
||||
|
disk { |
||||
|
volume_id = element(libvirt_volume.centos_disk.*.id, count.index) |
||||
|
} |
||||
|
|
||||
|
# Makes the tty0 available via `virsh console` |
||||
|
console { |
||||
|
type = "pty" |
||||
|
target_port = "0" |
||||
|
} |
||||
|
|
||||
|
network_interface { |
||||
|
network_name = var.network_name |
||||
|
mac = format(var.centos_mac_format, count.index + var.centos_mac_start) |
||||
|
|
||||
|
# When creating the domain resource, wait until the network interface gets |
||||
|
# a DHCP lease from libvirt, so that the computed IP addresses will be |
||||
|
# available when the domain is up and the plan applied. |
||||
|
wait_for_lease = true |
||||
|
} |
||||
|
|
||||
|
xml { |
||||
|
xslt = file("${path.module}/patch.xslt") |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
locals { |
||||
|
centos_machines = { for i in libvirt_domain.centos_machine : i.name => i.network_interface.0.addresses[0] } |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
terraform { |
||||
|
required_version = ">= 0.13" |
||||
|
required_providers { |
||||
|
libvirt = { |
||||
|
source = "dmacvicar/libvirt" |
||||
|
version = ">=0.6.3" |
||||
|
} |
||||
|
local = { |
||||
|
source = "hashicorp/local" |
||||
|
version = ">=2.0.0" |
||||
|
} |
||||
|
template = { |
||||
|
source = "hashicorp/template" |
||||
|
version = ">=2.2.0" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
output "machines" { |
||||
|
value = local.centos_machines |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
<?xml version="1.0" ?> |
||||
|
<xsl:stylesheet version="1.0" |
||||
|
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
||||
|
<xsl:output omit-xml-declaration="yes" indent="yes"/> |
||||
|
|
||||
|
<!-- Target portgroup --> |
||||
|
<xsl:param name="portgroup" select="'lab7'"/> |
||||
|
|
||||
|
<!-- XSLT Identity template --> |
||||
|
<xsl:template match="node()|@*"> |
||||
|
<xsl:copy> |
||||
|
<xsl:apply-templates select="node()|@*"/> |
||||
|
</xsl:copy> |
||||
|
</xsl:template> |
||||
|
|
||||
|
<!-- Put the NIC in the desired portgroup --> |
||||
|
<xsl:template match="/domain/devices/interface/source"> |
||||
|
<xsl:copy> |
||||
|
<xsl:apply-templates select="@*|node()"/> |
||||
|
<xsl:attribute name="portgroup"> |
||||
|
<xsl:value-of select="$portgroup"/> |
||||
|
</xsl:attribute> |
||||
|
</xsl:copy> |
||||
|
</xsl:template> |
||||
|
</xsl:stylesheet> |
||||
@ -0,0 +1,2 @@ |
|||||
|
provider "libvirt" { |
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
#cloud-config |
||||
|
# vim: syntax=yaml |
||||
|
|
||||
|
users: |
||||
|
- name: nicolas |
||||
|
gecos: Nicolas MASSE |
||||
|
groups: wheel |
||||
|
lock_passwd: false |
||||
|
# Generate encrypted password with "openssl passwd -6" |
||||
|
#passwd: $6$abc...xyz.0 |
||||
|
ssh_authorized_keys: |
||||
|
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPR1tt58X0+vbvsCR12gMAqr+g7vjt1Fx/qqz9EiboIs nicolas.masse@itix.fr |
||||
|
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFW62WJXI1ZCMfNA4w0dMpL0fsldhbEfULNGIUB0nQui nmasse@redhat.com |
||||
|
|
||||
|
runcmd: |
||||
|
# Enable KVM virsh console access |
||||
|
- [ "systemctl", "enable", "serial-getty@ttyS0.service" ] |
||||
|
- [ "systemctl", "start", "--no-block", "serial-getty@ttyS0.service" ] |
||||
|
- [ "sed", "-i.post-install", "-e", "s/PasswordAuthentication yes/PasswordAuthentication no/", "/etc/ssh/sshd_config" ] |
||||
|
- [ "systemctl", "restart", "sshd" ] |
||||
|
- [ "sed", "-i.post-install", "-e", "s/^%wheel\tALL=(ALL)\tALL/%wheel ALL=(ALL) NOPASSWD: ALL/", "/etc/sudoers" ] |
||||
@ -0,0 +1,4 @@ |
|||||
|
version: 2 |
||||
|
ethernets: |
||||
|
eth0: |
||||
|
dhcp4: true |
||||
@ -0,0 +1,40 @@ |
|||||
|
|
||||
|
variable "centos_machine_count" { |
||||
|
type = number |
||||
|
default = 1 |
||||
|
} |
||||
|
|
||||
|
variable "pool_name" { |
||||
|
type = string |
||||
|
default = "default" |
||||
|
} |
||||
|
|
||||
|
variable "volume_format" { |
||||
|
type = string |
||||
|
default = "qcow2" |
||||
|
} |
||||
|
|
||||
|
variable "centos_hostname_format" { |
||||
|
type = string |
||||
|
default = "centos-%02d" |
||||
|
} |
||||
|
|
||||
|
variable "centos_image" { |
||||
|
type = string |
||||
|
default = "centos-stream-8" |
||||
|
} |
||||
|
|
||||
|
variable "network_name" { |
||||
|
type = string |
||||
|
default = "lab" |
||||
|
} |
||||
|
|
||||
|
variable "centos_mac_format" { |
||||
|
type = string |
||||
|
default = "02:01:07:00:07:%02x" |
||||
|
} |
||||
|
|
||||
|
variable "centos_mac_start" { |
||||
|
type = number |
||||
|
default = 10 |
||||
|
} |
||||
Loading…
Reference in new issue