Direkt zum Hauptinhalt

Proxmox Wartungsmodus / Maintenance Mode - Diasable / Enable VMS at Start

Beschreibung

Man möchte den Server mehrmals neustarten, dann nervt es doch, wenn alle VMs/CTs mit starten.
Mit folgendem Script kann man das ein und auschalten

Installation des scriptesscripte

Auf den Server einloggen (sollte es ein Cluster sein, braucht das nur auf eine Node gepackt werden)
Nun eine Neue Datei erstellen

nano /root/maintenance.save_boot_state.sh

Den Inhalt einfügen

#!/bin/bash
Quelltext# ein/ausklappbarboot-save-disable.sh
# Speichert alle onboot=1 VMs/CTs und deaktiviert sie

SAVEFILE="/root/boot-state.txt"
> "$SAVEFILE"

echo "=== Speichere Boot-Status ==="

for conf in /etc/pve/qemu-server/*.conf /etc/pve/lxc/*.conf; do
    [ -f "$conf" ] || continue
    if grep -q "^onboot: 1" "$conf"; then
        id=$(basename "$conf" .conf)
        type="vm"
        [[ "$conf" == *"/lxc/"* ]] && type="ct"
        echo "$type $id" >> "$SAVEFILE"
        sed -i 's/^onboot: 1/onboot: 0/' "$conf"
        echo "  Deaktiviert: $type $id"
    fi
done

echo ""
echo "Gespeichert in: $SAVEFILE"
echo "$(wc -l < "$SAVEFILE") Einträge"

 

Ausführbar machen

chmod +x /root/save_boot_state.sh

Wiederherstellung Script

nano /root/restore-boot-state.sh

Inhalt

#!/bin/bash
# boot-restore.sh
# Stellt onboot=1 für alle gespeicherten VMs/CTs wieder her

SAVEFILE="/root/boot-state.txt"

if [ ! -f "$SAVEFILE" ]; then
    echo "Fehler: $SAVEFILE nicht gefunden!"
    exit 1
fi

echo "=== Stelle Boot-Status wieder her ==="

while read -r type id; do
    if [ "$type" = "vm" ]; then
        conf="/etc/pve/qemu-server/${id}.conf"
    else
        conf="/etc/pve/lxc/${id}.conf"
    fi

    if [ -f "$conf" ]; then
        if grep -q "^onboot:" "$conf"; then
            sed -i 's/^onboot: 0/onboot: 1/' "$conf"
        else
            echo "onboot: 1" >> "$conf"
        fi
        echo "  Aktiviert: $type $id"
    else
        echo "  FEHLER: $conf nicht gefunden"
    fi
done < "$SAVEFILE"

echo ""
echo "Fertig."

Ausführbar machen

chmod +x /root/restore_boot_state.sh

Status script

nano /root/status_boot_state.sh

Inhalt

#!/bin/bash
# boot-status.sh
# Zeigt aktuellen onboot-Status aller VMs/CTs

SAVEFILE="/root/boot-state.txt"

echo "=== Aktueller onboot-Status ==="
echo ""
printf "%-6s %-6s %-8s %s\n" "Typ" "ID" "onboot" "Name"
echo "---------------------------------------------------------
#                 Proxmox Maintenance Mode
#  -Darkhand81
#  https://github.com/Darkhand81/ProxmoxMaintenanceMode
#  Temporarily disable all VMs/CTs that are set to autoboot
#  at node startup to allow"

for nodeconf maintenance.in #/etc/pve/qemu-server/*.conf #/etc/pve/lxc/*.conf; do
    [ ---------------------------------------------------------f #"$conf" ---BEGIN---] #|| Requirecontinue
    rootid=$(basename if"$conf" .conf)
    type="vm"; [[ "$EUIDconf" -ne== 0*"/lxc/"* ]]; then&& echotype="ct"
    name=$(grep "^name:" "$0 is not running as root. Try using sudo."
    exit 2
fi

# Pathname of the lockfile that signals the script that we are in
# maintenance mode.  Contains the VMs/CTs that were disabled so they
# can be re-enabled later:
lockfile="/root/maintmode.lock"

# ---------
# Functions
# ---------

# Enable maintenance mode - Query all instances, check which are set to
# start at boot, record and disable them.
function enable_maintmode(){

  echo "Disabling (and saving) current onboot settings:"

  # List all VMs, filter only the first word, then filter only numerics (IDs):
  for vm in $(qm list | awk '{print $1}' | grep -Eo '[0-9]{1,5}')
  do
    # Of those, query each VMID and search for those with onboot: enabled:
    for vmstatus in $(qm config $vm | grep "onboot: 1"conf" | awk '{print $2}')
    do[ #Save matching IDs to the lockfile, prepend with VM to identify as a VM:
      echo-z "VM$vm"$name" >>] $lockfile&& # Disable onboot for matching VMIDs:
      qm set $vm -onboot 0
    done
  done

  # Repeat for CTs as they use a different command to enable/disable:
  for ct in name=$(pct list | awk '{print $1}' | grep -Eo '[0-9]{1,5}')
  do
    for ctstatus in $(pct config $ct | grep "onboot:^hostname:" 1""$conf" | awk '{print $2}')
    doonboot=$(grep #"^onboot:" Prepend"$conf" with| CTawk to'{print identify$2}')
    as[ a-z container:"$onboot" ] && onboot="0"
    printf "%-6s %-6s %-8s %s\n" "$type" "$id" "$onboot" "${name:-}"
done

echo "CT$ct" >> $lockfile
      # Disable onboot for matching containers:
      pct set $ct -onboot 0
      # pct currently doesn't provide an output like qm does, so simulate it here:
      echo "update CT $ct: -onboot 0"
    done
  done
}

# Disable maintenance mode - Parse the lockfile and re-enable onboot
# for those IDs:
function disable_maintmode(){

  file=$(cat $lockfile)
  echo -e "\nRe-enabling previous onboot settings:"

  for line in $file
  do
    # For each line starting with VM, run the qm command to enable VM onboot:
    for vm_on in $(echo -e "$line" | grep 'VM' | cut -c 3-)
    do
      qm set $vm_on -onboot 1
    done
  done

  for line in $file
  do
    # For each line starting with CT, run the pct command for CTs:
    for ct_on in $(echo -e "$line" | grep 'CT' | cut -c 3-)
    do
      pct set $ct_on -onboot 1
      # pct currently doesn't provide an output like qm does, so simulate it here:
      echo "update CT $ct_on: -onboot 1"
    done
  done

  # Remove the lockfile as we want to signal that we are out of maintenance mode:
  rm $lockfile
}

# -----
# Start
# -----

# If the lockfile doesn't exist, we want to enable maintenance mode (disable onboot).
# Otherwise we want to disable maintenance mode (enable onboot):
if [ ! -f "$lockfile"SAVEFILE" ]; then
    echo read"Gespeicherte Einträge in $SAVEFILE: $(wc -pl "Enable maintenance mode and disable all current VM/CT bootup? (y/n) " CONT
    if [< "$CONT" = "y" ]; then
      enable_maintmode;
      echo -e "\nMaintenance mode is now enabled! VM autostart is disabled. Run this script again to re-enable.SAVEFILE")"
else
    echo "Exiting.Kein Savefile vorhanden ($SAVEFILE)";
      exit
fi
else
  echo
  read -p "Maintenance mode is on! Re-enable previous VM/CT bootup? (y/n) " CONT
    if [ "$CONT" = "y" ]; then
      disable_maintmode
      echo -e "\nMaintenance mode is now disabled! All VMs/CTs that were previously set to autorun will do so at next bootup."
    else
      echo "Exiting.";
      exit
    fi
fi

Ausführbar machen

chmod +x /root/maintenance.status_boot_state.sh

Bedienung

 

Das script starten

. /root/maintenance.sh

Frage mit Y beantworten wenn der Wartungsmodus gestartet werden soll

image-1676704517598.png

Nun wird der Autostart deaktiviert

image-1676704920407.png

Möchte man den Wartungsmodus beenden, einfach das script nochmals aufrufen und mit y bestätigen das man den Wartungsmodus wieder verlassen möchte

. /root/maintenance.sh

image-1676705078307.png

Nun werden die VMs die vorher starten drin hatten auch wieder aktiviert

image-1676705123732.png

Voila, Fertig

Ein großen Dank an den ersteller des Scriptes

Quelle : https://github.com/Darkhand81/ProxmoxMaintenanceMode