osprofile.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 osprofile
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/pkg/utils"
  19. )
  20. const (
  21. OS_TYPE_MACOS = "macOS"
  22. OS_TYPE_VMWARE = "VMWare"
  23. OS_TYPE_LINUX = "Linux"
  24. OS_TYPE_WINDOWS = "Windows"
  25. OS_TYPE_ANDROID = "Android"
  26. )
  27. var OS_TYPES = []string{OS_TYPE_MACOS, OS_TYPE_VMWARE, OS_TYPE_LINUX, OS_TYPE_WINDOWS, OS_TYPE_ANDROID}
  28. var FS_TYPES = []string{"swap", "ext2", "ext3", "ext4", "xfs", "ntfs", "fat", "hfsplus"}
  29. var IMAGE_FORMAT_TYPES = []string{"qcow2", "raw", "docker", "iso", "vmdk", "vmdkflatver1", "vmdkflatver2", "vmdkflat",
  30. "vmdksparse", "vmdksparsever1", "vmdksparsever2", "vmdksesparse", "vhd"}
  31. var DISK_DRIVERS = []string{"virtio", "ide", "scsi", "sata", "pvscsi"}
  32. var DISK_CACHE_MODES = []string{"writeback", "none", "writethrough"}
  33. type SOSProfile struct {
  34. DiskDriver string
  35. NetDriver string
  36. FsFormat string
  37. OSType string
  38. Hypervisor string
  39. }
  40. func GetOSProfile(osname string, hypervisor string) SOSProfile {
  41. switch osname {
  42. case OS_TYPE_MACOS:
  43. return SOSProfile{
  44. DiskDriver: "sata",
  45. NetDriver: "e1000",
  46. FsFormat: "hfsplus",
  47. }
  48. case OS_TYPE_VMWARE:
  49. return SOSProfile{
  50. DiskDriver: "ide",
  51. NetDriver: "vmxnet3",
  52. }
  53. case OS_TYPE_WINDOWS:
  54. if hypervisor == "esxi" {
  55. return SOSProfile{
  56. DiskDriver: "ide",
  57. NetDriver: "e1000",
  58. FsFormat: "ntfs",
  59. }
  60. } else {
  61. return SOSProfile{
  62. DiskDriver: "scsi",
  63. NetDriver: "virtio",
  64. FsFormat: "ntfs",
  65. }
  66. }
  67. case OS_TYPE_LINUX:
  68. if hypervisor == "esxi" {
  69. return SOSProfile{
  70. DiskDriver: "pvscsi",
  71. NetDriver: "vmxnet3",
  72. FsFormat: "ext4",
  73. }
  74. } else {
  75. return SOSProfile{
  76. DiskDriver: "scsi",
  77. NetDriver: "virtio",
  78. FsFormat: "ext4",
  79. }
  80. }
  81. default:
  82. if hypervisor == "esxi" {
  83. return SOSProfile{
  84. DiskDriver: "scsi",
  85. NetDriver: "e1000",
  86. }
  87. } else {
  88. return SOSProfile{
  89. DiskDriver: "ide",
  90. NetDriver: "e1000",
  91. }
  92. }
  93. }
  94. }
  95. func NormalizeOSType(osname string) string {
  96. for _, n := range OS_TYPES {
  97. if strings.ToLower(n) == osname {
  98. return n
  99. }
  100. }
  101. return osname
  102. }
  103. func GetOSProfileFromImageProperties(imgProp map[string]string, hypervisor string) (SOSProfile, error) {
  104. osType, ok := imgProp["os_type"]
  105. if !ok {
  106. return SOSProfile{}, fmt.Errorf("Missing os_type in image properties")
  107. }
  108. var imgHypers []string
  109. imgHyperStr, ok := imgProp["hypervisor"]
  110. if ok && len(imgHyperStr) > 0 {
  111. imgHypers = strings.Split(imgHyperStr, ",")
  112. } else {
  113. imgHypers = []string{}
  114. }
  115. if len(hypervisor) == 0 && len(imgHypers) > 0 {
  116. hypervisor = imgHypers[0]
  117. } else if len(imgHypers) > 0 && len(hypervisor) > 0 && !utils.IsInStringArray(hypervisor, imgHypers) {
  118. return SOSProfile{}, fmt.Errorf("The template requires hypervisor %s", hypervisor)
  119. }
  120. osprofile := GetOSProfile(osType, hypervisor)
  121. diskDriver, ok := imgProp["disk_driver"]
  122. if ok && len(diskDriver) > 0 {
  123. osprofile.DiskDriver = diskDriver
  124. }
  125. netDriver, ok := imgProp["net_driver"]
  126. if ok && len(netDriver) > 0 {
  127. osprofile.NetDriver = netDriver
  128. }
  129. osprofile.OSType = osType
  130. if len(hypervisor) > 0 {
  131. osprofile.Hypervisor = hypervisor
  132. }
  133. return osprofile, nil
  134. }