Last active
May 18, 2024 07:56
-
-
Save hunzo/c984bbc170740eaa9ad8be8009800972 to your computer and use it in GitHub Desktop.
proxmox example cloud-init script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
VMID=9001 | |
STORAGE=ZFS-SSD | |
set -x | |
# rm -f noble-server-cloudimg-amd64.img | |
# wget -q https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img | |
# ubuntu release name: noble,mantic,jammy,luna | |
RELEASE=noble | |
# IMG_TEMPLATE=/var/lib/vz/template/iso/$RELEASE-server-cloudimg-amd64.img | |
# IMG_TEMPLATE=/var/lib/vz/template/iso/$RELEASE-server-cloudimg-amd64.img | |
IMG_TEMPLATE=/var/lib/vz/template/iso/ubuntu-24.04-minimal-cloudimg-amd64.img | |
# IMG_TEMPLATE=/var/lib/vz/template/iso/ubuntu-22.04-minimal-cloudimg-amd64.img | |
qemu-img resize $IMG_TEMPLATE 32G | |
qm create $VMID --name "ubuntu-$RELEASE-template" \ | |
--memory 8192 --balloon 0 \ | |
--agent 1 \ | |
--sockets 2 \ | |
--cores 2 \ | |
--vga serial0 --serial0 socket \ | |
--net0 virtio,bridge=vmbr0,mtu=1,tag=31 \ | |
--scsihw virtio-scsi-pci | |
qm set $VMID --scsi0 $STORAGE:0,import-from=$IMG_TEMPLATE,ssd=1 | |
qm set $VMID --boot order=scsi0 | |
qm set $VMID --ide2 $STORAGE:cloudinit | |
if [ "$RELEASE" == "noble" ]; then | |
cat << EOF | tee /var/lib/vz/snippets/ubuntu.yaml | |
#cloud-config | |
runcmd: | |
- sed -i 's/archive.ubuntu.com/mirror.kku.ac.th/g' /etc/apt/sources.list.d/ubuntu.sources | |
- sed -i 's/security.ubuntu.com/mirror.kku.ac.th/g' /etc/apt/sources.list.d/ubuntu.sources | |
- echo 'Acquire::ForceIPv4 "true";' > /etc/apt/apt.conf.d/99force-ipv4 | |
- apt-get update -y | |
- apt-get upgrade -y | |
- apt-get install -y qemu-guest-agent | |
- apt-get install -y bash-completion | |
- systemctl enable ssh | |
- reboot | |
# Taken from https://forum.proxmox.com/threads/combining-custom-cloud-init-with-auto-generated.59008/page-3#post-428772 | |
EOF | |
else | |
cat << EOF | tee /var/lib/vz/snippets/ubuntu.yaml | |
#cloud-config | |
runcmd: | |
- sed -i 's/archive.ubuntu.com/mirror.kku.ac.th/g' /etc/apt/sources.list | |
- sed -i 's/security.ubuntu.com/mirror.kku.ac.th/g' /etc/apt/sources.list | |
- cat 'Acquire::ForceIPv4 "true";' > /etc/apt/apt.conf.d/99force-ipv4 | |
- apt-get update -y | |
- apt-get upgrade -y | |
- apt-get install -y qemu-guest-agent | |
- apt-get install -y bash-completion | |
- systemctl enable ssh | |
- reboot | |
# Taken from https://forum.proxmox.com/threads/combining-custom-cloud-init-with-auto-generated.59008/page-3#post-428772 | |
EOF | |
fi | |
qm set $VMID --cicustom "vendor=local:snippets/ubuntu.yaml" | |
qm set $VMID --tags ubuntu-template,$RELEASE,cloudinit | |
read -p 'Username: ' username | |
qm set $VMID --ciupgrade 0 | |
qm set $VMID --ciuser $username | |
qm set $VMID --cipassword $(openssl passwd -6 $CLEARTEXT_PASSWORD) | |
qm set $VMID --sshkeys ./id_ed25519.pub | |
qm set $VMID --ipconfig0 ip=dhcp | |
qm template $VMID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment