modules.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package modules
  2. import (
  3. "bufio"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. "github.com/rancher/os/config"
  8. "github.com/rancher/os/pkg/log"
  9. "github.com/rancher/os/pkg/util"
  10. )
  11. func LoadModules(cfg *config.CloudConfig) (*config.CloudConfig, error) {
  12. mounted := map[string]bool{}
  13. f, err := os.Open("/proc/modules")
  14. if err != nil {
  15. return cfg, err
  16. }
  17. defer f.Close()
  18. reader := bufio.NewScanner(f)
  19. for reader.Scan() {
  20. mounted[strings.SplitN(reader.Text(), " ", 2)[0]] = true
  21. }
  22. if util.GetHypervisor() == "hyperv" {
  23. cfg.Rancher.Modules = append(cfg.Rancher.Modules, "hv_utils", "hv_storvsc", "hv_vmbus")
  24. }
  25. for _, module := range cfg.Rancher.Modules {
  26. if mounted[module] {
  27. continue
  28. }
  29. log.Debugf("Loading module %s", module)
  30. // split module and module parameters
  31. cmdParam := strings.SplitN(module, " ", -1)
  32. cmd := exec.Command("modprobe", cmdParam...)
  33. cmd.Stdout = os.Stdout
  34. cmd.Stderr = os.Stderr
  35. if err := cmd.Run(); err != nil {
  36. log.Errorf("Could not load module %s, err %v", module, err)
  37. }
  38. }
  39. return cfg, reader.Err()
  40. }