Skip to content

Commit 2784306

Browse files
committed
feat: Copy the firmware to /lib/firmware at start not create
Signed-off-by: Yejin Seo <yejseo01@arm.com>
1 parent 598d3fa commit 2784306

7 files changed

Lines changed: 36 additions & 22 deletions

File tree

internal/oci/annotations.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
const (
1010
SpecName = "remoteproc.name"
1111

12-
StateResolvedPath = "remoteproc.resolved-path"
13-
StateFirmware = "remoteproc.firmware"
12+
StateDriverPath = "remoteproc.resolved-path"
13+
StateFirmwarePath = "remoteproc.firmware"
1414
)
1515

1616
func validateSpecAnnotations(spec *specs.Spec) error {
@@ -22,7 +22,7 @@ func validateSpecAnnotations(spec *specs.Spec) error {
2222
}
2323

2424
func validateStateAnnotations(state *specs.State) error {
25-
return validateAnnotationsExist(state.Annotations, StateResolvedPath, StateFirmware)
25+
return validateAnnotationsExist(state.Annotations, StateDriverPath, StateFirmwarePath)
2626
}
2727

2828
func validateAnnotationsExist(annotations map[string]string, keys ...string) error {

internal/remoteproc/remoteproc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ func StoreFirmware(sourcePath string) (string, error) {
110110
return targetFileName, nil
111111
}
112112

113+
// RemoveFirmware deletes a firmware file from /lib/firmware.
113114
func RemoveFirmware(firmwareFileName string) error {
114115
return os.Remove(filepath.Join(rprocFirmwareStorePath, firmwareFileName))
115116
}
116117

118+
// SetFirmware writes the firmware file name to the remoteproc firmware sysfs entry.
117119
func SetFirmware(devicePath string, firmwareFileName string) error {
118120
state, err := GetState(devicePath)
119121
if err != nil {

internal/runtime/create.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,17 @@ func Create(containerID string, bundlePath string, pidFile string) error {
3535
if err := validateFirmwareExists(firmwarePath); err != nil {
3636
return err
3737
}
38-
storedFirmwareName, err := remoteproc.StoreFirmware(firmwarePath)
3938
if err != nil {
4039
return fmt.Errorf("failed to store firmware file %s: %w", firmwarePath, err)
4140
}
42-
needCleanup := true
43-
defer func() {
44-
if needCleanup {
45-
_ = remoteproc.RemoveFirmware(storedFirmwareName)
46-
}
47-
}()
4841

4942
pid, err := proxy.NewProcess(devicePath)
5043
if err != nil {
5144
return fmt.Errorf("failed to start proxy process: %w", err)
5245
}
46+
47+
needCleanup := true
48+
5349
defer func() {
5450
if needCleanup {
5551
_ = proxy.StopFirmware(pid)
@@ -58,8 +54,8 @@ func Create(containerID string, bundlePath string, pidFile string) error {
5854

5955
state := oci.NewState(containerID, bundlePath)
6056
state.Pid = pid
61-
state.Annotations[oci.StateResolvedPath] = devicePath
62-
state.Annotations[oci.StateFirmware] = storedFirmwareName
57+
state.Annotations[oci.StateDriverPath] = devicePath
58+
state.Annotations[oci.StateFirmwarePath] = firmwarePath
6359
if err := oci.WriteState(state); err != nil {
6460
return err
6561
}

internal/runtime/delete.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func Delete(logger *slog.Logger, containerID string, force bool) error {
2222
func delete(containerID string) error {
2323
state, err := oci.ReadState(containerID)
2424
if err != nil {
25-
return fmt.Errorf("failed to read state: %w", err)
25+
return fmt.Errorf("failed to read state 1: %w", err)
2626
}
2727

2828
if state.Status == specs.StateRunning {
2929
return fmt.Errorf("cannot delete running container %s", containerID)
3030
}
3131

32-
_ = remoteproc.RemoveFirmware(state.Annotations[oci.StateFirmware])
32+
_ = remoteproc.RemoveFirmware(state.Annotations[oci.StateFirmwarePath])
3333

3434
if err := oci.RemoveState(containerID); err != nil {
3535
return fmt.Errorf("failed to remove state: %w", err)
@@ -41,7 +41,7 @@ func delete(containerID string) error {
4141
func forceDelete(logger *slog.Logger, containerID string) {
4242
state, err := oci.ReadState(containerID)
4343
if err != nil {
44-
logger.Error("failed to read state", "error", err)
44+
logger.Error("failed to read state 2", "error", err)
4545
return
4646
}
4747

@@ -51,7 +51,7 @@ func forceDelete(logger *slog.Logger, containerID string) {
5151
}
5252
}
5353

54-
if err := remoteproc.RemoveFirmware(state.Annotations[oci.StateFirmware]); err != nil {
54+
if err := remoteproc.RemoveFirmware(state.Annotations[oci.StateFirmwarePath]); err != nil {
5555
logger.Error("failed to remove firmware", "error", err)
5656
}
5757

internal/runtime/kill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func Kill(containerID string, signal syscall.Signal) error {
1313
state, err := oci.ReadState(containerID)
1414
if err != nil {
15-
return fmt.Errorf("failed to read state: %w", err)
15+
return fmt.Errorf("failed to read state 3: %w", err)
1616
}
1717

1818
if state.Pid > 0 {

internal/runtime/start.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,28 @@ import (
1212
func Start(containerID string) error {
1313
state, err := oci.ReadState(containerID)
1414
if err != nil {
15-
return fmt.Errorf("failed to read state: %w", err)
15+
return fmt.Errorf("failed to read state 4: %w", err)
1616
}
17+
firmwarePath := state.Annotations[oci.StateFirmwarePath]
18+
if firmwarePath == "" {
19+
return fmt.Errorf("firmware path annotation missing")
20+
}
21+
22+
storedFirmwareName, err := remoteproc.StoreFirmware(firmwarePath)
23+
if err != nil {
24+
return fmt.Errorf("failed to store firmware file %s: %w", firmwarePath, err)
25+
}
26+
needsCleanup := true
27+
28+
defer func() {
29+
if needsCleanup {
30+
_ = remoteproc.RemoveFirmware(storedFirmwareName)
31+
}
32+
}()
1733

1834
if err := remoteproc.SetFirmware(
19-
state.Annotations[oci.StateResolvedPath],
20-
state.Annotations[oci.StateFirmware],
35+
state.Annotations[oci.StateDriverPath],
36+
storedFirmwareName,
2137
); err != nil {
2238
return fmt.Errorf("failed to set firmware: %w", err)
2339
}
@@ -30,6 +46,6 @@ func Start(containerID string) error {
3046
if err := oci.WriteState(state); err != nil {
3147
return fmt.Errorf("failed to write state: %w", err)
3248
}
33-
49+
needsCleanup = false
3450
return nil
3551
}

internal/runtime/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func State(containerID string) (*specs.State, error) {
1111
state, err := oci.ReadState(containerID)
1212
if err != nil {
13-
return nil, fmt.Errorf("failed to read state: %w", err)
13+
return nil, fmt.Errorf("failed to read state 5: %w", err)
1414
}
1515
return state, nil
1616
}

0 commit comments

Comments
 (0)