Skip to content

Commit

Permalink
feat(proxmox):add script to create template
Browse files Browse the repository at this point in the history
  • Loading branch information
smerlos committed Jan 24, 2024
1 parent 8d5abf5 commit a0022d5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ override.tf.json
# Ignore CLI configuration files
.terraformrc
terraform.rc

# custom files
proxmox_vm_module/cloud-inits/*.yml
scripts/**
terraform-plugin-proxmox.log
inventory.yaml
113 changes: 113 additions & 0 deletions scripts/cloud-init-qemu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash

# Stop on undefined variables
set -u

# Configuration
VM_ID=8001
VM_NAME="ubuntu-2204-cloudinit-template"
STORAGE_NAME="local"
IMAGE_URL="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
IMAGE_PATH="/var/lib/vz/template/iso/jammy-server-cloudimg-amd64.img"
CLOUD_INIT_USER="localadmin "
CLOUD_INIT_PASSWORD="hackme123" # Replace with your actual password
GITHUB_USER="user" # Replace with desired GitHub username
NETWORK_BRIDGE="vmbr1" # Replace with your network bridge

# Function to download SSH keys from GitHub
download_github_keys() {
local github_user=$1
local keys_url="https://github.com/${github_user}.keys"
wget -q -O - "$keys_url"
}

# Download and resize the Ubuntu image
if [ ! -f "$IMAGE_PATH" ]; then
wget -q "$IMAGE_URL" -O "$IMAGE_PATH"
qemu-img resize "$IMAGE_PATH" 32G
fi

# Check if the VM ID is already in use
if qm list | grep -q " $VM_ID "; then
echo "Error: VM ID $VM_ID is already in use."
exit 1
fi

# Create a new VM
qm create $VM_ID --name "$VM_NAME" --ostype l26 \
--memory 1024 \
--agent 1 \
--bios ovmf --machine q35 --efidisk0 $STORAGE_NAME:0,pre-enrolled-keys=0 \
--cpu host --socket 1 --cores 1 \
--vga serial0 --serial0 socket \
--net0 virtio,bridge=$NETWORK_BRIDGE

# Import the downloaded image to Proxmox storage
qm importdisk $VM_ID "$IMAGE_PATH" $STORAGE_NAME

# Configure hardware settings
qm set $VM_ID --scsihw virtio-scsi-pci --virtio0 $STORAGE_NAME:$VM_ID/vm-$VM_ID-disk-1.raw,discard=on
qm set $VM_ID --boot order=virtio0
qm set $VM_ID --ide2 $STORAGE_NAME:cloudinit

# Create vendor.yaml for CloudInit
cat << EOF | sudo tee /var/lib/vz/snippets/vendor.yaml
#cloud-config
users:
- name: smerlos
gecos: smerlos
groups: sudo, users, admin
shell: /bin/bash
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
ssh_import_id:
- gh:smerlos
system_info:
default_user:
name: hack-me
lock_passwd: false
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
random_seed:
file: /dev/urandom
command: ["pollinate", "-r", "-s", "https://entropy.ubuntu.com"]
command_required: true
package_upgrade: true
packages:
- open-vm-tools
- build-essential
- python3-pip
- python3
- vim
- screen
- apt-transport-https
- ca-certificates
- curl
- wget
- git
- ack-grep
- open-iscsi
- ifupdown
- resolvconf
- qemu-guest-agent
runcmd:
- apt update
- apt upgrade -y
- apt install -f -y
- apt full-upgrade -y
- apt-get -y dist-upgrade -y
- apt autoremove -y
- systemctl start qemu-guest-agent
power_state:
timeout: 5
mode: reboot
EOF

# Configuring CloudInit
qm set $VM_ID --cicustom "vendor=local:snippets/vendor.yaml,user=local:snippets/vendor.yaml,meta=local:snippets/vendor.yaml"
qm set $VM_ID --tags ubuntu-template,22.04,cloudinit

qm set $VM_ID --ipconfig0 ip=dhcp

# Convert the VM into a template
qm template $VM_ID

echo "Template $VM_NAME created successfully."

0 comments on commit a0022d5

Please sign in to comment.