-
Notifications
You must be signed in to change notification settings - Fork 45
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
@@ -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" | ||
|
@@ -89,6 +89,14 @@ func Start() error { | |
return err | ||
} | ||
|
||
if err = checkKmeshMod(); err != nil { | ||
return err | ||
} | ||
|
||
if err = mountCgroup2(&config); err != nil { | ||
return err | ||
} | ||
|
||
if config.EnableKmesh { | ||
if err = StartKmesh(); err != nil { | ||
return err | ||
|
@@ -142,3 +150,47 @@ func Stop() { | |
} | ||
} | ||
} | ||
|
||
func mountCgroup2(cfg *Config) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.