kvm.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sysutils
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "regexp"
  20. "strings"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/utils"
  23. "yunion.io/x/onecloud/pkg/util/fileutils2"
  24. "yunion.io/x/onecloud/pkg/util/procutils"
  25. )
  26. const (
  27. KVM_MODULE_INTEL = "kvm-intel"
  28. KVM_MODULE_AMD = "kvm-amd"
  29. KVM_MODULE = "kvm"
  30. KVM_MODULE_UNSUPPORT = "unsupport"
  31. KVM_MODULE_BUILDIN = "buildin"
  32. HOST_NEST_UNSUPPORT = "0"
  33. HOST_NEST_SUPPORT = "1"
  34. HOST_NEST_ENABLE = "3"
  35. )
  36. var (
  37. kvmModuleSupport string
  38. nestStatus string
  39. )
  40. func GetKVMModuleSupport() string {
  41. if len(kvmModuleSupport) == 0 {
  42. kvmModuleSupport = detectKVMModuleSupport()
  43. }
  44. return kvmModuleSupport
  45. }
  46. func IsKvmSupport() bool {
  47. GetKVMModuleSupport()
  48. if kvmModuleSupport == KVM_MODULE_UNSUPPORT {
  49. return false
  50. }
  51. return true
  52. }
  53. func IsProcessorIntel() bool {
  54. GetKVMModuleSupport()
  55. if kvmModuleSupport == KVM_MODULE_INTEL {
  56. return true
  57. }
  58. return false
  59. }
  60. func IsProcessorAmd() bool {
  61. GetKVMModuleSupport()
  62. if kvmModuleSupport == KVM_MODULE_AMD {
  63. return true
  64. }
  65. return false
  66. }
  67. func isDevKVMExists() bool {
  68. return fileutils2.Exists("/dev/kvm")
  69. }
  70. func detectKVMModuleSupport() string {
  71. var km = KVM_MODULE_UNSUPPORT
  72. if isDevKVMExists() {
  73. if IsKernelModuleLoaded(KVM_MODULE_INTEL) {
  74. km = KVM_MODULE_INTEL
  75. } else if IsKernelModuleLoaded(KVM_MODULE_AMD) {
  76. km = KVM_MODULE_AMD
  77. } else if IsKernelModuleLoaded(KVM_MODULE) {
  78. km = KVM_MODULE
  79. } else {
  80. km = KVM_MODULE_BUILDIN
  81. }
  82. } else {
  83. if ModprobeKvmModule(KVM_MODULE_INTEL, false, false) {
  84. km = KVM_MODULE_INTEL
  85. } else if ModprobeKvmModule(KVM_MODULE_AMD, false, false) {
  86. km = KVM_MODULE_AMD
  87. } else if ModprobeKvmModule(KVM_MODULE, false, false) {
  88. if isDevKVMExists() {
  89. km = KVM_MODULE
  90. }
  91. }
  92. if km == KVM_MODULE_UNSUPPORT {
  93. if isDevKVMExists() {
  94. km = KVM_MODULE_BUILDIN
  95. }
  96. }
  97. }
  98. return km
  99. }
  100. func ModprobeKvmModule(name string, remove, nest bool) bool {
  101. var params = []string{"modprobe"}
  102. if remove {
  103. params = append(params, "-r")
  104. }
  105. params = append(params, name)
  106. if nest {
  107. params = append(params, "nested=1")
  108. }
  109. if err := procutils.NewRemoteCommandAsFarAsPossible(params[0], params[1:]...).Run(); err != nil {
  110. log.Errorf("Modprobe kvm %v failed: %s", params, err)
  111. return false
  112. }
  113. return true
  114. }
  115. func IsNestEnabled() bool {
  116. return GetNestSupport() == HOST_NEST_ENABLE
  117. }
  118. func GetNestSupport() string {
  119. if len(nestStatus) == 0 {
  120. nestStatus = detectNestSupport()
  121. }
  122. return nestStatus
  123. }
  124. func detectNestSupport() string {
  125. moduleName := GetKVMModuleSupport()
  126. nestStatus := HOST_NEST_UNSUPPORT
  127. if moduleName != KVM_MODULE_UNSUPPORT && isNestSupport(moduleName) {
  128. log.Infof("Host is support kvm nest ...")
  129. nestStatus = HOST_NEST_SUPPORT
  130. }
  131. if nestStatus == HOST_NEST_SUPPORT && loadKvmModuleWithNest(moduleName) {
  132. log.Infof("Host kvm nest is enabled ...")
  133. nestStatus = HOST_NEST_ENABLE
  134. }
  135. return nestStatus
  136. }
  137. func isNestSupport(name string) bool {
  138. output, err := procutils.NewRemoteCommandAsFarAsPossible("modinfo", name).Output()
  139. if err != nil {
  140. log.Errorln(err)
  141. return false
  142. }
  143. // TODO Test
  144. var re = regexp.MustCompile(`parm:\s*nested:`)
  145. for _, line := range strings.Split(string(output), "\n") {
  146. if re.MatchString(line) {
  147. return true
  148. }
  149. }
  150. return false
  151. }
  152. func loadKvmModuleWithNest(name string) bool {
  153. var notload = true
  154. if IsKernelModuleLoaded(name) {
  155. nest := GetKernelModuleParameter(name, "nested")
  156. if nest == "Y" {
  157. return true
  158. }
  159. notload = unloadKvmModule(name)
  160. }
  161. if notload {
  162. if ModprobeKvmModule(name, false, true) {
  163. return true
  164. }
  165. }
  166. return false
  167. }
  168. func unloadKvmModule(name string) bool {
  169. return ModprobeKvmModule(name, true, false)
  170. }
  171. func GetKernelModuleParameter(name, moduel string) string {
  172. pa := path.Join("/sys/module/", strings.Replace(name, "-", "_", -1), "/parameters/", moduel)
  173. return GetSysConfig(pa)
  174. }
  175. func getSysConfig(pa string, quiet bool) string {
  176. if f, err := os.Stat(pa); err == nil {
  177. if f.IsDir() {
  178. return ""
  179. }
  180. cont, err := fileutils2.FileGetContents(pa)
  181. if err != nil {
  182. if !quiet {
  183. log.Errorln(err)
  184. }
  185. return ""
  186. }
  187. return strings.TrimSpace(cont)
  188. }
  189. return ""
  190. }
  191. func GetSysConfig(pa string) string {
  192. return getSysConfig(pa, false)
  193. }
  194. func GetSysConfigQuiet(pa string) string {
  195. return getSysConfig(pa, true)
  196. }
  197. func IsKernelModuleLoaded(name string) bool {
  198. output, err := procutils.NewRemoteCommandAsFarAsPossible("lsmod").Output()
  199. if err != nil {
  200. log.Errorln(err)
  201. return false
  202. }
  203. for _, line := range strings.Split(string(output), "\n") {
  204. lm := strings.Split(line, " ")
  205. if len(lm) > 0 && utils.IsInStringArray(strings.Replace(name, "-", "_", -1), lm) {
  206. return true
  207. }
  208. }
  209. return false
  210. }
  211. func SetSysConfig(cpath, val string) bool {
  212. if fileutils2.Exists(cpath) {
  213. oval, err := ioutil.ReadFile(cpath)
  214. if err != nil {
  215. log.Errorln(err)
  216. return false
  217. }
  218. if strings.TrimSpace(string(oval)) != val {
  219. err = fileutils2.FilePutContents(cpath, val, false)
  220. if err == nil {
  221. return true
  222. }
  223. log.Errorln(err)
  224. }
  225. }
  226. return false
  227. }
  228. func IsHypervisor() bool {
  229. cont, _ := fileutils2.FileGetContents("/proc/cpuinfo")
  230. if strings.Index(cont, " hypervisor") > 0 {
  231. return true
  232. }
  233. return false
  234. }