Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

agent: add support for loading kernel modules #616

Merged
merged 1 commit into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ var (

// set when StopTracing() is called.
stopTracingCalled = false

modprobePath = "/sbin/modprobe"
ganeshmaharaj marked this conversation as resolved.
Show resolved Hide resolved
)

type onlineResource struct {
Expand Down Expand Up @@ -1399,6 +1401,35 @@ func (a *agentGRPC) TtyWinResize(ctx context.Context, req *pb.TtyWinResizeReques
return emptyResp, nil
}

func loadKernelModule(module *pb.KernelModule) error {
if module == nil {
return fmt.Errorf("Kernel module is nil")
}

if module.Name == "" {
return fmt.Errorf("Kernel module name is empty")
}

log := agentLog.WithFields(logrus.Fields{
"module-name": module.Name,
"module-params": module.Parameters,
})

log.Debug("loading module")
cmd := exec.Command(modprobePath, "-v", module.Name)

if len(module.Parameters) > 0 {
cmd.Args = append(cmd.Args, module.Parameters...)
}

output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("could not load module: %v: %v", err, string(output))
}

return nil
}

func (a *agentGRPC) CreateSandbox(ctx context.Context, req *pb.CreateSandboxRequest) (*gpb.Empty, error) {
if a.sandbox.running {
return emptyResp, grpcStatus.Error(codes.AlreadyExists, "Sandbox already started, impossible to start again")
Expand All @@ -1414,6 +1445,12 @@ func (a *agentGRPC) CreateSandbox(ctx context.Context, req *pb.CreateSandboxRequ
a.sandbox.guestHooks = &specs.Hooks{}
a.sandbox.guestHooksPresent = false

for _, m := range req.KernelModules {
if err := loadKernelModule(m); err != nil {
return emptyResp, err
}
}

if req.GuestHookPath != "" {
a.sandbox.scanGuestHooks(req.GuestHookPath)
}
Expand Down
19 changes: 19 additions & 0 deletions grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1754,3 +1754,22 @@ func TestTtyWinResize(t *testing.T) {
_, err := a.TtyWinResize(context.Background(), req)
assert.Error(err)
}

func TestLoadKernelModule(t *testing.T) {
assert := assert.New(t)

err := loadKernelModule(nil)
assert.Error(err)

m := &pb.KernelModule{
Name: "",
Parameters: []string{"opt"},
}
err = loadKernelModule(m)
assert.Error(err)

modprobePath = "/bin/echo"
m.Name = "fake"
err = loadKernelModule(m)
assert.NoError(err)
}
Loading