commit e56e9ccd88593c2d3a188a5114f733922bf0767c Author: root Date: Mon Jul 15 12:58:32 2024 +0000 [global] feat: initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..91118fa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +variables.pkrvars.hcl diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..a7dd210 --- /dev/null +++ b/readme.md @@ -0,0 +1,41 @@ +# What does this stuff do? +Packer is a tool that can create Virtual Machine templates from code. This repository hosts the code for a customized templates used in Siempie's home environment using the Proxmox Hypervisor. + +# Template +Inside the template packer does: + - Install updates + - Install tools; curl, qemu-guest agent, sudo and wget. + - Install Ansible user with private key + - Clean guest (machine-id, logs, apt cache, etc) + +Other information to know: + - Template hostname: `localhost` + + - packer username: `packer` + - packer password: `packer` + - Ubuntu 24 Template: VM ID 999 + +More info: https://developer.hashicorp.com/packer/docs/intro + +| OS | VM Name | VM ID | +|----|---------|-------| +| Ubuntu 24 | ubuntu.template.siempie.internal | 999 | + +# Install packer +Install packer on your system: +https://developer.hashicorp.com/packer/tutorials/docker-get-started/get-started-install-cli + +# Variables +Copy the `variables.pkrvars.hcl.example` file to `variables.pkrvars.hcl` and change the variables. + +# Initialize the project and install the proxmox provider +``` +packer init -var-file variables.pkrvars.hcl ubuntu24/packer.pkr.hcl +``` + +# Usage +Ubuntu 24 +``` +packer build -force -var-file variables.pkrvars.hcl ubuntu24/packer.pkr.hcl +``` +Note: the `-force` flag removes an already existing VM or Template with the same ID (999). diff --git a/scripts/magic.sh b/scripts/magic.sh new file mode 100644 index 0000000..cd42771 --- /dev/null +++ b/scripts/magic.sh @@ -0,0 +1,91 @@ +#!/usr/bin/bash + +# Update the guest operating system +echo '> Updating the guest operating system ...' +sudo -u root apt update +sudo -u root DEBIAN_FRONTEND=noninteractive apt -y upgrade +sudo -u root apt autoremove -y + +# install rc.local script +cat << 'EOL' | sudo tee /etc/rc.local +#!/bin/sh -ef +# +test -f /etc/ssh/ssh_host_dsa_key || sudo -u root dpkg-reconfigure openssh-server +exit 0 +EOL +sudo -u root chmod +x /etc/rc.local + +# grow disk +echo '> grow disk ...' +sudo -u root lvresize --resizefs -l+100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv + +# cloud-init cleanup +while [ ! -f /var/lib/cloud/instance/boot-finished ]; do + echo '> Waiting for cloud-init...'; + sleep 1; +done +sudo -u root rm -f /etc/cloud/cloud.cfg.d/99-installer.cfg +sudo -u root cloud-init clean + +# cleaning logs +echo '> cleaning logs ...' +sudo -u root rm -rf /var/log/* + +# cleans persistent udev rules +echo '> cleaning persistent udev rules ...' +if [ -f /etc/udev/rules.d/70-persistent-net.rules ]; then +sudo -u root rm /etc/udev/rules.d/70-persistent-net.rules +fi + +# cleans /tmp directories +echo '> cleaning /tmp directories ...' +sudo -u root rm -rf /tmp/* +sudo -u root rm -rf /var/tmp/* + +# cleans SSH keys +echo '> cleaning ssh host keys ...' +sudo -u root rm -f /etc/ssh/ssh_host_* + +# sets hostname to localhost +echo '> setting hostname to localhost ...' +sudo -u root bash -c "cat /dev/null > /etc/hostname" +sudo -u root hostnamectl set-hostname localhost + +# cleans apt. +echo '> cleaning apt-get ...' +sudo -u root apt clean + +# cleans the machine-id +echo '> cleaning the machine-id ...' +sudo -u root truncate -s 0 /etc/machine-id +sudo -u root rm /var/lib/dbus/machine-id +sudo -u root ln -s /etc/machine-id /var/lib/dbus/machine-id + +# cleans shell history +echo '> cleaning shell history ...' +unset HISTFILE +history -cw +echo > ~/.bash_history +sudo -u root rm -fr /root/.bash_history + +# install the ansible user +echo '> create ansible user ...' +sudo -u root useradd -m ansible + +# configure ansible user +sudo -u root mkdir -p /home/ansible/.ssh +sudo -u root bash -c "echo \"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH9H3XK4IaK2pd8xegsbCm0at70kCi33vYkHDccV3Iyn\" >> /home/ansible/.ssh/authorized_keys" +sudo -u root chmod 700 /home/ansible/.ssh +sudo -u root chmod 600 /home/ansible/.ssh/authorized_keys +sudo -u root chown -R ansible:ansible /home/ansible + +# configure sudo for ansible +sudo -u root bash -c "echo \"ansible ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers.d/ansible" + +# zero fill the disk +echo '> zero fill disk ...' +sudo -u root bash -c "cat /dev/zero > /zero.file | exit 0 && sync && rm -f /zero.file" + +# all done +echo '> done.' +echo '> packer template build -- complete' diff --git a/ubuntu24/http/meta-data b/ubuntu24/http/meta-data new file mode 100644 index 0000000..e69de29 diff --git a/ubuntu24/http/user-data b/ubuntu24/http/user-data new file mode 100644 index 0000000..89210b6 --- /dev/null +++ b/ubuntu24/http/user-data @@ -0,0 +1,46 @@ +#cloud-config +autoinstall: + version: 1 + locale: en_US + keyboard: + layout: us + storage: + layout: + name: lvm + network: + version: 2 + ethernets: + all-en: + dhcp4: true + match: + name: en* + all-eth: + dhcp4: true + match: + name: eth* + + # packer user + identity: + hostname: packer + username: packer + password: "$6$WUe8MQLrLReDtp7S$cz5dzvQ8CZjL1.YvT7xMvyqC6DJ.vIkCJJHgkuRIS/LICSw.pqA0LgRh/rMUiv.UXZl.IMBIrDAhC.CzG.ASa1" + + # enable ssh + ssh: + install-server: yes + allow-pw: yes + + # create root account + user-data: + disable_root: false + + # install qemu guest agent + packages: + - curl + - qemu-guest-agent + - sudo + - wget + + # allow ubuntu user pwless sudo + late-commands: + - echo 'packer ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/packer diff --git a/ubuntu24/packer.pkr.hcl b/ubuntu24/packer.pkr.hcl new file mode 100644 index 0000000..c517df6 --- /dev/null +++ b/ubuntu24/packer.pkr.hcl @@ -0,0 +1,109 @@ +packer { + required_plugins { + proxmox = { + version = ">=1.1.7" + source = "github.com/hashicorp/proxmox" + } + } +} + +variable "proxmox_iso_pool" { + type = string + default = "cracky-fs-images:iso" +} + +variable "proxmox_node" { + type = string + default = "" +} + +variable "proxmox_password" { + type = string + default = "" +} + +variable "proxmox_storage_format" { + type = string + default = "raw" +} + +variable "proxmox_storage_pool" { + type = string + default = "cracky-fs" +} + +variable "proxmox_storage_pool_type" { + type = string + default = "rbd" +} + +variable "proxmox_url" { + type = string + default = "" +} + +variable "proxmox_username" { + type = string + default = "" +} + +variable "template_description" { + type = string + default = "Ubuntu 24.04 Template" +} + +variable "template_name" { + type = string + default = "template.test.siempie.internal" +} + +variable "ubuntu_image" { + type = string + default = "ubuntu-24.04-live-server-amd64.iso" +} + +variable "version" { + type = string + default = "" +} + +source "proxmox-iso" "template" { + boot_command = ["c", "linux /casper/vmlinuz -- autoinstall ds='nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/'", "", "initrd /casper/initrd", "", "boot"] + boot_wait = "10s" + cores = "2" + disks { + disk_size = "20G" + format = "${var.proxmox_storage_format}" + storage_pool = "${var.proxmox_storage_pool}" + type = "virtio" + } + http_directory = "ubuntu24/http" + insecure_skip_tls_verify = true + iso_file = "${var.proxmox_iso_pool}/${var.ubuntu_image}" + memory = "2048" + network_adapters { + bridge = "vmbr0" + } + node = "${var.proxmox_node}" + vm_id = "999" + tags = "mgmt" + os = "l26" + password = "${var.proxmox_password}" + proxmox_url = "${var.proxmox_url}" + scsi_controller = "virtio-scsi-single" + ssh_port = 22 + ssh_timeout = "10m" + ssh_username = "${var.template_username}" + ssh_password = "${var.template_password}" + template_description = "${var.template_description}" + template_name = "${var.template_name}" + unmount_iso = true + username = "${var.proxmox_username}" +} + +build { + sources = ["source.proxmox-iso.template"] + provisioner "shell" { + script = "scripts/magic.sh" + } +} diff --git a/variables.pkrvars.hcl.example b/variables.pkrvars.hcl.example new file mode 100644 index 0000000..a721dce --- /dev/null +++ b/variables.pkrvars.hcl.example @@ -0,0 +1,7 @@ +proxmox_node = "pve0" +proxmox_url = "https://pve0:8006/api2/json" +proxmox_storage_pool = "local-lvm" +proxmox_username = "packer@pve" +proxmox_password = "super-secure" +template_username = "packer" +template_password = "packer"