Add sudo support for container/VM hypervisor commands
All checks were successful
Build-Publish / build (linux/amd64) (push) Successful in 4s
Build-Publish / build (linux/arm64) (push) Successful in 12s
Build-Publish / create-manifest (push) Successful in 2s
Build-Publish / publish-template (push) Successful in 8s

This commit is contained in:
j
2026-03-08 13:51:55 +13:00
parent 6a9a3d5901
commit 58f542b96f
2 changed files with 44 additions and 11 deletions

View File

@@ -196,9 +196,18 @@ gather_container_stats() {
[ -n "$uptime_s" ] && echo "uptime_seconds=$uptime_s" [ -n "$uptime_s" ] && echo "uptime_seconds=$uptime_s"
} }
# Use sudo if available and needed (infmap user won't have direct access)
_sudo() {
if [ "$(id -u)" -eq 0 ]; then
"$@"
else
sudo -n "$@" 2>/dev/null
fi
}
# Proxmox LXC (pct) # Proxmox LXC (pct)
if command -v pct &>/dev/null; then if command -v pct &>/dev/null; then
pct list 2>/dev/null | tail -n +2 | while read -r vmid status _ name _; do _sudo pct list 2>/dev/null | tail -n +2 | while read -r vmid status _ name _; do
[ -z "$vmid" ] && continue [ -z "$vmid" ] && continue
echo "[container:pct-${vmid}]" echo "[container:pct-${vmid}]"
echo "type=lxc" echo "type=lxc"
@@ -207,14 +216,14 @@ if command -v pct &>/dev/null; then
echo "name=${name:-$vmid}" echo "name=${name:-$vmid}"
echo "status=$status" echo "status=$status"
if [ "$status" = "running" ]; then if [ "$status" = "running" ]; then
gather_container_stats "pct exec $vmid --" gather_container_stats "_sudo pct exec $vmid --"
fi fi
done done
fi fi
# Proxmox VMs (qm) # Proxmox VMs (qm)
if command -v qm &>/dev/null; then if command -v qm &>/dev/null; then
qm list 2>/dev/null | tail -n +2 | while read -r vmid name status _ mem _; do _sudo qm list 2>/dev/null | tail -n +2 | while read -r vmid name status _ mem _; do
[ -z "$vmid" ] && continue [ -z "$vmid" ] && continue
echo "[container:qm-${vmid}]" echo "[container:qm-${vmid}]"
echo "type=vm" echo "type=vm"
@@ -225,9 +234,9 @@ if command -v qm &>/dev/null; then
[ -n "$mem" ] && echo "mem_allocated_mb=$mem" [ -n "$mem" ] && echo "mem_allocated_mb=$mem"
# VM stats require guest agent - best effort # VM stats require guest agent - best effort
if [ "$status" = "running" ]; then if [ "$status" = "running" ]; then
agent_test=$(qm guest exec "$vmid" -- cat /proc/meminfo 2>/dev/null) agent_test=$(_sudo qm guest exec "$vmid" -- cat /proc/meminfo 2>/dev/null)
if [ -n "$agent_test" ]; then if [ -n "$agent_test" ]; then
gather_container_stats "qm guest exec $vmid --" gather_container_stats "_sudo qm guest exec $vmid --"
fi fi
fi fi
done done
@@ -235,7 +244,7 @@ fi
# Plain LXC (lxc/lxd) # Plain LXC (lxc/lxd)
if command -v lxc &>/dev/null && ! command -v pct &>/dev/null; then if command -v lxc &>/dev/null && ! command -v pct &>/dev/null; then
lxc list --format csv -c nsN 2>/dev/null | while IFS=',' read -r name status network; do _sudo lxc list --format csv -c nsN 2>/dev/null | while IFS=',' read -r name status network; do
[ -z "$name" ] && continue [ -z "$name" ] && continue
echo "[container:lxc-${name}]" echo "[container:lxc-${name}]"
echo "type=lxc" echo "type=lxc"
@@ -243,7 +252,7 @@ if command -v lxc &>/dev/null && ! command -v pct &>/dev/null; then
echo "name=$name" echo "name=$name"
echo "status=$status" echo "status=$status"
if [ "$status" = "RUNNING" ]; then if [ "$status" = "RUNNING" ]; then
gather_container_stats "lxc exec $name --" gather_container_stats "_sudo lxc exec $name --"
lxd_ip=$(echo "$network" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1) lxd_ip=$(echo "$network" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1)
[ -n "$lxd_ip" ] && echo "ip=$lxd_ip" [ -n "$lxd_ip" ] && echo "ip=$lxd_ip"
fi fi
@@ -252,16 +261,16 @@ fi
# libvirt VMs (virsh) # libvirt VMs (virsh)
if command -v virsh &>/dev/null; then if command -v virsh &>/dev/null; then
virsh list --all --name 2>/dev/null | while read -r name; do _sudo virsh list --all --name 2>/dev/null | while read -r name; do
[ -z "$name" ] && continue [ -z "$name" ] && continue
state=$(virsh domstate "$name" 2>/dev/null | head -1) state=$(_sudo virsh domstate "$name" 2>/dev/null | head -1)
echo "[container:virsh-${name}]" echo "[container:virsh-${name}]"
echo "type=vm" echo "type=vm"
echo "platform=libvirt" echo "platform=libvirt"
echo "name=$name" echo "name=$name"
echo "status=$state" echo "status=$state"
if [ "$state" = "running" ]; then if [ "$state" = "running" ]; then
virsh_ip=$(virsh domifaddr "$name" --source agent 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1) virsh_ip=$(_sudo virsh domifaddr "$name" --source agent 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1)
[ -n "$virsh_ip" ] && echo "ip=$virsh_ip" [ -n "$virsh_ip" ] && echo "ip=$virsh_ip"
fi fi
done done

View File

@@ -198,6 +198,27 @@ if [ "$OS" != "openwrt" ]; then
done done
fi fi
# --- Sudoers for container/VM commands ---
SUDOERS_FILE="/etc/sudoers.d/infmap"
SUDO_CMDS=""
# Detect which hypervisor tools are present
for cmd in pct qm lxc virsh; do
cmd_path=$(command -v "$cmd" 2>/dev/null)
if [ -n "$cmd_path" ]; then
SUDO_CMDS="${SUDO_CMDS}${USERNAME} ALL=(root) NOPASSWD: ${cmd_path}\n"
fi
done
if [ -n "$SUDO_CMDS" ]; then
printf "%b" "$SUDO_CMDS" > "$SUDOERS_FILE"
chmod 440 "$SUDOERS_FILE"
echo "Sudoers rules added for container/VM commands"
else
echo "No hypervisor tools found, skipping sudoers"
fi
# --- Summary --- # --- Summary ---
echo "" echo ""
@@ -207,6 +228,9 @@ echo " Home: $HOMEDIR"
echo " Auth: key-only (password disabled)" echo " Auth: key-only (password disabled)"
echo " SSH key: restricted to 'bash -s' (no shell, no forwarding)" echo " SSH key: restricted to 'bash -s' (no shell, no forwarding)"
echo " Packages: lm-sensors, pciutils, iproute2" echo " Packages: lm-sensors, pciutils, iproute2"
if [ -n "$SUDO_CMDS" ]; then
echo " Sudo: container/VM commands (pct, qm, lxc, virsh)"
fi
echo "" echo ""
echo "Add to your infrastructure.conf:" echo "Add to your infrastructure.conf:"
echo " ${USERNAME}@$(hostname)" echo " $(hostname)"