Add --link option to nvidia-ctk hook create-symlinks command

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2022-05-11 15:28:53 +02:00
parent b0f7a3809f
commit eac326c5ea

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv" "github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
@ -35,6 +36,7 @@ type command struct {
type config struct { type config struct {
hostRoot string hostRoot string
filenames cli.StringSlice filenames cli.StringSlice
links cli.StringSlice
containerSpec string containerSpec string
} }
@ -70,6 +72,11 @@ func (m command) build() *cli.Command {
Usage: "Specify a (CSV) filename to process", Usage: "Specify a (CSV) filename to process",
Destination: &cfg.filenames, Destination: &cfg.filenames,
}, },
&cli.StringSliceFlag{
Name: "link",
Usage: "Specify a specific link to create. The link is specified as source:target",
Destination: &cfg.links,
},
&cli.StringFlag{ &cli.StringFlag{
Name: "container-spec", Name: "container-spec",
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN", Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
@ -136,28 +143,18 @@ func (m command) run(c *cli.Context, cfg *config) error {
} }
} }
linkPath, err := changeRoot(cfg.hostRoot, containerRoot, candidate) links := cfg.links.Value()
if err != nil { for _, l := range links {
m.logger.Warnf("Failed to resolve path for link %v relative to %v: %v", candidate, cfg.hostRoot, err) parts := strings.Split(l, ":")
if len(parts) != 2 {
m.logger.Warnf("Invalid link specification %v", l)
continue continue
} }
if created[linkPath] { err := m.createLink(created, cfg.hostRoot, containerRoot, parts[0], parts[1])
m.logger.Debugf("Link %v already created", linkPath)
continue
}
m.logger.Infof("Symlinking %v to %v", linkPath, target)
err = os.MkdirAll(filepath.Dir(linkPath), 0755)
if err != nil { if err != nil {
m.logger.Warnf("Faild to create directory: %v", err) m.logger.Warnf("Failed to create link %v: %v", parts, err)
continue
} }
err = os.Symlink(target, linkPath)
if err != nil {
m.logger.Warnf("Failed to create symlink: %v", err)
continue
}
created[linkPath] = true
} }
return nil return nil