forked from arm/remoteproc-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
94 lines (82 loc) · 2.42 KB
/
Copy pathcreate.go
File metadata and controls
94 lines (82 loc) · 2.42 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
package runtime
import (
"fmt"
"os"
"path/filepath"
"github.com/arm/remoteproc-runtime/internal/oci"
"github.com/arm/remoteproc-runtime/internal/proxy"
"github.com/arm/remoteproc-runtime/internal/remoteproc"
"github.com/opencontainers/runtime-spec/specs-go"
)
func Create(containerID string, bundlePath string, pidFile string) error {
spec, err := oci.ReadSpec(bundlePath)
if err != nil {
return fmt.Errorf("failed to read container specification: %w", err)
}
name := spec.Annotations[oci.SpecName]
devicePath, err := remoteproc.FindDevicePath(name)
if err != nil {
return fmt.Errorf("can't determine remoteproc path: %w", err)
}
firmwareName, err := extractFirmwareName(spec)
if err != nil {
return fmt.Errorf("can't extract firmware name: %w", err)
}
absRootFS := spec.Root.Path
if !filepath.IsAbs(absRootFS) {
absRootFS = filepath.Join(bundlePath, absRootFS)
}
firmwarePath := filepath.Join(absRootFS, firmwareName)
if err := validateFirmwareExists(firmwarePath); err != nil {
return err
}
storedFirmwareName, err := remoteproc.StoreFirmware(firmwarePath)
if err != nil {
return fmt.Errorf("failed to store firmware file %s: %w", firmwarePath, err)
}
needCleanup := true
defer func() {
if needCleanup {
_ = remoteproc.RemoveFirmware(storedFirmwareName)
}
}()
pid, err := proxy.NewProcess(spec, devicePath)
if err != nil {
return fmt.Errorf("failed to start proxy process: %w", err)
}
defer func() {
if needCleanup {
_ = proxy.StopFirmware(pid)
}
}()
state := oci.NewState(containerID, bundlePath)
state.Pid = pid
state.Annotations[oci.StateResolvedPath] = devicePath
state.Annotations[oci.StateFirmware] = storedFirmwareName
if err := oci.WriteState(state); err != nil {
return err
}
if pidFile != "" {
if err := writePidFile(pidFile, pid); err != nil {
return fmt.Errorf("failed to write PID file: %w", err)
}
}
needCleanup = false
return nil
}
func extractFirmwareName(spec *specs.Spec) (string, error) {
if len(spec.Process.Args) != 1 {
return "", fmt.Errorf("expected exactly one process argument")
}
return spec.Process.Args[0], nil
}
func validateFirmwareExists(firmwareFilePath string) error {
if _, err := os.Stat(firmwareFilePath); err != nil {
return fmt.Errorf("requested firmware does not exist: %w", err)
}
return nil
}
func writePidFile(pidFile string, pid int) error {
content := fmt.Sprintf("%d", pid)
return os.WriteFile(pidFile, []byte(content), 0o644)
}