kmod.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sys
  14. import (
  15. "fmt"
  16. "os/exec"
  17. "strings"
  18. pkgexec "github.com/rook/rook/pkg/util/exec"
  19. )
  20. func getKernelVersion() (string, error) {
  21. var output []byte
  22. cmd := exec.Command("uname", "-r")
  23. output, err := cmd.Output()
  24. out := strings.TrimSpace(string(output))
  25. if err != nil {
  26. return out, err
  27. }
  28. return out, nil
  29. }
  30. func IsBuiltinKernelModule(name string, executor pkgexec.Executor) (bool, error) {
  31. kv, err := getKernelVersion()
  32. if err != nil {
  33. return false, fmt.Errorf("failed to get kernel version: %+v", err)
  34. }
  35. kv = fmt.Sprintf("/lib/modules/%s/modules.builtin", kv)
  36. out, err := executor.ExecuteCommandWithCombinedOutput("cat", kv)
  37. if err != nil {
  38. return false, fmt.Errorf("failed to cat %s: %+v", kv, err)
  39. }
  40. result := Grep(out, name)
  41. return result != "", nil
  42. }
  43. func LoadKernelModule(name string, options []string, executor pkgexec.Executor) error {
  44. if options == nil {
  45. options = []string{}
  46. }
  47. args := append([]string{name}, options...)
  48. if err := executor.ExecuteCommand("modprobe", args[:]...); err != nil {
  49. return fmt.Errorf("failed to load kernel module %s: %+v", name, err)
  50. }
  51. return nil
  52. }
  53. func CheckKernelModuleParam(name, param string, executor pkgexec.Executor) (bool, error) {
  54. out, err := executor.ExecuteCommandWithOutput("modinfo", "-F", "parm", name)
  55. if err != nil {
  56. return false, fmt.Errorf("failed to check for %s module %s param: %+v", name, param, err)
  57. }
  58. result := Grep(out, fmt.Sprintf("^%s", param))
  59. return result != "", nil
  60. }