Upload files to "/"

This commit is contained in:
Blackwhitebear8 2024-12-06 16:57:11 +01:00
commit 1a51466ec1
2 changed files with 308 additions and 0 deletions

203
create.sh Normal file
View file

@ -0,0 +1,203 @@
#!/bin/bash
# Vereiste packages installeren
sudo apt install libguestfs-tools -y
# Functie om het eerstvolgende vrije VMID te bepalen
find_next_free_vmid() {
# Haal de lijst van bestaande VMIDs op uit het JSON-bestand
local existing_ids=$(jq -r '.ids | keys | .[]' /etc/pve/.vmlist | sort -n)
local vmid=100 # Begin bij een standaard VMID, bijvoorbeeld 100
for id in $existing_ids; do
if [[ $vmid -lt $id ]]; then
# Als de huidige $vmid kleiner is dan de eerstvolgende in de lijst, is deze vrij
echo $vmid
return
fi
# Anders verhogen we $vmid om naar het volgende te kijken
((vmid++))
done
# Als we geen vrije ID in de lijst vinden, retourneer de volgende beschikbare ID
echo $vmid
}
# Configuratievariabelen
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)
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"
TEMPLATE="n" # Of de VM als template moet worden ingesteld (j/n)
# 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}" ;;
d) DISK_SIZE="${OPTARG}" ;;
b) BRIDGE="${OPTARG}" ;;
m) MEMORY="${OPTARG}" ;;
c) CORES="${OPTARG}" ;;
s) STORAGE="${OPTARG}" ;;
o) OSKEUZE="${OPTARG}" ;;
t) TEMPLATE="${OPTARG}" ;;
\?) echo "Usage: $0 [-n vmname] [-u username] [-p password] [-k ssh_key] [-d disk_size] [-b bridge] [-m memory] [-c cores] [-s storage] [-o oskeuze] [-t template]" ;;
esac
done
# Echo de variabelen voor bevestiging
echo "VM Name: $VMNAME"
echo "VM User: $VMUSER"
echo "VM Password: $VMPASSWORD"
echo "SSH Key Path: $SSH_KEYS_PATH"
echo "Disk Size: $DISK_SIZE"
echo "Bridge: $BRIDGE"
echo "Memory: $MEMORY"
echo "Cores: $CORES"
echo "Storage: $STORAGE"
echo "OS Choice: $OSKEUZE"
echo "Template: $TEMPLATE"
# Controleer verplichte variabelen
if [[ -z "$VMID" || -z "$VMNAME" || -z "$VMUSER" || -z "$DISK_SIZE" || -z "$STORAGE" || -z "$OSKEUZE" ]]; then
echo "Error: Een of meer verplichte variabelen zijn niet ingesteld. Vul alle vereiste variabelen in."
exit 1
fi
# OS-specificatie en image-toewijzing
case "$OSKEUZE" in
"Debian11")
IMAGE_URL="https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2"
IMAGE_NAME="debian-11-generic-amd64.qcow2"
;;
"Debian12")
IMAGE_URL="https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2"
IMAGE_NAME="debian-12-generic-amd64.qcow2"
;;
"Ubuntu22.04")
IMAGE_URL="https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
IMAGE_NAME="jammy-server-cloudimg-amd64.img"
;;
"Ubuntu24.04")
IMAGE_URL="https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
IMAGE_NAME="noble-server-cloudimg-amd64.img"
;;
"Almalinux9")
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"
;;
*)
echo "Error: Ongeldige OS-keuze. Controleer de waarde van OSKEUZE."
exit 1
;;
esac
# Download de OS image
wget -O /tmp/"$IMAGE_NAME" "$IMAGE_URL"
# 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"
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
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
virt-customize -a /tmp/"$IMAGE_NAME" --truncate /etc/machine-id --truncate /var/lib/dbus/machine-id
# Maak een nieuwe VM
qm create $VMID --name $VMNAME --memory $MEMORY --cores $CORES --net0 virtio,bridge=$BRIDGE,firewall=1 --agent 1
# Importeer de aangepaste image
qm importdisk $VMID /tmp/"$IMAGE_NAME" $STORAGE
# Configureer de VM disks
if [[ $DISKTYPE == "dir" || $DISKTYPE == "btrfs" ]]; then
qm set $VMID --scsihw virtio-scsi-single --scsi0 $STORAGE:$VMID/vm-$VMID-disk-0.raw,discard=on,iothread=1,ssd=1,format=raw
else
qm set $VMID --scsihw virtio-scsi-single --scsi0 $STORAGE:vm-$VMID-disk-0,discard=on,iothread=1,ssd=1,format=raw
fi
qm set $VMID --ide0 $STORAGE:cloudinit,format=raw
qm set $VMID --ide2 none,media=cdrom
qm set $VMID --bios ovmf
qm set $VMID --machine q35
qm set $VMID --tablet 0
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"
# Configureer Cloud-Init
qm set $VMID --ciuser $VMUSER
qm set $VMID --cipassword $VMPASSWORD
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"
qm set $VMID --efidisk0 $STORAGE:0,format=raw,pre-enrolled-keys=1
# Stel de bootvolgorde en disk grootte in
qm set $VMID --boot order="scsi0;ide2;net0"
qm resize $VMID scsi0 "${DISK_SIZE}G"
# Zet de VM om in een template als geselecteerd
if [[ "$TEMPLATE" == "j" ]]; then
qm template $VMID
fi
# starten vm
rm -f /tmp/"$IMAGE_NAME"
qm start $VMID
# ip ophalen
# Maximum aantal pogingen (1 minuut / 5 seconden = 12 pogingen)
MAX_ATTEMPTS=12
ATTEMPT=0
# 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)
# Wacht 5 seconden voordat je het opnieuw probeert
sleep 5
((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."
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' | \
while IFS=$'\t' read -r ip_type ip; do
echo "eth0 - $ip_type: $ip"
done
fi
# Opruimen
echo "VM $VMNAME met ID $VMID succesvol aangemaakt."