|
| 1 | +/** |
| 2 | +# Copyright (c) 2022, NVIDIA CORPORATION. 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 dotsosymlinks |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/urfave/cli/v2" |
| 26 | + |
| 27 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" |
| 28 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" |
| 29 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" |
| 30 | +) |
| 31 | + |
| 32 | +type command struct { |
| 33 | + logger logger.Interface |
| 34 | +} |
| 35 | + |
| 36 | +type config struct { |
| 37 | + containerSpec string |
| 38 | + driverVersion string |
| 39 | +} |
| 40 | + |
| 41 | +// NewCommand constructs a hook command with the specified logger |
| 42 | +func NewCommand(logger logger.Interface) *cli.Command { |
| 43 | + c := command{ |
| 44 | + logger: logger, |
| 45 | + } |
| 46 | + return c.build() |
| 47 | +} |
| 48 | + |
| 49 | +// build |
| 50 | +func (m command) build() *cli.Command { |
| 51 | + cfg := config{} |
| 52 | + |
| 53 | + // Create the '' command |
| 54 | + c := cli.Command{ |
| 55 | + Name: "create-dot-so-symlinks", |
| 56 | + Usage: "A hook to create .so symlinks in the container.", |
| 57 | + Action: func(c *cli.Context) error { |
| 58 | + return m.run(c, &cfg) |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + c.Flags = []cli.Flag{ |
| 63 | + &cli.StringFlag{ |
| 64 | + Name: "container-spec", |
| 65 | + Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN", |
| 66 | + Destination: &cfg.containerSpec, |
| 67 | + }, |
| 68 | + &cli.StringFlag{ |
| 69 | + Name: "driver-version", |
| 70 | + Usage: "specify the driver version for which the symlinks are to be created. This assumes driver libraries have the .so.`VERSION` suffix.", |
| 71 | + Destination: &cfg.driverVersion, |
| 72 | + Required: true, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + return &c |
| 77 | +} |
| 78 | + |
| 79 | +func (m command) run(c *cli.Context, cfg *config) error { |
| 80 | + s, err := oci.LoadContainerState(cfg.containerSpec) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("failed to load container state: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + containerRoot, err := s.GetContainerRoot() |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("failed to determined container root: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + locator := lookup.NewLibraryLocator( |
| 91 | + lookup.WithLogger(m.logger), |
| 92 | + lookup.WithRoot(containerRoot), |
| 93 | + lookup.WithOptional(true), |
| 94 | + ) |
| 95 | + libs, err := locator.Locate("*.so." + cfg.driverVersion) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("failed to locate libraries for driver version %v: %v", cfg.driverVersion, err) |
| 98 | + } |
| 99 | + |
| 100 | + for _, lib := range libs { |
| 101 | + if !strings.HasSuffix(lib, ".so."+cfg.driverVersion) { |
| 102 | + continue |
| 103 | + } |
| 104 | + libSoPath := strings.TrimSuffix(lib, "."+cfg.driverVersion) |
| 105 | + libSoXPaths, err := filepath.Glob(libSoPath + ".[0-9]") |
| 106 | + if len(libSoXPaths) != 1 || err != nil { |
| 107 | + continue |
| 108 | + } |
| 109 | + err = os.Symlink(filepath.Base(libSoXPaths[0]), libSoPath) |
| 110 | + if err != nil { |
| 111 | + continue |
| 112 | + } |
| 113 | + } |
| 114 | + return nil |
| 115 | +} |
0 commit comments