@@ -45,14 +45,32 @@ func WithFirmware(src extract.Source) Option { return func(b *Backend) { b.firmw
4545// Ignored when Sources are directory-based.
4646func WithCacheDir (dir string ) Option { return func (b * Backend ) { b .cacheDir = dir } }
4747
48+ // WithUserNamespaceUID configures the runner to spawn inside a Linux user
49+ // namespace (CLONE_NEWUSER) with a single UID/GID mapping. The child
50+ // process gains CAP_SETUID and CAP_SETGID within the namespace, which
51+ // allows libkrun's virtiofs passthrough to call set_creds() without
52+ // requiring host-level capabilities.
53+ //
54+ // uid and gid specify the namespace-side IDs that map to the host
55+ // process's real UID/GID. For example, if the guest expects UID 1000
56+ // and the host runs as UID 1000, pass uid=1000, gid=1000.
57+ //
58+ // On non-Linux platforms, this option is accepted but has no effect.
59+ func WithUserNamespaceUID (uid , gid uint32 ) Option {
60+ return func (b * Backend ) {
61+ b .userNamespace = & runner.UserNamespaceConfig {UID : uid , GID : gid }
62+ }
63+ }
64+
4865// Backend implements hypervisor.Backend using libkrun.
4966type Backend struct {
50- runnerPath string
51- libDir string
52- spawner runner.Spawner
53- runtime extract.Source
54- firmware extract.Source
55- cacheDir string
67+ runnerPath string
68+ libDir string
69+ spawner runner.Spawner
70+ runtime extract.Source
71+ firmware extract.Source
72+ cacheDir string
73+ userNamespace * runner.UserNamespaceConfig
5674}
5775
5876// NewBackend creates a libkrun backend with the given options.
@@ -131,17 +149,18 @@ func (b *Backend) Start(ctx context.Context, cfg hypervisor.VMConfig) (hyperviso
131149 }
132150
133151 runCfg := runner.Config {
134- RootPath : cfg .RootFSPath ,
135- NumVCPUs : cfg .NumVCPUs ,
136- RAMMiB : cfg .RAMMiB ,
137- NetSocket : netSocket ,
138- PortForwards : toRunnerPortForwards (cfg .PortForwards ),
139- VirtioFS : toRunnerVirtioFS (cfg .FilesystemMounts ),
140- ConsoleLog : cfg .ConsoleLogPath ,
141- LogLevel : cfg .LogLevel ,
142- LibDir : libDir ,
143- RunnerPath : runnerPath ,
144- VMLogPath : filepath .Join (cfg .DataDir , "vm.log" ),
152+ RootPath : cfg .RootFSPath ,
153+ NumVCPUs : cfg .NumVCPUs ,
154+ RAMMiB : cfg .RAMMiB ,
155+ NetSocket : netSocket ,
156+ PortForwards : toRunnerPortForwards (cfg .PortForwards ),
157+ VirtioFS : toRunnerVirtioFS (cfg .FilesystemMounts ),
158+ ConsoleLog : cfg .ConsoleLogPath ,
159+ LogLevel : cfg .LogLevel ,
160+ LibDir : libDir ,
161+ RunnerPath : runnerPath ,
162+ VMLogPath : filepath .Join (cfg .DataDir , "vm.log" ),
163+ UserNamespace : b .userNamespace ,
145164 }
146165
147166 spawner := b .spawner
0 commit comments