-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbinaries.go
More file actions
183 lines (153 loc) · 4.31 KB
/
binaries.go
File metadata and controls
183 lines (153 loc) · 4.31 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package firecracker
import (
"embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"github.com/kernel/hypeman/lib/paths"
)
type Version string
const (
V1_14_2 Version = "v1.14.2"
)
const defaultVersion = V1_14_2
var supportedVersions = []Version{
V1_14_2,
}
//go:embed binaries
var binaryFS embed.FS
var (
customBinaryPathMu sync.RWMutex
customBinaryPath string
extractBinaryMu sync.Mutex
)
var versionRegex = regexp.MustCompile(`v?\d+\.\d+\.\d+`)
// SetCustomBinaryPath configures a runtime override for the firecracker binary.
// When set, this path always takes precedence over embedded binaries.
func SetCustomBinaryPath(path string) {
customBinaryPathMu.Lock()
defer customBinaryPathMu.Unlock()
customBinaryPath = strings.TrimSpace(path)
}
func getCustomBinaryPath() string {
customBinaryPathMu.RLock()
defer customBinaryPathMu.RUnlock()
return customBinaryPath
}
func resolveBinaryPath(p *paths.Paths, version string) (string, error) {
if path := getCustomBinaryPath(); path != "" {
if err := validateExecutable(path); err != nil {
return "", fmt.Errorf("invalid firecracker custom binary path %q: %w", path, err)
}
return path, nil
}
if p == nil {
return "", fmt.Errorf("paths are required when using embedded firecracker binaries")
}
return extractBinary(p, parseVersion(version))
}
func parseVersion(version string) Version {
if version == "" {
return defaultVersion
}
for _, supported := range supportedVersions {
if version == string(supported) {
return supported
}
}
return defaultVersion
}
func extractBinary(p *paths.Paths, version Version) (string, error) {
arch, err := normalizeArch()
if err != nil {
return "", err
}
embeddedPath := filepath.ToSlash(filepath.Join("binaries", "firecracker", string(version), arch, "firecracker"))
extractPath := p.FirecrackerBinary(string(version), arch)
if err := validateExecutable(extractPath); err == nil {
return extractPath, nil
}
extractBinaryMu.Lock()
defer extractBinaryMu.Unlock()
// Another goroutine may have already extracted the binary while we waited.
if err := validateExecutable(extractPath); err == nil {
return extractPath, nil
}
data, err := binaryFS.ReadFile(embeddedPath)
if err != nil {
return "", fmt.Errorf("embedded firecracker binary not found at %s (run `make download-firecracker-binaries` or set hypervisor.firecracker_binary_path): %w", embeddedPath, err)
}
if err := os.MkdirAll(filepath.Dir(extractPath), 0755); err != nil {
return "", fmt.Errorf("create firecracker binary directory: %w", err)
}
tmpFile, err := os.CreateTemp(filepath.Dir(extractPath), "firecracker-*")
if err != nil {
return "", fmt.Errorf("create firecracker temp binary: %w", err)
}
tmpPath := tmpFile.Name()
cleanupTmp := true
defer func() {
if cleanupTmp {
_ = os.Remove(tmpPath)
}
}()
if _, err := tmpFile.Write(data); err != nil {
_ = tmpFile.Close()
return "", fmt.Errorf("write firecracker temp binary: %w", err)
}
if err := tmpFile.Chmod(0755); err != nil {
_ = tmpFile.Close()
return "", fmt.Errorf("chmod firecracker temp binary: %w", err)
}
if err := tmpFile.Close(); err != nil {
return "", fmt.Errorf("close firecracker temp binary: %w", err)
}
if err := os.Rename(tmpPath, extractPath); err != nil {
return "", fmt.Errorf("install firecracker binary: %w", err)
}
cleanupTmp = false
return extractPath, nil
}
func detectVersion(binaryPath string) (string, error) {
cmd := exec.Command(binaryPath, "--version")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("run firecracker --version: %w", err)
}
match := versionRegex.FindString(string(out))
if match == "" {
return "", fmt.Errorf("could not parse firecracker version from output: %s", strings.TrimSpace(string(out)))
}
if !strings.HasPrefix(match, "v") {
match = "v" + match
}
return match, nil
}
func normalizeArch() (string, error) {
switch runtime.GOARCH {
case "amd64":
return "x86_64", nil
case "arm64":
return "aarch64", nil
default:
return "", fmt.Errorf("unsupported architecture: %s", runtime.GOARCH)
}
}
func validateExecutable(path string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
if info.IsDir() {
return fmt.Errorf("path is a directory")
}
if info.Mode()&0111 == 0 {
return fmt.Errorf("file is not executable")
}
return nil
}