|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# BulkDisableAutoStart.sh |
| 4 | +# |
| 5 | +# Disables autostart for all VM (qm) and LXC (pct) resources inside a range of |
| 6 | +# nested Proxmox virtual machines identified by VMIDs on the parent host. |
| 7 | +# For each VMID in the provided range, the script attempts to determine the |
| 8 | +# management IP via the QEMU guest agent; if unavailable, it falls back to |
| 9 | +# parsing the VM config for an IP (best-effort). It then connects via SSH using |
| 10 | +# provided credentials and disables autostart flags on all internal resources. |
| 11 | +# |
| 12 | +# Usage: |
| 13 | +# ./BulkDisableAutoStart.sh <startVmId> <endVmId> <sshUsername> <sshPassword> |
| 14 | +# |
| 15 | +# Example: |
| 16 | +# ./BulkDisableAutoStart.sh 200 210 root passw0rd |
| 17 | +# |
| 18 | +# Notes: |
| 19 | +# - Must be run as root on the outer Proxmox host. |
| 20 | +# - Requires 'qm' with guest agent responding for reliable IP detection. |
| 21 | +# - SSH password auth is used (non-interactive). Keys can be adapted if desired. |
| 22 | +# - Only modifies autostart flags (onboot=0) within nested environment; no reboots performed. |
| 23 | +# |
| 24 | +# Function Index: |
| 25 | +# - usage |
| 26 | +# - get_ip_for_vmid |
| 27 | +# - ssh_exec |
| 28 | +# - disable_autostart_inner |
| 29 | +# - process_vmid |
| 30 | +# - main |
| 31 | +# |
| 32 | + |
| 33 | +############################################################################### |
| 34 | +# Usage |
| 35 | +############################################################################### |
| 36 | +usage() { |
| 37 | + cat <<USAGE |
| 38 | +Usage: ${0##*/} <startVmId> <endVmId> <sshUsername> <sshPassword> |
| 39 | +
|
| 40 | +Disables autostart (onboot) for all VMs and LXCs inside each nested Proxmox |
| 41 | +instance whose VMID is in the inclusive range [startVmId, endVmId]. |
| 42 | +
|
| 43 | +Arguments: |
| 44 | + startVmId Starting VMID (inclusive) |
| 45 | + endVmId Ending VMID (inclusive) |
| 46 | + sshUsername SSH username for nested Proxmox host |
| 47 | + sshPassword SSH password for nested Proxmox host |
| 48 | +
|
| 49 | +Environment: |
| 50 | + UTILITYPATH (optional, exported by GUI.sh) to source shared helpers. |
| 51 | +USAGE |
| 52 | +} |
| 53 | + |
| 54 | +############################################################################### |
| 55 | +# Setup / Imports |
| 56 | +############################################################################### |
| 57 | +source "${UTILITYPATH}/Prompts.sh" |
| 58 | +source "${UTILITYPATH}/Communication.sh" |
| 59 | +source "${UTILITYPATH}/Queries.sh" |
| 60 | +source "${UTILITYPATH}/SSH.sh" |
| 61 | + |
| 62 | +__check_root__ |
| 63 | +__check_proxmox__ |
| 64 | +__install_or_prompt__ "sshpass" |
| 65 | +__install_or_prompt__ "jq" |
| 66 | + |
| 67 | + |
| 68 | +############################################################################### |
| 69 | +# Argument Parsing |
| 70 | +############################################################################### |
| 71 | +if [[ $# -lt 4 ]]; then |
| 72 | + echo "Error: Missing arguments." >&2 |
| 73 | + usage |
| 74 | + exit 1 |
| 75 | +fi |
| 76 | + |
| 77 | +startVmId="$1" |
| 78 | +endVmId="$2" |
| 79 | +sshUser="$3" |
| 80 | +sshPass="$4" |
| 81 | + |
| 82 | +if ! [[ "$startVmId" =~ ^[0-9]+$ && "$endVmId" =~ ^[0-9]+$ && $endVmId -ge $startVmId ]]; then |
| 83 | + echo "Invalid VMID range: $startVmId..$endVmId" >&2 |
| 84 | + exit 1 |
| 85 | +fi |
| 86 | + |
| 87 | +############################################################################### |
| 88 | +# Helpers |
| 89 | +############################################################################### |
| 90 | +# IP retrieval uses utility: __get_ip_from_vmid__ (from Queries.sh) |
| 91 | +# SSH reachability waits via: __wait_for_ssh__ (from SSH.sh) |
| 92 | + |
| 93 | +# disable_autostart_inner <host> |
| 94 | +# Runs inside nested host: disable autostart for qm + pct resources. |
| 95 | +disable_autostart_inner() { |
| 96 | + local host="$1" |
| 97 | + sshpass -p "$sshPass" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \ |
| 98 | + "$sshUser@$host" bash -s <<'INNER' |
| 99 | +set -euo pipefail |
| 100 | +if ! command -v qm &>/dev/null; then |
| 101 | + echo "Not a Proxmox environment (qm missing)." >&2 |
| 102 | + exit 1 |
| 103 | +fi |
| 104 | +# Disable autostart for QEMU VMs |
| 105 | +declare -a vms |
| 106 | +mapfile -t vms < <(qm list | awk 'NR>1 {print $1}') |
| 107 | +for id in "${vms[@]}"; do |
| 108 | + if qm config "$id" | grep -q '^onboot: *1'; then |
| 109 | + qm set "$id" -onboot 0 >/dev/null 2>&1 || true |
| 110 | + echo "VM $id: onboot -> 0" |
| 111 | + else |
| 112 | + echo "VM $id: already 0 or no onboot flag" |
| 113 | + fi |
| 114 | +done |
| 115 | +# Disable autostart for LXCs if pct exists |
| 116 | +if command -v pct &>/dev/null; then |
| 117 | + declare -a cts |
| 118 | + mapfile -t cts < <(pct list | awk 'NR>1 {print $1}') |
| 119 | + for cid in "${cts[@]}"; do |
| 120 | + if pct config "$cid" | grep -q '^onboot: *1'; then |
| 121 | + pct set "$cid" -onboot 0 >/dev/null 2>&1 || true |
| 122 | + echo "CT $cid: onboot -> 0" |
| 123 | + else |
| 124 | + echo "CT $cid: already 0 or no onboot flag" |
| 125 | + fi |
| 126 | + done |
| 127 | +fi |
| 128 | +INNER |
| 129 | +} |
| 130 | + |
| 131 | +# process_vmid <vmid> |
| 132 | +process_vmid() { |
| 133 | + local vmid="$1" |
| 134 | + if command -v __info__ &>/dev/null; then |
| 135 | + __info__ "Processing VMID $vmid" |
| 136 | + else |
| 137 | + echo "=== VMID $vmid ===" |
| 138 | + fi |
| 139 | + local ip |
| 140 | + if ! ip="$( __get_ip_from_vmid__ "$vmid" 2>/dev/null )"; then |
| 141 | + if command -v __err__ &>/dev/null; then |
| 142 | + __err__ "Could not determine IP for VMID $vmid" |
| 143 | + else |
| 144 | + echo "Error: Could not determine IP for VMID $vmid" >&2 |
| 145 | + fi |
| 146 | + return 1 |
| 147 | + fi |
| 148 | + # Ensure SSH is reachable (best-effort) before attempting changes |
| 149 | + __wait_for_ssh__ "$ip" "$sshUser" "$sshPass" 2>/dev/null || true |
| 150 | + if command -v __update__ &>/dev/null; then |
| 151 | + __update__ "Disabling autostart on nested host $ip" |
| 152 | + else |
| 153 | + echo "Connecting to $ip ..." |
| 154 | + fi |
| 155 | + if disable_autostart_inner "$ip"; then |
| 156 | + command -v __ok__ &>/dev/null && __ok__ "Done VMID $vmid" || echo "Completed VMID $vmid" |
| 157 | + else |
| 158 | + command -v __err__ &>/dev/null && __err__ "Failed VMID $vmid" || echo "Failed VMID $vmid" >&2 |
| 159 | + fi |
| 160 | +} |
| 161 | + |
| 162 | +############################################################################### |
| 163 | +# MAIN |
| 164 | +############################################################################### |
| 165 | +main() { |
| 166 | + for ((id=startVmId; id<=endVmId; id++)); do |
| 167 | + process_vmid "$id" |
| 168 | + done |
| 169 | + echo "All requested VMIDs processed ($startVmId..$endVmId)." |
| 170 | + |
| 171 | + __prompt_keep_installed_packages__ |
| 172 | +} |
| 173 | + |
| 174 | +main "$@" |
| 175 | + |
| 176 | + |
| 177 | +############################################################################### |
| 178 | +# Testing status |
| 179 | +############################################################################### |
| 180 | +# Tested: (pending) range on lab cluster; guest agent required for accurate IP discovery. |
0 commit comments