github.com/containerd/nerdctl@v1.7.7/pkg/defaults/defaults_linux.go (about)

     1  /*
     2     Copyright The containerd Authors.
     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 defaults
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/containerd/containerd/plugin"
    26  	gocni "github.com/containerd/go-cni"
    27  	"github.com/containerd/log"
    28  	"github.com/containerd/nerdctl/pkg/rootlessutil"
    29  )
    30  
    31  const (
    32  	AppArmorProfileName = "nerdctl-default"
    33  	SeccompProfileName  = "builtin"
    34  	Runtime             = plugin.RuntimeRuncV2
    35  )
    36  
    37  func DataRoot() string {
    38  	if !rootlessutil.IsRootless() {
    39  		return "/var/lib/nerdctl"
    40  	}
    41  	xdh, err := rootlessutil.XDGDataHome()
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  	return filepath.Join(xdh, "nerdctl")
    46  }
    47  
    48  func CNIPath() string {
    49  	candidates := []string{
    50  		gocni.DefaultCNIDir, // /opt/cni/bin
    51  		"/usr/local/libexec/cni",
    52  		"/usr/local/lib/cni",
    53  		"/usr/libexec/cni", // Fedora
    54  		"/usr/lib/cni",     // debian (containernetworking-plugins)
    55  	}
    56  	if rootlessutil.IsRootless() {
    57  		home := os.Getenv("HOME")
    58  		if home == "" {
    59  			panic("environment variable HOME is not set")
    60  		}
    61  		candidates = append([]string{
    62  			// NOTE: These user paths are not defined in XDG
    63  			filepath.Join(home, "opt/cni/bin"),
    64  			filepath.Join(home, ".local/libexec/cni"),
    65  			filepath.Join(home, ".local/lib/cni"),
    66  		}, candidates...)
    67  	}
    68  
    69  	for _, f := range candidates {
    70  		if _, err := os.Stat(f); err == nil {
    71  			return f
    72  		}
    73  	}
    74  
    75  	// default: /opt/cni/bin
    76  	return gocni.DefaultCNIDir
    77  }
    78  
    79  func CNINetConfPath() string {
    80  	if !rootlessutil.IsRootless() {
    81  		return gocni.DefaultNetDir
    82  	}
    83  	xch, err := rootlessutil.XDGConfigHome()
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  	return filepath.Join(xch, "cni/net.d")
    88  }
    89  
    90  func CNIRuntimeDir() string {
    91  	if !rootlessutil.IsRootless() {
    92  		return "/run/cni"
    93  	}
    94  	xdr, err := rootlessutil.XDGRuntimeDir()
    95  	if err != nil {
    96  		log.L.Warn(err)
    97  		xdr = fmt.Sprintf("/run/user/%d", rootlessutil.ParentEUID())
    98  	}
    99  	return fmt.Sprintf("%s/cni", xdr)
   100  }
   101  
   102  func BuildKitHost() string {
   103  	if !rootlessutil.IsRootless() {
   104  		return "unix:///run/buildkit/buildkitd.sock"
   105  	}
   106  	xdr, err := rootlessutil.XDGRuntimeDir()
   107  	if err != nil {
   108  		log.L.Warn(err)
   109  		xdr = fmt.Sprintf("/run/user/%d", rootlessutil.ParentEUID())
   110  	}
   111  	return fmt.Sprintf("unix://%s/buildkit/buildkitd.sock", xdr)
   112  }
   113  
   114  func NerdctlTOML() string {
   115  	if !rootlessutil.IsRootless() {
   116  		return "/etc/nerdctl/nerdctl.toml"
   117  	}
   118  	xch, err := rootlessutil.XDGConfigHome()
   119  	if err != nil {
   120  		panic(err)
   121  	}
   122  	return filepath.Join(xch, "nerdctl/nerdctl.toml")
   123  }
   124  
   125  func HostsDirs() []string {
   126  	if !rootlessutil.IsRootless() {
   127  		return []string{"/etc/containerd/certs.d", "/etc/docker/certs.d"}
   128  	}
   129  	xch, err := rootlessutil.XDGConfigHome()
   130  	if err != nil {
   131  		panic(err)
   132  	}
   133  	return []string{
   134  		filepath.Join(xch, "containerd/certs.d"),
   135  		filepath.Join(xch, "docker/certs.d"),
   136  	}
   137  }
   138  
   139  // HostGatewayIP returns the non-loop-back host ip if available and returns empty string if running into error.
   140  func HostGatewayIP() string {
   141  	addrs, err := net.InterfaceAddrs()
   142  	if err != nil {
   143  		return ""
   144  	}
   145  	for _, addr := range addrs {
   146  		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
   147  			if ipnet.IP.To4() != nil {
   148  				return ipnet.IP.String()
   149  			}
   150  		}
   151  	}
   152  	return ""
   153  }