Skip to content

Commit 59e68dd

Browse files
committed
Adding link fifo handler
This fixes that fifo actually become visible to the jailer. Signed-off-by: xibz <[email protected]>
1 parent 9c641fe commit 59e68dd

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

runtime/jailer.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import (
2121
)
2222

2323
const (
24-
jailerFolder = "jail"
25-
firecrackerBinName = "firecracker"
26-
kernelImageFileName = "kernel-image"
27-
jailerHandlerName = "firecracker-containerd-jail-handler"
28-
runcConfigPath = "/etc/containerd/firecracker-runc-config.json"
24+
jailerFolder = "jail"
25+
firecrackerBinName = "firecracker"
26+
kernelImageFileName = "kernel-image"
27+
jailerHandlerName = "firecracker-containerd-jail-handler"
28+
jailerFifoHandlerName = "firecracker-containerd-jail-fifo-handler"
29+
runcConfigPath = "/etc/containerd/firecracker-runc-config.json"
2930

3031
// JailingOn is used to signify whether or not jailing has been turned on
3132
JailingOn = "on"
@@ -91,6 +92,13 @@ func (j jailer) RootPath() string {
9192
return filepath.Join(j.jailPath, "rootfs")
9293
}
9394

95+
func (j jailer) ContentsPath() string {
96+
return filepath.Join(j.RootPath(), "var", "lib", "firecracker-containerd")
97+
}
98+
99+
// BuildHandler will link the necessary files except for the fifos due to the
100+
// fifos needing to be created. Also, this will create the proper device nodes
101+
// needed by Firecracker
94102
func (j *jailer) BuildHandler(logger *logrus.Entry, cfg *Config, socketPath *string, vmID string) firecracker.Handler {
95103
jailPath := j.JailPath()
96104
rootPath := j.RootPath()
@@ -117,7 +125,7 @@ func (j *jailer) BuildHandler(logger *logrus.Entry, cfg *Config, socketPath *str
117125
return errors.Wrapf(err, "failed to create device path: %v", devPath)
118126
}
119127

120-
contentsPath := filepath.Join(rootPath, "var", "lib", "firecracker-containerd")
128+
contentsPath := j.ContentsPath()
121129
logger.Debugf("Creating firecracker contents path %v", contentsPath)
122130
if err := os.MkdirAll(contentsPath, 0777); err != nil {
123131
return errors.Wrapf(err, "failed to create contents path: %v", contentsPath)
@@ -181,6 +189,32 @@ func (j *jailer) BuildHandler(logger *logrus.Entry, cfg *Config, socketPath *str
181189
}
182190
}
183191

192+
// BuildLinkFifoHandler will return a new firecracker.Handler with the function
193+
// that will allow linking of the fifos making them visible to Firecracker.
194+
func (j jailer) BuildLinkFifoHandler() firecracker.Handler {
195+
return firecracker.Handler{
196+
Name: jailerFifoHandlerName,
197+
Fn: func(ctx context.Context, m *firecracker.Machine) error {
198+
contentsPath := j.ContentsPath()
199+
fifoFileName := filepath.Base(m.Cfg.LogFifo)
200+
newFifoPath := filepath.Join(contentsPath, fifoFileName)
201+
if err := os.Link(m.Cfg.LogFifo, newFifoPath); err != nil {
202+
return err
203+
}
204+
m.Cfg.LogFifo = newFifoPath
205+
206+
metricFifoFileName := filepath.Base(m.Cfg.MetricsFifo)
207+
newMetricFifoPath := filepath.Join(contentsPath, metricFifoFileName)
208+
if err := os.Link(m.Cfg.MetricsFifo, newMetricFifoPath); err != nil {
209+
return err
210+
}
211+
m.Cfg.MetricsFifo = newMetricFifoPath
212+
213+
return nil
214+
},
215+
}
216+
}
217+
184218
// createDevices will create a series of device nodes at the given path
185219
func createDevices(path string) error {
186220
devices := []struct {

runtime/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ func (s *service) createVM(requestCtx context.Context, request *proto.CreateVMRe
490490
}
491491
if !s.config.DisableJailing {
492492
handler := s.jailer.BuildHandler(s.logger, s.config, &s.machineConfig.SocketPath, s.vmID)
493+
fifoHandler := s.jailer.BuildLinkFifoHandler()
493494
client := firecracker.NewClient(s.machineConfig.SocketPath, s.logger, s.machineConfig.Debug)
494495

495496
opts = append(
@@ -498,6 +499,7 @@ func (s *service) createVM(requestCtx context.Context, request *proto.CreateVMRe
498499
firecracker.WithClient(client),
499500
func(m *firecracker.Machine) {
500501
m.Handlers.FcInit = m.Handlers.FcInit.Prepend(handler)
502+
m.Handlers.FcInit = m.Handlers.FcInit.AppendAfter(firecracker.CreateLogFilesHandlerName, fifoHandler)
501503
},
502504
)
503505
}

0 commit comments

Comments
 (0)