Last active
September 29, 2024 09:50
-
-
Save HafizAhmadJamil/f05b2ed7cd225de6a2ba1674830166da to your computer and use it in GitHub Desktop.
Create Ubuntu Cloud Init Template with Guest tools installed
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 | |
# Function to prompt for password | |
prompt_for_password() { | |
read -s -p "Enter root password for the image: " ROOT_PASSWORD | |
echo | |
} | |
# Check for existing image | |
if [ -f "noble-server-cloudimg-amd64.img" ] || [ -f "$HOME/noble-server-cloudimg-amd64.img" ]; then | |
echo "Image file found, skipping download." | |
else | |
echo "Image file not found, downloading..." | |
wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img | |
fi | |
# Update and install required packages | |
sudo apt update -y && sudo apt install libguestfs-tools -y | |
# Install qemu-guest-agent on the image | |
sudo virt-customize -a noble-server-cloudimg-amd64.img --install qemu-guest-agent | |
# Prompt for root password | |
prompt_for_password | |
# Create a temporary password file | |
echo "$ROOT_PASSWORD" > password_root.txt | |
# Set root user password from the temporary file | |
sudo virt-customize -a noble-server-cloudimg-amd64.img --root-password file:password_root.txt | |
# Delete temporary password file safely | |
shred -uv password_root.txt | |
# Update all packages in the image | |
sudo virt-customize -a noble-server-cloudimg-amd64.img --update | |
# Create a Proxmox VM template | |
qm create 2404 --memory 2048 --cores 2 --name U2404 --net0 virtio,bridge=vmbr0 | |
qm importdisk 2404 noble-server-cloudimg-amd64.img local-lvm | |
qm set 2404 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-2404-disk-0 | |
qm set 2404 --ide2 local-lvm:cloudinit | |
qm set 2404 --boot c --bootdisk scsi0 | |
qm set 2404 --serial0 socket --vga serial0 | |
# Final message | |
echo "VM template created successfully. You can now clone this template or use it with Terraform Proxmox, etc." | |
echo "SSH login is only possible with user 'ubuntu' and SSH keys specified in the cloud-init image." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment