Skip to content

Instantly share code, notes, and snippets.

@eXsoR65
Last active September 1, 2024 09:22
Show Gist options
  • Save eXsoR65/f620aa1e461883da50472ecec4ab2d3b to your computer and use it in GitHub Desktop.
Save eXsoR65/f620aa1e461883da50472ecec4ab2d3b to your computer and use it in GitHub Desktop.
Creating a Proxmox Template from a Generic Cloud image.
# Chanage directories to /tmp
cd /tmp
# Download the Generic Cloud image from the distro downloads
wget https://Link_to_Generic_Cloud_image.com
# Validate the file is a QEMU QCOW2 image.
file ./Generic_Cloud_Image.qcow2


# Set this to your Proxmox host name; it's case sensitive! Check with `pvesh ls /nodes`
export ProxHost="PVE-Host01"
# Set this to the correct network bridge; for most people it should be vmbr0.
export ProxNetworkBridge="vmbr0"
# Set the name of the Storage Volume for Proxmox
export ProxStorageVolume="vm-storage"
# Set the name of the Storage Volume Path
export ProxStoragePath="/mnt/pve/${ProxStorageVolume}/"
# Set this to the Virtual Machine ID you want to set your template to. exmaple: 9000.
export VMID="9000"
# Set the default Disk Size in GB
export DiskSize="20"
# VM Template Name
export TEMPLATE_NAME="Template-Name"
# Set your SSH PublicKey
export SSHPUBKEY=" Your Public SSH Key"
# Set your qcow2 Disk Image Path
export DiskPath="/tmp/Generic_Cloud_Image.qcow2"
# Set your Template Cloud-Init user name
export TemplateUser="Cloud-User"

# Create your VM Template
pvesh create /nodes/${ProxHost}/qemu \
    --serial0=socket --vga=serial0 \
    --boot=c --agent=1 \
    --bootdisk=scsi0 \
    --net0='model=e1000,bridge='${ProxNetworkBridge}'' \
    --ide2=${ProxStorageVolume}:cloudinit \
    --sockets=1 --cores=2 --memory=2048 \
    -scsihw='virtio-scsi-pci' \
    --ostype=l26 --numa 0 \
    --template=1 \
    --name=${TEMPLATE_NAME} \
    --vmid=${VMID}

# Ensure that `jq` is installed on your system
sudo apt install jq -y

# Import the disk image
qm importdisk ${VMID} ${DiskPath} ${ProxStorageVolume} -f qcow2

# Mount the disk image and set the Cloud-Init settings

pvesh set /nodes/${ProxHost}/qemu/${VMID}/config \
    --scsi0=${ProxStorageVolume}:vm-${VMID}-disk-0 \
    -ipconfig0='ip=dhcp' \
    --ciuser="${TemplateUser}" \
    --sshkeys="$(printf %s "${SSHPUBKEY}" | jq -sRr @uri)"

# Set the boot drive size in GB.
pvesh set /nodes/${ProxHost}/qemu/${VMID}/resize \
    -disk=scsi0 --size="${DiskSize}G"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment