11package main
22
33import (
4+ "bufio"
45 "flag"
56 "fmt"
67 "os"
@@ -10,6 +11,7 @@ import (
1011 "github.com/Jeffail/gabs/v2"
1112 "github.com/docker/docker/pkg/parsers/kernel"
1213 "github.com/spf13/cobra"
14+ "golang.org/x/sys/unix"
1315 utilfeature "k8s.io/apiserver/pkg/util/feature"
1416 cliflag "k8s.io/component-base/cli/flag"
1517
@@ -296,20 +298,46 @@ func mergeConfigList(configs [][]byte, f *feature) (string, error) {
296298 return g .StringIndent ("" , " " ), nil
297299}
298300
299- const moundCmd = `nsenter -t 1 -m -- bash -c '
300- mount | grep "/sys/fs/bpf type bpf" || {
301- # Mount the filesystem until next reboot
302- echo "Mounting BPF filesystem..."
303- mount bpffs /sys/fs/bpf -t bpf
301+ func mountHostBpf () error {
302+ target := "/sys/fs/bpf"
304303
305- echo "Node initialization complete"
306- }'`
304+ // 确保目标目录存在
305+ err := os .MkdirAll (target , 0755 )
306+ if err != nil {
307+ return fmt .Errorf ("failed to create mount point: %v" , err )
308+ }
307309
308- func mountHostBpf () error {
309- out , err := exec . Command ( "/bin/sh" , "-c" , moundCmd ). CombinedOutput ( )
310+ // 检查是否已挂载
311+ mounted , err := isMounted ( target )
310312 if err != nil {
311- return fmt .Errorf ("mount bpf failed: %v, %s" , err , out )
313+ return fmt .Errorf ("failed to check mount status: %v" , err )
314+ }
315+ if mounted {
316+ return nil
312317 }
313- _ , _ = fmt .Fprint (os .Stdout , string (out ))
318+
319+ // 执行 mount bpffs /sys/fs/bpf -t bpf
320+ err = unix .Mount ("bpffs" , target , "bpf" , 0 , "" )
321+ if err != nil {
322+ return fmt .Errorf ("mount failed: %v" , err )
323+ }
324+
314325 return nil
315326}
327+
328+ func isMounted (path string ) (bool , error ) {
329+ f , err := os .Open ("/proc/mounts" )
330+ if err != nil {
331+ return false , err
332+ }
333+ defer f .Close ()
334+
335+ scanner := bufio .NewScanner (f )
336+ for scanner .Scan () {
337+ fields := strings .Split (scanner .Text (), " " )
338+ if len (fields ) >= 2 && fields [1 ] == path {
339+ return true , nil
340+ }
341+ }
342+ return false , nil
343+ }
0 commit comments