mirror of
https://github.com/getcompanion-ai/computer-host.git
synced 2026-04-15 07:04:43 +00:00
fix: seed mmds data via --metadata flag at boot, eliminates race with guest init
This commit is contained in:
parent
9dd606f26a
commit
44bd0ff089
4 changed files with 32 additions and 17 deletions
|
|
@ -141,3 +141,21 @@ func writeConfigFile(chrootRootDir string, spec MachineSpec, paths machinePaths,
|
||||||
|
|
||||||
return "/vm_config.json", nil
|
return "/vm_config.json", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeMetadataFile(chrootRootDir string, spec MachineSpec) (string, error) {
|
||||||
|
if spec.MMDS == nil || spec.MMDS.Data == nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := json.Marshal(spec.MMDS.Data)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("marshal mmds data: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
metadataPath := filepath.Join(chrootRootDir, "mmds.json")
|
||||||
|
if err := os.WriteFile(metadataPath, data, 0o644); err != nil {
|
||||||
|
return "", fmt.Errorf("write mmds data: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return "/mmds.json", nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ func configureMachine(ctx context.Context, client *apiClient, paths machinePaths
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func launchJailedFirecracker(paths machinePaths, machineID MachineID, firecrackerBinaryPath string, jailerBinaryPath string, enablePCI bool, configFilePath string) (*exec.Cmd, error) {
|
func launchJailedFirecracker(paths machinePaths, machineID MachineID, firecrackerBinaryPath string, jailerBinaryPath string, enablePCI bool, configFilePath string, metadataFilePath string) (*exec.Cmd, error) {
|
||||||
args := []string{
|
args := []string{
|
||||||
"--id", string(machineID),
|
"--id", string(machineID),
|
||||||
"--uid", strconv.Itoa(os.Getuid()),
|
"--uid", strconv.Itoa(os.Getuid()),
|
||||||
|
|
@ -81,6 +81,9 @@ func launchJailedFirecracker(paths machinePaths, machineID MachineID, firecracke
|
||||||
}
|
}
|
||||||
if configFilePath != "" {
|
if configFilePath != "" {
|
||||||
args = append(args, "--config-file", configFilePath)
|
args = append(args, "--config-file", configFilePath)
|
||||||
|
if metadataFilePath != "" {
|
||||||
|
args = append(args, "--metadata", metadataFilePath)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
args = append(args,
|
args = append(args,
|
||||||
"--log-path", paths.JailedFirecrackerLogPath,
|
"--log-path", paths.JailedFirecrackerLogPath,
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ func TestLaunchJailedFirecrackerPassesDaemonAndLoggingFlags(t *testing.T) {
|
||||||
t.Fatalf("create log dir: %v", err)
|
t.Fatalf("create log dir: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := launchJailedFirecracker(paths, "vm-1", "/usr/bin/firecracker", jailerPath, false, ""); err != nil {
|
if _, err := launchJailedFirecracker(paths, "vm-1", "/usr/bin/firecracker", jailerPath, false, "", ""); err != nil {
|
||||||
t.Fatalf("launch jailed firecracker: %v", err)
|
t.Fatalf("launch jailed firecracker: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,7 +62,7 @@ func TestLaunchJailedFirecrackerPassesEnablePCIWhenConfigured(t *testing.T) {
|
||||||
t.Fatalf("create log dir: %v", err)
|
t.Fatalf("create log dir: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := launchJailedFirecracker(paths, "vm-1", "/usr/bin/firecracker", jailerPath, true, ""); err != nil {
|
if _, err := launchJailedFirecracker(paths, "vm-1", "/usr/bin/firecracker", jailerPath, true, "", ""); err != nil {
|
||||||
t.Fatalf("launch jailed firecracker: %v", err)
|
t.Fatalf("launch jailed firecracker: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -125,7 +125,13 @@ func (r *Runtime) Boot(ctx context.Context, spec MachineSpec, usedNetworks []Net
|
||||||
return nil, fmt.Errorf("write config file: %w", err)
|
return nil, fmt.Errorf("write config file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
command, err := launchJailedFirecracker(paths, spec.ID, r.firecrackerBinaryPath, r.jailerBinaryPath, r.enablePCI, configFilePath)
|
metadataFilePath, err := writeMetadataFile(paths.ChrootRootDir, spec)
|
||||||
|
if err != nil {
|
||||||
|
cleanup(network, paths, nil, 0)
|
||||||
|
return nil, fmt.Errorf("write metadata file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
command, err := launchJailedFirecracker(paths, spec.ID, r.firecrackerBinaryPath, r.jailerBinaryPath, r.enablePCI, configFilePath, metadataFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanup(network, paths, nil, 0)
|
cleanup(network, paths, nil, 0)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -137,18 +143,6 @@ func (r *Runtime) Boot(ctx context.Context, spec MachineSpec, usedNetworks []Net
|
||||||
}
|
}
|
||||||
socketPath := procSocketPath(firecrackerPID)
|
socketPath := procSocketPath(firecrackerPID)
|
||||||
|
|
||||||
if spec.MMDS != nil && spec.MMDS.Data != nil {
|
|
||||||
client := newAPIClient(socketPath)
|
|
||||||
if err := waitForSocket(ctx, client, socketPath); err != nil {
|
|
||||||
cleanup(network, paths, command, firecrackerPID)
|
|
||||||
return nil, fmt.Errorf("wait for firecracker socket: %w", err)
|
|
||||||
}
|
|
||||||
if err := client.PutMMDS(ctx, spec.MMDS.Data); err != nil {
|
|
||||||
cleanup(network, paths, command, firecrackerPID)
|
|
||||||
return nil, fmt.Errorf("put mmds data: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
state := MachineState{
|
state := MachineState{
|
||||||
ID: spec.ID,
|
ID: spec.ID,
|
||||||
|
|
@ -288,7 +282,7 @@ func (r *Runtime) RestoreBoot(ctx context.Context, loadSpec SnapshotLoadSpec, us
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
command, err := launchJailedFirecracker(paths, loadSpec.ID, r.firecrackerBinaryPath, r.jailerBinaryPath, r.enablePCI, "")
|
command, err := launchJailedFirecracker(paths, loadSpec.ID, r.firecrackerBinaryPath, r.jailerBinaryPath, r.enablePCI, "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanup(network, paths, nil, 0)
|
cleanup(network, paths, nil, 0)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue