diff --git a/.gitignore b/.gitignore index e69de29..8e4485e 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,15 @@ +# Byte-compiled / temp files +*.pyc +__pycache__/ + +# Editor swap files +*~ +.*.swp +.*.swo + +# Logs +*.log + +# OS files +.DS_Store +Thumbs.db diff --git a/LICENSE b/LICENSE index 1201355..ef699f2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ -MIT License +# MIT License -Copyright (c) 2025 ==TIM.©.B == +Copyright (c) 2025 TimInTech Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 028d6e7..ccebf76 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,78 @@ -# Proxmox VM/CT Management Tool +# Proxmox VM/CT Manager – Version 2.7.2 (updated 2025-09-07) -![Proxmox Console Screenshot](docs/screenshots/proxmox-console.png) +

Terminal tool to manage Proxmox VMs and containers from the host shell

-A lightweight terminal user interface (TUI) tool to manage Proxmox VMs and containers via `qm` and `pct`. +

+ GitHub Stars + GitHub Forks + License + Buy Me A Coffee +

--- -## ✨ Features - -- Clean overview with colored status icons (🟢 running, 🔴 stopped, 🟠 paused) -- Start / Stop / Restart / Status management -- Console access (`pct enter`, `qm terminal`, fallback `qm monitor`) -- Snapshot management: list, create, rollback, delete -- SPICE integration: show connection info, generate `.vv` viewer files, enable SPICE -- Built-in permission checks and root validation -- Works directly on a Proxmox host without extra dependencies +## Quick Links +- Main script: [`proxmox-manager.sh`](proxmox-manager.sh) +- Optional helper: [`install_dependencies.sh`](install_dependencies.sh) +- Project overview: [Quickstart](#quickstart) · [Usage](#usage) · [CLI Options](#cli-options) · [Troubleshooting](#troubleshooting) +- Audit artefacts: [`.audit/`](.audit/) +- Issues & feedback: [Create issue](../../issues) --- -## ⚡ Quick Start +## ✅ Requirements +- Proxmox VE 7.4, 8.x, or 9.x host +- Run directly on the Proxmox node as `root` +- `qm` and/or `pct` CLI tools available on the host +- Optional helpers: `remote-viewer` for SPICE, `jq` for utilities, `shellcheck` for linting -Run as **root** on a Proxmox host: +--- + +## Introduction +This repository contains a lightweight terminal UI script that lists and manages both VMs and LXC containers on a Proxmox host. It provides status-aware actions, console access, snapshot helpers, and SPICE integration without depending on external services. + +> The script targets interactive use on the Proxmox host itself. + +--- + +## Technologies & Dependencies +![Proxmox VE](https://img.shields.io/badge/Proxmox-VE-EE7F2D?logo=proxmox&logoColor=white&style=flat) +![Debian](https://img.shields.io/badge/Debian-11--13-A81D33?logo=debian&logoColor=white&style=flat) +![Ubuntu](https://img.shields.io/badge/Ubuntu-24.04-E95420?logo=ubuntu&logoColor=white&style=flat) +![Bash](https://img.shields.io/badge/Bash-✔-4EAA25?logo=gnubash&logoColor=white&style=flat) +![systemd](https://img.shields.io/badge/systemd-✔-FFDD00?logo=linux&logoColor=black&style=flat) +![SPICE](https://img.shields.io/badge/SPICE-✔-CC0000?logo=redhat&logoColor=white&style=flat) +![virt-viewer](https://img.shields.io/badge/Virt--Viewer-✔-555555?style=flat) +![jq](https://img.shields.io/badge/jq-✔-3E6E93?style=flat) +![ShellCheck](https://img.shields.io/badge/ShellCheck-✔-4B9CD3?style=flat) + +--- + +## 📊 Status +Stable for day-to-day VM and LXC management on the host shell. + +--- + +## Quickstart + +### Installation ```bash apt update && apt install -y git cd /root -git clone https://github.com/TimInTech/proxmox-manager.git -cd proxmox-manager +git clone https://github.com/TimInTech/timintech-proxmox-manager.git +cd timintech-proxmox-manager chmod +x proxmox-manager.sh install_dependencies.sh -./install_dependencies.sh # optional: helper tools (e.g. shellcheck, remote-viewer) -./proxmox-manager.sh -```` +./install_dependencies.sh # optional: remote-viewer, jq, shellcheck +``` -Optional: make it globally available: +### Run + +```bash +./proxmox-manager.sh +``` + +Optional system-wide install: ```bash cp proxmox-manager.sh /usr/local/sbin/proxmox-manager @@ -42,39 +82,52 @@ proxmox-manager --- -## 📦 Requirements - -* Run as `root` on a Proxmox host -* Proxmox CLI tools `qm` and/or `pct` must be available -* For SPICE support: `remote-viewer` (Virt-Viewer) is recommended +## Features +- Unified VM and CT overview with status symbols: 🟢 running · 🔴 stopped · 🟠 paused · 🟡 unknown +- Actions: start, stop, restart, and status for each ID +- Console helpers: `pct enter`, `qm terminal`, or fallback `qm monitor` +- Snapshot helpers: list, create, rollback, delete snapshots +- SPICE tools: connection details, `.vv` file generation, optional SPICE enablement +- Built-in root check, locale normalization, and resilient ID parsing --- -## 🛠️ How It Works - -* Detects VMs and containers via `qm list` and `pct list` -* Resolves container names reliably (handles missing/empty fields) -* Status detection: `running`, `stopped`, `paused`, with fallback handling -* SPICE: automatically provides connection URI (`spice://host:port`) and writes `.vv` files under `/tmp/` +## Usage +- Launch the script as `root` directly on the Proxmox host. +- Select a VMID/CTID to access action menus for lifecycle operations, console access, and SPICE utilities. +- Use `r` to refresh the overview and `q` to exit at any time. +- To avoid screen clearing in constrained terminals, run with `--no-clear`. --- -## 📌 Roadmap - -* Optional JSON/YAML output for automation -* Batch actions (start/stop multiple VMs/CTs) -* Improved error logging -* Packaging for Debian/Proxmox hosts +## CLI Options +- `--list`: Print a single, plain-text overview of every VM/CT with the emoji legend above the table. +- `--json`: Emit a machine-readable JSON array (`id`, `type`, `status`, `symbol`, `name`) for automation. +- `--no-clear`: Skip terminal clears even in interactive mode; useful for logs or screen sessions. +- `--once`: Run the interactive overview exactly once and exit. +- `--help`: Show usage information and exit. --- -## 🤝 Contributing - -Contributions are welcome! -Open an [issue](https://github.com/TimInTech/proxmox-manager/issues) for bug reports or feature requests, or submit a pull request. +## SPICE Notes +- `remote-viewer` (virt-viewer) offers the best experience for `.vv` files. +- If a VM lacks a SPICE device, the helper can add one; restart the VM afterwards. +- Ports fall back to detected defaults when Proxmox has not yet assigned one. --- -## 📄 License +## Troubleshooting +- **No entries listed:** Ensure execution on the Proxmox host as `root` with `qm`/`pct` available. +- **Console unavailable:** `qm terminal` requires a serial console; use the fallback `qm monitor`. +- **Missing SPICE port:** Configure SPICE in the VM or enable it via the built-in helper. +- **JSON tooling missing:** The JSON output is generated without external dependencies; `jq` is optional for consuming it. -MIT License – see [LICENSE](LICENSE). +--- + +## Contributing +Pull requests and issues are welcome. Please run `shellcheck` locally to keep lint clean. + +--- + +## License +[MIT](LICENSE) diff --git a/install_dependencies.sh b/install_dependencies.sh index 74d2a29..c7b1741 100755 --- a/install_dependencies.sh +++ b/install_dependencies.sh @@ -1,4 +1,7 @@ -#!/usr/bin/env bash +git rev-parse --abbrev-ref HEAD # aktueller Branch +git log --oneline --decorate -n 10 # letzte 10 lokale commits +git fetch origin +git log --oneline origin/$(git rev-parse --abbrev-ref HEAD) -n 10 # letzte 10 remote commits auf dem gleichen branch#!/usr/bin/env bash # install_dependencies.sh - install optional helpers for proxmox-manager # Idempotent install script for Debian/Proxmox nodes @@ -20,7 +23,7 @@ for pkg in "${PKGS[@]}"; do fi done -if (( ${#MISSING[@]} )); then +if ((${#MISSING[@]})); then log "Updating package lists..." if ! apt-get update; then err "apt-get update failed" diff --git a/proxmox-manager.sh b/proxmox-manager.sh index 5e7d472..a5f3b1b 100755 --- a/proxmox-manager.sh +++ b/proxmox-manager.sh @@ -9,26 +9,125 @@ set -Eeuo pipefail IFS=$'\n\t' export LC_ALL=C +# ===== Defaults ===== +CLEAR_SCREEN=1 +MODE="interactive" +RUN_ONCE=0 +LIST_FLAG=0 +JSON_FLAG=0 + # ===== Colors (nur TTY) ===== if [[ -t 1 && -z "${NO_COLOR:-}" ]]; then - BOLD=$'\e[1m'; RED=$'\e[31m'; GREEN=$'\e[32m'; YELLOW=$'\e[33m'; BLUE=$'\e[34m'; CYAN=$'\e[36m'; NC=$'\e[0m' + BOLD=$'\e[1m' + RED=$'\e[31m' + GREEN=$'\e[32m' + BLUE=$'\e[34m' + CYAN=$'\e[36m' + NC=$'\e[0m' else - BOLD=''; RED=''; GREEN=''; YELLOW=''; BLUE=''; CYAN=''; NC='' + BOLD='' + RED='' + GREEN='' + BLUE='' + CYAN='' + NC='' fi trap 'printf "\n%s\n" "Beendet."; exit 0' INT TERM have() { command -v "$1" >/dev/null 2>&1; } -err() { printf '%b\n' "${RED}Error:${NC} $*" >&2; } -ok() { printf '%b\n' "${GREEN}$*${NC}"; } +err() { printf '%b\n' "${RED}Error:${NC} $*" >&2; } +ok() { printf '%b\n' "${GREEN}$*${NC}"; } note() { printf '%b\n' "${CYAN}$*${NC}"; } # ===== Helpers ===== -read_line() { local -n __o=$1; if ! IFS= read -r __o; then __o=''; fi; } -trim() { local v="$*"; v="${v#"${v%%[![:space:]]*}"}"; v="${v%"${v##*[![:space:]]}"}"; printf '%s' "$v"; } +read_line() { + local -n __o=$1 + if ! IFS= read -r __o; then __o=''; fi +} +trim() { + local v="$*" + v="${v#"${v%%[![:space:]]*}"}" + v="${v%"${v##*[![:space:]]}"}" + printf '%s' "$v" +} -require_root() { (( EUID == 0 )) || { err "Als root ausführen."; exit 1; } } -require_tools() { { have qm || have pct; } || { err "qm/pct fehlen. Auf Proxmox-Host starten."; exit 1; } } +require_root() { ((EUID == 0)) || { + err "Als root ausführen." + exit 1 +}; } +require_tools() { { have qm || have pct; } || { + err "qm/pct fehlen. Auf Proxmox-Host starten." + exit 1 +}; } + +usage() { + cat <<'EOF' +Usage: proxmox-manager.sh [options] + +Options: + --list Print a plain-text overview of all VMs/CTs (no TUI) + --json Print machine-readable JSON with VM/CT information + --no-clear Do not clear the screen in interactive mode + --once Run a single interactive refresh (useful for TTY recording) + -h, --help Show this help +EOF +} + +parse_args() { + while [[ $# -gt 0 ]]; do + case "$1" in + --list) + MODE="list" + LIST_FLAG=1 + ;; + --json) + MODE="json" + JSON_FLAG=1 + ;; + --no-clear) + CLEAR_SCREEN=0 + ;; + --once) + RUN_ONCE=1 + ;; + -h | --help) + usage + exit 0 + ;; + --) + shift + break + ;; + -*) + err "Unbekannte Option: $1" + usage + exit 1 + ;; + *) + err "Unerwartetes Argument: $1" + usage + exit 1 + ;; + esac + shift + done + if ((LIST_FLAG == 1 && JSON_FLAG == 1)); then + err "Optionen --list und --json sind nicht kombinierbar." + exit 1 + fi +} + +# ===== JSON helpers ===== +json_escape() { + local s="$1" + s="${s//\\/\\\\}" + s="${s//\"/\\\"}" + s="${s//$'\n'/\\n}" + s="${s//$'\r'/\\r}" + s="${s//$'\t'/\\t}" + printf '%s' "$s" +} # Nur Zeilen mit führender numerischer ID zulassen is_data_line() { @@ -39,23 +138,30 @@ is_data_line() { # ===== Typ/Status ===== type_of_id() { local id="$1" - if have pct && pct list 2>/dev/null | awk 'NR>1 {print $1}' | grep -qx -- "$id"; then printf 'CT'; return; fi - if have qm && qm list 2>/dev/null | awk 'NR>1 {print $1}' | grep -qx -- "$id"; then printf 'VM'; return; fi + if have pct && pct list 2>/dev/null | awk 'NR>1 {print $1}' | grep -qx -- "$id"; then + printf 'CT' + return + fi + if have qm && qm list 2>/dev/null | awk 'NR>1 {print $1}' | grep -qx -- "$id"; then + printf 'VM' + return + fi printf '' } status_of() { - local id="$1" t="${2:-}"; [[ -z "$t" ]] && t="$(type_of_id "$id")" + local id="$1" t="${2:-}" + [[ -z "$t" ]] && t="$(type_of_id "$id")" case "$t" in CT) pct status "$id" 2>/dev/null | awk '{print tolower($NF)}' || printf 'unknown' ;; - VM) qm status "$id" 2>/dev/null | awk '{print tolower($NF)}' || printf 'unknown' ;; - *) printf 'unknown' ;; + VM) qm status "$id" 2>/dev/null | awk '{print tolower($NF)}' || printf 'unknown' ;; + *) printf 'unknown' ;; esac } # ===== Namen ===== ct_name_from_config() { pct config "$1" 2>/dev/null | awk -F': *' '/^hostname:/ {print $2; exit}'; } -vm_name_from_config() { qm config "$1" 2>/dev/null | awk -F': *' '/^name:/ {print $2; exit}'; } +vm_name_from_config() { qm config "$1" 2>/dev/null | awk -F': *' '/^name:/ {print $2; exit}'; } # ===== Sammlung: IDTYPESTATUSSYMBOLNAME ===== collect_instances() { @@ -69,7 +175,10 @@ collect_instances() { name="$(awk '{print $NF}' <<<"$line")" [[ -z "$name" || "$name" == "-" ]] && name="$(ct_name_from_config "$id")" [[ -z "$name" ]] && name="CT-${id}" - sym="🟡"; [[ "$status" == "running" ]] && sym="🟢"; [[ "$status" == "stopped" ]] && sym="🔴"; [[ "$status" == "paused" ]] && sym="🟠" + sym="🟡" + [[ "$status" == "running" ]] && sym="🟢" + [[ "$status" == "stopped" ]] && sym="🔴" + [[ "$status" == "paused" ]] && sym="🟠" printf "%s\tCT\t%s\t%s\t%s\n" "$id" "$status" "$sym" "$name" done < <(pct list 2>/dev/null || true) fi @@ -84,15 +193,45 @@ collect_instances() { status="$(awk '{print $3}' <<<"$line")" [[ -z "$name" || "$name" == "-" ]] && name="$(vm_name_from_config "$id")" [[ -z "$name" ]] && name="VM-${id}" - sym="🟡"; [[ "$status" == "running" ]] && sym="🟢"; [[ "$status" == "stopped" ]] && sym="🔴"; [[ "$status" == "paused" ]] && sym="🟠" + sym="🟡" + [[ "$status" == "running" ]] && sym="🟢" + [[ "$status" == "stopped" ]] && sym="🔴" + [[ "$status" == "paused" ]] && sym="🟠" printf "%s\tVM\t%s\t%s\t%s\n" "$id" "$status" "$sym" "$name" done < <(qm list 2>/dev/null || true) fi } +print_json() { + mapfile -t rows < <(collect_instances | sort -n -t$'\t' -k1,1) + if ((${#rows[@]} == 0)); then + printf '[]\n' + return 0 + fi + printf '[' + local first=1 + for row in "${rows[@]}"; do + IFS=$'\t' read -r id ty st sym nm <<<"$row" + local sep="," + if ((first)); then + sep='' + first=0 + fi + local id_json="$id" + if ! [[ "$id_json" =~ ^[0-9]+$ ]]; then + id_json="\"$(json_escape "$id_json")\"" + fi + printf '%s{"id":%s,"type":"%s","status":"%s","symbol":"%s","name":"%s"}' \ + "$sep" "$id_json" "$(json_escape "$ty")" "$(json_escape "$st")" "$(json_escape "$sym")" "$(json_escape "$nm")" + done + printf ']\n' +} + # ===== UI ===== header() { - clear + if ((CLEAR_SCREEN == 1)) && [[ -t 1 ]]; then + clear + fi printf '%b\n' "${BOLD}${BLUE}═══════════════════════════════════════════════════${NC}" printf '%b\n' "${BOLD}${BLUE} Proxmox VM/CT Management Tool ${NC}" printf '%b\n' "${BOLD}${BLUE}═══════════════════════════════════════════════════${NC}" @@ -108,12 +247,13 @@ print_table() { any=1 printf "%-6s %-6s %-10s %-6s %-30s\n" "$id" "$ty" "$st" "$sym" "$nm" done < <(collect_instances | sort -n -t$'\t' -k1,1) - if (( any == 0 )); then + if ((any == 0)); then printf '%b\n' "${RED}Keine VMs oder Container gefunden.${NC}" printf '%s\n' "Direkt auf dem Proxmox-Host als root starten." return 1 fi printf '%s\n' "─────────────────────────────────────────────────────────────" + printf '%s\n' "Legende: 🟢 running · 🔴 stopped · 🟠 paused · 🟡 unknown" return 0 } @@ -122,17 +262,27 @@ main_menu() { if ! print_table; then return 1; fi echo "Eingaben: VMID starten | 'r' neu laden | 'q' beenden" printf '%s' "Auswahl: " - local choice; read_line choice + local choice + read_line choice case "$choice" in - q|Q) exit 0 ;; - r|R|'') return 0 ;; - * ) + q | Q) exit 0 ;; + r | R | '') return 0 ;; + *) if [[ "$choice" =~ ^[0-9]+$ ]]; then local sel_type sel_name found=0 while IFS=$'\t' read -r id ty _ _ nm; do - if [[ "$id" == "$choice" ]]; then sel_type="$ty"; sel_name="$nm"; found=1; break; fi + if [[ "$id" == "$choice" ]]; then + sel_type="$ty" + sel_name="$nm" + found=1 + break + fi done < <(collect_instances) - (( found == 1 )) && action_menu "$choice" "$sel_type" "$sel_name" || err "VMID $choice nicht gefunden." + if ((found == 1)); then + action_menu "$choice" "$sel_type" "$sel_name" + else + err "VMID $choice nicht gefunden." + fi else err "Ungültige Eingabe." fi @@ -142,7 +292,8 @@ main_menu() { action_menu() { local id="$1" ty="$2" name="$3" - local st; st="$(status_of "$id" "$ty")" + local st + st="$(status_of "$id" "$ty")" echo note "Aktionen für $ty $id ($name) — Status: $st" echo "1) Start 2) Stop 3) Restart 4) Status" @@ -152,47 +303,110 @@ action_menu() { fi echo "9) Zurück" printf '%s' "Auswahl [1-9]: " - local opt; read_line opt + local opt + read_line opt case "$opt" in - 1) do_action "$id" "$ty" start "$name" ;; - 2) do_action "$id" "$ty" stop "$name" ;; + 1) do_action "$id" "$ty" start "$name" ;; + 2) do_action "$id" "$ty" stop "$name" ;; 3) do_action "$id" "$ty" restart "$name" ;; - 4) do_action "$id" "$ty" status "$name" ;; + 4) do_action "$id" "$ty" status "$name" ;; 5) open_console "$id" "$ty" "$name" ;; 6) snapshots_menu "$id" "$ty" "$name" ;; - 7) [[ "$ty" == "VM" ]] && spice_info "$id" "$name" || err "Nur für VMs." ;; - 8) [[ "$ty" == "VM" ]] && spice_enable "$id" || err "Nur für VMs." ;; + 7) + if [[ "$ty" == "VM" ]]; then + spice_info "$id" "$name" + else + err "Nur für VMs." + fi + ;; + 8) + if [[ "$ty" == "VM" ]]; then + spice_enable "$id" + else + err "Nur für VMs." + fi + ;; 9) : ;; *) err "Ungültig." ;; esac - printf '%s' "Weiter mit Enter… "; local _; read_line _ + printf '%s' "Weiter mit Enter… " + local _ + read_line _ } do_action() { local id="$1" ty="$2" act="$3" name="$4" - local st; st="$(status_of "$id" "$ty")" + local st + st="$(status_of "$id" "$ty")" case "$act" in start) - [[ "$st" == "running" ]] && { ok "$ty $id läuft bereits."; return; } - if [[ "$ty" == "CT" ]]; then pct start "$id" >/dev/null 2>&1 || err "Start CT $id fehlgeschlagen." - else qm start "$id" >/dev/null 2>&1 || err "Start VM $id fehlgeschlagen."; fi - ok "$ty $id gestartet." + [[ "$st" == "running" ]] && { + ok "$ty $id läuft bereits." + return + } + if [[ "$ty" == "CT" ]]; then + if pct start "$id" >/dev/null 2>&1; then + ok "$ty $id gestartet." + else + err "Start CT $id fehlgeschlagen." + fi + else + if qm start "$id" >/dev/null 2>&1; then + ok "$ty $id gestartet." + else + err "Start VM $id fehlgeschlagen." + fi + fi ;; stop) - [[ "$st" != "running" ]] && { ok "$ty $id ist nicht aktiv."; return; } - if [[ "$ty" == "CT" ]]; then pct stop "$id" >/dev/null 2>&1 || err "Stop CT $id fehlgeschlagen." - else qm stop "$id" >/dev/null 2>&1 || err "Stop VM $id fehlgeschlagen."; fi - ok "$ty $id gestoppt." + [[ "$st" != "running" ]] && { + ok "$ty $id ist nicht aktiv." + return + } + if [[ "$ty" == "CT" ]]; then + if pct stop "$id" >/dev/null 2>&1; then + ok "$ty $id gestoppt." + else + err "Stop CT $id fehlgeschlagen." + fi + else + if qm stop "$id" >/dev/null 2>&1; then + ok "$ty $id gestoppt." + else + err "Stop VM $id fehlgeschlagen." + fi + fi ;; restart) - if [[ "$st" != "running" ]]; then note "$ty $id lief nicht. Starte statt Neustart."; do_action "$id" "$ty" start "$name"; return; fi - if [[ "$ty" == "CT" ]]; then pct stop "$id" >/dev/null 2>&1 && sleep 1 && pct start "$id" >/dev/null 2>&1 || err "Restart CT fehlgeschlagen." - else qm stop "$id" >/dev/null 2>&1 && sleep 1 && qm start "$id" >/dev/null 2>&1 || err "Restart VM fehlgeschlagen."; fi - ok "$ty $id neu gestartet." + if [[ "$st" != "running" ]]; then + note "$ty $id lief nicht. Starte statt Neustart." + do_action "$id" "$ty" start "$name" + return + fi + if [[ "$ty" == "CT" ]]; then + if pct stop "$id" >/dev/null 2>&1 && sleep 1 && pct start "$id" >/dev/null 2>&1; then + ok "$ty $id neu gestartet." + else + err "Restart CT fehlgeschlagen." + fi + else + if qm stop "$id" >/dev/null 2>&1 && sleep 1 && qm start "$id" >/dev/null 2>&1; then + ok "$ty $id neu gestartet." + else + err "Restart VM fehlgeschlagen." + fi + fi ;; status) - if [[ "$ty" == "CT" ]]; then pct status "$id" 2>/dev/null || err "Status CT $id nicht abrufbar." - else qm status "$id" 2>/dev/null || err "Status VM $id nicht abrufbar."; fi + if [[ "$ty" == "CT" ]]; then + if ! pct status "$id" 2>/dev/null; then + err "Status CT $id nicht abrufbar." + fi + else + if ! qm status "$id" 2>/dev/null; then + err "Status VM $id nicht abrufbar." + fi + fi ;; *) err "Unbekannte Aktion: $act" ;; esac @@ -202,9 +416,15 @@ open_console() { local id="$1" ty="$2" name="$3" note "Konsole für $ty $id ($name) öffnen…" if [[ "$ty" == "CT" ]]; then - have pct && { echo "CTRL+D zum Beenden."; pct enter "$id"; } || err "pct fehlt." + if have pct; then + echo "CTRL+D zum Beenden." + pct enter "$id" + else + err "pct fehlt." + fi else - if qm terminal "$id" 2>/dev/null; then : + if qm terminal "$id" 2>/dev/null; then + : else note "'qm terminal' nicht verfügbar. Fallback 'qm monitor'." qm monitor "$id" || err "Konsole für VM $id fehlgeschlagen." @@ -215,21 +435,46 @@ open_console() { snapshots_menu() { local id="$1" ty="$2" name="$3" s echo "1) Auflisten 2) Erstellen 3) Rollback 4) Löschen 5) Zurück" - printf '%s' "Auswahl [1-5]: "; read_line s + printf '%s' "Auswahl [1-5]: " + read_line s case "$s" in - 1) [[ "$ty" == "CT" ]] && pct listsnapshot "$id" 2>/dev/null || qm listsnapshot "$id" 2>/dev/null || echo "(keine oder Fehler)";; - 2) printf 'Name: '; read_line sn; [[ -z "$sn" ]] && { echo "Abbruch."; return; } - if [[ "$ty" == "CT" ]]; then pct snapshot "$id" "$sn" >/dev/null 2>&1 || err "Snapshot fehlgeschlagen." - else qm snapshot "$id" "$sn" >/dev/null 2>&1 || err "Snapshot fehlgeschlagen."; fi - ok "Snapshot '$sn' erstellt." ;; - 3) printf 'Rollback zu Snapshot: '; read_line sn; [[ -z "$sn" ]] && { echo "Abbruch."; return; } - if [[ "$ty" == "CT" ]]; then pct rollback "$id" "$sn" >/dev/null 2>&1 || err "Rollback fehlgeschlagen." - else qm rollback "$id" "$sn" >/dev/null 2>&1 || err "Rollback fehlgeschlagen."; fi - ok "Rollback auf '$sn' ok." ;; - 4) printf 'Snapshot löschen: '; read_line sn; [[ -z "$sn" ]] && { echo "Abbruch."; return; } - if [[ "$ty" == "CT" ]]; then pct delsnapshot "$id" "$sn" >/dev/null 2>&1 || err "Löschen fehlgeschlagen." - else qm delsnapshot "$id" "$sn" >/dev/null 2>&1 || err "Löschen fehlgeschlagen."; fi - ok "Snapshot '$sn' gelöscht." ;; + 1) [[ "$ty" == "CT" ]] && pct listsnapshot "$id" 2>/dev/null || qm listsnapshot "$id" 2>/dev/null || echo "(keine oder Fehler)" ;; + 2) + printf 'Name: ' + read_line sn + [[ -z "$sn" ]] && { + echo "Abbruch." + return + } + if [[ "$ty" == "CT" ]]; then + pct snapshot "$id" "$sn" >/dev/null 2>&1 || err "Snapshot fehlgeschlagen." + else qm snapshot "$id" "$sn" >/dev/null 2>&1 || err "Snapshot fehlgeschlagen."; fi + ok "Snapshot '$sn' erstellt." + ;; + 3) + printf 'Rollback zu Snapshot: ' + read_line sn + [[ -z "$sn" ]] && { + echo "Abbruch." + return + } + if [[ "$ty" == "CT" ]]; then + pct rollback "$id" "$sn" >/dev/null 2>&1 || err "Rollback fehlgeschlagen." + else qm rollback "$id" "$sn" >/dev/null 2>&1 || err "Rollback fehlgeschlagen."; fi + ok "Rollback auf '$sn' ok." + ;; + 4) + printf 'Snapshot löschen: ' + read_line sn + [[ -z "$sn" ]] && { + echo "Abbruch." + return + } + if [[ "$ty" == "CT" ]]; then + pct delsnapshot "$id" "$sn" >/dev/null 2>&1 || err "Löschen fehlgeschlagen." + else qm delsnapshot "$id" "$sn" >/dev/null 2>&1 || err "Löschen fehlgeschlagen."; fi + ok "Snapshot '$sn' gelöscht." + ;; 5) : ;; *) err "Ungültig." ;; esac @@ -239,7 +484,7 @@ spice_info() { local id="$1" name="$2" local host port host="$(hostname -I 2>/dev/null | awk '{print $1}')" - port="$(qm monitor "$id" <<< "info spice" 2>/dev/null | awk '/port/ {for(i=1;i<=NF;i++) if($i ~ /^[0-9]+$/){print $i; exit}}')" + port="$(qm monitor "$id" <<<"info spice" 2>/dev/null | awk '/port/ {for(i=1;i<=NF;i++) if($i ~ /^[0-9]+$/){print $i; exit}}')" [[ -z "$port" ]] && port="$(grep -E "(spice).*port" "/var/log/qemu-server/${id}.log" 2>/dev/null | tail -1 | sed -n 's/.*port=\([0-9]\+\).*/\1/p')" [[ -z "$port" ]] && port="$((61000 + id))" printf '%s\n' "SPICE: spice://${host}:${port}" @@ -257,12 +502,14 @@ EOF } spice_enable() { - local id="$1" port="$((61000 + id))" + local id="$1" + local port="$((61000 + id))" qm set "$id" --vga qxl >/dev/null 2>&1 || true if qm set "$id" --spice "port=${port},addr=0.0.0.0" >/dev/null 2>&1; then ok "SPICE für VM ${id} aktiviert. Port: ${port}. Neustart erforderlich." printf '%s' "Jetzt neu starten? (y/N): " - local a; read_line a + local a + read_line a [[ "$a" =~ ^[yYjJ]$ ]] && do_action "$id" "VM" restart "VM-${id}" else err "SPICE konnte nicht aktiviert werden." @@ -271,8 +518,34 @@ spice_enable() { # ===== Main ===== main() { + parse_args "$@" require_root require_tools - while true; do main_menu || true; done + if [[ ! -t 1 ]]; then + CLEAR_SCREEN=0 + fi + case "$MODE" in + list) + if print_table; then + exit 0 + else + exit 1 + fi + ;; + json) + print_json + exit 0 + ;; + interactive) + while true; do + main_menu || true + ((RUN_ONCE == 1)) && break + done + ;; + *) + err "Unbekannter Modus: $MODE" + exit 1 + ;; + esac } -main +main "$@"