Skip to content

feat(vm): cocoon vm clone --nics N to override snapshot NIC count#43

Merged
CMGS merged 2 commits into
masterfrom
feat/vm-net-zero-recover
May 12, 2026
Merged

feat(vm): cocoon vm clone --nics N to override snapshot NIC count#43
CMGS merged 2 commits into
masterfrom
feat/vm-net-zero-recover

Conversation

@CMGS
Copy link
Copy Markdown
Contributor

@CMGS CMGS commented May 12, 2026

Summary

Adds --nics N flag to cocoon vm clone to override the NIC count inherited from the snapshot at clone time. -1 (default) keeps inheritance behavior.

Why

The new cocoon vm net (PR #42) allows runtime NIC resize on running VMs — but only when CH was started with at least 1 NIC. CH process's netns is decided at boot:

// hypervisor/cloudhypervisor/start.go
if withNetwork && rec.NetworkConfigs[0].NetnsPath != "" {
    netnsPath = rec.NetworkConfigs[0].NetnsPath
}

A VM resized to 0 NICs → snapshot'd → cloned ends up with CH in host netns because NetworkConfigs is empty. A later vm net --nics 1 creates per-VM-netns plumbing CH cannot reach (it ends up making a phantom TAP in host netns, no traffic flows).

Linux + CH doesn't allow changing another process's netns from outside, so we can't fix it at vm net time without breaking the netns boundary (persisting BridgeDev, adding NetnsPath to VMRecord, etc.).

This PR sidesteps the problem cleanly: at clone time the user picks the NIC count. If they want to recover networking after a zero-NIC snapshot, clone --nics 1 puts the new clone on the standard "started with N NICs" path, CH lands in the per-VM netns at boot, and vm net resize later works as designed.

Also updates the vm net zero-NIC error to point users at this new flag.

Tested

  • cocoon vm clone --nics 1 SNAPSHOT_WITH_0_NICS on a Linux OCI ubuntu produces a clone with the expected per-VM netns + working DHCP.
  • cocoon vm clone --nics 0 on a snapshot with NICs strips them at clone (intentional zero start; can't resize up later, consistent with vm run --nics 0).
  • cocoon vm clone SNAPSHOT (no --nics) preserves snapshot's NIC count (unchanged behavior).
  • make lint + go test ./... green.

Test plan

  • Lint + tests
  • Manual: clone zero-NIC snapshot with --nics 1 and verify DHCP
  • Manual: clone with --nics 0 and verify VM has no networking
  • Manual: clone without --nics inherits as before

🤖 Generated with Claude Code

@CMGS CMGS force-pushed the feat/vm-net-zero-recover branch from a79b0f5 to cdc5ccc Compare May 12, 2026 14:30
@CMGS CMGS changed the title feat(vm): --network/--bridge for zero-NIC resize-up feat(vm): cocoon vm clone --nics N to override snapshot NIC count May 12, 2026
clone has always inherited NIC count from the snapshot. With the new
runtime net resize, a VM resized down to 0 NICs then snapshot'd produces
a clone that can't recover networking later: CH started in host netns
because NetworkConfigs was empty, so a subsequent vm net adds NICs in
the per-VM netns CH never entered.

Mirror cocoon vm run's --nics: passing --nics N at clone time forces
N NICs at clone start, putting CH into the per-VM netns from the start
and keeping the rest of the lifecycle aligned with --nics ≥ 1 path.
-1 (default) preserves inheritance from snapshot.

Adjust the vm net error to point users at the clone override when they
hit the zero-NIC resize-up wall.
@CMGS CMGS force-pushed the feat/vm-net-zero-recover branch from cdc5ccc to 4dbc79d Compare May 12, 2026 15:16
@CMGS CMGS merged commit 39f52a2 into master May 12, 2026
4 checks passed
@CMGS CMGS deleted the feat/vm-net-zero-recover branch May 12, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant