Skip to content

Commit 239ed49

Browse files
authored
feat: Copy firmware when container starts not when it's created (#18)
Signed-off-by: Yejin Seo <yejseo01@arm.com>
1 parent 04c7c34 commit 239ed49

4 files changed

Lines changed: 34 additions & 20 deletions

File tree

internal/oci/annotations.go

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

12-
StateResolvedPath = "remoteproc.resolved-path"
13-
StateFirmware = "remoteproc.firmware"
12+
StateDriverPath = "remoteproc.driver-path"
13+
StateFirmwarePath = "remoteproc.firmware-path"
14+
15+
OptionalStateStoredFirmwareName = "remoteproc.stored-firmware-name"
1416
)
1517

1618
func validateSpecAnnotations(spec *specs.Spec) error {
@@ -22,7 +24,7 @@ func validateSpecAnnotations(spec *specs.Spec) error {
2224
}
2325

2426
func validateStateAnnotations(state *specs.State) error {
25-
return validateAnnotationsExist(state.Annotations, StateResolvedPath, StateFirmware)
27+
return validateAnnotationsExist(state.Annotations, StateDriverPath, StateFirmwarePath)
2628
}
2729

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

internal/runtime/create.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,12 @@ 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)
39-
if err != nil {
40-
return fmt.Errorf("failed to store firmware file %s: %w", firmwarePath, err)
41-
}
42-
needCleanup := true
43-
defer func() {
44-
if needCleanup {
45-
_ = remoteproc.RemoveFirmware(storedFirmwareName)
46-
}
47-
}()
4838

4939
pid, err := proxy.NewProcess(devicePath)
5040
if err != nil {
5141
return fmt.Errorf("failed to start proxy process: %w", err)
5242
}
43+
needCleanup := true
5344
defer func() {
5445
if needCleanup {
5546
_ = proxy.StopFirmware(pid)
@@ -58,8 +49,8 @@ func Create(containerID string, bundlePath string, pidFile string) error {
5849

5950
state := oci.NewState(containerID, bundlePath)
6051
state.Pid = pid
61-
state.Annotations[oci.StateResolvedPath] = devicePath
62-
state.Annotations[oci.StateFirmware] = storedFirmwareName
52+
state.Annotations[oci.StateDriverPath] = devicePath
53+
state.Annotations[oci.StateFirmwarePath] = firmwarePath
6354
if err := oci.WriteState(state); err != nil {
6455
return err
6556
}

internal/runtime/delete.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ func delete(containerID string) error {
2929
return fmt.Errorf("cannot delete running container %s", containerID)
3030
}
3131

32-
_ = remoteproc.RemoveFirmware(state.Annotations[oci.StateFirmware])
32+
firmwareName, ok := state.Annotations[oci.OptionalStateStoredFirmwareName]
33+
if ok {
34+
if err := remoteproc.RemoveFirmware(firmwareName); err != nil {
35+
return fmt.Errorf("failed to remove firmware: %w", err)
36+
}
37+
}
3338

3439
if err := oci.RemoveState(containerID); err != nil {
3540
return fmt.Errorf("failed to remove state: %w", err)
@@ -51,8 +56,11 @@ func forceDelete(logger *slog.Logger, containerID string) {
5156
}
5257
}
5358

54-
if err := remoteproc.RemoveFirmware(state.Annotations[oci.StateFirmware]); err != nil {
55-
logger.Error("failed to remove firmware", "error", err)
59+
firmwareName, ok := state.Annotations[oci.OptionalStateStoredFirmwareName]
60+
if ok {
61+
if err := remoteproc.RemoveFirmware(firmwareName); err != nil {
62+
logger.Error("failed to remove firmware", "error", err)
63+
}
5664
}
5765

5866
if err := oci.RemoveState(containerID); err != nil {

internal/runtime/start.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@ func Start(containerID string) error {
1414
if err != nil {
1515
return fmt.Errorf("failed to read state: %w", err)
1616
}
17+
firmwarePath := state.Annotations[oci.StateFirmwarePath]
18+
storedFirmwareName, err := remoteproc.StoreFirmware(firmwarePath)
19+
if err != nil {
20+
return fmt.Errorf("failed to store firmware file %s: %w", firmwarePath, err)
21+
}
22+
state.Annotations[oci.OptionalStateStoredFirmwareName] = storedFirmwareName
23+
needCleanup := true
24+
defer func() {
25+
if needCleanup {
26+
_ = remoteproc.RemoveFirmware(storedFirmwareName)
27+
}
28+
}()
1729

1830
if err := remoteproc.SetFirmware(
19-
state.Annotations[oci.StateResolvedPath],
20-
state.Annotations[oci.StateFirmware],
31+
state.Annotations[oci.StateDriverPath],
32+
storedFirmwareName,
2133
); err != nil {
2234
return fmt.Errorf("failed to set firmware: %w", err)
2335
}
@@ -31,5 +43,6 @@ func Start(containerID string) error {
3143
return fmt.Errorf("failed to write state: %w", err)
3244
}
3345

46+
needCleanup = false
3447
return nil
3548
}

0 commit comments

Comments
 (0)