Skip to content

Commit

Permalink
add build-packer-templates.yaml to test
Browse files Browse the repository at this point in the history
  • Loading branch information
goffinet committed Oct 18, 2024
1 parent 3827e2c commit 6c45198
Show file tree
Hide file tree
Showing 47 changed files with 1,083 additions and 60 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
FROM hashicorp/packer:light

RUN apk add --update qemu qemu-system-x86_64 qemu-img openssh python3 py3-pip samba

RUN packer plugins install github.com/hashicorp/ansible

RUN packer plugins install github.com/hashicorp/qemu
62 changes: 28 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# packer-kvm

Create VM templates with Packer for usage with Libvirt/KVM virtualization : CentOS 7, CentOS 8 (Stream), CentOS 9 (Stream), Alma Linux 8, Alma Linux 9, Rocky Linux 8, Bionic (Ubuntu 1804), Focal (Ubuntu 2004), Jammy (Ubuntu 2204), Debian 11 (stable), Kali Linux, Fedora 35.
Create VM templates with Packer for usage with Libvirt/KVM virtualization : CentOS 9 (Stream), AlmaLinux 9, Focal (Ubuntu 2004), Fedora 40, Jammy (Ubuntu 2204), Noble (Ubuntu 2404).

Only for education and learning purposes. Do not use it in production.

Expand Down Expand Up @@ -116,22 +116,17 @@ pip3 install docker-compose

Each JSON file is a template for a distribution :

* [almalinux8.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/almalinux8.pkr.hcl)
* [almalinux9.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/almalinux9.pkr.hcl)
* [bionic.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/bionic.pkr.hcl)
* [centos7.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/centos7.pkr.hcl)
* [centos8.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/centos8.pkr.hcl)
* [centos9.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/centos9.pkr.hcl)
* [debian11.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/debian11.pkr.hcl)
* [fedora35.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/fedora35.pkr.hcl)
* [focal.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/focal.pkr.hcl)
* [jammy.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/jammy.pkr.hcl)
* [rocky8.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/rocky8.pkr.hcl)
* [fedora40.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/fedora40.pkr.hcl)
* [ubuntu2004.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/ubuntu2004.pkr.hcl)
* [ubuntu2204.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/ubuntu2204.pkr.hcl)
* [ubuntu2404.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/ubuntu2404.pkr.hcl)

For example :

```bash
packer build centos7.pkr.hcl
packer build almalinux9.pkr.hcl
```

## Build with Docker qemu based image
Expand All @@ -149,13 +144,13 @@ docker run --rm \
-v `pwd`:/opt/ \
-e AWS_ACCESS_KEY=$AWS_ACCESS_KEY \
-e AWS_SECRET_KEY=$AWS_SECRET_KEY \
-w /opt/ goffinet/packer-qemu build centos7.pkr.hcl
-w /opt/ goffinet/packer-qemu build almalinux9.pkr.hcl
```

The script `build.sh` do it with the template filename as first argument.

```bash
./build.sh centos7.pkr.hcl
./build.sh almalinux9.pkr.hcl
```

To build the image localy with the [Dockerfile](https://github.com/goffinet/packer-kvm/blob/master/Dockerfile) :
Expand All @@ -174,21 +169,12 @@ You can have more details from Packet with the env var configured : `PACKER_LOG=

I build images for qemu/KVM with this project and I [publish them](http://download.goffinet.org/kvm/index.html) for use in those other IaC projects: [Virt-scripts](https://github.com/goffinet/virt-scripts) and **[Terraform with Libvirt/KVM provider](https://github.com/goffinet/terraform-libvirt)**.

- [almalinux8.qcow2](http://download.goffinet.org/kvm/almalinux8.qcow2) [[md5sum]](http://download.goffinet.org/kvm/almalinux8.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/almalinux8.qcow2.sha256sum)
- [almalinux9.qcow2](http://download.goffinet.org/kvm/almalinux9.qcow2) [[md5sum]](http://download.goffinet.org/kvm/almalinux9.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/almalinux9.qcow2.sha256sum)
- [bionic.qcow2 (Ubuntu 18.04)](http://download.goffinet.org/kvm/bionic.qcow2) [[md5sum]](http://download.goffinet.org/kvm/bionic.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/bionic.qcow2.sha256sum)
- [centos7.qcow2](http://download.goffinet.org/kvm/centos7.qcow2) [[md5sum]](http://download.goffinet.org/kvm/centos7.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/centos7.qcow2.sha256sum)
- [centos8.qcow2](http://download.goffinet.org/kvm/centos8.qcow2) [[md5sum]](http://download.goffinet.org/kvm/centos8.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/centos8.qcow2.sha256sum)
- [centos9.qcow2](http://download.goffinet.org/kvm/centos9.qcow2) [[md5sum]](http://download.goffinet.org/kvm/centos9.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/centos9.qcow2.sha256sum)
- [debian11.qcow2](http://download.goffinet.org/kvm/debian11.qcow2) [[md5sum]](http://download.goffinet.org/kvm/debian11.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/debian11.qcow2.sha256sum)
- [fedora32.qcow2](http://download.goffinet.org/kvm/fedora32.qcow2) [[md5sum]](http://download.goffinet.org/kvm/fedora32.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/fedora32.qcow2.sha256sum)
- [fedora33.qcow2](http://download.goffinet.org/kvm/fedora33.qcow2) [[md5sum]](http://download.goffinet.org/kvm/fedora33.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/fedora33.qcow2.sha256sum)
- [fedora34.qcow2](http://download.goffinet.org/kvm/fedora34.qcow2) [[md5sum]](http://download.goffinet.org/kvm/fedora34.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/fedora34.qcow2.sha256sum)
- [fedora35.qcow2](http://download.goffinet.org/kvm/fedora35.qcow2) [[md5sum]](http://download.goffinet.org/kvm/fedora35.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/fedora35.qcow2.sha256sum)
- [focal.qcow2 (Ubuntu 20.04)](http://download.goffinet.org/kvm/focal.qcow2) [[md5sum]](http://download.goffinet.org/kvm/focal.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/focal.qcow2.sha256sum)
- [jammy.qcow2 (Ubuntu 20.04)](http://download.goffinet.org/kvm/jammy.qcow2) [[md5sum]](http://download.goffinet.org/kvm/jammy.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/jammy.qcow2.sha256sum)
- [kali20211.qcow2](http://download.goffinet.org/kvm/kali20211.qcow2) [[md5sum]](http://download.goffinet.org/kvm/kali20211.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/kali20211.qcow2.sha256sum)
- [rocky8.qcow2](http://download.goffinet.org/kvm/rocky8.qcow2) [[md5sum]](http://download.goffinet.org/kvm/rocky8.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/rocky8.qcow2.sha256sum)
- [fedora40.qcow2](http://download.goffinet.org/kvm/fedora40.qcow2) [[md5sum]](http://download.goffinet.org/kvm/fedora40.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/fedora40.qcow2.sha256sum)
- [ubuntu2004.qcow2 (Focal)](http://download.goffinet.org/kvm/ubuntu2004.qcow2) [[md5sum]](http://download.goffinet.org/kvm/ubuntu2004.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/ubuntu2004.qcow2.sha256sum)
- [ubuntu2204.qcow2 (Jammy)](http://download.goffinet.org/kvm/ubuntu2204.qcow2) [[md5sum]](http://download.goffinet.org/kvm/ubuntu2204.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/ubuntu2204.qcow2.sha256sum)
- [ubuntu2404.qcow2 (Noble)](http://download.goffinet.org/kvm/ubuntu2404.qcow2) [[md5sum]](http://download.goffinet.org/kvm/ubuntu2404.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/ubuntu2404.qcow2.sha256sum)


You can easily download them to `/var/lib/libvirt/images` with this script :
Expand Down Expand Up @@ -230,14 +216,14 @@ This is always beter to know how Libvirt is working. Can you read fundamentals a
```
```raw
Please provide the image name :
centos7 bionic debian11
almalinux9 centos9 fedora40 ubuntu2004 ubuntu2204 ubuntu2404
```

3. Launch two new machines

```bash
sudo ./define-guest-image.sh c1 centos7
sudo ./define-guest-image.sh u1 bionic
sudo ./define-guest-image.sh c1 almalinux9
sudo ./define-guest-image.sh u1 ubuntu2204
```

4. Enjoy
Expand All @@ -250,6 +236,8 @@ This is always beter to know how Libvirt is working. Can you read fundamentals a
ssh $(dig @192.168.122.1 +short u1)
```

> This section should be revised
### Enjoy with Terraform (with libvirt)

[https://github.com/goffinet/terraform-libvirt](https://github.com/goffinet/terraform-libvirt)
Expand Down Expand Up @@ -319,13 +307,19 @@ To get the default ssh public key :
curl https://raw.githubusercontent.com/goffinet/packer-kvm/master/sshkeys/id_rsa.pub
```

### How are these Packer templates produced?

An `build-packer-templates.yaml` Ansible playbook generates the files for each distribution using a custom `build-packer-templates` role.

## ToDo

* unique model : for efficience, a unique template should be sufficient with a data source with these elements (https://devops.stackexchange.com/q/4312).
* random secret for provisonning (https://www.packer.io/docs/templates/engine#template-variables)
* Remove swap post-processing
* docker-compose for automation
* add versions of post-processing and images meta-datas
- [ ] Test the `build-packer-templates.yaml` playbook production
- [x] unique model : for efficience, a unique template should be sufficient with a data source with these elements (https://devops.stackexchange.com/q/4312).
- [x] random secret for provisonning (https://www.packer.io/docs/templates/engine#template-variables)
- [ ] Remove swap post-processing
- [ ] docker-compose for automation
- [ ] add versions of post-processing and images meta-datas
- [ ] Include Windows templates

Wath are the variants in those templates?

Expand Down
21 changes: 11 additions & 10 deletions almalinux9.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#Generated by goffinet/packer-kvm/build-packer-templates.yaml at 2024-10-18T18:42:22Z

variable "config_file" {
type = string
Expand Down Expand Up @@ -36,7 +37,7 @@ variable "iso_url" {

variable "name" {
type = string
default = "almalinux"
default = "almalinux9"
}

variable "ram" {
Expand All @@ -46,23 +47,22 @@ variable "ram" {

variable "ssh_password" {
type = string
default = "testtest"
}
default = "testtest
}
variable "ssh_username" {
type = string
default = "root"
}
default = "root
}

variable "version" {
type = string
default = "9"
default = "9.4"
}

source "qemu" "almalinux9" {
accelerator = "kvm"
boot_command = ["<tab><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs> inst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/http/${var.config_file}<enter><wait>"]
boot_wait = "40s"
boot_command = ['<tab><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs> inst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/http/${var.config_file}<enter><wait>']
disk_cache = "none"
disk_compression = true
disk_discard = "unmap"
Expand All @@ -81,6 +81,7 @@ source "qemu" "almalinux9" {
ssh_password = var.ssh_password
ssh_username = var.ssh_username
ssh_wait_timeout = "30m"
boot_wait = "40s"
}

build {
Expand All @@ -89,7 +90,7 @@ build {
provisioner "shell" {
execute_command = "{{ .Vars }} sudo -E bash '{{ .Path }}'"
inline = ["dnf -y install epel-release", "dnf repolist", "dnf -y install ansible"]
}
}

provisioner "ansible-local" {
playbook_dir = "ansible"
Expand All @@ -100,4 +101,4 @@ build {
environment_vars = ["IMAGE_NAME=${var.name}", "IMAGE_VERSION=${var.version}", "DESTINATION_SERVER=${var.destination_server}"]
script = "scripts/push-image.sh"
}
}
}
50 changes: 50 additions & 0 deletions build-packer-templates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
- hosts: localhost
gather_facts: yes
vars:
packer_templates_dir: ./
destination_server: download.goffinet.org
images:
- type: "redhat"
flavor: "almalinux"
version: "9.4"
iso_url: "https://repo.almalinux.org/almalinux/9.4/isos/x86_64"
iso_name: "AlmaLinux-9.4-x86_64-boot.iso"
checksum_filename: "CHECKSUM"
- type: "redhat"
flavor: "centos"
version: "9"
iso_url: "https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/iso"
iso_name: "CentOS-Stream-9-20241016.0-x86_64-boot.iso"
checksum_filename: "SHA256SUM"
- type: "redhat"
flavor: "fedora"
version: "40"
iso_url: "https://fedora.cu.be/linux/releases/40/Server/x86_64/iso/"
iso_name: "Fedora-Server-netinst-x86_64-40-1.14.iso"
checksum_filename: "Fedora-Server-40-1.14-x86_64-CHECKSUM"
- type: "debian"
flavor: "ubuntu"
version: "20.04"
iso_url: "http://releases.ubuntu.com/20.04"
iso_name: "ubuntu-20.04.6-live-server-amd64.iso"
checksum_filename: "SHA256SUMS"
- type: "debian"
flavor: "ubuntu"
version: "22.04"
iso_url: "http://releases.ubuntu.com/22.04"
iso_name: "ubuntu-22.04.5-live-server-amd64.iso"
checksum_filename: "SHA256SUMS"
- type: "debian"
flavor: "ubuntu"
version: "24.04"
iso_url: "http://releases.ubuntu.com/24.04"
iso_name: "ubuntu-24.04.1-live-server-amd64.iso"
checksum_filename: "SHA256SUMS"
pre_tasks:
- name: Create packer-templates directory
file:
dest: "{{ packer_templates_dir }}"
state: directory
roles:
- name: Create packer-templates
role: build-packer-templates
23 changes: 12 additions & 11 deletions centos9.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#Generated by goffinet/packer-kvm/build-packer-templates.yaml at 2024-10-18T18:42:22Z

variable "config_file" {
type = string
Expand Down Expand Up @@ -26,17 +27,17 @@ variable "headless" {

variable "iso_checksum" {
type = string
default = "sha256:d6dccd66ba317a2825821fe92169a53079df95b04581c065a8244f67303e1f46"
default = "sha256:fe7c98200887342b16d78d6da9a62c7c5f9a81c7d370eb482d419d7a1814f5af"
}

variable "iso_url" {
type = string
default = "http://mirror.stream.centos.org/9-stream/BaseOS/x86_64/iso/CentOS-Stream-9-20220531.0-x86_64-boot.iso"
default = "https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/iso/CentOS-Stream-9-20241016.0-x86_64-boot.iso"
}

variable "name" {
type = string
default = "centos"
default = "centos9"
}

variable "ram" {
Expand All @@ -46,13 +47,13 @@ variable "ram" {

variable "ssh_password" {
type = string
default = "testtest"
}
default = "testtest
}
variable "ssh_username" {
type = string
default = "root"
}
default = "root
}

variable "version" {
type = string
Expand All @@ -61,8 +62,7 @@ variable "version" {

source "qemu" "centos9" {
accelerator = "kvm"
boot_command = ["<tab><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs> inst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/http/${var.config_file}<enter><wait>"]
boot_wait = "40s"
boot_command = ['<tab><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs> inst.text inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/http/${var.config_file}<enter><wait>']
disk_cache = "none"
disk_compression = true
disk_discard = "unmap"
Expand All @@ -81,6 +81,7 @@ source "qemu" "centos9" {
ssh_password = var.ssh_password
ssh_username = var.ssh_username
ssh_wait_timeout = "30m"
boot_wait = "40s"
}

build {
Expand All @@ -89,7 +90,7 @@ build {
provisioner "shell" {
execute_command = "{{ .Vars }} sudo -E bash '{{ .Path }}'"
inline = ["dnf -y install epel-release", "dnf repolist", "dnf -y install ansible"]
}
}

provisioner "ansible-local" {
playbook_dir = "ansible"
Expand All @@ -100,4 +101,4 @@ build {
environment_vars = ["IMAGE_NAME=${var.name}", "IMAGE_VERSION=${var.version}", "DESTINATION_SERVER=${var.destination_server}"]
script = "scripts/push-image.sh"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 6c45198

Please sign in to comment.