Lifecycle Package Reference
pkg/lifecycle/— VM lifecycle management, Firecracker API communication.
Overview
Section titled “Overview”The pkg/lifecycle package manages Firecracker VM lifecycle operations: start, stop, pause, resume, and state tracking.
Package Structure:
pkg/lifecycle/├── vmm.go # VMMManager and VMInstance├── mocks_test.go # Test mocksVMMManager
Section titled “VMMManager”File: vmm.go
The VMMManager manages Firecracker VM processes and API communication.
Type Definition
Section titled “Type Definition”type VMMManager struct { config *ManagerConfig vms map[string]*VMInstance mu sync.RWMutex socketDir string}Configuration
Section titled “Configuration”type ManagerConfig struct { KernelPath string // vmlinux path RootfsDir string // rootfs storage directory SocketDir string // API socket directory DefaultVCPUs int // default vCPU count DefaultMemoryMB int // default memory in MB EnableJailer bool // enable jailer sandbox}Constructor
Section titled “Constructor”func NewVMMManager(config interface{}) types.VMMManagerParameters:
config—*ManagerConfig(a non-*ManagerConfigvalue falls back to a defaultManagerConfigwithSocketDir: "/var/run/firecracker")
Defaults Applied:
| Field | Default Value |
|---|---|
SocketDir |
"/var/run/firecracker" |
Example:
config := &lifecycle.ManagerConfig{ KernelPath: "/usr/share/firecracker/vmlinux", RootfsDir: "/var/lib/firecracker/rootfs", SocketDir: "/var/run/firecracker", DefaultVCPUs: 2, DefaultMemoryMB: 1024,}
vmm := lifecycle.NewVMMManager(config)VMInstance
Section titled “VMInstance”File: vmm.go
Type Definition
Section titled “Type Definition”type VMInstance struct { ID string // Task ID PID int // Firecracker process PID Config interface{} // VM configuration state VMState // Current state (private) CreatedAt time.Time // Creation timestamp SocketPath string // API socket path InitSystem string // Init type (tini/dumb-init) GracePeriodSec int // Shutdown grace period
mu sync.RWMutex // Protects state}State Methods
Section titled “State Methods”func (v *VMInstance) SetState(newState VMState)func (v *VMInstance) GetState() VMStateThread-safe state access.
VM States
Section titled “VM States”type VMState string
const ( VMStateNew VMState = "new" // Created but not started VMStateStarting VMState = "starting" // Firecracker booting VMStateRunning VMState = "running" // VM operational VMStateStopping VMState = "stopping" // Shutdown initiated VMStateStopped VMState = "stopped" // VM terminated VMStateCrashed VMState = "crashed" // Unexpected failure)Methods
Section titled “Methods”func (vm *VMMManager) Start(ctx context.Context, task *types.Task, config interface{}) errorPurpose: Start a Firecracker VM for the task.
Steps:
-
Find Firecracker binary
fcBinary, err := exec.LookPath("firecracker") -
Start process with API socket
cmd := exec.Command(fcBinary, "--api-sock", socketPath)cmd.Stdout = os.Stdoutcmd.Stderr = os.Stderrcmd.Start() -
Wait for API ready
waitForAPIServer(socketPath, 10*time.Second) -
Configure VM via API
vm.configureVM(ctx, socketPath, config) -
Send InstanceStart action
client := newUnixClient(socketPath, 5*time.Second)actions := ActionsType{ActionType: "InstanceStart"}body, _ := json.Marshal(actions)req, _ := http.NewRequestWithContext(ctx, "PUT","http://localhost/actions", bytes.NewReader(body))req.Header.Set("Content-Type", "application/json")resp, err := client.Do(req) -
Track instance
vm.vms[task.ID] = &VMInstance{ID: task.ID,PID: cmd.Process.Pid,SocketPath: socketPath,state: VMStateRunning,CreatedAt: time.Now(),}
Error Handling:
- Deferred cleanup on failure (kill process, remove socket)
- Validates config is not nil
- Checks for duplicate VM
func (vm *VMMManager) Stop(ctx context.Context, task *types.Task) errorPurpose: Stop a running VM.
Steps:
- Look up the VM instance by
task.ID - Mark it
VMStateStopping - If an init system is set, attempt a graceful SIGTERM shutdown; otherwise
send
SendCtrlAltDel - Force-kill after the grace period if the process is still alive
- Update state to
VMStateStopped
Pause is implemented in pkg/snapshot by pauseVM(), which issues a
PATCH /vm request (Firecracker v1.14+):
payload := map[string]interface{}{"state": "Paused"}// PATCH /vmpkg/lifecycle itself has no pause method; VMMManager.Snapshot delegates to
pkg/snapshot.Manager.
Resume
Section titled “Resume”Resume is implemented in pkg/snapshot by resumeVM(), using the same
endpoint with {"state": "Resumed"}:
payload := map[string]interface{}{"state": "Resumed"}// PATCH /vmA restore resumes automatically via the resume_vm: true flag on
PUT /snapshot/load.
func (vm *VMMManager) Wait(ctx context.Context, task *types.Task) (*types.TaskStatus, error)Purpose: Report the current status of the VM (running / complete / orphaned).
Describe
Section titled “Describe”func (vm *VMMManager) Describe(ctx context.Context, task *types.Task) (*types.TaskStatus, error)Purpose: Describe the VM’s state, including vm_id, pid, state and uptime.
Remove
Section titled “Remove”func (vm *VMMManager) Remove(ctx context.Context, task *types.Task) errorPurpose: Remove a VM and clean up resources.
Cleanup Steps:
- Kill the VM process if still running
- Remove the API socket file
- Remove the VM from the tracking map
Snapshot
Section titled “Snapshot”func (vm *VMMManager) Snapshot(ctx context.Context, task *types.Task, opts interface{}) (interface{}, error)Status: Placeholder — returns an error directing callers to use
pkg/snapshot.Manager directly.
Restore
Section titled “Restore”func (vm *VMMManager) Restore(ctx context.Context, task *types.Task, snap interface{}) errorStatus: Placeholder — returns an error directing callers to use
pkg/snapshot.Manager directly.
SetConsoleWriter
Section titled “SetConsoleWriter”func (vm *VMMManager) SetConsoleWriter(w io.Writer)Purpose: Redirect the serial console of VMs started from now on to w
(a detached caller points this at a log file so the long-lived Firecracker
child does not keep the caller’s stdio open).
Firecracker API
Section titled “Firecracker API”Client
Section titled “Client”func newUnixClient(socketPath string, timeout time.Duration) *http.ClientnewUnixClient returns a standard *http.Client whose transport dials the
Firecracker API over a Unix socket. Callers build requests directly (for
example a PUT to http://localhost/actions with the action body) rather than
going through helper methods.
Uses HTTP over Unix socket:
// HTTP client with Unix transportclient := &http.Client{ Timeout: timeout, Transport: &http.Transport{ DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { var d net.Dialer return d.DialContext(ctx, "unix", socketPath) }, },}API Types
Section titled “API Types”type ActionsType struct { ActionType string `json:"action_type"`}
type BootSource struct { KernelImagePath string `json:"kernel_image_path"` BootArgs string `json:"boot_args,omitempty"` Drives []Drive `json:"drives,omitempty"`}
type Drive struct { DriveID string `json:"drive_id"` IsRootDevice bool `json:"is_root_device"` IsReadOnly bool `json:"is_read_only"` PathOnHost string `json:"path_on_host"`}
type MachineConfig struct { VCPUs int `json:"vcpu_count"` MemSizeMib int `json:"mem_size_mib"` HtEnabled bool `json:"ht_enabled"`}API Endpoints
Section titled “API Endpoints”| Endpoint | Method | Purpose |
|---|---|---|
/actions |
PUT | Start/stop VM |
/boot-source |
PUT | Configure kernel |
/drives/{id} |
PUT | Configure disk |
/machine-config |
PUT | Configure CPU/memory |
/network-interfaces/{id} |
PUT | Configure network |
/vm |
GET | Get VM info |
/vm |
PATCH | Pause/resume ({"state": "Paused"} / {"state": "Resumed"}) |
Configure VM
Section titled “Configure VM”func (vm *VMMManager) configureVM(ctx context.Context, socketPath string, config interface{}) errorPurpose: Apply full VM configuration via API.
The config is a map[string]interface{} (or JSON string) whose keys are parsed
case-by-case. Only the sections present are applied.
Steps:
-
Configure boot source (if
boot-sourcepresent)// PUT http://localhost/boot-source -
Configure machine (if
machine-configpresent)// PUT http://localhost/machine-config -
Configure drives (if
drivespresent)// PUT http://localhost/drives/<drive_id>
Graceful Shutdown
Section titled “Graceful Shutdown”When InitSystem is set (tini/dumb-init):
// 1. Send SIGTERM to init processsyscall.Kill(pid, syscall.SIGTERM)
// 2. Wait grace periodtime.Sleep(time.Duration(gracePeriodSec) * time.Second)
// 3. Force kill if still runningif processStillRunning { syscall.Kill(pid, syscall.SIGKILL)}Configuration:
executor: init_system: "tini" init_grace_period: 10 # secondsProcess Monitoring
Section titled “Process Monitoring”// Wait for process exitfunc waitForProcess(pid int, timeout time.Duration) error { for i := 0; i < int(timeout/time.Second); i++ { if !processExists(pid) { return nil } time.Sleep(time.Second) } return fmt.Errorf("process did not exit within timeout")}API Server Ready Check
Section titled “API Server Ready Check”func waitForAPIServer(socketPath string, timeout time.Duration) error { for i := 0; i < int(timeout/time.Millisecond); i += 100 { if _, err := net.Dial("unix", socketPath); err == nil { return nil } time.Sleep(100 * time.Millisecond) } return fmt.Errorf("API server not ready after %v", timeout)}Testing
Section titled “Testing”Mock VMMManager
Section titled “Mock VMMManager”type MockVMMManager struct { VMs map[string]*VMInstance StartErr error StopErr error}
func (m *MockVMMManager) Start(ctx context.Context, task *types.Task, config interface{}) error { if m.StartErr != nil { return m.StartErr } m.VMs[task.ID] = &VMInstance{ ID: task.ID, PID: 12345, state: VMStateRunning, } return nil}Error Handling
Section titled “Error Handling”Common Errors
Section titled “Common Errors”| Error | Cause | Resolution |
|---|---|---|
"task cannot be nil" |
Nil task passed | Provide valid task |
"VM already exists" |
Duplicate task ID | Remove existing VM first |
"firecracker binary not found" |
Not installed | Install Firecracker |
"API server not ready" |
Boot timeout | Check Firecracker logs |
"failed to configure VM" |
API error | Validate config values |
"process did not exit" |
Graceful shutdown timeout | Increase grace period |
Related Documentation
Section titled “Related Documentation”| Topic | Document |
|---|---|
| SwarmKit executor | SwarmKit Reference |
| Network setup | Network Reference |
| Init systems | Image Reference |
| Snapshot operations | Operations Guide |