uefi_predicate.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 baremetal
  15. import (
  16. "context"
  17. "database/sql"
  18. "fmt"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/image"
  21. "yunion.io/x/onecloud/pkg/compute/models"
  22. "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates"
  23. "yunion.io/x/onecloud/pkg/scheduler/core"
  24. )
  25. type UEFIImagePredicate struct {
  26. predicates.BasePredicate
  27. cacheImage *models.SCachedimage
  28. }
  29. func (f *UEFIImagePredicate) Name() string {
  30. return "uefi_image"
  31. }
  32. func (f *UEFIImagePredicate) Clone() core.FitPredicate {
  33. return new(UEFIImagePredicate)
  34. }
  35. func (f *UEFIImagePredicate) PreExecute(ctx context.Context, u *core.Unit, cs []core.Candidater) (bool, error) {
  36. disks := u.SchedData().Disks
  37. if len(disks) == 0 {
  38. return false, nil
  39. }
  40. imageId := disks[0].ImageId
  41. if len(imageId) == 0 {
  42. return false, nil
  43. }
  44. obj, err := models.CachedimageManager.FetchById(imageId)
  45. if err != nil {
  46. // 忽略第一次上传到glance镜像后未缓存的记录
  47. if err == sql.ErrNoRows {
  48. return false, nil
  49. }
  50. return false, errors.Wrapf(err, "fetch cached image %q", imageId)
  51. }
  52. cacheImage := obj.(*models.SCachedimage)
  53. f.cacheImage = cacheImage
  54. return true, nil
  55. }
  56. func (f *UEFIImagePredicate) isImageUEFI() bool {
  57. if f.cacheImage.UEFI.Bool() {
  58. return true
  59. }
  60. support, err := f.cacheImage.Info.Bool("properties", api.IMAGE_UEFI_SUPPORT)
  61. if err != nil {
  62. return false
  63. }
  64. return support
  65. }
  66. func (f *UEFIImagePredicate) isImageBIOS() bool {
  67. support, err := f.cacheImage.Info.Bool("properties", api.IMAGE_BIOS_SUPPORT)
  68. if err != nil {
  69. return false
  70. }
  71. return support
  72. }
  73. func (f *UEFIImagePredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  74. h := predicates.NewPredicateHelper(f, u, c)
  75. bios := u.SchedData().Bios
  76. imgName := f.cacheImage.GetName()
  77. hostName := c.Getter().Name()
  78. isBIOSImage := f.isImageBIOS()
  79. isUEFIImage := f.isImageUEFI()
  80. if !isUEFIImage { // if not uefi image, set default bios image
  81. isBIOSImage = true
  82. }
  83. isUEFIHost := c.Getter().Host().IsUEFIBoot()
  84. if bios == "UEFI" {
  85. if !isUEFIImage || !isUEFIHost {
  86. h.Exclude(fmt.Sprintf("request boot UEFI, but host %s UEFI is %v, image %s UEFI is %v",
  87. hostName, isUEFIHost, imgName, isUEFIImage))
  88. }
  89. } else if bios == "BIOS" {
  90. if !isBIOSImage || isUEFIHost {
  91. h.Exclude(fmt.Sprintf("request boot BIOS, but host %s UEFI is %v, image %s BIOS is %v",
  92. hostName, isUEFIHost, imgName, isBIOSImage))
  93. }
  94. } else {
  95. if isUEFIHost {
  96. if !isUEFIImage {
  97. h.Exclude(fmt.Sprintf("host %s UEFI is %v, image %s UEFI is %v",
  98. hostName, isUEFIHost, imgName, isUEFIImage))
  99. }
  100. } else {
  101. if !isBIOSImage {
  102. h.Exclude(fmt.Sprintf("host %s UEFI is %v, image %s BIOS is %v",
  103. hostName, isUEFIHost, imgName, isBIOSImage))
  104. }
  105. }
  106. }
  107. return h.GetResult()
  108. }