|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package libkrun |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "github.com/stacklok/propolis/hypervisor" |
| 12 | + "github.com/stacklok/propolis/image" |
| 13 | + "github.com/stacklok/propolis/runner" |
| 14 | +) |
| 15 | + |
| 16 | +// Option configures a libkrun Backend. |
| 17 | +type Option func(*Backend) |
| 18 | + |
| 19 | +// WithRunnerPath sets the path to the propolis-runner binary. |
| 20 | +// When empty, the runner is found via $PATH or alongside the calling binary. |
| 21 | +func WithRunnerPath(p string) Option { return func(b *Backend) { b.runnerPath = p } } |
| 22 | + |
| 23 | +// WithLibDir sets the path to a directory containing libkrun/libkrunfw |
| 24 | +// shared libraries. The runner subprocess will use this via LD_LIBRARY_PATH. |
| 25 | +func WithLibDir(d string) Option { return func(b *Backend) { b.libDir = d } } |
| 26 | + |
| 27 | +// WithSpawner sets a custom spawner for the runner subprocess. |
| 28 | +// When nil (default), the standard runner.DefaultSpawner is used. |
| 29 | +func WithSpawner(s runner.Spawner) Option { return func(b *Backend) { b.spawner = s } } |
| 30 | + |
| 31 | +// Backend implements hypervisor.Backend using libkrun. |
| 32 | +type Backend struct { |
| 33 | + runnerPath string |
| 34 | + libDir string |
| 35 | + spawner runner.Spawner |
| 36 | +} |
| 37 | + |
| 38 | +// NewBackend creates a libkrun backend with the given options. |
| 39 | +func NewBackend(opts ...Option) *Backend { |
| 40 | + b := &Backend{} |
| 41 | + for _, o := range opts { |
| 42 | + o(b) |
| 43 | + } |
| 44 | + return b |
| 45 | +} |
| 46 | + |
| 47 | +// Name returns "libkrun". |
| 48 | +func (b *Backend) Name() string { return "libkrun" } |
| 49 | + |
| 50 | +// PrepareRootFS writes .krun_config.json into the rootfs directory. |
| 51 | +func (b *Backend) PrepareRootFS(_ context.Context, rootfsPath string, initCfg hypervisor.InitConfig) (string, error) { |
| 52 | + kc := image.KrunConfig{ |
| 53 | + Cmd: initCfg.Cmd, |
| 54 | + Env: initCfg.Env, |
| 55 | + WorkingDir: initCfg.WorkingDir, |
| 56 | + } |
| 57 | + if err := kc.WriteTo(rootfsPath); err != nil { |
| 58 | + return "", fmt.Errorf("write krun config: %w", err) |
| 59 | + } |
| 60 | + return rootfsPath, nil |
| 61 | +} |
| 62 | + |
| 63 | +// Start launches the VM via the propolis-runner subprocess. |
| 64 | +func (b *Backend) Start(ctx context.Context, cfg hypervisor.VMConfig) (hypervisor.VMHandle, error) { |
| 65 | + var netSocket string |
| 66 | + if cfg.NetEndpoint.Type == hypervisor.NetEndpointUnixSocket { |
| 67 | + netSocket = cfg.NetEndpoint.Path |
| 68 | + } |
| 69 | + |
| 70 | + runCfg := runner.Config{ |
| 71 | + RootPath: cfg.RootFSPath, |
| 72 | + NumVCPUs: cfg.NumVCPUs, |
| 73 | + RAMMiB: cfg.RAMMiB, |
| 74 | + NetSocket: netSocket, |
| 75 | + PortForwards: toRunnerPortForwards(cfg.PortForwards), |
| 76 | + VirtioFS: toRunnerVirtioFS(cfg.FilesystemMounts), |
| 77 | + ConsoleLog: cfg.ConsoleLogPath, |
| 78 | + LibDir: b.libDir, |
| 79 | + RunnerPath: b.runnerPath, |
| 80 | + VMLogPath: filepath.Join(cfg.DataDir, "vm.log"), |
| 81 | + } |
| 82 | + |
| 83 | + spawner := b.spawner |
| 84 | + if spawner == nil { |
| 85 | + spawner = runner.DefaultSpawner{} |
| 86 | + } |
| 87 | + |
| 88 | + proc, err := spawner.Spawn(ctx, runCfg) |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("spawn runner: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + return &processHandle{proc: proc}, nil |
| 94 | +} |
| 95 | + |
| 96 | +func toRunnerPortForwards(ports []hypervisor.PortForward) []runner.PortForward { |
| 97 | + out := make([]runner.PortForward, len(ports)) |
| 98 | + for i, p := range ports { |
| 99 | + out[i] = runner.PortForward{Host: p.Host, Guest: p.Guest} |
| 100 | + } |
| 101 | + return out |
| 102 | +} |
| 103 | + |
| 104 | +func toRunnerVirtioFS(mounts []hypervisor.FilesystemMount) []runner.VirtioFSMount { |
| 105 | + out := make([]runner.VirtioFSMount, len(mounts)) |
| 106 | + for i, m := range mounts { |
| 107 | + out[i] = runner.VirtioFSMount{Tag: m.Tag, HostPath: m.HostPath} |
| 108 | + } |
| 109 | + return out |
| 110 | +} |
0 commit comments