/cookbooks/fb_modprobe/libraries/default.rb

https://github.com/facebook/chef-cookbooks · Ruby · 70 lines · 39 code · 9 blank · 22 comment · 4 complexity · 42bcf397377541fd38df88cc0677498b MD5 · raw file

  1. # Copyright (c) 2016-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. module FB
  17. # add kmod related functions previously in fb_hardware
  18. class Modprobe
  19. def self.module_version(loaded_mod)
  20. loaded_mod.tr!('-', '_')
  21. version_file = "/sys/module/#{loaded_mod}/version"
  22. if File.exist?(version_file)
  23. return IO.read(version_file).strip
  24. end
  25. nil
  26. end
  27. def self.module_refcnt(loaded_mod)
  28. loaded_mod.tr!('-', '_')
  29. version_file = "/sys/module/#{loaded_mod}/refcnt"
  30. if File.exist?(version_file)
  31. return IO.read(version_file).strip
  32. end
  33. nil
  34. end
  35. def self.supports_ipv6_autoconf_param?
  36. cmd = '/sbin/modinfo ipv6 | /bin/grep -q autoconf:'
  37. Mixlib::ShellOut.new(cmd).run_command.exitstatus.zero?
  38. end
  39. def self.module_loaded?(loaded_mod)
  40. # modprobe handles both, but /proc/modules only uses underscores
  41. loaded_mod.tr!('-', '_')
  42. # Handle built-in modules correctly
  43. File.exist?("/sys/module/#{loaded_mod}")
  44. end
  45. # This is a significantly better test to see if a module is usable
  46. def self.module_initialized?(loaded_mod)
  47. loaded_mod.tr!('-', '_')
  48. initstate_path = "/sys/module/#{loaded_mod}/initstate"
  49. if File.exist?(initstate_path)
  50. initstate = IO.read(initstate_path)
  51. return initstate.strip == 'live'
  52. elsif File.exist?("/sys/module/#{loaded_mod}")
  53. # Modules that are built-in don't have the initstate file. Since they're
  54. # built-in, the fact that Chef is running means the MODULE_INIT function
  55. # must have completed, otherwise we would hang forever at boot.
  56. return true
  57. end
  58. false
  59. end
  60. end
  61. end