arch.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 arch
  15. import (
  16. "fmt"
  17. "yunion.io/x/onecloud/pkg/hostman/guestman/desc"
  18. )
  19. const (
  20. Arch_x86_64 string = "x86_64"
  21. Arch_aarch64 string = "aarch64"
  22. Arch_riscv64 string = "riscv64"
  23. )
  24. type Arch interface {
  25. GenerateCpuDesc(cpus uint, cpuMax uint, s KVMGuestInstance) (*desc.SGuestCpu, error)
  26. GenerateMemDesc() *desc.SGuestMem
  27. GenerateMachineDesc(accel string) *desc.SGuestMachine
  28. GenerateCdromDesc(osName string, cdrom *desc.SGuestCdrom)
  29. GenerateFloppyDesc(osName string, floppy *desc.SGuestFloppy)
  30. GenerateQgaDesc(qgaPath string) *desc.SGuestQga
  31. GeneratePvpanicDesc() *desc.SGuestPvpanic
  32. GenerateIsaSerialDesc() *desc.SGuestIsaSerial
  33. }
  34. type KVMGuestInstance interface {
  35. IsOldWindows() bool
  36. GetOsName() string
  37. GetKernelVersion() string
  38. CpuMax() (uint, error)
  39. IsNestedVirt() bool
  40. IsKvmSupport() bool
  41. HideKVM() bool
  42. HideHypervisor() bool
  43. }
  44. func NewArch(arch string) Arch {
  45. switch arch {
  46. case Arch_x86_64:
  47. return &X86{}
  48. case Arch_aarch64:
  49. return &ARM{}
  50. case Arch_riscv64:
  51. return &RISCV{}
  52. }
  53. return nil
  54. }
  55. type archBase struct {
  56. }
  57. func (*archBase) GenerateQgaDesc(qgaPath string) *desc.SGuestQga {
  58. charDev := "qga0"
  59. socket := &desc.CharDev{
  60. Backend: "socket",
  61. Id: charDev,
  62. Options: map[string]string{
  63. "path": qgaPath,
  64. "server": "",
  65. "nowait": "",
  66. },
  67. }
  68. serialPort := &desc.VirtSerialPort{
  69. Chardev: charDev,
  70. Name: "org.qemu.guest_agent.0",
  71. }
  72. return &desc.SGuestQga{
  73. Socket: socket,
  74. SerialPort: serialPort,
  75. }
  76. }
  77. func (*archBase) GeneratePvpanicDesc() *desc.SGuestPvpanic {
  78. return nil
  79. }
  80. func (*archBase) GenerateIsaSerialDesc() *desc.SGuestIsaSerial {
  81. return nil
  82. }
  83. type otherArchBase struct {
  84. }
  85. // -device scsi-cd,drive=cd0,share-rw=true
  86. // if=none,file=%s,id=cd0,media=cdrom
  87. func (*otherArchBase) GenerateCdromDesc(osName string, cdrom *desc.SGuestCdrom) {
  88. id := fmt.Sprintf("scsi%d-cd0", cdrom.Ordinal)
  89. scsiDev := desc.NewScsiDevice("", "scsi-cd", id)
  90. scsiDev.Options = map[string]string{"share-rw": "true"}
  91. driveOptions := map[string]string{
  92. "if": "none",
  93. "media": "cdrom",
  94. }
  95. cdrom.Scsi = scsiDev
  96. cdrom.DriveOptions = driveOptions
  97. cdrom.Id = id
  98. }
  99. func (*otherArchBase) GenerateFloppyDesc(osName string, floppy *desc.SGuestFloppy) {
  100. }