Files
loc_az_hci/scripts/vm-management/configure/fix-vm-config.sh
defiQUG c39465c2bd
Some checks failed
Test / test (push) Has been cancelled
Initial commit: loc_az_hci (smom-dbis-138 excluded via .gitignore)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 09:04:46 -08:00

191 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
source ~/.bashrc
# Fix VM Configuration Warnings
# Corrects network, disk, and ISO configurations
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
# Load environment variables
if [ -f .env ]; then
set -a
source <(grep -v '^#' .env | grep -v '^$' | sed 's/#.*$//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | grep '=')
set +a
fi
PVE_USERNAME="${PVE_USERNAME:-root@pam}"
PVE_PASSWORD="${PVE_ROOT_PASS:-}"
PROXMOX_HOST="${1:-192.168.1.206}"
PROXMOX_URL="https://${PROXMOX_HOST}:8006"
PROXMOX_NODE="${2:-pve}"
ISO_FILE="${ISO_FILE:-ubuntu-24.04.3-live-server-amd64.iso}"
# Get authentication ticket
get_ticket() {
local response=$(curl -k -s -d "username=$PVE_USERNAME&password=$PVE_PASSWORD" \
"$PROXMOX_URL/api2/json/access/ticket")
local ticket=$(echo "$response" | grep -o '"ticket":"[^"]*' | cut -d'"' -f4)
local csrf=$(echo "$response" | grep -o '"CSRFPreventionToken":"[^"]*' | cut -d'"' -f4)
if [ -z "$ticket" ] || [ -z "$csrf" ]; then
log_error "Failed to authenticate with Proxmox"
return 1
fi
echo "$ticket|$csrf"
}
# Fix VM configuration
fix_vm_config() {
local auth=$1
local vmid=$2
local name=$3
local disk_size=$4
local ticket=$(echo "$auth" | cut -d'|' -f1)
local csrf=$(echo "$auth" | cut -d'|' -f2)
log_step "Fixing configuration for $name (ID: $vmid)..."
# Stop VM if running
log_info "Stopping VM $vmid..."
curl -k -s -X POST \
-H "Cookie: PVEAuthCookie=$ticket" \
-H "CSRFPreventionToken: $csrf" \
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/status/stop" > /dev/null 2>&1
sleep 2
# Fix network - use proper format
log_info "Configuring network..."
local net_response=$(curl -k -s -X PUT \
-H "Cookie: PVEAuthCookie=$ticket" \
-H "CSRFPreventionToken: $csrf" \
-d "net0=model=virtio,bridge=vmbr0" \
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
if echo "$net_response" | grep -q '"errors"'; then
log_warn "Network config may have issues, but continuing..."
else
log_info "✓ Network configured"
fi
# Fix disk - ensure proper format
log_info "Configuring disk..."
local disk_response=$(curl -k -s -X PUT \
-H "Cookie: PVEAuthCookie=$ticket" \
-H "CSRFPreventionToken: $csrf" \
-d "scsi0=local-lvm:${disk_size},format=raw" \
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
if echo "$disk_response" | grep -q '"errors"'; then
# Try with local storage instead
curl -k -s -X PUT \
-H "Cookie: PVEAuthCookie=$ticket" \
-H "CSRFPreventionToken: $csrf" \
-d "scsi0=local:${disk_size},format=raw" \
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1
log_info "✓ Disk configured (using local storage)"
else
log_info "✓ Disk configured"
fi
# Fix ISO - ensure proper format
log_info "Configuring ISO..."
local iso_volid="local:iso/${ISO_FILE}"
local iso_response=$(curl -k -s -X PUT \
-H "Cookie: PVEAuthCookie=$ticket" \
-H "CSRFPreventionToken: $csrf" \
-d "ide2=${iso_volid},media=cdrom" \
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" 2>&1)
if echo "$iso_response" | grep -q '"errors"'; then
log_warn "ISO config may have issues"
else
log_info "✓ ISO configured"
fi
# Set boot order
log_info "Setting boot order..."
curl -k -s -X PUT \
-H "Cookie: PVEAuthCookie=$ticket" \
-H "CSRFPreventionToken: $csrf" \
-d "boot=order=ide2;scsi0" \
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/config" > /dev/null 2>&1
log_info "✓ Boot order configured"
# Start VM
log_info "Starting VM..."
curl -k -s -X POST \
-H "Cookie: PVEAuthCookie=$ticket" \
-H "CSRFPreventionToken: $csrf" \
"$PROXMOX_URL/api2/json/nodes/$PROXMOX_NODE/qemu/$vmid/status/start" > /dev/null 2>&1
log_info "✓ VM $vmid configuration fixed and restarted"
echo ""
}
# VM configurations
declare -A VMS=(
["100"]="cloudflare-tunnel:40"
["101"]="k3s-master:80"
["102"]="git-server:100"
["103"]="observability:200"
)
main() {
echo "========================================="
echo "Fix VM Configuration Warnings"
echo "========================================="
echo ""
if [ -z "$PVE_PASSWORD" ]; then
log_error "PVE_ROOT_PASS not set in .env"
exit 1
fi
# Authenticate
auth=$(get_ticket)
if [ $? -ne 0 ]; then
exit 1
fi
log_info "Fixing configurations for all VMs..."
echo ""
for vmid in "${!VMS[@]}"; do
IFS=':' read -r name disk_size <<< "${VMS[$vmid]}"
fix_vm_config "$auth" "$vmid" "$name" "$disk_size"
done
log_info "========================================="
log_info "Configuration Fix Complete"
log_info "========================================="
}
main "$@"