Update create.sh

This commit is contained in:
Blackwhitebear8 2024-12-09 14:24:52 +01:00
parent 681bc6467a
commit a9fc7fd64e

138
create.sh
View file

@ -1,7 +1,7 @@
#!/bin/bash
# Vereiste packages installeren
sudo apt install libguestfs-tools -y
sudo apt install libguestfs-tools jq -y
# Functie om het eerstvolgende vrije VMID te bepalen
find_next_free_vmid() {
@ -28,22 +28,24 @@ VMID=$(find_next_free_vmid) # VM ID
VMNAME="test" # Naam van de VM
VMUSER="test" # Gebruikersnaam
VMPASSWORD="test" # Wachtwoord voor de gebruiker (leeg voor alleen SSH-sleutels)
SSH_KEYS_PATH="" # Pad naar SSH-sleutelbestand (leeg als niet nodig)
SSH_KEYS_PATH1=""
DISK_SIZE="20" # Disk grootte in GiB
BRIDGE="vmbr0"
MEMORY="4096"
CORES="4"
STORAGE="btrfs" # Opslaglocatie
OSKEUZE="Debian12" # Naam en versie van het besturingssysteem, bijv. "Debian 12"
OSKEUZE="5" # Naam en versie van het besturingssysteem, bijv. "Debian 12"
TEMPLATE="n" # Of de VM als template moet worden ingesteld (j/n)
SSH_KEY_TEXT=""
# Parseer de argumenten met getopts
while getopts "n:u:p:k:d:b:m:c:s:o:t:" opt; do
case ${opt} in
n) VMNAME="${OPTARG}" ;;
u) VMUSER="${OPTARG}" ;;
p) VMPASSWORD="${OPTARG}" ;;
k) SSH_KEYS_PATH="${OPTARG}" ;;
k) SSH_KEYS_PATH1="${OPTARG}" ;;
d) DISK_SIZE="${OPTARG}" ;;
b) BRIDGE="${OPTARG}" ;;
m) MEMORY="${OPTARG}" ;;
@ -59,7 +61,7 @@ done
echo "VM Name: $VMNAME"
echo "VM User: $VMUSER"
echo "VM Password: $VMPASSWORD"
echo "SSH Key Path: $SSH_KEYS_PATH"
echo "SSH Key Path: $SSH_KEYS_PATH1"
echo "Disk Size: $DISK_SIZE"
echo "Bridge: $BRIDGE"
echo "Memory: $MEMORY"
@ -76,23 +78,23 @@ fi
# OS-specificatie en image-toewijzing
case "$OSKEUZE" in
"Debian11")
"1")
IMAGE_URL="https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2"
IMAGE_NAME="debian-11-generic-amd64.qcow2"
;;
"Debian12")
"2")
IMAGE_URL="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
IMAGE_NAME="debian-12-generic-amd64.qcow2"
;;
"Ubuntu22.04")
"3")
IMAGE_URL="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
IMAGE_NAME="jammy-server-cloudimg-amd64.img"
;;
"Ubuntu24.04")
"4")
IMAGE_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
IMAGE_NAME="noble-server-cloudimg-amd64.img"
;;
"Almalinux9")
"5")
IMAGE_URL="https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2"
IMAGE_NAME="AlmaLinux-9-GenericCloud-latest.x86_64.qcow2"
;;
@ -103,24 +105,63 @@ case "$OSKEUZE" in
esac
# Download de OS image
mkdir -p /tmp
wget -O /tmp/"$IMAGE_NAME" "$IMAGE_URL"
if [ -n "$SSH_KEYS_PATH1" ] && [ -f "$SSH_KEYS_PATH1" ]; then
touch /tmp/temporary_ssh_key.pub
echo "$SSH_KEYS_PATH1" > /tmp/temporary_ssh_key.pub
# Sla de naam van het tijdelijke bestand op in een variabele
SSH_KEYS_PATH="/tmp/temporary_ssh_key.pub"
else
SSH_KEYS_PATH=""
fi
# Controleer de disk type
DISKTYPE=$(pvesm status | awk -v storage="$STORAGE" 'NR>1 && $1 == storage {print $2}')
# Pas de image aan
virt-customize --install qemu-guest-agent,htop,curl -a /tmp/"$IMAGE_NAME"
case $OSKEUZE in
1|2|3|4) # Debian en Ubuntu
virt-customize --install qemu-guest-agent,htop,curl,avahi-daemon,console-setup,cron,cifs-utils,mtr,bash-completion,bind9-utils,lsof,mc,mlocate,screen,sysstat,tmux,wget,zabbix-agent -a /tmp/"$IMAGE_NAME"
virt-customize --run-command "systemctl enable qemu-guest-agent" -a /tmp/"$IMAGE_NAME"
# SSH-sleutels configureren
if [[ -n "$SSH_KEYS_PATH" && -f "$SSH_KEYS_PATH" ]]; then
virt-customize -a /tmp/"$IMAGE_NAME" --ssh-inject "$VMUSER:file:$SSH_KEYS_PATH"
elif [[ -n "$VMPASSWORD" ]]; then
case $SSH_KEYS_PATH in
'') # leeg
virt-customize -a /tmp/"$IMAGE_NAME" \
--run-command 'sed -i "s/^#PasswordAuthentication yes/PasswordAuthentication yes/" /etc/ssh/sshd_config' \
--run-command 'sed -i "s/^PasswordAuthentication no/PasswordAuthentication yes/" /etc/ssh/sshd_config' \
--run-command 'systemctl restart sshd'
fi
;;
/tmp/temporary_ssh_key.pub) # iets
;;
esac
virt-customize -a /tmp/"$IMAGE_NAME" --truncate /etc/machine-id --truncate /var/lib/dbus/machine-id
;;
5) # RHEL
virt-customize --install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -a /tmp/"$IMAGE_NAME"
virt-customize --install qemu-guest-agent,htop,curl,cifs-utils,firewalld,mtr,bash-completion,bind-utils,lsof,mc,mlocate,perl,perl-Date-Calc,perl-Data-Dumper,perl-Getopt-Long,perl-libs,screen,sysstat,tmux,wget,zabbix-agent -a /tmp/"$IMAGE_NAME"
virt-customize --selinux-relabel -a /tmp/"$IMAGE_NAME"
virt-customize --run-command "systemctl enable firewalld" -a /tmp/"$IMAGE_NAME"
virt-customize --run-command "systemctl enable qemu-guest-agent" -a /tmp/"$IMAGE_NAME"
case $SSH_KEYS_PATH in
'') # leeg
virt-customize -a /tmp/"$IMAGE_NAME" \
--run-command 'sed -i "s/^#PasswordAuthentication yes/PasswordAuthentication yes/" /etc/ssh/sshd_config' \
--run-command 'sed -i "s/^PasswordAuthentication no/PasswordAuthentication yes/" /etc/ssh/sshd_config' \
--run-command 'systemctl restart sshd'
;;
/tmp/temporary_ssh_key.pub) # iets
;;
esac
;;
*) # Anders
;;
esac
virt-customize -a /tmp/"$IMAGE_NAME" --truncate /etc/machine-id --truncate /var/lib/dbus/machine-id
@ -131,11 +172,14 @@ qm create $VMID --name $VMNAME --memory $MEMORY --cores $CORES --net0 virtio,bri
qm importdisk $VMID /tmp/"$IMAGE_NAME" $STORAGE
# Configureer de VM disks
if [[ $DISKTYPE == "dir" || $DISKTYPE == "btrfs" ]]; then
case $DISKTYPE in
dir|btrfs) #file en btrfs storage
qm set $VMID --scsihw virtio-scsi-single --scsi0 $STORAGE:$VMID/vm-$VMID-disk-0.raw,discard=on,iothread=1,ssd=1,format=raw
else
;;
lvm|lvmthin|zfspool) #lvm en zfs storage
qm set $VMID --scsihw virtio-scsi-single --scsi0 $STORAGE:vm-$VMID-disk-0,discard=on,iothread=1,ssd=1,format=raw
fi
;;
esac
qm set $VMID --ide0 $STORAGE:cloudinit,format=raw
qm set $VMID --ide2 none,media=cdrom
@ -147,6 +191,8 @@ qm set $VMID --serial0 socket
# Stel netwerkinstellingen en CPU-configuraties in
qm set $VMID --ipconfig0 ip=dhcp,ip6=auto
qm set $VMID --cpu cputype=host,flags="+md-clear;+spec-ctrl;+aes"
qm set $VMID --numa 1
qm set $VMID --sockets 1
# Configureer Cloud-Init
qm set $VMID --ciuser $VMUSER
@ -155,6 +201,14 @@ qm set $VMID --ciupgrade 1
qm set $VMID --onboot 1
[[ -n "$SSH_KEYS_PATH" && -f "$SSH_KEYS_PATH" ]] && qm set $VMID --sshkeys "$SSH_KEYS_PATH"
#case $SSH_KEYS_PATH in
# '') # leeg
# ;;
# /tmp/temporary_ssh_key.pub) # iets
# qm set $VMID --sshkeys /tmp/temporary_ssh_key.pub
# ;;
#esac
qm set $VMID --efidisk0 $STORAGE:0,format=raw,pre-enrolled-keys=1
# Stel de bootvolgorde en disk grootte in
@ -168,32 +222,52 @@ fi
# starten vm
rm -f /tmp/"$IMAGE_NAME"
rm -f /tmp/temporary_ssh_key.pub
qm start $VMID
# ip ophalen
# Maximum aantal pogingen (1 minuut / 5 seconden = 12 pogingen)
MAX_ATTEMPTS=12
MAX_ATTEMPTS=24 # Aantal pogingen voordat de foutmelding wordt weergegeven
ATTEMPT=0
ETH0_STATS="" # Zorg ervoor dat ETH0_STATS wordt geleegd bij elke iteratie
# Probeer om de IP-informatie op te halen
while [ -z "$IP_INFO" ] && [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
echo "Trying to retrieve IP information... Attempt $((ATTEMPT + 1)) of $MAX_ATTEMPTS"
# Vul hier je commando in om de IP-informatie op te halen (bijvoorbeeld door het gebruik van QEMU-guest-agent)
IP_INFO=$(qm agent $VMID network-get-interfaces)
# Probeer om de IP-informatie en statistieken op te halen
while [ -z "$ETH0_STATS" ] && [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
echo "Trying to retrieve IP and stats information... Attempt $((ATTEMPT + 1)) of $MAX_ATTEMPTS"
# Wacht 5 seconden voordat je het opnieuw probeert
sleep 5
# Haal de netwerkinformatie op
IP_INFO=$(qm agent $VMID network-get-interfaces 2>/dev/null)
# Controleer of de QEMU guest agent beschikbaar is
if [[ "$IP_INFO" == *"QEMU guest agent is not running"* ]]; then
echo "QEMU guest agent is not running. Retrying..."
elif [[ -z "$IP_INFO" ]]; then
echo "No network information received. Retrying..."
else
# Controleer specifiek of eth0 bestaat en statistieken bevat
ETH0_STATS=$(echo "$IP_INFO" | jq -r '.[] | select(.name == "eth0" and .statistics != null)')
if [ -n "$ETH0_STATS" ]; then
echo "Valid IP and statistics found for eth0!"
break # Als eth0 statistieken heeft, stop de poging
else
echo "No valid statistics found for eth0. Retrying..."
fi
fi
# Wacht 1 seconde voordat je opnieuw probeert
sleep 1
((ATTEMPT++))
done
# Als na 12 pogingen de IP-informatie nog steeds niet is opgehaald, geef dan een foutmelding
if [ -z "$IP_INFO" ]; then
echo "Unable to retrieve IP. Ensure the QEMU Guest Agent is running inside the VM."
# Controleer of we een geldig resultaat hebben gekregen
if [ -z "$ETH0_STATS" ]; then
echo "Unable to retrieve IP or statistics for eth0 after $MAX_ATTEMPTS attempts."
exit 1
else
# Parse and display IPv4 and IPv6 addresses for the eth0 interface
echo "$IP_INFO" | jq -r '.[] | select(.name == "eth0" and .["ip-addresses"] != null) | .["ip-addresses"][] | [.["ip-address-type"], .["ip-address"]] | @tsv' | \
# Parse en toon IPv4 en IPv6 adressen voor de eth0 interface met statistieken
echo "$ETH0_STATS" | jq -r '.["ip-addresses"][] | [.["ip-address-type"], .["ip-address"]] | @tsv' | \
while IFS=$'\t' read -r ip_type ip; do
echo "eth0 - $ip_type: $ip"
done