1717package config
1818
1919import (
20+ "fmt"
2021 "os"
2122 "strings"
2223)
@@ -34,29 +35,62 @@ type ContainerCLIConfig struct {
3435 NoPivot bool `toml:"no-pivot,omitempty"`
3536 NoCgroups bool `toml:"no-cgroups"`
3637 User string `toml:"user"`
37- Ldconfig string `toml:"ldconfig"`
38+ // Ldconfig represents the path to the ldconfig binary to be used to update
39+ // the ldcache in a container as it is being created.
40+ // If this path starts with a '@' the path is relative to the host and if
41+ // not it is treated as a container path.
42+ //
43+ // Note that the use of container paths are disabled by default and if this
44+ // is required, the features.allow-ldconfig-from-container feature gate must
45+ // be enabled explicitly.
46+ Ldconfig ldconfigPath `toml:"ldconfig"`
3847}
3948
4049// NormalizeLDConfigPath returns the resolved path of the configured LDConfig binary.
4150// This is only done for host LDConfigs and is required to handle systems where
4251// /sbin/ldconfig is a wrapper around /sbin/ldconfig.real.
4352func (c * ContainerCLIConfig ) NormalizeLDConfigPath () string {
44- return NormalizeLDConfigPath (c .Ldconfig )
53+ return string (c .Ldconfig . normalize () )
4554}
4655
47- // NormalizeLDConfigPath returns the resolved path of the configured LDConfig binary.
56+ // An ldconfigPath is used to represent the path to ldconfig.
57+ type ldconfigPath string
58+
59+ func (p ldconfigPath ) assertValid (allowContainerRelativePath bool ) error {
60+ if p .isHostRelative () {
61+ return nil
62+ }
63+ if allowContainerRelativePath {
64+ return nil
65+ }
66+ return fmt .Errorf ("nvidia-container-cli.ldconfig value %q is not host-relative (does not start with a '@')" , p )
67+ }
68+
69+ func (p ldconfigPath ) isHostRelative () bool {
70+ return strings .HasPrefix (string (p ), "@" )
71+ }
72+
73+ // normalize returns the resolved path of the configured LDConfig binary.
4874// This is only done for host LDConfigs and is required to handle systems where
4975// /sbin/ldconfig is a wrapper around /sbin/ldconfig.real.
50- func NormalizeLDConfigPath ( path string ) string {
51- if ! strings . HasPrefix ( path , "@" ) {
52- return path
76+ func ( p ldconfigPath ) normalize () ldconfigPath {
77+ if ! p . isHostRelative ( ) {
78+ return p
5379 }
5480
81+ path := string (p )
5582 trimmedPath := strings .TrimSuffix (strings .TrimPrefix (path , "@" ), ".real" )
5683 // If the .real path exists, we return that.
5784 if _ , err := os .Stat (trimmedPath + ".real" ); err == nil {
58- return "@" + trimmedPath + ".real"
85+ return ldconfigPath ( "@" + trimmedPath + ".real" )
5986 }
6087 // If the .real path does not exists (or cannot be read) we return the non-.real path.
61- return "@" + trimmedPath
88+ return ldconfigPath ("@" + trimmedPath )
89+ }
90+
91+ // NormalizeLDConfigPath returns the resolved path of the configured LDConfig binary.
92+ // This is only done for host LDConfigs and is required to handle systems where
93+ // /sbin/ldconfig is a wrapper around /sbin/ldconfig.real.
94+ func NormalizeLDConfigPath (path string ) string {
95+ return string (ldconfigPath (path ).normalize ())
6296}
0 commit comments