Last active
July 13, 2025 18:01
-
-
Save Rene-Roscher/9bb1d85d436648afb832304e9dbbb9a2 to your computer and use it in GitHub Desktop.
Proxmox Cloud-Init / Image Pre-Configurator
This file contains hidden or 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 | |
# Farbdefinitionen | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[1;33m' | |
BLUE='\033[0;34m' | |
WHITE='\033[1;37m' | |
NC='\033[0m' # Keine Farbe | |
availableVersions="debian11, debian12, ubuntu20, ubuntu22, fedora37, fedora38, centos8, centos9" | |
# Check ob alle Parameter mitgegbeen werden | |
if [ $# -lt 5 ]; then | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Verwendung: $0 <version> <vmid> <templatename> <storage> <vmBridge>" | |
echo -e "${BLUE}HINWEIS!<version>: ${availableVersions}" | |
echo -e "${BLUE}HINWEIS!<vmid>: ID, die das Template erhalten soll. Diese muss einzigartig sein." | |
echo -e "${BLUE}HINWEIS!<templatename>: Wie das Template heißen soll." | |
echo -e "${BLUE}HINWEIS!<storage>: Das Storage, wo die VM abgelegt werden soll." | |
echo -e "${BLUE}HINWEIS!<vmBridge>: Die bridge, worüber die vom erstellten Template geklonten VMs später Internet erhalten." | |
echo -e "${NC}" | |
exit 1 | |
fi | |
# Debian Version, mögliche Eingabe: debian11, debian12, ubuntu20, ubuntu22, fedora37, fedora38, centos8, centos9 | |
version=$1 | |
# Template ID | |
vmid=$2 | |
# Name des templates | |
templatename=$3 | |
# Storage des Hosts | |
storage=$4 | |
# VM-Bridge des Hosts | |
vmBridge=$5 | |
imageURL="" | |
imageName="" | |
isFedora="false" | |
isCentOS="false" | |
start_template_creation(){ | |
download_and_prepare_image | |
# Pakete und Anpassungen vornehmen | |
if [ "${isFedora}" == "true" ]; then | |
install_packages_fedora | |
elif [ "${isCentOS}" == "true" ]; then | |
install_packages_centos | |
else | |
install_packages | |
fi | |
# Root login erlauben | |
configure_ssh_settings | |
create_and_configure_vm | |
if [ "$isCentOS" == "true" ]; then | |
set_template_and_cleanup_centos | |
else | |
set_template_and_cleanup | |
fi | |
} | |
# Funktion zum Herunterladen und Vorbereiten des Images | |
download_and_prepare_image() { | |
echo -e "${WHITE}INFO: Lösche mögliches vorhandenes Image und lade die aktuellste Version herunter${NC}" | |
rm -f ${imageName} | |
wget -q ${imageURL} | |
} | |
# Funktion zur Installation von Paketen für Ubuntu und Debian | |
install_packages() { | |
echo -e "${WHITE}INFO: Installiere Pakete für Ubuntu oder Debian${NC}" | |
sudo virt-customize -a "${imageName}" --install qemu-guest-agent,htop,nload,iftop,iotop,git,rsync,traceroute,dnsutils,net-tools,curl,wget,cron | |
} | |
# Funktion zur Installation von Paketen für CentOS | |
install_packages_centos() { | |
echo -e "${WHITE}INFO: Installiere Pakete für CentOS${NC}" | |
sudo virt-customize -a "${imageName}" --install epel-release | |
sudo virt-customize -a "${imageName}" --install qemu-guest-agent,htop,nload,iftop,iotop,git,rsync,traceroute,bind-utils,net-tools,curl,wget,crontabs,nano | |
} | |
# Funktion zur Installation von Paketen für Fedora | |
install_packages_fedora() { | |
echo -e "${WHITE}INFO: Installiere Pakete für Fedora${NC}" | |
sudo virt-customize -a "${imageName}" --install qemu-guest-agent,htop,nload,iftop,iotop,git,rsync,traceroute,bind-utils,net-tools,curl,wget,cronie,nano | |
} | |
# Funktion zum Konfigurieren der SSH-Einstellungen | |
configure_ssh_settings() { | |
echo -e "${WHITE}INFO: Konfiguriere SSH-Einstellungen${NC}" | |
sudo virt-customize -a "${imageName}" \ | |
--run-command 'wget -O /etc/systemd/system/first-boot.service "https://gist.githubusercontent.com/Rene-Roscher/9bb1d85d436648afb832304e9dbbb9a2/raw/25c093df5a3c4ddee7a8e8720400bea713837cad/first-boot.service"' \ | |
--run-command 'wget -O /etc/first-boot.sh "https://gist.githubusercontent.com/Rene-Roscher/9bb1d85d436648afb832304e9dbbb9a2/raw/25c093df5a3c4ddee7a8e8720400bea713837cad/first-boot.sh"' \ | |
--run-command 'chmod 777 /etc/first-boot.sh' \ | |
--run-command 'systemctl enable first-boot.service' | |
} | |
# Funktion zum Erstellen und Konfigurieren der VM | |
create_and_configure_vm() { | |
echo -e "${WHITE}INFO: Erstelle und konfiguriere VM${NC}" | |
sudo qm destroy "${vmid}" | |
sudo qm create "${vmid}" --name "${templatename}" --memory 2048 --cores 2 --net0 virtio,bridge="${vmBridge}",rate=31.25,firewall=0 --ciuser root --ostype l26 --agent=1,freeze-fs-on-backup=0 --searchdomain 1.1.1.1 --nameserver 8.8.8.8 | |
sudo qm importdisk "${vmid}" "${imageName}" "${storage}" | |
sudo qm set "${vmid}" --scsihw virtio-scsi-pci --scsi0 "$storage":vm-"$vmid"-disk-0 | |
sudo qm set "${vmid}" --boot c --bootdisk scsi0 | |
sudo qm set "${vmid}" --ide2 "${storage}":cloudinit | |
sudo qm set "${vmid}" --serial0 socket | |
sudo qm set "${vmid}" --agent enabled=1 | |
} | |
set_template_and_cleanup(){ | |
sudo qm template "${vmid}" | |
rm -f "${imageName}" | |
} | |
set_template_and_cleanup_centos(){ | |
qm set "${vmid}" -cpu host | |
sudo qm template "${vmid}" | |
rm -f "${imageName}" | |
} | |
case $version in | |
debian11) | |
# Befehle für das Erstellen des Debian 11 Templates | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Erstelle Debian 11 Template '${templatename}' mit VMID ${vmid}" | |
echo "" | |
imageURL="https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-genericcloud-amd64.qcow2" | |
imageName="debian-11-genericcloud-amd64.qcow2" | |
start_template_creation | |
echo -e "${WHITE}INFO: Template wurde erfolgreich erstellt${NC}" | |
# Beenden | |
;; | |
debian12) | |
# Befehle für das Erstellen des Debian 12 Templates | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Erstelle Debian 12 Template '${templatename}' mit VMID ${vmid}${NC}" | |
echo "" | |
imageURL="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2" | |
imageName="debian-12-genericcloud-amd64.qcow2" | |
start_template_creation | |
echo -e "${WHITE}INFO: Template wurde erfolgreich erstellt${NC}" | |
# Beenden | |
;; | |
ubuntu20) | |
# Befehle für das Erstellen des Ubuntu 20 Templates | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Erstelle Ubuntu 20 Template '${templatename}' mit VMID ${vmid}${NC}" | |
echo "" | |
imageURL="https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img" | |
imageName="focal-server-cloudimg-amd64.img" | |
start_template_creation | |
echo -e "${WHITE}INFO: Template wurde erfolgreich erstellt${NC}" | |
# Beenden | |
;; | |
ubuntu22) | |
# Befehle für das Erstellen des Ubuntu 22 Templates | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Erstelle Ubuntu 22 Template '${templatename}' mit VMID ${vmid}${NC}" | |
echo "" | |
imageURL="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img" | |
imageName="jammy-server-cloudimg-amd64.img" | |
start_template_creation | |
echo -e "${WHITE}INFO: Template wurde erfolgreich erstellt${NC}" | |
# Beenden | |
;; | |
fedora37) | |
# Befehle für das Erstellen des Fedora 37 Templates | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Erstelle Fedora 37 Template '${templatename}' mit VMID ${vmid}${NC}" | |
echo "" | |
imageURL="https://ftp.fau.de/fedora/linux/releases/37/Cloud/x86_64/images/Fedora-Cloud-Base-37-1.7.x86_64.qcow2" | |
imageName="Fedora-Cloud-Base-37-1.7.x86_64.qcow2" | |
isFedora="true" | |
start_template_creation | |
echo -e "${WHITE}INFO: Template wurde erfolgreich erstellt${NC}" | |
# Beenden | |
;; | |
fedora38) | |
# Befehle für das Erstellen des Fedora 38 Templates | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Erstelle Fedora 38 Template '${templatename}' mit VMID ${vmid}${NC}" | |
echo "" | |
imageURL="https://ftp.fau.de/fedora/linux/releases/38/Cloud/x86_64/images/Fedora-Cloud-Base-38-1.6.x86_64.qcow2" | |
imageName="Fedora-Cloud-Base-38-1.6.x86_64.qcow2" | |
isFedora="true" | |
start_template_creation | |
echo -e "${WHITE}INFO: Template wurde erfolgreich erstellt${NC}" | |
# Beenden | |
;; | |
centos8) | |
# Befehle für das Erstellen des CentOS 8 Templates | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Erstelle CentOS 8 Template '${templatename}' mit VMID ${vmid}${NC}" | |
echo "" | |
imageURL="https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-GenericCloud-8-latest.x86_64.qcow2" | |
imageName="CentOS-Stream-GenericCloud-8-latest.x86_64.qcow2" | |
isCentOS="true" | |
start_template_creation | |
echo -e "${WHITE}INFO: Template wurde erfolgreich erstellt${NC}" | |
# Beenden | |
;; | |
centos9) | |
# Befehle für das Erstellen des CentOS 9 Templates | |
echo "" | |
echo -e "${YELLOW}ACHTUNG! Erstelle CentOS 9 Template '${templatename}' mit VMID ${vmid}${NC}" | |
echo "" | |
imageURL="https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2" | |
imageName="CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2" | |
isCentOS="true" | |
start_template_creation | |
echo -e "${WHITE}INFO: Template wurde erfolgreich erstellt${NC}" | |
# Beenden | |
;; | |
*) | |
echo "" | |
echo -e "${RED}FEHLER! Ungültige Version: $version" | |
echo -e "${YELLOW}ACHTUNG! Erlaubte Versionen sind: ${availableVersions}" | |
echo -e "${NC}" | |
exit 1 | |
;; | |
esac |
This file contains hidden or 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
[Unit] | |
Description=First Boot Script | |
After=network.target | |
[Service] | |
Type=oneshot | |
ExecStart=/etc/first-boot.sh | |
RemainAfterExit=true | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or 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 | |
# Ändern der SSH-Konfiguration | |
sed -i "/^#\\?PasswordAuthentication/c\PasswordAuthentication yes" /etc/ssh/sshd_config | |
sed -i "/PasswordAuthentication no/PasswordAuthentication yes" /etc/ssh/sshd_config | |
sed -i "/^#\\?PermitRootLogin/c\PermitRootLogin yes" /etc/ssh/sshd_config | |
systemctl restart sshd | |
# Setzen des Nameservers | |
rm /etc/resolv.conf | |
touch /etc/resolv.conf | |
echo "nameserver 8.8.8.8" > /etc/resolv.conf | |
systemctl restart systemd-resolved | |
systemctl disable first-boot.service | |
rm /etc/systemd/system/first-boot.service | |
systemctl daemon-reload | |
# Löschen des Skripts nach Ausführung | |
rm -- "$0" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment