github.com/cilium/cilium@v1.16.2/pkg/datapath/linux/modules/modules.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package modules 5 6 import ( 7 "fmt" 8 9 "github.com/cilium/hive/cell" 10 11 "github.com/cilium/cilium/pkg/command/exec" 12 "github.com/cilium/cilium/pkg/defaults" 13 "github.com/cilium/cilium/pkg/slices" 14 ) 15 16 // Manager stores information about loaded modules and provides a search operation. 17 type Manager struct { 18 modulesList []string 19 } 20 21 func (m *Manager) Start(cell.HookContext) (err error) { 22 m.modulesList, err = listModules() 23 return err 24 } 25 26 // FindModules checks whether the given kernel modules are loaded and also 27 // returns a slice with names of modules which are not loaded. 28 func (m *Manager) FindModules(expectedNames ...string) (bool, []string) { 29 return slices.SubsetOf(expectedNames, m.modulesList) 30 } 31 32 // FindOrLoadModules checks whether the given kernel modules are loaded and 33 // tries to load those which are not. 34 func (m *Manager) FindOrLoadModules(expectedNames ...string) error { 35 found, diff := m.FindModules(expectedNames...) 36 if found { 37 return nil 38 } 39 for _, unloadedModule := range diff { 40 if _, err := exec.WithTimeout( 41 defaults.ExecTimeout, moduleLoader(), unloadedModule).CombinedOutput( 42 nil, false); err != nil { 43 return fmt.Errorf("could not load module %s: %w", 44 unloadedModule, err) 45 } 46 } 47 return nil 48 } 49 50 func newManager(lc cell.Lifecycle) *Manager { 51 m := &Manager{} 52 53 lc.Append(cell.Hook{OnStart: m.Start}) 54 55 return m 56 }