Skip to content

An open-source, faster Devin alternative

TypeScript

patch guest network sequentially

Brian Reardonbd850c4da4f0
diff --git a/internal/qemu/manager.go b/internal/qemu/manager.go
index b02de8a8..8e43c3c8 100644
--- a/internal/qemu/manager.go
+++ b/internal/qemu/manager.go
@@ -961,19 +961,40 @@ func (m *Manager) createFromGolden(ctx context.Context, cfg types.SandboxConfig,
// patchGuestNetwork reconfigures the guest's eth0 with the new IP/gateway.
// This is needed because the golden snapshot was booted with a different IP.
+//
+// Each step is independent and idempotent — DNS and /etc/hosts writes always
+// run regardless of whether the network ops succeed. Earlier versions chained
+// every step with `&&`, so a transient `ip addr add` failure (e.g. address
+// already configured) short-circuited the chain and left /etc/hosts un-patched,
+// which surfaces downstream as `sudo: unable to resolve host sandbox` on every
+// sudo call. We verify the final network state at the end and only fail then.
func patchGuestNetwork(ctx context.Context, agent *AgentClient, netCfg *NetworkConfig) error {
// Calculate prefix length from mask (e.g. "255.255.255.252" → 30)
prefixLen := maskToPrefixLen(netCfg.Mask)
- script := fmt.Sprintf(
- "ip addr flush dev eth0 && "+
- "ip addr add %s/%d dev eth0 && "+
- "ip link set eth0 up && "+
- "ip route add default via %s && "+
- "echo 'nameserver 8.8.8.8' > /etc/resolv.conf && "+
- "echo 'nameserver 1.1.1.1' >> /etc/resolv.conf && "+
- "grep -q \"$(hostname)\" /etc/hosts || echo \"127.0.0.1 $(hostname)\" >> /etc/hosts",
+ // `ip route replace` is idempotent — works whether the route exists or not.
+ // `ip addr add` may say "File exists" if the address is already there, which
+ // is a no-op for our purposes; suppress and verify at the end.
+ script := fmt.Sprintf(`set +e
+ip addr flush dev eth0
+ip addr add %s/%d dev eth0 2>/dev/null
+ip link set eth0 up
+ip route replace default via %s
+
+# DNS — always write, independent of network ops
+echo 'nameserver 8.8.8.8' > /etc/resolv.conf
+echo 'nameserver 1.1.1.1' >> /etc/resolv.conf
+
+# /etc/hosts — always ensure entry for current hostname
+grep -q "$(hostname)" /etc/hosts || echo "127.0.0.1 $(hostname)" >> /etc/hosts
+
+# Final verification — only fail if the network didn't reach desired state
+ip addr show eth0 | grep -q "%s" || exit 1
+ip route show default | grep -q "%s" || exit 2
+exit 0
+`,
netCfg.GuestIP, prefixLen, netCfg.HostIP,
+ netCfg.GuestIP, netCfg.HostIP,
)
execCtx, cancel := context.WithTimeout(ctx, 5*time.Second)