Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically mount cgroup2 at kmesh startup #229

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
58 changes: 55 additions & 3 deletions pkg/bpf/bpf.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 The Kmesh Authors.
* Copyright 2024 The Kmesh Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.

* Author: LemmyHuang
* Create: 2021-10-09
*/

package bpf

import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/rlimit"
Expand Down Expand Up @@ -89,6 +89,14 @@ func Start() error {
return err
}

if err = checkKmeshMod(); err != nil {
return err
}

if err = mountCgroup2(&config); err != nil {
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, cgroup v2 only available in linux kernel v5.8 or above.

Is mouting cgroup v2 a necessary step use kmesh? If not we could just print a warning message and not return error directly, otherwise kmesh could not run on older versions of the kernel.

I guess v5.8 is a pretty high version for production environments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/kmesh-net/kmesh/blob/main/build/docker/README.md#start_kmeshsh

It is stated that to start kmesh, have to mount cgroupv2.

}

if config.EnableKmesh {
if err = StartKmesh(); err != nil {
return err
Expand Down Expand Up @@ -142,3 +150,47 @@ func Stop() {
}
}
}

func mountCgroup2(cfg *Config) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to do some cleanups during pod termination.

Otherwise this should fail.

Take a look at kmesh-start-pre.sh

if err := os.MkdirAll(cfg.Cgroup2Path, 0755); err != nil {
log.Errorf("failed to create dir %s: %v", cfg.Cgroup2Path, err)
return err
}

if err := syscall.Mount("none", cfg.Cgroup2Path, "cgroup2", 0, ""); err != nil {
cleanupMountPath()
log.Errorf("failed to mount %s: %v", cfg.Cgroup2Path, err)
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need clean up resources before return err

}

return nil
}

func checkKmeshMod() error {
output, err := exec.Command("lsmod", "|", "grep", "kmesh").Output()
if err != nil {
return err
}

if len(output) != 0 {
if err := exec.Command("rmmod", "kmesh").Run(); err != nil {
return err
}
}

if err := exec.Command("depmod", "-a").Run(); err != nil {
return err
}

return exec.Command("modprobe", "kmesh").Run()
}

func cleanupMountPath() {
if err := syscall.Unmount("/mnt/kmesh_cgroup2", 0); err != nil {
log.Errorf("unmount /mnt/kmesh_cgroup2 error: ", err)
}

if err := os.RemoveAll("/mnt/kmesh_cgroup2"); err != nil {
log.Errorf("remove /mnt/kmesh_cgroup2 error: ", err)
}
}
Loading