{"id":13464794,"url":"https://github.com/goffinet/packer-kvm","last_synced_at":"2025-04-12T22:31:43.995Z","repository":{"id":38027927,"uuid":"185459930","full_name":"goffinet/packer-kvm","owner":"goffinet","description":"Create VM templates with Packer for usage with Libvirt/KVM virtualization : CentOS 9 (Stream), CentOS 10 (Stream), AlmaLinux 9, Rocky 9, Focal (Ubuntu 2004), Fedora 40, Jammy (Ubuntu 2204), Noble (Ubuntu 2404), Debian 12 (Bookworm).","archived":false,"fork":false,"pushed_at":"2024-10-21T16:37:46.000Z","size":410,"stargazers_count":203,"open_issues_count":1,"forks_count":59,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-21T18:54:22.163Z","etag":null,"topics":["almalinux","centos","debian","fedora","fedora-project","kali-linux","kickstart","kvm","kvm-virtualization","packer","packer-kvm","packer-template","qcow2","qemu","qemu-images","qemu-kvm","rockylinux","ubuntu"],"latest_commit_sha":null,"homepage":"https://linux.goffinet.org","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goffinet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://leanpub.com/b/linux-administration-complet"]}},"created_at":"2019-05-07T18:48:15.000Z","updated_at":"2024-10-21T16:37:50.000Z","dependencies_parsed_at":"2023-02-10T13:31:45.374Z","dependency_job_id":"23fe8f0d-69eb-4d5e-92e1-6eaa3311761e","html_url":"https://github.com/goffinet/packer-kvm","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goffinet%2Fpacker-kvm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goffinet%2Fpacker-kvm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goffinet%2Fpacker-kvm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goffinet%2Fpacker-kvm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goffinet","download_url":"https://codeload.github.com/goffinet/packer-kvm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248640344,"owners_count":21138020,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["almalinux","centos","debian","fedora","fedora-project","kali-linux","kickstart","kvm","kvm-virtualization","packer","packer-kvm","packer-template","qcow2","qemu","qemu-images","qemu-kvm","rockylinux","ubuntu"],"created_at":"2024-07-31T14:00:50.445Z","updated_at":"2025-04-12T22:31:43.973Z","avatar_url":"https://github.com/goffinet.png","language":"HCL","funding_links":["https://leanpub.com/b/linux-administration-complet"],"categories":["Shell"],"sub_categories":[],"readme":"# packer-kvm\n\nCreate VM templates with Packer for usage with Libvirt/KVM virtualization : CentOS 9 (Stream), CentOS 10 (Stream), AlmaLinux 9, Rocky 9, Focal (Ubuntu 2004), Fedora 40, Jammy (Ubuntu 2204), Noble (Ubuntu 2404), Debian 12 (Bookworm).\n\nOnly for education and learning purposes. Do not use it in production.\n\n## Packer Concepts\n\nPacker is an open source tool for creating identical machine images for multiple platforms from a single source configuration called a  template. ([Introduction to Packer, What is Packer?](https://www.packer.io/intro#what-is-packer)). It uses different plugins as builders, provisioners or post-processors.\n\nWhile the JSON format template is still supported, certain new features in the Packer core will only be implemented for the newer HCL2 format. Therefore, this project has been updated to [HCL2](https://www.packer.io/docs/templates/hcl_templates).\n\n_Builders_ are responsible for creating machines and generating images from them for various platforms. For example, there are separate builders for EC2, VMware, VirtualBox, Qemu, etc. Packer comes with many builders by default, and can also be extended to add new builders. ([Builders](https://www.packer.io/docs/builders))\n\nPacker can enable an http server to serve kickstart, preseed or cloud-init configuration files at boot.\n\n_Provisioners_ use builtin and third-party software to install and configure the machine image after booting. ([Provisioners](https://www.packer.io/docs/provisioners))\n\n_Post-processors_ run after the image is built by the builder and provisioned by the provisioner(s). ([Post-Processors](https://www.packer.io/docs/post-processors))\n\n## Proof of Concept to generate Linux qemu images\n\nThis is a Packer \"Proof of Concept\" with :\n\n* qemu/kvm as image _builder_ (qcow2)\n* \"shell\" and \"ansible-local\" as _provisionners_\n* \"shell-local\" as _post-processor_ to generate a [gns3a appliance file](https://docs.gns3.com/1MAdxz0BSEAfGM7tA-w-o3TMmf8XOx7nBf0z6d9nRz_c/index.html), checksum and upload to a server\n\nOptionnal :\n\n* run this inside a docker container\n* build your own container\n\nEnjoy those images with :\n\n* Libvirt native tools\n* Terraform as IaC tool with a third party Libvirtd Provider plugin\n\nThe built images are intended to be published on a S3 bucket.\n\n## Pre-requisites\n\nThe run this project with success, you need a virtualization server and some softwares installed :\n\n* Libvirt/KVM, Packer and aws s3 cli\n* Docker (to run the build inside a container)\n\nUse `./setup.sh` for a quick setup of Libvirt/KVM, Packer and `aws s3 cli` but please read before the following manual instructions.\n\nFor Docker usage, install it and put your aws S3 credits in your `~/.profile`.\n\nAnyway, you can remove the post-processor in your image JSON template to avoid S3 upload attemps.\n\n### AWS S3\n\nConfigure your S3 credits :\n\n```bash\necho \"export AWS_ACCESS_KEY=\u003cyour AWS_ACCESS_KEY\u003e\" \u003e\u003e ~/.profile\necho \"export AWS_SECRET_KEY=\u003cyour AWS_SECRET_KEY\u003e\" \u003e\u003e ~/.profile\nsource ~/.profile\n```\n\n### Libvirt and Packer\n\nInstall Livirt/KVM on your server :\n\n```bash\nif [ -f /etc/debian_version ]; then\napt-get update \u0026\u0026 apt-get -y upgrade\napt-get -y install qemu-kvm libvirt-dev virtinst virt-viewer libguestfs-tools virt-manager uuid-runtime curl linux-source libosinfo-bin\nvirsh net-start default\nvirsh net-autostart default\nelif [ -f /etc/redhat-release ]; then\nyum -y install epel-release\nyum -y upgrade\nyum -y group install \"Virtualization Host\"\nyum -y install virt-manager libvirt virt-install qemu-kvm xauth dejavu-lgc-sans-fonts virt-top libguestfs-tools virt-viewer virt-manager curl\nln -s /usr/libexec/qemu-kvm /usr/bin/qemu-system-x86_64\nfi\n```\n\nInstall the Packer binary :\n\n```bash\nyum -y install wget unzip || apt update \u0026\u0026 apt -y install wget unzip\nlatest=$(curl -L -s https://releases.hashicorp.com/packer | grep 'packer_' | sed 's/^.*\u003c.*\\\"\u003epacker_\\(.*\\)\u003c\\/a\u003e/\\1/' | head -1)\nwget https://releases.hashicorp.com/packer/${latest}/packer_${latest}_linux_amd64.zip\nunzip packer*.zip\nchmod +x packer\nmv packer /usr/local/bin/\n```\n\nInstall the plugins:\n\n```bash\npacker plugins install github.com/hashicorp/qemu\npacker plugins install github.com/hashicorp/ansible\n```\n\n### Docker\n\nGet Docker and docker-compose :\n\n```bash\ncurl -fsSL https://get.docker.com -o get-docker.sh \u0026\u0026 sh get-docker.sh\nif [ -f /etc/debian_version ]; then\napt-get update \u0026\u0026 apt-get -y install python3-pip\nelif [ -f /etc/redhat-release ]; then\nyum -y install python3-pip\nfi\npip3 install docker-compose\n```\n\n## Build with Packer\n\nEach JSON file is a template for a distribution :\n\n* [almalinux9.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/almalinux9.pkr.hcl)\n* [rocky9.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/rocky9.pkr.hcl)\n* [centos9.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/centos9.pkr.hcl)\n* [centos10.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/centos10.pkr.hcl)\n* [fedora40.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/fedora40.pkr.hcl)\n* [ubuntu2004.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/ubuntu2004.pkr.hcl)\n* [ubuntu2204.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/ubuntu2204.pkr.hcl)\n* [ubuntu2404.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/ubuntu2404.pkr.hcl)\n* [debian12.pkr.hcl](https://github.com/goffinet/packer-kvm/blob/master/debian12.pkr.hcl)\n\nFor example :\n\n```bash\npacker build almalinux9.pkr.hcl\n```\n\n## Build with Docker qemu based image\n\n`goffinet/packer-qemu` is a Docker image for building qemu images with packer and is available on Docker Hub.\n\n\n```bash\ndocker run --rm \\\n  -e PACKER_LOG=1 \\\n  -e PACKER_LOG_PATH=\"packer-docker.log\" \\\n  -it \\\n  --privileged \\\n  --cap-add=ALL -v /lib/modules:/lib/modules \\\n  -v `pwd`:/opt/ \\\n  -e AWS_ACCESS_KEY=$AWS_ACCESS_KEY \\\n  -e AWS_SECRET_KEY=$AWS_SECRET_KEY \\\n  -w /opt/ goffinet/packer-qemu build almalinux9.pkr.hcl\n```\n\nThe script `build.sh` do it with the template filename as first argument.\n\n```bash\n./build.sh almalinux9.pkr.hcl\n```\n\nTo build the image localy with the [Dockerfile](https://github.com/goffinet/packer-kvm/blob/master/Dockerfile) :\n\n```bash\ndocker build -t packer-qemu .\n```\n\n## Packing monitoring\n\nPacker use VNC to launch a temporary VM, you can check this window with a VNC client like `vinagre`.\n\nYou can have more details from Packet with the env var configured : `PACKER_LOG=1`.\n\n## Cloud images for qemu/KVM/Libvirt built with Packer\n\nI 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)**.\n\n- [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)\n- [rocky9.qcow2](http://download.goffinet.org/kvm/rocky9.qcow2) [[md5sum]](http://download.goffinet.org/kvm/rocky9.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/rocky9.qcow2.sha256sum)\n- [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)\n- [centos10.qcow2](http://download.goffinet.org/kvm/centos10.qcow2) [[md5sum]](http://download.goffinet.org/kvm/centos10.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/centos10.qcow2.sha256sum)\n- [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)\n- [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)\n- [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)\n- [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)\n- [debian12.qcow2 (Bookworm)](http://download.goffinet.org/kvm/debian12.qcow2) [[md5sum]](http://download.goffinet.org/kvm/debian12.qcow2.md5sum) [[sha256sum]](http://download.goffinet.org/kvm/debian12.qcow2.sha256sum)\n\nYou can easily download them to `/var/lib/libvirt/images` with this script :\n\n```bash\ncurl -s -o /usr/local/bin/download-images.sh https://raw.githubusercontent.com/goffinet/virt-scripts/master/download-images.sh\nchmod +x /usr/local/bin/download-images.sh\ndownload-images.sh\n```\n\n## How to exploit those built images\n\nHow to exploit those built images?\n\n- In the old way with Libvirt and some bash scripts\n- In a beter way with a tool like Terraform\n\nThis is always beter to know how Libvirt is working. Can you read fundamentals about [KVM virtualization in french](https://linux.goffinet.org/administration/virtualisation-kvm/).\n\n### Enjoy with Libvirt\n\n[https://github.com/goffinet/virt-scripts](https://github.com/goffinet/virt-scripts)\n\n1. Clone virt-scripts repo and prepare the machine\n\n  ```bash\n  sudo apt update \u0026\u0026 apt -y install git\n  git clone https://github.com/goffinet/virt-scripts\n  cd virt-scripts\n  sudo ./autoprep.sh\n  ```\n\n2. Build or download images\n\n  Put builded images in `/var/lib/libvirt/images` or download them :\n\n  ```bash\n  sudo ./download-images.sh\n  ```\n  ```raw\n  Please provide the image name :\n  almalinux9 centos9 centos10 debian12 fedora40 ubuntu2004 ubuntu2204 ubuntu2404\n  ```\n\n3. Launch two new machines\n\n  ```bash\n  sudo ./define-guest-image.sh c1 almalinux9\n  sudo ./define-guest-image.sh u1 ubuntu2204\n  ```\n\n4. Enjoy\n\n  ```bash\n  sudo virsh console u1\n  ```\n\n  ```bash\n  ssh $(dig @192.168.122.1 +short u1)\n  ```\n\n### Enjoy with Terraform (with libvirt)\n\n\u003e This section should be revised\n\n[https://github.com/goffinet/terraform-libvirt](https://github.com/goffinet/terraform-libvirt)\n\nInstall Terraform 0.13 with a third party Libvirt provider plugin :\n\n```bash\necho \"security_driver = \\\"none\\\"\" \u003e\u003e /etc/libvirt/qemu.conf\nsystemctl restart libvirtd\nsudo yum -y install wget unzip || sudo apt update \u0026\u0026 sudo apt -y install wget unzip\nwget https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_amd64.zip\nunzip terraform_0.13.2_linux_amd64.zip\nchmod +x terraform\nmv terraform /usr/local/bin/\nwget https://github.com/dmacvicar/terraform-provider-libvirt/releases/download/v0.6.2/terraform-provider-libvirt-0.6.2+git.1585292411.8cbe9ad0.Ubuntu_18.04.amd64.tar.gz\ntar xvf terraform-provider-libvirt-0.6.2+git.1585292411.8cbe9ad0.Ubuntu_18.04.amd64.tar.gz\nmkdir -p ~/.local/share/terraform/plugins/registry.terraform.io/dmacvicar/libvirt/0.6.2/linux_amd64\ncp -r terraform-provider-libvirt ~/.local/share/terraform/plugins/registry.terraform.io/dmacvicar/libvirt/0.6.2/linux_amd64/\n```\n\nCompose your libvirt infrastructure :\n\n```bash\ngit clone https://github.com/goffinet/terraform-libvirt\ncd terraform-libvirt/ubuntu_cloudinit\nterraform plan\ncd ../count\nterraform plan\n```\n\n## Customization\n\n### To customize post-processing\n\nThe `scripts/push-image.sh` generate somme meta-data and push the generated image to a pre-defined S3 Bucket.\n\nTo customize this process, you can change the content as it :\n\n```bash\n#!/bin/bash\n\nname=$IMAGE_NAME\nversion=$IMAGE_VERSION\nimage=\"${name}${version}\"\necho \"artifacts/qemu/${image} post-processing ...\"\n```\n\nAnyway, you can remove the post-processor in your image JSON template to avoid this script call.\n\n### Customize SSH keys\n\nTo generate the ssh keys for provisionning and put it in the `sshkeys/` folder :\n\n```bash\nssh-keygen -q -t rsa -N '' -C 'packer-kvm-default-key' -f sshkeys/id_rsa\n```\n\nTo get the default ssh private key :\n\n```bash\ncurl https://raw.githubusercontent.com/goffinet/packer-kvm/master/sshkeys/id_rsa\n```\n\nTo get the default ssh public key :\n\n```bash\ncurl https://raw.githubusercontent.com/goffinet/packer-kvm/master/sshkeys/id_rsa.pub\n```\n\n### How are these Packer templates produced?\n\nAn `build-packer-templates.yaml` Ansible playbook generates the files for each distribution using a custom [`build_packer_templates`](https://github.com/goffinet/packer-kvm/tree/master/roles/build_packer_templates) role.\n\n## ToDo\n\n- [ ] unique model : for efficience, a unique template should be sufficient with a data source with these elements (https://devops.stackexchange.com/q/4312).\n- [ ] random secret for provisonning (https://www.packer.io/docs/templates/engine#template-variables)\n- [ ] remove swap post-processing\n- [ ] add versions of post-processing and images meta-datas\n- [ ] include Windows templates: see [rgl/windows-vagrant](https://github.com/rgl/windows-vagrant)\n\nWath are the variants in those templates?\n\n- the `iso_urls` and the `iso_checksum` template parameters\n- the `ssh_username`, `ssh_password` and `ssh_private_key_file` template parameters as well as `sshkey` in kickstart files.\n- the `boot_command` and provisioner commands as template parameters\n- the configuration file type : kickstart, preseed or cloud-init\n\n## Initials credits\n\n* [https://github.com/idi-ops/packer-kvm-centos](https://github.com/idi-ops/packer-kvm-centos)\n* [https://github.com/jakobadam/packer-qemu-templates](https://github.com/jakobadam/packer-qemu-templates)\n* [https://github.com/leonkyneur/packer-qemu](https://github.com/leonkyneur/packer-qemu)\n* [https://github.com/kaorimatz/packer-templates](https://github.com/kaorimatz/packer-templates)\n* [https://github.com/bramford/packer-debian9](https://github.com/bramford/packer-debian9)\n* [https://github.com/bpetit/packer-templates](https://github.com/bpetit/packer-templates)\n* [https://github.com/NeCTAR-RC/nectar-images/](https://github.com/NeCTAR-RC/nectar-images/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoffinet%2Fpacker-kvm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoffinet%2Fpacker-kvm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoffinet%2Fpacker-kvm/lists"}