Skip to content

Commit 9131747

Browse files
committed
Adds cgroup path to CreateVMResponse
This commit adds a response to CreateVM. The CreateVMResponse contains the CgroupPath field that will be returned when jailing is enabled. Signed-off-by: xibz <[email protected]>
1 parent 65cc752 commit 9131747

File tree

12 files changed

+136
-60
lines changed

12 files changed

+136
-60
lines changed

firecracker-control/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func newLocal(ic *plugin.InitContext) (*local, error) {
7878
// CreateVM creates new Firecracker VM instance. It creates a runtime shim for the VM and the forwards
7979
// the CreateVM request to that shim. If there is already a VM created with the provided VMID, then
8080
// AlreadyExists is returned.
81-
func (s *local) CreateVM(requestCtx context.Context, req *proto.CreateVMRequest) (*empty.Empty, error) {
81+
func (s *local) CreateVM(requestCtx context.Context, req *proto.CreateVMRequest) (*proto.CreateVMResponse, error) {
8282
var err error
8383

8484
id := req.GetVMID()

firecracker-control/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (s *service) RegisterTTRPC(server *ttrpc.Server) error {
6767
return nil
6868
}
6969

70-
func (s *service) CreateVM(ctx context.Context, req *proto.CreateVMRequest) (*empty.Empty, error) {
70+
func (s *service) CreateVM(ctx context.Context, req *proto.CreateVMRequest) (*proto.CreateVMResponse, error) {
7171
log.G(ctx).Debugf("create VM request: %+v", req)
7272
return s.local.CreateVM(ctx, req)
7373
}

proto/firecracker.pb.go

Lines changed: 76 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/firecracker.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ message CreateVMRequest {
3636
JailerConfig JailerConfig = 10;
3737
}
3838

39+
message CreateVMResponse {
40+
string CgroupPath = 1;
41+
}
42+
3943
message StopVMRequest {
4044
string VMID = 1;
4145
uint32 TimeoutSeconds = 2;

proto/service/fccontrol/fccontrol.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ option go_package = "fccontrol";
88

99
service Firecracker {
1010
// Runs new Firecracker VM instance
11-
rpc CreateVM(CreateVMRequest) returns (google.protobuf.Empty);
11+
rpc CreateVM(CreateVMRequest) returns (CreateVMResponse);
1212

1313
// Stops existing Firecracker instance by VM ID
1414
rpc StopVM(StopVMRequest) returns (google.protobuf.Empty);

proto/service/fccontrol/ttrpc/fccontrol.pb.go

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/jailer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ type jailer interface {
5757
StubDrivesOptions() []stubDrivesOpt
5858
}
5959

60+
type cgroupPather interface {
61+
CgroupPath() string
62+
}
63+
6064
// newJailer is used to construct a jailer from the CreateVM request. If no
6165
// request or jailer config was provided, then the noopJailer will be returned.
6266
func newJailer(
@@ -72,5 +76,5 @@ func newJailer(
7276
}
7377

7478
l := logger.WithField("jailer", "runc")
75-
return newRuncJailer(ctx, l, ociBundlePath, service.config.JailerConfig.RuncBinaryPath, jailerUID, jailerGID)
79+
return newRuncJailer(ctx, l, ociBundlePath, service, jailerUID, jailerGID)
7680
}

runtime/noop_jailer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,8 @@ func (j noopJailer) StubDrivesOptions() []stubDrivesOpt {
7272
j.logger.Debug("noop operation for StubDrivesOptions")
7373
return []stubDrivesOpt{}
7474
}
75+
76+
func (j noopJailer) CgroupPath() string {
77+
j.logger.Debug("noop operation for CgroupPath")
78+
return ""
79+
}

runtime/runc_jailer.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,21 @@ type runcJailer struct {
4545
runcBinaryPath string
4646
uid uint32
4747
gid uint32
48+
vmID string
4849
}
4950

50-
func newRuncJailer(ctx context.Context, logger *logrus.Entry, ociBundlePath, runcBinPath string, uid, gid uint32) (*runcJailer, error) {
51+
func newRuncJailer(ctx context.Context, logger *logrus.Entry, ociBundlePath string, service *service, uid, gid uint32) (*runcJailer, error) {
5152
l := logger.WithField("ociBundlePath", ociBundlePath).
52-
WithField("runcBinaryPath", runcBinPath)
53+
WithField("runcBinaryPath", service.config.JailerConfig.RuncBinaryPath)
5354

5455
j := &runcJailer{
5556
ctx: ctx,
5657
logger: l,
5758
ociBundlePath: ociBundlePath,
58-
runcBinaryPath: runcBinPath,
59+
runcBinaryPath: service.config.JailerConfig.RuncBinaryPath,
5960
uid: uid,
6061
gid: gid,
62+
vmID: service.vmID,
6163
}
6264

6365
rootPath := j.RootPath()
@@ -371,6 +373,10 @@ func (j runcJailer) overwriteConfig(cfg *Config, socketPath, configPath string)
371373
return nil
372374
}
373375

376+
func (j runcJailer) CgroupPath() string {
377+
return filepath.Join("/firecracker-containerd", j.vmID)
378+
}
379+
374380
// setDefaultConfigValues will process the spec file provided and allow any
375381
// empty/zero values to be replaced with default values.
376382
func (j runcJailer) setDefaultConfigValues(cfg *Config, socketPath string, spec specs.Spec) specs.Spec {
@@ -389,6 +395,8 @@ func (j runcJailer) setDefaultConfigValues(cfg *Config, socketPath string, spec
389395
spec.Process.Args = cmd.Args
390396
}
391397

398+
spec.Linux.CgroupsPath = j.CgroupPath()
399+
392400
return spec
393401
}
394402

runtime/runc_jailer_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ func TestBuildJailedRootHandler_Isolated(t *testing.T) {
5252
defer firecrackerFd.Close()
5353

5454
l := logrus.NewEntry(logrus.New())
55-
jailer, err := newRuncJailer(context.Background(), l, dir, "bin-path", 123, 456)
55+
s := &service{
56+
config: &Config{
57+
JailerConfig: JailerConfig{},
58+
},
59+
}
60+
jailer, err := newRuncJailer(context.Background(), l, dir, s, 123, 456)
5661
require.NoError(t, err, "failed to create runc jailer")
5762

5863
cfg := Config{

runtime/service.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,13 @@ func (s *service) waitVMReady() error {
407407

408408
// CreateVM will attempt to create the VM as specified in the provided request, but only on the first request
409409
// received. Any subsequent requests will be ignored and get an AlreadyExists error response.
410-
func (s *service) CreateVM(requestCtx context.Context, request *proto.CreateVMRequest) (*empty.Empty, error) {
410+
func (s *service) CreateVM(requestCtx context.Context, request *proto.CreateVMRequest) (*proto.CreateVMResponse, error) {
411411
defer logPanicAndDie(s.logger)
412412

413413
var (
414414
err error
415415
createRan bool
416+
resp proto.CreateVMResponse
416417
)
417418

418419
s.vmStartOnce.Do(func() {
@@ -442,7 +443,11 @@ func (s *service) CreateVM(requestCtx context.Context, request *proto.CreateVMRe
442443

443444
// let all the other methods know that the VM is ready for tasks
444445
close(s.vmReady)
445-
return &empty.Empty{}, nil
446+
447+
if c, ok := s.jailer.(cgroupPather); ok {
448+
resp.CgroupPath = c.CgroupPath()
449+
}
450+
return &resp, nil
446451
}
447452

448453
func (s *service) publishVMStart() error {

0 commit comments

Comments
 (0)