feat: Inline local variable declarations (#100)

This commit is contained in:
Kroese
2026-07-24 13:51:14 +02:00
committed by GitHub
parent 2c941ff870
commit d138b7efcf
+49 -58
View File
@@ -51,8 +51,7 @@ getMTU() {
minMTU() { minMTU() {
local mtu="" local mtu min=""
local min=""
for mtu in "$@"; do for mtu in "$@"; do
[[ -z "$mtu" || "$mtu" == "0" ]] && continue [[ -z "$mtu" || "$mtu" == "0" ]] && continue
@@ -91,7 +90,7 @@ gatewayMAC() {
maskToCIDR() { maskToCIDR() {
local mask="$1" local mask="$1"
local prefix="" local prefix
if ! command -v ipcalc > /dev/null 2>&1; then if ! command -v ipcalc > /dev/null 2>&1; then
error "Required command 'ipcalc' is not installed!" error "Required command 'ipcalc' is not installed!"
@@ -121,7 +120,7 @@ maskToCIDR() {
networkCIDR() { networkCIDR() {
local ip="$1" local ip="$1"
local network="" local network
network=$(ipcalc -n -b "$ip/$MASK" 2>/dev/null | awk ' network=$(ipcalc -n -b "$ip/$MASK" 2>/dev/null | awk '
/^Network:/ { /^Network:/ {
@@ -160,8 +159,6 @@ detectInterface() {
detectAddresses() { detectAddresses() {
local rc=0
GATEWAY=$(ip route list dev "$DEV" | awk '/^default/ { print $3 }' | head -n 1) GATEWAY=$(ip route list dev "$DEV" | awk '/^default/ { print $3 }' | head -n 1)
{ UPLINK=$(ip address show dev "$DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/ | head -n 1); } 2>/dev/null || : { UPLINK=$(ip address show dev "$DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/ | head -n 1); } 2>/dev/null || :
@@ -170,7 +167,7 @@ detectAddresses() {
if [ -f /proc/net/if_inet6 ] && if [ -f /proc/net/if_inet6 ] &&
[[ "$(cat /proc/sys/net/ipv6/conf/all/disable_ipv6 2>/dev/null)" != "1" ]]; then [[ "$(cat /proc/sys/net/ipv6/conf/all/disable_ipv6 2>/dev/null)" != "1" ]]; then
{ IP6=$(ip -6 addr show dev "$DEV" scope global up); rc=$?; } 2>/dev/null || : { IP6=$(ip -6 addr show dev "$DEV" scope global up); local rc=$?; } 2>/dev/null || :
(( rc != 0 )) && IP6="" (( rc != 0 )) && IP6=""
[ -n "$IP6" ] && [ -n "$IP6" ] &&
@@ -182,7 +179,7 @@ detectAddresses() {
detectAdapter() { detectAdapter() {
local result="" local result
NIC="" NIC=""
BUS="" BUS=""
@@ -208,7 +205,7 @@ detectAdapter() {
containerID() { containerID() {
local id="" local id
id=$(hostname -s 2>/dev/null || true) id=$(hostname -s 2>/dev/null || true)
@@ -216,7 +213,7 @@ containerID() {
id=$(< /etc/machine-id) id=$(< /etc/machine-id)
fi fi
if [ -z "$id" ] && [ -s /proc/sys/kernel/random/boot_id ]; then if [ -z "$id" ] && [ -r /proc/sys/kernel/random/boot_id ]; then
id=$(< /proc/sys/kernel/random/boot_id) id=$(< /proc/sys/kernel/random/boot_id)
fi fi
@@ -242,7 +239,7 @@ disableIPv6() {
subnetInUse() { subnetInUse() {
local subnet="$1" local subnet="$1"
local broader="" narrower="" routes="" local broader narrower routes
if ! broader=$(ip -4 route show table all match "$subnet" 2>/dev/null); then if ! broader=$(ip -4 route show table all match "$subnet" 2>/dev/null); then
error "Failed to inspect existing routes for subnet $subnet." error "Failed to inspect existing routes for subnet $subnet."
@@ -266,22 +263,18 @@ subnetInUse() {
subnetBase() { subnetBase() {
local ip="$1" local ip="$1"
local third="" local third second
local second=""
local base=""
local subnet=""
local rc=0
third=$(cut -d. -f3 <<< "$ip") third=$(cut -d. -f3 <<< "$ip")
for second in {30..254}; do for second in {30..254}; do
base="172.$second.$third" local base="172.$second.$third"
subnet="$base.0/$PREFIX" local subnet="$base.0/$PREFIX"
if subnetInUse "$subnet"; then if subnetInUse "$subnet"; then
continue continue
else else
rc=$? local rc=$?
(( rc == 1 )) || return 1 (( rc == 1 )) || return 1
fi fi
@@ -411,10 +404,10 @@ EOF2
createBridge() { createBridge() {
local gateway="$1" local gateway="$1"
local rc msg="" local msg
# Create a bridge with a static IP for the VM LAN # Create a bridge with a static IP for the VM LAN
{ msg=$(ip link add dev "$BRIDGE" type bridge 2>&1); rc=$?; } || : { msg=$(ip link add dev "$BRIDGE" type bridge 2>&1); local rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
[ -n "$msg" ] && echo "$msg" >&2 [ -n "$msg" ] && echo "$msg" >&2
@@ -452,10 +445,10 @@ createBridge() {
createTap() { createTap() {
local tuntap="$1" local tuntap="$1"
local rc msg="" local msg
# Set tap to the bridge created # Set tap to the bridge created
{ msg=$(ip tuntap add dev "$TAP" mode tap 2>&1); rc=$?; } || : { msg=$(ip tuntap add dev "$TAP" mode tap 2>&1); local rc=$?; } || :
if (( rc != 0 )); then if (( rc != 0 )); then
[ -n "$msg" ] && echo "$msg" >&2 [ -n "$msg" ] && echo "$msg" >&2
@@ -493,7 +486,7 @@ createTap() {
getTablesBackend() { getTablesBackend() {
local version="" local version
version=$(iptables --version 2>/dev/null || true) version=$(iptables --version 2>/dev/null || true)
case "$version" in case "$version" in
@@ -506,7 +499,7 @@ getTablesBackend() {
setTables() { setTables() {
local mode="$1" local mode="$1"
local path="" local path
path=$(command -v "iptables-$mode" 2>/dev/null || true) path=$(command -v "iptables-$mode" 2>/dev/null || true)
[ -z "$path" ] && return 1 [ -z "$path" ] && return 1
@@ -520,7 +513,7 @@ showRules() {
local chain="$2" local chain="$2"
local label="$3" local label="$3"
local rule_tag="$4" local rule_tag="$4"
local rules="" local rules
local own_rule="--comment[[:space:]]+\"?$rule_tag\"?([[:space:]]|\$)" local own_rule="--comment[[:space:]]+\"?$rule_tag\"?([[:space:]]|\$)"
enabled "$DEBUG" || return 0 enabled "$DEBUG" || return 0
@@ -539,7 +532,7 @@ showRules() {
checkExistingTables() { checkExistingTables() {
local msg="" rules="" conflicts="" local rules conflicts
local rule_tag="PROXMOX_NAT" local rule_tag="PROXMOX_NAT"
local own_rule="--comment[[:space:]]+\"?$rule_tag\"?([[:space:]]|\$)" local own_rule="--comment[[:space:]]+\"?$rule_tag\"?([[:space:]]|\$)"
@@ -554,7 +547,7 @@ checkExistingTables() {
<<< "$rules" || true) <<< "$rules" || true)
if [ -n "$conflicts" ]; then if [ -n "$conflicts" ]; then
msg="your existing firewall rules may block traffic forwarded to or from the VM subnet" local msg="your existing firewall rules may block traffic forwarded to or from the VM subnet"
if enabled "$DEBUG"; then if enabled "$DEBUG"; then
warn "${msg}." warn "${msg}."
@@ -573,13 +566,13 @@ runTableRule() {
local silent="$1" local silent="$1"
local result="$2" local result="$2"
local rc msg="" local msg
shift 2 shift 2
printf -v "$result" '%s' "" printf -v "$result" '%s' ""
{ msg=$("$@" 2>&1); rc=$?; } || : { msg=$("$@" 2>&1); local rc=$?; } || :
(( rc == 0 )) && return 0 (( rc == 0 )) && return 0
printf -v "$result" '%s' "$msg" printf -v "$result" '%s' "$msg"
@@ -638,7 +631,7 @@ applyTables() {
local subnet="$1" local subnet="$1"
local silent="${2:-N}" local silent="${2:-N}"
local table_error="" local table_error
local rule_tag="PROXMOX_NAT" local rule_tag="PROXMOX_NAT"
# NAT traffic from the VM subnet leaving through any external interface. # NAT traffic from the VM subnet leaving through any external interface.
@@ -682,8 +675,8 @@ applyTables() {
clearTables() { clearTables() {
local table="" line="" local table=""
local rules="" remaining="" message="" local line rules remaining
local rule_tag="PROXMOX_NAT" local rule_tag="PROXMOX_NAT"
local own_rule="--comment[[:space:]]+\"?$rule_tag\"?([[:space:]]|\$)" local own_rule="--comment[[:space:]]+\"?$rule_tag\"?([[:space:]]|\$)"
@@ -692,6 +685,7 @@ clearTables() {
if ! rules=$(iptables-save 2> /dev/null); then if ! rules=$(iptables-save 2> /dev/null); then
if enabled "$DEBUG"; then if enabled "$DEBUG"; then
local message
message=$(iptables-save 2>&1 > /dev/null || true) message=$(iptables-save 2>&1 > /dev/null || true)
showTableCleanupError "iptables-save" "$message" showTableCleanupError "iptables-save" "$message"
fi fi
@@ -716,6 +710,7 @@ clearTables() {
line="${line/-A /-D }" line="${line/-A /-D }"
# Parse the quoting produced by iptables-save before deleting the rule. # Parse the quoting produced by iptables-save before deleting the rule.
local message
if ! message=$( if ! message=$(
printf '%s\n' "$line" | printf '%s\n' "$line" |
xargs -r iptables -t "$table" 2>&1 xargs -r iptables -t "$table" 2>&1
@@ -732,6 +727,7 @@ clearTables() {
if ! rules=$(iptables-save 2> /dev/null); then if ! rules=$(iptables-save 2> /dev/null); then
if enabled "$DEBUG"; then if enabled "$DEBUG"; then
local message
message=$(iptables-save 2>&1 > /dev/null || true) message=$(iptables-save 2>&1 > /dev/null || true)
showTableCleanupError "iptables-save" "$message" showTableCleanupError "iptables-save" "$message"
fi fi
@@ -757,7 +753,7 @@ clearTables() {
hasTaggedRules() { hasTaggedRules() {
local save="$1" local save="$1"
local rules="" local rules
local rule_tag="PROXMOX_NAT" local rule_tag="PROXMOX_NAT"
local own_rule="--comment[[:space:]]+\"?$rule_tag\"?([[:space:]]|\$)" local own_rule="--comment[[:space:]]+\"?$rule_tag\"?([[:space:]]|\$)"
@@ -776,9 +772,8 @@ hasTaggedRules() {
configureTables() { configureTables() {
local subnet="$1" local subnet="$1"
local preferred="" local preferred
local alternate="" local alternate_save
local alternate_save=""
local preferred_clean="N" local preferred_clean="N"
local alternate_dirty="N" local alternate_dirty="N"
@@ -788,8 +783,8 @@ configureTables() {
} }
case "$preferred" in case "$preferred" in
"nft" ) alternate="legacy" ;; "nft" ) local alternate="legacy" ;;
"legacy" ) alternate="nft" ;; "legacy" ) local alternate="nft" ;;
* ) * )
error "unsupported IP tables backend: $preferred" error "unsupported IP tables backend: $preferred"
return 1 ;; return 1 ;;
@@ -950,10 +945,7 @@ configureTables() {
configureNAT() { configureNAT() {
local base="" local base subnet
local rc msg=""
local subnet=""
local gateway=""
local forwarding="" local forwarding=""
local tuntap="TUN device is missing. $ADD_ERR --device /dev/net/tun" local tuntap="TUN device is missing. $ADD_ERR --device /dev/net/tun"
@@ -963,7 +955,8 @@ configureNAT() {
if [ ! -c /dev/net/tun ]; then if [ ! -c /dev/net/tun ]; then
[ ! -d /dev/net ] && mkdir -m 755 /dev/net > /dev/null 2>&1 || : [ ! -d /dev/net ] && mkdir -m 755 /dev/net > /dev/null 2>&1 || :
{ msg=$(mknod /dev/net/tun c 10 200 2>&1); rc=$?; } || : local msg
{ msg=$(mknod /dev/net/tun c 10 200 2>&1); local rc=$?; } || :
if (( rc == 0 )); then if (( rc == 0 )); then
chmod 666 /dev/net/tun chmod 666 /dev/net/tun
@@ -982,7 +975,7 @@ configureNAT() {
forwarding=$(< /proc/sys/net/ipv4/ip_forward) forwarding=$(< /proc/sys/net/ipv4/ip_forward)
if [[ "$forwarding" != "1" ]]; then if [[ "$forwarding" != "1" ]]; then
{ sysctl -w net.ipv4.ip_forward=1 > /dev/null 2>&1; rc=$?; } || : { sysctl -w net.ipv4.ip_forward=1 > /dev/null 2>&1; local rc=$?; } || :
forwarding="" forwarding=""
[ -r /proc/sys/net/ipv4/ip_forward ] && [ -r /proc/sys/net/ipv4/ip_forward ] &&
@@ -995,14 +988,14 @@ configureNAT() {
fi fi
base=$(subnetBase "$UPLINK") || return 1 base=$(subnetBase "$UPLINK") || return 1
gateway="$base.1" local gateway="$base.1"
subnet=$(networkCIDR "$gateway") || return 1 subnet=$(networkCIDR "$gateway") || return 1
if subnetInUse "$subnet"; then if subnetInUse "$subnet"; then
error "VM subnet $subnet conflicts with an existing route inside the container." error "VM subnet $subnet conflicts with an existing route inside the container."
return 1 return 1
else else
rc=$? local rc=$?
(( rc == 1 )) || return 1 (( rc == 1 )) || return 1
fi fi
@@ -1112,8 +1105,7 @@ configureMTU() {
configureMAC() { configureMAC() {
local container="" local container
container=$(containerID) container=$(containerID)
if [ -z "$MAC" ]; then if [ -z "$MAC" ]; then
@@ -1159,8 +1151,7 @@ formatAddress() {
showHostInfo() { showHostInfo() {
local mtu="" host="" uplink="" prefix="" local prefix
prefix=$(ip -4 -o address show dev "$DEV" scope global 2>/dev/null | prefix=$(ip -4 -o address show dev "$DEV" scope global 2>/dev/null |
awk -v ip="$UPLINK" ' awk -v ip="$UPLINK" '
{ {
@@ -1172,11 +1163,13 @@ showHostInfo() {
} }
') ')
local uplink
uplink=$(formatAddress "$UPLINK" "$prefix" || true) uplink=$(formatAddress "$UPLINK" "$prefix" || true)
[ -z "$uplink" ] && uplink="(none)" [ -z "$uplink" ] && uplink="(none)"
local line=" Host: $uplink" local line=" Host: $uplink"
local host
host=$(containerID) host=$(containerID)
[ -n "$host" ] && line+=" ($host)" [ -n "$host" ] && line+=" ($host)"
@@ -1200,6 +1193,7 @@ showHostInfo() {
[ -z "$iface" ] && iface="(none)" [ -z "$iface" ] && iface="(none)"
[[ "$iface" != "eth0" ]] && line+=" | Interface: $iface" [[ "$iface" != "eth0" ]] && line+=" | Interface: $iface"
local mtu
mtu=$(getMTU "$DEV") mtu=$(getMTU "$DEV")
if [ -n "$mtu" ] && [[ "$mtu" != "0" && "$mtu" != "1500" ]]; then if [ -n "$mtu" ] && [[ "$mtu" != "0" && "$mtu" != "1500" ]]; then
line+=" | MTU: $mtu" line+=" | MTU: $mtu"
@@ -1235,22 +1229,19 @@ showBridgeInfo() {
local subnet="$1" local subnet="$1"
local gateway="$2" local gateway="$2"
local mtu=""
local base=""
local dhcp=""
local display=""
local display
display=$(formatAddress "$gateway" "$PREFIX" || true) display=$(formatAddress "$gateway" "$PREFIX" || true)
base="${gateway%.*}" local base="${gateway%.*}"
dhcp="$base.2-$base.254" local dhcp="$base.2-$base.254"
local line=" Bridge: $BRIDGE | Gateway: $display | DHCP: $dhcp" local line=" Bridge: $BRIDGE | Gateway: $display | DHCP: $dhcp"
if [[ "$PREFIX" != "24" ]]; then if [[ "$PREFIX" != "24" ]]; then
line+=" | Subnet: $subnet" line+=" | Subnet: $subnet"
fi fi
local mtu
mtu=$(getMTU "$BRIDGE") mtu=$(getMTU "$BRIDGE")
if [ -n "$mtu" ] && [[ "$mtu" != "0" && "$mtu" != "1500" ]]; then if [ -n "$mtu" ] && [[ "$mtu" != "0" && "$mtu" != "1500" ]]; then
line+=" | MTU: $mtu" line+=" | MTU: $mtu"