2022-03-17 13:16:24 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package symlinks
|
|
|
|
|
|
|
|
import (
|
2024-10-16 22:36:10 +00:00
|
|
|
"errors"
|
2022-03-17 13:16:24 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-05-11 13:28:53 +00:00
|
|
|
"strings"
|
2022-03-17 13:16:24 +00:00
|
|
|
|
2023-12-01 01:10:10 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2024-10-16 22:36:10 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
|
2022-03-17 13:16:24 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
|
|
|
)
|
|
|
|
|
|
|
|
type command struct {
|
2023-03-22 12:27:43 +00:00
|
|
|
logger logger.Interface
|
2022-03-17 13:16:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
2022-05-11 13:28:53 +00:00
|
|
|
links cli.StringSlice
|
2022-03-17 13:16:24 +00:00
|
|
|
containerSpec string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCommand constructs a hook command with the specified logger
|
2023-03-22 12:27:43 +00:00
|
|
|
func NewCommand(logger logger.Interface) *cli.Command {
|
2022-03-17 13:16:24 +00:00
|
|
|
c := command{
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
return c.build()
|
|
|
|
}
|
|
|
|
|
2024-10-18 14:26:20 +00:00
|
|
|
// build creates the create-symlink command.
|
2022-03-17 13:16:24 +00:00
|
|
|
func (m command) build() *cli.Command {
|
|
|
|
cfg := config{}
|
|
|
|
|
|
|
|
c := cli.Command{
|
|
|
|
Name: "create-symlinks",
|
2024-10-18 14:23:54 +00:00
|
|
|
Usage: "A hook to create symlinks in the container.",
|
2022-03-17 13:16:24 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
return m.run(c, &cfg)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Flags = []cli.Flag{
|
2022-05-11 13:28:53 +00:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "link",
|
2022-10-24 14:10:23 +00:00
|
|
|
Usage: "Specify a specific link to create. The link is specified as target::link",
|
2022-05-11 13:28:53 +00:00
|
|
|
Destination: &cfg.links,
|
|
|
|
},
|
2024-10-18 14:26:20 +00:00
|
|
|
// The following flags are testing-only flags.
|
2022-03-17 13:16:24 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "container-spec",
|
2024-10-18 14:26:20 +00:00
|
|
|
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN. This is only intended for testing.",
|
2022-03-17 13:16:24 +00:00
|
|
|
Destination: &cfg.containerSpec,
|
2024-10-18 14:26:20 +00:00
|
|
|
Hidden: true,
|
2022-03-17 13:16:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m command) run(c *cli.Context, cfg *config) error {
|
|
|
|
s, err := oci.LoadContainerState(cfg.containerSpec)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load container state: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-10 07:57:57 +00:00
|
|
|
containerRoot, err := s.GetContainerRoot()
|
2022-03-17 13:16:24 +00:00
|
|
|
if err != nil {
|
2022-05-10 07:57:57 +00:00
|
|
|
return fmt.Errorf("failed to determined container root: %v", err)
|
2022-03-17 13:16:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
created := make(map[string]bool)
|
2024-10-18 14:23:54 +00:00
|
|
|
for _, l := range cfg.links.Value() {
|
2024-10-22 09:53:28 +00:00
|
|
|
if created[l] {
|
|
|
|
m.logger.Debugf("Link %v already processed", l)
|
|
|
|
continue
|
|
|
|
}
|
2022-10-24 14:10:23 +00:00
|
|
|
parts := strings.Split(l, "::")
|
2022-05-11 13:28:53 +00:00
|
|
|
if len(parts) != 2 {
|
2024-09-20 18:29:30 +00:00
|
|
|
m.logger.Warningf("Invalid link specification %v", l)
|
|
|
|
continue
|
2022-03-17 13:16:24 +00:00
|
|
|
}
|
|
|
|
|
2024-10-22 10:11:41 +00:00
|
|
|
err := m.createLink(containerRoot, parts[0], parts[1])
|
2022-03-17 13:16:24 +00:00
|
|
|
if err != nil {
|
2024-09-20 18:29:30 +00:00
|
|
|
m.logger.Warningf("Failed to create link %v: %v", parts, err)
|
2022-03-17 13:16:24 +00:00
|
|
|
}
|
2024-10-22 09:53:28 +00:00
|
|
|
created[l] = true
|
2022-03-17 13:16:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-16 21:50:59 +00:00
|
|
|
// createLink creates a symbolic link in the specified container root.
|
|
|
|
// This is equivalent to:
|
|
|
|
//
|
|
|
|
// chroot {{ .containerRoot }} ln -s {{ .target }} {{ .link }}
|
|
|
|
//
|
|
|
|
// If the specified link already exists and points to the same target, this
|
|
|
|
// operation is a no-op. If the link points to a different target, an error is
|
|
|
|
// returned.
|
|
|
|
//
|
|
|
|
// Note that if the link path resolves to an absolute path oudside of the
|
|
|
|
// specified root, this is treated as an absolute path in this root.
|
2024-10-22 10:11:41 +00:00
|
|
|
func (m command) createLink(containerRoot string, targetPath string, link string) error {
|
2024-10-22 10:16:31 +00:00
|
|
|
linkPath := filepath.Join(containerRoot, link)
|
2022-05-11 13:28:02 +00:00
|
|
|
|
2024-10-16 22:36:10 +00:00
|
|
|
exists, err := doesLinkExist(targetPath, linkPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check if link exists: %w", err)
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
m.logger.Debugf("Link %s already exists", linkPath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-11 13:28:02 +00:00
|
|
|
m.logger.Infof("Symlinking %v to %v", linkPath, targetPath)
|
2024-10-16 22:36:10 +00:00
|
|
|
err = os.MkdirAll(filepath.Dir(linkPath), 0755)
|
2022-05-11 13:28:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create directory: %v", err)
|
|
|
|
}
|
2024-10-22 10:08:31 +00:00
|
|
|
err = os.Symlink(targetPath, linkPath)
|
2022-05-11 13:28:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create symlink: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-16 22:36:10 +00:00
|
|
|
|
|
|
|
// doesLinkExist returns true if link exists and points to target.
|
|
|
|
// An error is returned if link exists but points to a different target.
|
|
|
|
func doesLinkExist(target string, link string) (bool, error) {
|
|
|
|
currentTarget, err := symlinks.Resolve(link)
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to resolve existing symlink %s: %w", link, err)
|
|
|
|
}
|
|
|
|
if currentTarget == target {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return true, fmt.Errorf("unexpected link target: %s", currentTarget)
|
|
|
|
}
|