Skip to content

Instantly share code, notes, and snippets.

@mofelee
Forked from mike1237/base.pkr.hcl
Last active August 31, 2023 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mofelee/578e7d7e88107af49c1ead3767f198df to your computer and use it in GitHub Desktop.
Save mofelee/578e7d7e88107af49c1ead3767f198df to your computer and use it in GitHub Desktop.
#!/bin/bash
export SRC_IMG="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
export IMG_NAME="debian-12-generic-amd64.qcow2"
export TEMPL_NAME="debian12-generic"
export VMID="9001"
export MEM="1024"
export DISK_SIZE="8G"
export DISK_STOR="local-lvm"
export NET_BRIDGE="vmbr0"
#!/bin/bash
export SRC_IMG="https://cloud-images.ubuntu.com/lunar/current/lunar-server-cloudimg-amd64-disk-kvm.img"
export IMG_NAME="lunar-server-cloudimg-amd64-disk-kvm.qcow2"
export TEMPL_NAME="ubuntu2304-cloud"
export VMID="9002"
export MEM="1024"
export DISK_SIZE="8G"
export DISK_STOR="local-lvm"
export NET_BRIDGE="vmbr0"
#!/bin/bash
export SRC_IMG="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64-disk-kvm.img"
export IMG_NAME="jammy-server-cloudimg-amd64-disk-kvm.qcow2"
export TEMPL_NAME="ubuntu2204-cloud"
export VMID="9000"
export MEM="1024"
export DISK_SIZE="8G"
export DISK_STOR="local-lvm"
export NET_BRIDGE="vmbr0"
#!/bin/bash
wget -O $IMG_NAME $SRC_IMG
#!/bin/bash
apt update
apt install -y libguestfs-tools
virt-customize --install qemu-guest-agent -a $IMG_NAME
#!/bin/bash
# Must run on Proxmox VE 8 server
# Not sure how to handle a cluster - either run on each node or copy template after creating on one?
# e.g. $ ssh root@proxmox.server < proxmox-create-cloud-template.sh
# SRC_IMG="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64-disk-kvm.img"
# IMG_NAME="jammy-server-cloudimg-amd64-disk-kvm.qcow2"
# TEMPL_NAME="ubuntu2204-cloud"
# VMID="9000"
# MEM="1024"
# DISK_SIZE="8G"
# DISK_STOR="local-lvm"
# NET_BRIDGE="vmbr0"
# Install libguesetfs-tools to modify cloud image
# apt update
# apt install -y libguestfs-tools
# Download kvm image and rename
# Ubuntu img is actually qcow2 format and Proxmox doesn't like wrong extensions
# wget -O $IMG_NAME $SRC_IMG
# Ubuntu cloud img doesn't include qemu-guest-agent required for packer to get IP details from proxmox
# Add any additional packages you want installed in the template
# virt-customize --install qemu-guest-agent -a $IMG_NAME
# Create cloud-init enabled Proxmox VM with DHCP addressing
qm create $VMID --name $TEMPL_NAME --memory $MEM --net0 virtio,bridge=$NET_BRIDGE
qm importdisk $VMID $IMG_NAME $DISK_STOR
qm set $VMID --scsihw virtio-scsi-pci --scsi0 $DISK_STOR:vm-$VMID-disk-0
qm set $VMID --ide2 $DISK_STOR:cloudinit
qm set $VMID --boot c --bootdisk scsi0
qm set $VMID --serial0 socket --vga serial0
qm set $VMID --ipconfig0 ip=dhcp
qm resize $VMID scsi0 $DISK_SIZE
# Convert to template
qm template $VMID
# Remove downloaded image
rm $IMG_NAME
# Next, use packer to clone this template and customize it!
# References
# https://gist.github.com/mike1237/cce83a74f898b11c2cec911204568cf9
# https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466
# https://whattheserver.com/proxmox-cloud-init-os-template-creation/
# https://norocketscience.at/deploy-proxmox-virtual-machines-using-cloud-init/
# https://pve.proxmox.com/wiki/Cloud-Init_Support
# https://blog.dustinrue.com/2020/05/going-deeper-with-proxmox-cloud-init/
variable "proxmox_host_node" {
type = string
}
variable "proxmox_password" {
type = string
sensitive = true
}
variable "proxmox_source_template" {
type = string
}
variable "proxmox_template_name" {
type = string
}
variable "proxmox_url" {
type = string
}
variable "proxmox_username" {
type = string
default = "root@pam"
}
source "proxmox-clone" "test-cloud-init" {
insecure_skip_tls_verify = true
full_clone = false
template_name = "${var.proxmox_template_name}"
clone_vm = "${var.proxmox_source_template}"
os = "l26"
cores = "1"
memory = "512"
scsi_controller = "virtio-scsi-pci"
ssh_username = "ubuntu"
qemu_agent = true
network_adapters {
bridge = "vmbr0"
model = "virtio"
}
node = "${var.proxmox_host_node}"
username = "${var.proxmox_username}"
password = "${var.proxmox_password}"
proxmox_url = "${var.proxmox_url}"
}
build {
sources = ["source.proxmox-clone.test-cloud-init"]
provisioner "shell" {
inline = ["sudo cloud-init clean"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment