Skip to content

Instantly share code, notes, and snippets.

@LarsenClose
Last active September 6, 2022 11:51
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 LarsenClose/8242df1252551dc9944a229e9df2b542 to your computer and use it in GitHub Desktop.
Save LarsenClose/8242df1252551dc9944a229e9df2b542 to your computer and use it in GitHub Desktop.
kinetic kudu psionic non-voodoo
#!/bin/bash
# Proxmox Ubuntu Cloud Image ${VARIENT}
# This script is designed to be run on the Proxmox host.
# Source
# https://www.yanboyang.com/clouldinit/
# and
# https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466
# and
# https://github.com/modem7/create-cloud-template.sh
# and
# then
# changed, improved, adapted
# now with integrity checking
# to pull down this to a proxmox host wget the raw
# wget https://gist.githubusercontent.com/LarsenClose/8242df1252551dc9944a229e9df2b542/raw/5d0a21a3759894c4e704ab7802893ba13ce9d13c/clouds-very.sh
# or clone the .git
# git clone https://gist.github.com/8242df1252551dc9944a229e9df2b542.git
# Prerequesites:
# Install "apt-get install libguestfs-tools".
# Check if libguestfs-tools is installed - exit if it isn't.
REQUIRED_PKG="libguestfs-tools"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo "Checking for $REQUIRED_PKG: $PKG_OK"
if [ "" = "$PKG_OK" ]; then
echo "No $REQUIRED_PKG. Please run apt-get install $REQUIRED_PKG."
exit
fi
VARIENT_DEFAULT="kinetic"
read -p "Enter the Ubuntu Cloud Varient desired ie kinetic jammy impish focal ect $VARIENT [$VMID_DEFAULT]: " VARIENT
VARIENT=${VARIENT:-$VARIENT_DEFAULT}
# Image variables
SRC_IMG="https://cloud-images.ubuntu.com/${VARIENT}/current/${VARIENT}-server-cloudimg-amd64.img"
SRC_NAME="${VARIENT}-server-cloudimg-amd64.img"
IMG_NAME="${VARIENT}-server-cloudimg-amd64.qcow2"
SHASUMS="https://cloud-images.ubuntu.com/${VARIENT}/current/SHA256SUMS"
WORK_DIR="/tmp"
# Download image
cd $WORK_DIR
wget $SRC_IMG
wget -O $WORK_DIR/SHASUMS $SHASUMS
# Verify integrity
if ! shasum --check --ignore-missing $WORK_DIR/SHASUMS; then
echo "Checksum failed" >&2
exit 1
fi
# rename formatting
mv $WORK_DIR/$SRC_NAME $IMG_NAME
# Image variables
OSNAME="${VARIENT}"
TEMPL_NAME="${VARIENT}-cloudimg"
VMID_DEFAULT="10000"
read -p "Enter a VM ID for $OSNAME [$VMID_DEFAULT]: " VMID
VMID=${VMID:-$VMID_DEFAULT}
CLOUD_USER_DEFAULT="root"
read -p "Enter a Cloud-Init Username for $OSNAME [$CLOUD_USER_DEFAULT]: " CLOUD_USER
CLOUD_USER=${CLOUD_USER:-$CLOUD_USER_DEFAULT}
GENPASS=$(date +%s | sha256sum | base64 | head -c 16 ; echo)
CLOUD_PASSWORD_DEFAULT=$GENPASS
read -p "Enter a Cloud-Init Password for $OSNAME [$CLOUD_PASSWORD_DEFAULT]: " CLOUD_PASSWORD
CLOUD_PASSWORD=${CLOUD_PASSWORD:-$CLOUD_PASSWORD_DEFAULT}
MEM="2048"
CORES='2'
BALLOON="512"
DISK_SIZE="20G"
DISK_STOR="octo-fs"
NET_BRIDGE="vmbr910"
VLAN="" # Set if you have VLAN requirements
QUEUES="2"
OS_TYPE="l26"
AGENT_ENABLE="1" #change to 0 if you don't want the guest agent
FSTRIM="1"
CITYPE="nocloud"
BIOS="ovmf" # Choose between ovmf or seabios
# install qemu-guest-agent inside image
virt-customize --install qemu-guest-agent -a $IMG_NAME
# create VM
qm create $VMID --name $TEMPL_NAME --memory $MEM --balloon $BALLOON --bios $BIOS --net0 virtio,bridge=${NET_BRIDGE}${VLAN:+,tag=$VLAN}
qm set $VMID --agent enabled=$AGENT_ENABLE,fstrim_cloned_disks=$FSTRIM
qm set $VMID --cores $CORES
qm set $VMID --ostype $OS_TYPE
qm importdisk $VMID $WORK_DIR/$IMG_NAME $DISK_STOR
qm set $VMID --scsihw virtio-scsi-pci --scsi0 $DISK_STOR:vm-$VMID-disk-0,cache=writethrough,discard=on
qm set $VMID --ide2 $DISK_STOR:cloudinit
qm set $VMID --efidisk0 $DISK_STOR:0,efitype=4m,,,pre-enrolled-keys=1,size=528K
qm set $VMID --citype $CITYPE
qm set $VMID --ciuser $CLOUD_USER
qm set $VMID --cipassword $CLOUD_PASSWORD
qm set $VMID --boot c --bootdisk scsi0
qm set $VMID --serial0 socket --vga serial0
qm resize $VMID scsi0 $DISK_SIZE
qm template $VMID
# Delete previously downloaded file
rm -v $WORK_DIR/$IMG_NAME
rm -v $WORK_DIR/SHASUMS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment