|
| 1 | +/** |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +**/ |
| 17 | + |
| 18 | +package create_soname_symlinks |
| 19 | + |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "log" |
| 24 | + "os" |
| 25 | + |
| 26 | + "github.com/moby/sys/reexec" |
| 27 | + "github.com/urfave/cli/v2" |
| 28 | + |
| 29 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/ldconfig" |
| 30 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" |
| 31 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + reexecUpdateLdCacheCommandName = "reexec-create-soname-symlinks" |
| 36 | +) |
| 37 | + |
| 38 | +type command struct { |
| 39 | + logger logger.Interface |
| 40 | +} |
| 41 | + |
| 42 | +type options struct { |
| 43 | + folders cli.StringSlice |
| 44 | + ldconfigPath string |
| 45 | + containerSpec string |
| 46 | +} |
| 47 | + |
| 48 | +func init() { |
| 49 | + reexec.Register(reexecUpdateLdCacheCommandName, createSonameSymlinksHandler) |
| 50 | + if reexec.Init() { |
| 51 | + os.Exit(0) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// NewCommand constructs an create-soname-symlinks command with the specified logger |
| 56 | +func NewCommand(logger logger.Interface) *cli.Command { |
| 57 | + c := command{ |
| 58 | + logger: logger, |
| 59 | + } |
| 60 | + return c.build() |
| 61 | +} |
| 62 | + |
| 63 | +// build the create-soname-symlinks command |
| 64 | +func (m command) build() *cli.Command { |
| 65 | + cfg := options{} |
| 66 | + |
| 67 | + // Create the 'create-soname-symlinks' command |
| 68 | + c := cli.Command{ |
| 69 | + Name: "create-soname-symlinks", |
| 70 | + Usage: "Create soname symlinks libraries in specified directories", |
| 71 | + Before: func(c *cli.Context) error { |
| 72 | + return m.validateFlags(c, &cfg) |
| 73 | + }, |
| 74 | + Action: func(c *cli.Context) error { |
| 75 | + return m.run(c, &cfg) |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + c.Flags = []cli.Flag{ |
| 80 | + &cli.StringSliceFlag{ |
| 81 | + Name: "folder", |
| 82 | + Usage: "Specify a directory to generate soname symlinks in. Can be specified multiple times", |
| 83 | + Destination: &cfg.folders, |
| 84 | + }, |
| 85 | + &cli.StringFlag{ |
| 86 | + Name: "ldconfig-path", |
| 87 | + Usage: "Specify the path to ldconfig on the host", |
| 88 | + Destination: &cfg.ldconfigPath, |
| 89 | + Value: "/sbin/ldconfig", |
| 90 | + }, |
| 91 | + &cli.StringFlag{ |
| 92 | + Name: "container-spec", |
| 93 | + Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN", |
| 94 | + Destination: &cfg.containerSpec, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + return &c |
| 99 | +} |
| 100 | + |
| 101 | +func (m command) validateFlags(c *cli.Context, cfg *options) error { |
| 102 | + if cfg.ldconfigPath == "" { |
| 103 | + return errors.New("ldconfig-path must be specified") |
| 104 | + } |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (m command) run(c *cli.Context, cfg *options) error { |
| 109 | + s, err := oci.LoadContainerState(cfg.containerSpec) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to load container state: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + containerRootDir, err := s.GetContainerRoot() |
| 115 | + if err != nil || containerRootDir == "" || containerRootDir == "/" { |
| 116 | + return fmt.Errorf("failed to determined container root: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + cmd, err := ldconfig.NewRunner( |
| 120 | + reexecUpdateLdCacheCommandName, |
| 121 | + cfg.ldconfigPath, |
| 122 | + containerRootDir, |
| 123 | + cfg.folders.Value()..., |
| 124 | + ) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + return cmd.Run() |
| 130 | +} |
| 131 | + |
| 132 | +// createSonameSymlinksHandler wraps createSonameSymlinks with error handling. |
| 133 | +func createSonameSymlinksHandler() { |
| 134 | + if err := createSonameSymlinks(os.Args); err != nil { |
| 135 | + log.Printf("Error updating ldcache: %v", err) |
| 136 | + os.Exit(1) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// createSonameSymlinks ensures that soname symlinks are created in the |
| 141 | +// specified directories. |
| 142 | +// It is invoked from a reexec'd handler and provides namespace isolation for |
| 143 | +// the operations performed by this hook. At the point where this is invoked, |
| 144 | +// we are in a new mount namespace that is cloned from the parent. |
| 145 | +// |
| 146 | +// args[0] is the reexec initializer function name |
| 147 | +// args[1] is the path of the ldconfig binary on the host |
| 148 | +// args[2] is the container root directory |
| 149 | +// The remaining args are directories where soname symlinks need to be created. |
| 150 | +func createSonameSymlinks(args []string) error { |
| 151 | + if len(args) < 3 { |
| 152 | + return fmt.Errorf("incorrect arguments: %v", args) |
| 153 | + } |
| 154 | + hostLdconfigPath := args[1] |
| 155 | + containerRootDirPath := args[2] |
| 156 | + |
| 157 | + ldconfig, err := ldconfig.New( |
| 158 | + hostLdconfigPath, |
| 159 | + containerRootDirPath, |
| 160 | + ) |
| 161 | + if err != nil { |
| 162 | + return fmt.Errorf("failed to construct ldconfig runner: %w", err) |
| 163 | + } |
| 164 | + |
| 165 | + return ldconfig.CreateSonameSymlinks(args[3:]...) |
| 166 | +} |
0 commit comments