|
| 1 | +/** |
| 2 | +# Copyright (c) Advanced Micro Devices, Inc. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "os" |
| 24 | + "strings" |
| 25 | + |
| 26 | + gpuTracker "github.com/ROCm/container-toolkit/internal/gpu-tracker" |
| 27 | + "github.com/ROCm/container-toolkit/internal/logger" |
| 28 | + "github.com/ROCm/container-toolkit/internal/oci" |
| 29 | +) |
| 30 | + |
| 31 | +func doPrestart() error { |
| 32 | + logger.Log.Println("Running prestart hook") |
| 33 | + |
| 34 | + // Read hook state from stdin (Docker/containerd provides this) |
| 35 | + hookState, err := ioutil.ReadAll(os.Stdin) |
| 36 | + if err != nil { |
| 37 | + return fmt.Errorf("failed to read hook state from stdin: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Create OCI interface for hook context |
| 41 | + ociInterface, err := oci.NewFromStdin() |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to create OCI interface: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Load spec from bundle path in hook state |
| 47 | + ociImpl, ok := ociInterface.(*oci.oci_t) |
| 48 | + if !ok { |
| 49 | + return fmt.Errorf("failed to cast OCI interface to oci_t") |
| 50 | + } |
| 51 | + |
| 52 | + if err := ociImpl.LoadSpecFromHookState(hookState); err != nil { |
| 53 | + return fmt.Errorf("failed to load spec from hook state: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Check if GPU devices are requested |
| 57 | + spec := ociInterface.GetSpec() |
| 58 | + if spec == nil || spec.Process == nil { |
| 59 | + logger.Log.Println("No process spec found, skipping GPU configuration") |
| 60 | + return nil |
| 61 | + } |
| 62 | + |
| 63 | + hasGPURequest := false |
| 64 | + for _, env := range spec.Process.Env { |
| 65 | + if strings.HasPrefix(env, "AMD_VISIBLE_DEVICES=") || |
| 66 | + strings.HasPrefix(env, "DOCKER_RESOURCE_") { |
| 67 | + hasGPURequest = true |
| 68 | + break |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if !hasGPURequest { |
| 73 | + logger.Log.Println("No GPU devices requested, skipping configuration") |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + // Add GPU devices to spec |
| 78 | + if err := ociInterface.UpdateSpec(oci.AddGPUDevices); err != nil { |
| 79 | + return fmt.Errorf("failed to add GPU devices: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + // Write updated spec back |
| 83 | + if err := ociInterface.WriteSpec(); err != nil { |
| 84 | + return fmt.Errorf("failed to write updated spec: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + logger.Log.Println("Successfully configured GPU devices") |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func doPoststop() error { |
| 92 | + logger.Log.Println("Running poststop hook") |
| 93 | + |
| 94 | + // Read hook state to get container ID |
| 95 | + hookState, err := ioutil.ReadAll(os.Stdin) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("failed to read hook state from stdin: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + var state struct { |
| 101 | + ID string `json:"id"` |
| 102 | + } |
| 103 | + if err := json.Unmarshal(hookState, &state); err != nil { |
| 104 | + return fmt.Errorf("failed to parse hook state: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Release GPUs via tracker |
| 108 | + tracker, err := gpuTracker.New() |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("failed to create GPU tracker: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + tracker.ReleaseGPUs(state.ID) |
| 114 | + logger.Log.Printf("Released GPUs for container %s", state.ID) |
| 115 | + return nil |
| 116 | +} |
0 commit comments