-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocate.go
More file actions
106 lines (91 loc) · 2.6 KB
/
locate.go
File metadata and controls
106 lines (91 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package qemuctl
import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
)
// ErrQemuNotFound is returned when QEMU cannot be located.
var ErrQemuNotFound = errors.New("QEMU binary not found")
// qemuSearchPaths are additional paths to search for QEMU binaries.
var qemuSearchPaths = []string{
"/pkg/main/app-emulation.qemu.core/bin",
"/usr/bin",
"/usr/local/bin",
}
// archToQemu maps GOARCH values to QEMU binary suffixes.
var archToQemu = map[string]string{
"amd64": "x86_64",
"386": "i386",
"arm64": "aarch64",
"arm": "arm",
"riscv64": "riscv64",
"ppc64": "ppc64",
"ppc64le": "ppc64",
"mips": "mips",
"mips64": "mips64",
"s390x": "s390x",
}
// LocateQemu finds the QEMU system emulator for the given architecture.
// It searches in the following order:
// 1. If customPath is provided and valid, use it
// 2. Search in PATH
// 3. Search in /pkg/main/app-emulation.qemu.core/bin/
// 4. Search in common system paths
//
// The arch parameter should be a GOARCH-style value (e.g., "amd64", "arm64").
// If arch is empty, it defaults to runtime.GOARCH.
func LocateQemu(arch string, customPath string) (string, error) {
if arch == "" {
arch = runtime.GOARCH
}
qemuArch, ok := archToQemu[arch]
if !ok {
return "", &UnsupportedArchError{Arch: arch}
}
binaryName := "qemu-system-" + qemuArch
// 1. Check custom path
if customPath != "" {
if info, err := os.Stat(customPath); err == nil && !info.IsDir() {
return customPath, nil
}
// Check if it's a directory containing the binary
fullPath := filepath.Join(customPath, binaryName)
if info, err := os.Stat(fullPath); err == nil && !info.IsDir() {
return fullPath, nil
}
}
// 2. Search in PATH
if path, err := exec.LookPath(binaryName); err == nil {
return path, nil
}
// 3. Search in known paths
for _, dir := range qemuSearchPaths {
fullPath := filepath.Join(dir, binaryName)
if info, err := os.Stat(fullPath); err == nil && !info.IsDir() {
return fullPath, nil
}
}
return "", ErrQemuNotFound
}
// UnsupportedArchError is returned when the architecture is not supported.
type UnsupportedArchError struct {
Arch string
}
func (e *UnsupportedArchError) Error() string {
return "unsupported architecture: " + e.Arch
}
// SupportedArches returns a list of supported GOARCH values.
func SupportedArches() []string {
arches := make([]string, 0, len(archToQemu))
for arch := range archToQemu {
arches = append(arches, arch)
}
return arches
}
// QemuArchName converts a GOARCH value to the QEMU architecture name.
func QemuArchName(goarch string) (string, bool) {
name, ok := archToQemu[goarch]
return name, ok
}