Skip to content

An open-source, faster Devin alternative

TypeScript

fix env vars vmm

motatoesf11f3448d7e6
diff --git a/internal/firecracker/manager.go b/internal/firecracker/manager.go
index fde046f7..e04f598b 100644
--- a/internal/firecracker/manager.go
+++ b/internal/firecracker/manager.go
@@ -36,6 +36,7 @@ type VMInstance struct {
MemoryMB int
HostPort int
GuestPort int
+ Envs map[string]string // sandbox-level environment variables
// VM internals
pid int // Firecracker VMM process PID
@@ -56,11 +57,12 @@ type VMInstance struct {
// It records VM config so that on hard kill recovery, a sandbox can be
// cold-booted from template + existing workspace without needing DB access.
type SandboxMeta struct {
- SandboxID string `json:"sandboxId"`
- Template string `json:"template"`
- CpuCount int `json:"cpuCount"`
- MemoryMB int `json:"memoryMB"`
- GuestPort int `json:"guestPort"`
+ SandboxID string `json:"sandboxId"`
+ Template string `json:"template"`
+ CpuCount int `json:"cpuCount"`
+ MemoryMB int `json:"memoryMB"`
+ GuestPort int `json:"guestPort"`
+ Envs map[string]string `json:"envs,omitempty"`
}
// Config holds configuration for the Firecracker Manager.
@@ -377,6 +379,7 @@ func (m *Manager) createWithID(ctx context.Context, id string, cfg types.Sandbox
MemoryMB: memMB,
HostPort: hostPort,
GuestPort: guestPort,
+ Envs: cfg.Envs,
pid: cmd.Process.Pid,
cmd: cmd,
network: netCfg,
@@ -412,6 +415,7 @@ func (m *Manager) createWithID(ctx context.Context, id string, cfg types.Sandbox
CpuCount: cpus,
MemoryMB: memMB,
GuestPort: guestPort,
+ Envs: cfg.Envs,
}
if metaJSON, err := json.Marshal(sbMeta); err == nil {
_ = os.WriteFile(filepath.Join(sandboxDir, "sandbox-meta.json"), metaJSON, 0644)
@@ -669,10 +673,19 @@ func (m *Manager) Exec(ctx context.Context, sandboxID string, cfg types.ProcessC
command = "/bin/sh"
}
+ // Merge sandbox-level envs with per-command envs (per-command takes precedence)
+ envs := make(map[string]string, len(vm.Envs)+len(cfg.Env))
+ for k, v := range vm.Envs {
+ envs[k] = v
+ }
+ for k, v := range cfg.Env {
+ envs[k] = v
+ }
+
resp, err := vm.agent.Exec(ctx, &pb.ExecRequest{
Command: command,
Args: args,
- Envs: cfg.Env,
+ Envs: envs,
Cwd: cfg.Cwd,
TimeoutSeconds: timeout,
})
@@ -1053,6 +1066,7 @@ func (m *Manager) RecoverLocalSandboxes() []LocalRecovery {
CpuCount: snapMeta.CpuCount,
MemoryMB: snapMeta.MemoryMB,
GuestPort: snapMeta.GuestPort,
+ Envs: snapMeta.Envs,
},
})
continue
diff --git a/internal/firecracker/snapshot.go b/internal/firecracker/snapshot.go
index a362fac5..d7b8c010 100644
--- a/internal/firecracker/snapshot.go
+++ b/internal/firecracker/snapshot.go
@@ -25,18 +25,19 @@ import (
// SnapshotMeta holds metadata persisted alongside snapshot files.
// This is needed to restore the exact same VM configuration on wake.
type SnapshotMeta struct {
- SandboxID string `json:"sandboxId"`
- Network *NetworkConfig `json:"network"`
- GuestCID uint32 `json:"guestCID"`
- GuestMAC string `json:"guestMAC"`
- BootArgs string `json:"bootArgs"`
- RootfsPath string `json:"rootfsPath"`
- WorkspacePath string `json:"workspacePath"`
- VsockPath string `json:"vsockPath"`
- CpuCount int `json:"cpuCount"`
- MemoryMB int `json:"memoryMB"`
- Template string `json:"template"`
- GuestPort int `json:"guestPort"`
+ SandboxID string `json:"sandboxId"`
+ Network *NetworkConfig `json:"network"`
+ GuestCID uint32 `json:"guestCID"`
+ GuestMAC string `json:"guestMAC"`
+ BootArgs string `json:"bootArgs"`
+ RootfsPath string `json:"rootfsPath"`
+ WorkspacePath string `json:"workspacePath"`
+ VsockPath string `json:"vsockPath"`
+ CpuCount int `json:"cpuCount"`
+ MemoryMB int `json:"memoryMB"`
+ Template string `json:"template"`
+ GuestPort int `json:"guestPort"`
+ Envs map[string]string `json:"envs,omitempty"`
}
// doHibernate pauses a running VM, creates a full memory snapshot, and kicks off
@@ -122,6 +123,7 @@ func (m *Manager) doHibernate(ctx context.Context, vm *VMInstance, checkpointSto
MemoryMB: vm.MemoryMB,
Template: vm.Template,
GuestPort: vm.GuestPort,
+ Envs: vm.Envs,
}
metaJSON, err := json.Marshal(meta)
if err != nil {
@@ -463,6 +465,7 @@ func (m *Manager) doWake(ctx context.Context, sandboxID, checkpointKey string, c
MemoryMB: meta.MemoryMB,
HostPort: hostPort,
GuestPort: netCfg.GuestPort,
+ Envs: meta.Envs,
pid: cmd.Process.Pid,
cmd: cmd,
network: netCfg,
@@ -658,6 +661,7 @@ func (m *Manager) coldBootLocal(ctx context.Context, sandboxID string, timeout i
MemoryMB: memMB,
HostPort: hostPort,
GuestPort: guestPort,
+ Envs: meta.Envs,
pid: cmd.Process.Pid,
cmd: cmd,
network: netCfg,
@@ -1047,6 +1051,7 @@ func (m *Manager) CreateCheckpoint(ctx context.Context, sandboxID, checkpointID
MemoryMB: vm.MemoryMB,
Template: vm.Template,
GuestPort: vm.GuestPort,
+ Envs: vm.Envs,
}
metaJSON, _ := json.MarshalIndent(meta, "", " ")
if writeErr := os.WriteFile(filepath.Join(snapshotDir, "snapshot-meta.json"), metaJSON, 0644); writeErr != nil {
@@ -1304,6 +1309,7 @@ func (m *Manager) warmRestoreFromCheckpoint(ctx context.Context, vm *VMInstance,
MemoryMB: meta.MemoryMB,
HostPort: hostPort,
GuestPort: netCfg.GuestPort,
+ Envs: meta.Envs,
pid: cmd.Process.Pid,
cmd: cmd,
network: netCfg,
@@ -1366,11 +1372,13 @@ func (m *Manager) coldBootRestoreFromCheckpoint(ctx context.Context, vm *VMInsta
cpuCount := 0
memoryMB := 0
guestPort := 0
+ var envs map[string]string
if vm != nil {
template = vm.Template
cpuCount = vm.CpuCount
memoryMB = vm.MemoryMB
guestPort = vm.GuestPort
+ envs = vm.Envs
}
cfg := types.SandboxConfig{
@@ -1378,6 +1386,7 @@ func (m *Manager) coldBootRestoreFromCheckpoint(ctx context.Context, vm *VMInsta
CpuCount: cpuCount,
MemoryMB: memoryMB,
Port: guestPort,
+ Envs: envs,
NetworkEnabled: true,
TemplateRootfsKey: "local://" + cachedRootfs,
TemplateWorkspaceKey: "local://" + cachedWorkspace,
@@ -1805,6 +1814,7 @@ func (m *Manager) warmForkFromCheckpoint(ctx context.Context, checkpointID strin
CpuCount: meta.CpuCount,
MemoryMB: meta.MemoryMB,
GuestPort: netCfg.GuestPort,
+ Envs: cfg.Envs,
}
sbMetaJSON, _ := json.Marshal(sbMeta)
_ = os.WriteFile(filepath.Join(sandboxDir, "sandbox-meta.json"), sbMetaJSON, 0644)
@@ -1826,6 +1836,7 @@ func (m *Manager) warmForkFromCheckpoint(ctx context.Context, checkpointID strin
MemoryMB: meta.MemoryMB,
HostPort: hostPort,
GuestPort: netCfg.GuestPort,
+ Envs: cfg.Envs,
pid: cmd.Process.Pid,
cmd: cmd,
network: netCfg,
@@ -2267,6 +2278,7 @@ func (m *Manager) createFromGoldenSnapshot(ctx context.Context, id string, cfg t
CpuCount: cpus,
MemoryMB: memMB,
GuestPort: guestPort,
+ Envs: cfg.Envs,
}
sbMetaJSON, _ := json.Marshal(sbMeta)
_ = os.WriteFile(filepath.Join(sandboxDir, "sandbox-meta.json"), sbMetaJSON, 0644)
@@ -2288,6 +2300,7 @@ func (m *Manager) createFromGoldenSnapshot(ctx context.Context, id string, cfg t
MemoryMB: memMB,
HostPort: hostPort,
GuestPort: guestPort,
+ Envs: cfg.Envs,
pid: cmd.Process.Pid,
cmd: cmd,
network: netCfg,