You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/engine/virsh_panel/shell_contracts.py

377 lines
14 KiB
Python

"""Shared shell fragments for remote host reconciliation scripts."""
LOCAL_NEIGHBOR_SH = r'''
# The bridge has no IPv4 address from which the kernel can issue useful ARP
# requests. Its local guest next hops therefore use exact, durable bindings.
# These helpers never flush neighbors or overwrite another MAC's binding.
vc_local_neighbor_pairs() {
awk -F'|' '
function ipv4(v,a,n,i) {
n=split(v,a,"."); if (n!=4) return 0
for(i=1;i<=4;i++) if(a[i]!~/^[0-9]+$/ || a[i]>255 ||
sprintf("%d",a[i])!=a[i]) return 0
return a[1]==10 || (a[1]==172 && a[2]>=16 && a[2]<=31) ||
(a[1]==192 && a[2]==168)
}
function mac(v,a,n,i) {
n=split(v,a,":"); if(n!=6 || v=="00:00:00:00:00:00") return 0
for(i=1;i<=6;i++) if(length(a[i])!=2 || a[i]!~/^[0-9a-f]+$/) return 0
return substr(a[1],2,1)~/^[02468ace]$/
}
NF==0 {next}
{if(NF!=2 || !ipv4($1) || !mac($2) || ips[$1]++ || macs[$2]++) bad=1
else print $1 "|" $2}
END {exit bad ? 1 : 0}
'
}
vc_local_neighbor_expected() (
[ "$#" -eq 1 ] && [ -f "$1" ] && [ ! -L "$1" ] || exit 1
vc_ln_expected=$(awk -F'|' '
$1=="local_neighbor" {if(NF!=3) bad=1; else print $2 "|" $3}
END {exit bad ? 1 : 0}
' "$1") || exit 1
vc_ln_expected=$(printf '%s\n' "$vc_ln_expected" |
vc_local_neighbor_pairs) || exit 1
[ -z "$vc_ln_expected" ] || printf '%s\n' "$vc_ln_expected" | LC_ALL=C sort
)
vc_local_neighbor_records() (
[ "$#" -eq 1 ] || exit 1
case "$1" in ''|*[!A-Za-z0-9_.-]*) exit 1 ;; esac
[ "${#1}" -le 15 ] || exit 1
vc_ln_raw=$("$IP" -o -4 neigh show dev "$1" 2>/dev/null) || exit 1
vc_ln_records=$(printf '%s\n' "$vc_ln_raw" | awk -v bridge="$1" '
function ipv4(v,a,n,i) {
n=split(v,a,"."); if(n!=4) return 0
for(i=1;i<=4;i++) if(a[i]!~/^[0-9]+$/ || a[i]>255 ||
sprintf("%d",a[i])!=a[i]) return 0
return 1
}
function mac(v,a,n,i) {
n=split(v,a,":"); if(n!=6 || v=="00:00:00:00:00:00") return 0
for(i=1;i<=6;i++) if(length(a[i])!=2 || a[i]!~/^[0-9a-f]+$/) return 0
return substr(a[1],2,1)~/^[02468ace]$/
}
NF==0 {next}
{
if(!ipv4($1)) {bad=1; next}
split($1,octets,".")
if((octets[1]>=224 && octets[1]<=239) || $1=="255.255.255.255") next
ip=$1; address="-"; state=""; dev=0; lladdr=0
for(i=2;i<=NF;i++) {
if($i=="dev" && i<NF) {if(dev++ || $(++i)!=bridge) bad=1}
else if($i=="lladdr" && i<NF) {
address=tolower($(++i)); if(lladdr++ || !mac(address)) bad=1
} else if($i~/^(PERMANENT|REACHABLE|STALE|DELAY|PROBE|INCOMPLETE|FAILED|NOARP|NONE)$/) {
if(state!="") bad=1; state=$i
} else bad=1
}
if(state=="" || seen[ip]++ ||
(address=="-" && state!~/^(INCOMPLETE|FAILED|NONE)$/)) bad=1
print ip "|" address "|" state
}
END {exit bad ? 1 : 0}
') || exit 1
[ -z "$vc_ln_records" ] || printf '%s\n' "$vc_ln_records" | LC_ALL=C sort
)
vc_local_neighbor_preflight() (
[ "$#" -eq 2 ] || exit 1
vc_ln_target=$(printf '%s\n' "$2" | vc_local_neighbor_pairs) || exit 1
vc_ln_current=$(vc_local_neighbor_records "$1") || exit 1
printf '%s\n' "$vc_ln_current" | awk -F'|' -v expected="$vc_ln_target" '
BEGIN {n=split(expected,rows,"\n"); for(i=1;i<=n;i++) {
if(rows[i]=="") continue; split(rows[i],p,"|"); wanted[p[1]]=p[2]
}}
NF==0 {next}
{if(NF!=3 || !($1 in wanted) || ($2!="-" && $2!=wanted[$1]) ||
($2=="-" && $3!~/^(INCOMPLETE|FAILED|NONE)$/)) bad=1}
END {exit bad ? 1 : 0}
'
)
vc_local_neighbor_ensure() (
[ "$#" -eq 2 ] || exit 1
vc_ln_bridge=$1
vc_ln_target=$(printf '%s\n' "$2" | vc_local_neighbor_pairs) || exit 1
vc_ln_target=$(printf '%s\n' "$vc_ln_target" | sed '/^$/d' | LC_ALL=C sort) || exit 1
vc_local_neighbor_preflight "$vc_ln_bridge" "$vc_ln_target" || exit 1
while IFS='|' read -r vc_ln_ip vc_ln_mac; do
[ -n "$vc_ln_ip" ] || continue
"$IP" -4 neigh replace "$vc_ln_ip" lladdr "$vc_ln_mac" nud permanent \
dev "$vc_ln_bridge" || exit 1
done <<EOF
$vc_ln_target
EOF
vc_ln_after=$(vc_local_neighbor_records "$vc_ln_bridge") || exit 1
vc_ln_wanted=$(printf '%s\n' "$vc_ln_target" |
awk 'NF {print $0 "|PERMANENT"}') || exit 1
[ "$vc_ln_after" = "$vc_ln_wanted" ]
)
'''.strip()
VPC_SYSCTL_VALIDATION_SH = r'''
vc_vpc_sysctl_artifact_owned() {
vc_sysctl_path=$1
vc_sysctl_vpc=$2
vc_sysctl_revision=$3
vc_sysctl_digest=$4
vc_sysctl_bridge=$5
case "$vc_sysctl_revision" in ''|*[!0-9]*) return 1 ;; esac
[ "$vc_sysctl_revision" -ge 1 ] || return 1
printf '%s\n' "$vc_sysctl_digest" | grep -Eq '^[0-9a-f]{64}$' || return 1
[ -f "$vc_sysctl_path" ] && [ ! -L "$vc_sysctl_path" ] || return 1
[ "$($STAT -c '%u:%a' "$vc_sysctl_path" 2>/dev/null)" = 0:600 ] || return 1
awk -v identity="# vpc=$vc_sysctl_vpc revision=$vc_sysctl_revision digest=$vc_sysctl_digest" \
-v bridge="$vc_sysctl_bridge" '
NR==1 && $0=="# virt-cloud-vpc-dataplane-v1" {owner++; next}
NR==2 && $0==identity {identity_seen++; next}
NR==3 && $0=="net.ipv4.conf." bridge ".rp_filter = 2" {rp++; next}
NR==4 && $0=="net.ipv4.conf." bridge ".proxy_arp = 0" {proxy++; next}
NR==5 && $0=="net.ipv4.conf." bridge ".proxy_arp_pvlan = 1" {pvlan++; next}
{bad=1}
END {
exit (!bad && NR==5 && owner==1 && identity_seen==1 && rp==1 &&
proxy==1 && pvlan==1) ? 0 : 1
}
' "$vc_sysctl_path"
}
vc_vpc_sysctl_artifacts_equal() {
vc_sysctl_live=$1
vc_sysctl_desired=$2
vc_sysctl_vpc=$3
vc_sysctl_revision=$4
vc_sysctl_digest=$5
vc_sysctl_bridge=$6
[ -n "${CMP:-}" ] || return 1
vc_vpc_sysctl_artifact_owned "$vc_sysctl_live" "$vc_sysctl_vpc" \
"$vc_sysctl_revision" "$vc_sysctl_digest" "$vc_sysctl_bridge" || return 1
vc_vpc_sysctl_artifact_owned "$vc_sysctl_desired" "$vc_sysctl_vpc" \
"$vc_sysctl_revision" "$vc_sysctl_digest" "$vc_sysctl_bridge" || return 1
"$CMP" -s "$vc_sysctl_live" "$vc_sysctl_desired"
}
'''.strip()
# ``ip rule`` has no ``replace`` operation. Policy rules therefore use a
# two-phase contract: callers preflight every priority before mutating any of
# them, then add only missing rules and verify the exact readback. Existing
# matching rules are left untouched, so an idempotent replay has no routing
# gap. The caller supplies separate validation and command-failure exit codes
# because VPC apply and boot repair expose different error classes.
IP_RULE_CONVERGENCE_SH = r'''vc_ip_rule_read() {
vc_rule_pref=$1
vc_rule_iif=$2
vc_rule_to=$3
vc_rule_table=$4
vc_rule_invalid=$5
vc_rule_command_failed=$6
if ! VC_IP_RULE_EXISTING=$("$IP" -o -4 -N rule show pref "$vc_rule_pref" 2>/dev/null); then
return "$vc_rule_command_failed"
fi
if [ -z "$VC_IP_RULE_EXISTING" ]; then
VC_IP_RULE_PRESENT=no
return 0
fi
vc_rule_count=$(printf '%s\n' "$VC_IP_RULE_EXISTING" | awk 'NF {count++} END {print count + 0}')
[ "$vc_rule_count" = 1 ] || return "$vc_rule_invalid"
if [ "$vc_rule_iif" = - ]; then
vc_rule_expected="$vc_rule_pref: from all to $vc_rule_to lookup $vc_rule_table"
else
vc_rule_expected="$vc_rule_pref: from all to $vc_rule_to iif $vc_rule_iif lookup $vc_rule_table"
fi
vc_rule_normalized=$(printf '%s\n' "$VC_IP_RULE_EXISTING" | awk '{$1=$1; print}')
[ "$vc_rule_normalized" = "$vc_rule_expected" ] || return "$vc_rule_invalid"
VC_IP_RULE_PRESENT=yes
return 0
}
vc_ip_rule_preflight() {
vc_rule_snapshot=$7
vc_ip_rule_read "$1" "$2" "$3" "$4" "$5" "$6" || return $?
if [ "$VC_IP_RULE_PRESENT" = yes ] && [ "$vc_rule_snapshot" != - ]; then
printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4" >> "$vc_rule_snapshot" || return "$6"
fi
return 0
}
vc_ip_rule_scope_check() {
vc_rule_scope_to=$1
vc_rule_scope_table=$2
vc_rule_scope_base_pref=$3
vc_rule_scope_iif=$4
vc_rule_scope_mode=$5
vc_rule_scope_invalid=$6
vc_rule_scope_command_failed=$7
case "$vc_rule_scope_mode" in
managed|empty) : ;;
*) return "$vc_rule_scope_invalid" ;;
esac
if ! VC_IP_RULE_SCOPE=$("$IP" -o -4 -N rule show 2>/dev/null); then
return "$vc_rule_scope_command_failed"
fi
if ! printf '%s\n' "$VC_IP_RULE_SCOPE" | awk \
-v expected_to="$vc_rule_scope_to" \
-v expected_table="$vc_rule_scope_table" \
-v base_pref="$vc_rule_scope_base_pref" \
-v expected_iif="$vc_rule_scope_iif" \
-v mode="$vc_rule_scope_mode" '
NF {
$1=$1
normalized=$0
destination=""
table=""
for (i=1; i<=NF; i++) {
if ($i == "to" && i < NF) destination=$(i+1)
if ($i == "lookup" && i < NF) table=$(i+1)
}
if (destination != expected_to && table != expected_table) next
if (mode == "empty") { bad=1; exit }
expected_base=base_pref ": from all to " expected_to \
" lookup " expected_table
expected_fabric=(base_pref + 1) ": from all to " expected_to \
" iif " expected_iif " lookup " expected_table
if (normalized == expected_base) {
if (++seen_base > 1) { bad=1; exit }
next
}
if (normalized == expected_fabric) {
if (++seen_fabric > 1) { bad=1; exit }
next
}
bad=1
exit
}
END {exit bad ? 1 : 0}
'; then
return "$vc_rule_scope_invalid"
fi
return 0
}
vc_ip_rule_ensure() {
vc_rule_attempted=$7
vc_rule_created=$8
vc_ip_rule_read "$1" "$2" "$3" "$4" "$5" "$6" || return $?
if [ "$VC_IP_RULE_PRESENT" = no ]; then
if [ "$vc_rule_attempted" != - ]; then
printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4" >> "$vc_rule_attempted" || return "$6"
fi
if [ "$2" = - ]; then
"$IP" -4 rule add pref "$1" to "$3" lookup "$4" || return "$6"
else
"$IP" -4 rule add pref "$1" iif "$2" to "$3" lookup "$4" || return "$6"
fi
vc_ip_rule_read "$1" "$2" "$3" "$4" "$5" "$6" || return $?
[ "$VC_IP_RULE_PRESENT" = yes ] || return "$5"
if [ "$vc_rule_created" != - ]; then
printf '%s|%s|%s|%s\n' "$1" "$2" "$3" "$4" >> "$vc_rule_created" || return "$6"
fi
fi
return 0
}
'''
BRIDGE_PORT_RECORDS_SH = r'''
# Query generic link membership. `bridge link show` may include hardware
# offload/VEB records for unrelated NICs on an otherwise empty bridge.
vc_bridge_port_records() {
vc_port_record_bridge=$1
case "$vc_port_record_bridge" in ''|*[!A-Za-z0-9_.-]*) return 1 ;; esac
[ "${#vc_port_record_bridge}" -le 15 ] || return 1
vc_port_records_raw=$("$IP" -o link show master "$vc_port_record_bridge" 2>/dev/null) || return 1
[ "${#vc_port_records_raw}" -le 262144 ] || return 1
printf '%s\n' "$vc_port_records_raw" | awk -v wanted="$vc_port_record_bridge" '
NF == 0 {next}
NF < 5 || $1 !~ /^[1-9][0-9]*:$/ || $3 !~ /^<[^>]+>$/ {bad=1; next}
{
raw=$2
if (raw !~ /^[A-Za-z0-9_.-]+(@[A-Za-z0-9_.:-]+)?:$/) {
bad=1; next
}
sub(/:$/, "", raw)
sub(/@.*/, "", raw)
if (length(raw) < 1 || length(raw) > 15 || seen[raw]++) {
bad=1; next
}
master_count=0
for (i=4; i<=NF; i++) {
if ($i=="master") {
master_count++
if (i==NF || $(i+1)!=wanted) bad=1
}
}
if (master_count!=1 || ++count>4096) {bad=1; next}
records[count]=$0
}
END {
if (bad) exit 1
for (i=1; i<=count; i++) print records[i]
}
'
}
vc_bridge_ifindex() {
vc_identity_bridge=$1
case "$vc_identity_bridge" in ''|*[!A-Za-z0-9_.-]*) return 1 ;; esac
[ "${#vc_identity_bridge}" -le 15 ] || return 1
vc_identity_raw=$("$IP" -o link show dev "$vc_identity_bridge" 2>/dev/null) || return 1
[ "${#vc_identity_raw}" -le 262144 ] || return 1
printf '%s\n' "$vc_identity_raw" | awk -v wanted="$vc_identity_bridge" '
NF == 0 {next}
{
count++
value=$1; sub(/:$/, "", value)
name=$2; sub(/:$/, "", name); sub(/@.*/, "", name)
if ($1 !~ /^[1-9][0-9]*:$/ || value+0>2147483647 ||
name!=wanted || $3 !~ /^<[^>]+>$/) bad=1
}
END {if (bad || count!=1) exit 1; print value}
'
}
'''.strip()
WG_ALLOWED_IPS_SH = r'''vc_wg_allowed_for_peer() (
[ "$#" = 2 ] || exit 1
vc_wg_rows=$("$WG" show "$1" allowed-ips 2>/dev/null) || exit 1
vc_wg_size=$(printf '%s' "$vc_wg_rows" | wc -c | tr -d '[:space:]') || exit 1
[ "$vc_wg_size" -le 262144 ] 2>/dev/null || exit 1
# wg show uses whitespace between prefixes; wg set/showconf use commas.
# Read the complete selected row and emit one unambiguous CSV record.
printf '%s\n' "$vc_wg_rows" | awk -v wanted="$2" '
function ipv4_prefix(value, parts, octets, pos, unit, address) {
if (split(value, parts, "/")!=2 || parts[2] !~ /^(0|[1-9][0-9]*)$/ || parts[2]>32 ||
split(parts[1], octets, ".")!=4) return 0
for (pos=1; pos<=4; pos++)
if (octets[pos] !~ /^(0|[1-9][0-9]*)$/ || octets[pos]>255) return 0
address=(octets[1]*256+octets[2])*65536+octets[3]*256+octets[4]
unit=1
for (pos=0; pos<32-parts[2]; pos++) unit*=2
return address % unit == 0
}
BEGIN {
if (length(wanted)!=44 || wanted !~ /^[A-Za-z0-9+\/]+=$/) bad=1
}
$1==wanted {
rows++
line=$0
sub(/^[[:space:]]*/, "", line)
sub(/^[^[:space:]]+[[:space:]]*/, "", line)
sub(/[[:space:]]*$/, "", line)
if (line=="" || line ~ /(^|,)[[:space:]]*(,|$)/) bad=1
gsub(/,/, " ", line)
count=split(line, values, /[[:space:]]+/)
if (count<1 || count>16384) bad=1
for (i=1; i<=count; i++) {
if (!ipv4_prefix(values[i]) || seen[values[i]]++) bad=1
result=result (i==1 ? "" : ",") values[i]
}
}
END {
if (bad || rows!=1) exit 1
print result
}
'
)
'''.strip()