@kvbox/server 0.6.25 → 0.6.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kvbox/server",
3
- "version": "0.6.25",
3
+ "version": "0.6.26",
4
4
  "description": "kvbox host daemon: owns Firecracker microVMs on a Linux host and serves the kvbox API. Managed with the @kvbox/cli CLI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -47,6 +47,8 @@
47
47
  #
48
48
  # KVBOX_BRIDGE (default kvbox0) KVBOX_SUBNET (default 172.30.0.0/24)
49
49
  # KVBOX_GATEWAY (default 172.30.0.1) the host's address on the bridge
50
+ # A host with a box range of its own (#240) passes the wide network, 172.30.0.0/16: `up` then widens a
51
+ # bridge that is up, under its boxes.
50
52
  set -euo pipefail
51
53
 
52
54
  BRIDGE="${KVBOX_BRIDGE:-kvbox0}"
@@ -200,6 +202,24 @@ bridge_has_ip() { ip -4 -o addr show dev "$BRIDGE" 2>/dev/null | grep -q " inet
200
202
  # Administrative UP flag only: an empty bridge reports operstate "down" (NO-CARRIER) until a tap joins.
201
203
  bridge_is_up() { ip link show "$BRIDGE" 2>/dev/null | head -1 | grep -q "[<,]UP[,>]"; }
202
204
 
205
+ # A network that was narrower before (#240: a host given a range of its own widens to the network every box
206
+ # sees, under its running boxes): the bridge's address with the old, longer mask and the NAT rule of the old
207
+ # range go, once the new ones are in place (a NAT rule of another range as wide goes too). Never a wider one: a run with a narrower config than the bridge
208
+ # has (one that raced the widening) never takes the wide network away. Nothing to do on one that never changed.
209
+ drop_old_network() {
210
+ local cidr rule src
211
+ while read -r _ _ _ cidr _; do
212
+ [[ "$cidr" == "$GATEWAY/"* && "${cidr#*/}" -gt "$PREFIX" ]] || continue
213
+ ip addr del "$cidr" dev "$BRIDGE" && ok "took this host's old address $cidr off $BRIDGE (boxes are now in $SUBNET)"
214
+ done < <(ip -4 -o addr show dev "$BRIDGE" 2>/dev/null)
215
+ while IFS= read -r rule; do
216
+ src="$(sed -n 's/.*-s \([0-9.]*\/[0-9]*\) .*/\1/p' <<<"$rule")"
217
+ [[ -n "$src" && "$src" != "$SUBNET" && "${src#*/}" -ge "$PREFIX" ]] || continue
218
+ # shellcheck disable=SC2086 # iptables-save wrote the rule as a word list
219
+ eval "iptables -t nat -D $rule" && ok "removed the NAT rule of the boxes' old range"
220
+ done < <(iptables-save -t nat 2>/dev/null | sed -n 's/^-A //p' | sed 's/--comment "\([^"]*\)"/--comment \1/' | grep -F -- "--comment $TAG " | grep -F -- "-j MASQUERADE" || true)
221
+ }
222
+
203
223
  # ── up ───────────────────────────────────────────────────────────────────────
204
224
  do_up() {
205
225
  if bridge_exists; then skip "bridge $BRIDGE already exists"; else ip link add "$BRIDGE" type bridge; ok "created bridge $BRIDGE (the virtual switch boxes plug into)"; fi
@@ -210,22 +230,34 @@ do_up() {
210
230
  # The base rules jump to them, so they must exist before the rules are checked.
211
231
  iptables -t filter -N "$LINKS" 2>/dev/null || true
212
232
  iptables -t filter -N "$CLUSTERS" 2>/dev/null || true
213
- local i missing=0 present=()
233
+ local i missing=0 present=() spec restore
214
234
  for i in "${!RULE_SPEC[@]}"; do
215
235
  # shellcheck disable=SC2086 # the rule is a word list by design
216
- if has_rule ${RULE_SPEC[$i]}; then present[i]=1; else missing=1; fi
236
+ if has_rule ${RULE_SPEC[$i]}; then present[i]=1; elif [[ "${RULE_SPEC[$i]}" == filter\ * ]]; then missing=1; fi
217
237
  done
218
- # Order matters (accepts above drops), and a rule inserted alone would land on top of the others:
219
- # when anything is missing, take the set out and put it all back in order.
238
+ # Order matters among the filter rules (accepts above drops), and a rule inserted alone would land on top
239
+ # of the others: when one is missing, the set is taken out and put back in order in one iptables-restore,
240
+ # so there is never a moment without the drops while boxes run (#240 widens under running boxes). The NAT
241
+ # rules do not order against the filter guards: a missing one (a widened subnet's MASQUERADE) is added alone.
220
242
  if [[ $missing -eq 1 ]]; then
221
- # shellcheck disable=SC2086
222
- for i in "${!RULE_SPEC[@]}"; do del_rule ${RULE_SPEC[$i]}; done
243
+ restore="*filter"$'\n'
244
+ for i in "${!RULE_SPEC[@]}"; do
245
+ read -r -a spec <<<"${RULE_SPEC[$i]}"
246
+ [[ "${spec[0]}" == filter && -n "${present[i]:-}" ]] && restore+="-D ${spec[*]:1}"$'\n'
247
+ done
248
+ for i in "${!RULE_SPEC[@]}"; do
249
+ read -r -a spec <<<"${RULE_SPEC[$i]}"
250
+ [[ "${spec[0]}" == filter ]] && restore+="-I ${spec[1]} 1 ${spec[*]:2}"$'\n'
251
+ done
252
+ restore+="COMMIT"$'\n'
253
+ printf '%s' "$restore" | iptables-restore --noflush
223
254
  fi
224
255
  for i in "${!RULE_SPEC[@]}"; do
225
256
  # shellcheck disable=SC2086
226
- [[ $missing -eq 1 ]] && add_rule ${RULE_SPEC[$i]}
257
+ [[ -z "${present[i]:-}" && "${RULE_SPEC[$i]}" == nat\ * ]] && add_rule ${RULE_SPEC[$i]}
227
258
  if [[ -n "${present[i]:-}" ]]; then skip "${RULE_DESC[$i]} (already set)"; else ok "${RULE_DESC[$i]}"; fi
228
259
  done
260
+ drop_old_network
229
261
  local tap
230
262
  for tap in $(taps_on_bridge); do isolate_tap "$tap"; done
231
263
  echo