talos

Форк
0
176 строк · 3.6 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package qemu
6

7
import (
8
	"os/exec"
9
	"path/filepath"
10
)
11

12
// Arch abstracts away differences between different architectures.
13
type Arch string
14

15
// Arch constants.
16
const (
17
	ArchAmd64 Arch = "amd64"
18
	ArchArm64 Arch = "arm64"
19
)
20

21
// Valid checks whether the architecture is supported.
22
func (arch Arch) Valid() bool {
23
	switch arch {
24
	case ArchAmd64, ArchArm64:
25
		return true
26
	default:
27
		return false
28
	}
29
}
30

31
// QemuArch defines which qemu binary to use.
32
func (arch Arch) QemuArch() string {
33
	switch arch {
34
	case ArchAmd64:
35
		return "x86_64"
36
	case ArchArm64:
37
		return "aarch64"
38
	default:
39
		panic("unsupported architecture")
40
	}
41
}
42

43
// QemuMachine defines the machine type for qemu.
44
func (arch Arch) QemuMachine() string {
45
	switch arch {
46
	case ArchAmd64:
47
		return "q35"
48
	case ArchArm64:
49
		return "virt,gic-version=max"
50
	default:
51
		panic("unsupported architecture")
52
	}
53
}
54

55
// Console defines proper argument for the kernel to send logs to serial console.
56
func (arch Arch) Console() string {
57
	switch arch {
58
	case ArchAmd64:
59
		return "ttyS0"
60
	case ArchArm64:
61
		return "ttyAMA0,115200n8"
62
	default:
63
		panic("unsupported architecture")
64
	}
65
}
66

67
// PFlash for UEFI boot.
68
type PFlash struct {
69
	Size        int64
70
	SourcePaths []string
71
}
72

73
// PFlash returns settings for parallel flash.
74
func (arch Arch) PFlash(uefiEnabled bool, extraUEFISearchPaths []string) []PFlash {
75
	switch arch {
76
	case ArchArm64:
77
		uefiSourcePaths := []string{"/usr/share/qemu-efi-aarch64/QEMU_EFI.fd", "/usr/share/OVMF/QEMU_EFI.fd"}
78
		for _, p := range extraUEFISearchPaths {
79
			uefiSourcePaths = append(uefiSourcePaths, filepath.Join(p, "QEMU_EFI.fd"))
80
		}
81

82
		return []PFlash{
83
			{
84
				Size:        64 * 1024 * 1024,
85
				SourcePaths: uefiSourcePaths,
86
			},
87
			{
88
				Size: 64 * 1024 * 1024,
89
			},
90
		}
91
	case ArchAmd64:
92
		if !uefiEnabled {
93
			return nil
94
		}
95

96
		// Default search paths
97
		uefiSourcePathPrefixes := []string{
98
			"/usr/share/ovmf",
99
			"/usr/share/OVMF",
100
			"/usr/share/qemu",
101
		}
102

103
		// Secure boot enabled firmware files
104
		uefiSourceFiles := []string{
105
			"OVMF_CODE_4M.secboot.fd",
106
			"OVMF_CODE.secboot.fd",
107
			"OVMF.secboot.fd",
108
			"edk2-x86_64-secure-code.fd", // Alpine Linux
109
			"ovmf-x86_64-ms-4m-code.bin",
110
		}
111

112
		// Non-secure boot firmware files
113
		uefiSourceFilesInsecure := []string{
114
			"OVMF_CODE_4M.fd",
115
			"OVMF_CODE.fd",
116
			"OVMF.fd",
117
			"ovmf-x86_64-4m-code.bin",
118
		}
119

120
		// Empty vars files
121
		uefiVarsFiles := []string{
122
			"OVMF_VARS_4M.fd",
123
			"OVMF_VARS.fd",
124
			"ovmf-x86_64-4m-vars.bin",
125
		}
126

127
		uefiSourceFiles = append(uefiSourceFiles, uefiSourceFilesInsecure...)
128

129
		// Append extra search paths
130
		uefiSourcePathPrefixes = append(uefiSourcePathPrefixes, extraUEFISearchPaths...)
131

132
		var uefiSourcePaths []string
133

134
		var uefiVarsPaths []string
135

136
		for _, p := range uefiSourcePathPrefixes {
137
			for _, f := range uefiSourceFiles {
138
				uefiSourcePaths = append(uefiSourcePaths, filepath.Join(p, f))
139
			}
140

141
			for _, f := range uefiVarsFiles {
142
				uefiVarsPaths = append(uefiVarsPaths, filepath.Join(p, f))
143
			}
144
		}
145

146
		return []PFlash{
147
			{
148
				Size:        0,
149
				SourcePaths: uefiSourcePaths,
150
			},
151
			{
152
				Size:        0,
153
				SourcePaths: uefiVarsPaths,
154
			},
155
		}
156
	default:
157
		return nil
158
	}
159
}
160

161
// QemuExecutable returns name of qemu executable for the arch.
162
func (arch Arch) QemuExecutable() string {
163
	binaries := []string{
164
		"qemu-system-" + arch.QemuArch(),
165
		"qemu-kvm",
166
		"/usr/libexec/qemu-kvm",
167
	}
168

169
	for _, binary := range binaries {
170
		if path, err := exec.LookPath(binary); err == nil {
171
			return path
172
		}
173
	}
174

175
	return ""
176
}
177

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.