github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/daemon/config/config_linux.go (about)

     1  package config // import "github.com/Prakhar-Agarwal-byte/moby/daemon/config"
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os/exec"
     7  	"path/filepath"
     8  
     9  	"github.com/Prakhar-Agarwal-byte/moby/api/types/container"
    10  	"github.com/Prakhar-Agarwal-byte/moby/api/types/system"
    11  	"github.com/Prakhar-Agarwal-byte/moby/opts"
    12  	"github.com/Prakhar-Agarwal-byte/moby/pkg/homedir"
    13  	"github.com/Prakhar-Agarwal-byte/moby/pkg/rootless"
    14  	units "github.com/docker/go-units"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  const (
    19  	// DefaultIpcMode is default for container's IpcMode, if not set otherwise
    20  	DefaultIpcMode = container.IPCModePrivate
    21  
    22  	// DefaultCgroupNamespaceMode is the default mode for containers cgroup namespace when using cgroups v2.
    23  	DefaultCgroupNamespaceMode = container.CgroupnsModePrivate
    24  
    25  	// DefaultCgroupV1NamespaceMode is the default mode for containers cgroup namespace when using cgroups v1.
    26  	DefaultCgroupV1NamespaceMode = container.CgroupnsModeHost
    27  
    28  	// StockRuntimeName is the reserved name/alias used to represent the
    29  	// OCI runtime being shipped with the docker daemon package.
    30  	StockRuntimeName = "runc"
    31  )
    32  
    33  // BridgeConfig stores all the bridge driver specific
    34  // configuration.
    35  type BridgeConfig struct {
    36  	commonBridgeConfig
    37  
    38  	// Fields below here are platform specific.
    39  	MTU                         int    `json:"mtu,omitempty"`
    40  	DefaultIP                   net.IP `json:"ip,omitempty"`
    41  	IP                          string `json:"bip,omitempty"`
    42  	DefaultGatewayIPv4          net.IP `json:"default-gateway,omitempty"`
    43  	DefaultGatewayIPv6          net.IP `json:"default-gateway-v6,omitempty"`
    44  	InterContainerCommunication bool   `json:"icc,omitempty"`
    45  
    46  	EnableIPv6          bool   `json:"ipv6,omitempty"`
    47  	EnableIPTables      bool   `json:"iptables,omitempty"`
    48  	EnableIP6Tables     bool   `json:"ip6tables,omitempty"`
    49  	EnableIPForward     bool   `json:"ip-forward,omitempty"`
    50  	EnableIPMasq        bool   `json:"ip-masq,omitempty"`
    51  	EnableUserlandProxy bool   `json:"userland-proxy,omitempty"`
    52  	UserlandProxyPath   string `json:"userland-proxy-path,omitempty"`
    53  	FixedCIDRv6         string `json:"fixed-cidr-v6,omitempty"`
    54  }
    55  
    56  // Config defines the configuration of a docker daemon.
    57  // It includes json tags to deserialize configuration from a file
    58  // using the same names that the flags in the command line uses.
    59  type Config struct {
    60  	CommonConfig
    61  
    62  	// Fields below here are platform specific.
    63  	Runtimes             map[string]system.Runtime `json:"runtimes,omitempty"`
    64  	DefaultInitBinary    string                    `json:"default-init,omitempty"`
    65  	CgroupParent         string                    `json:"cgroup-parent,omitempty"`
    66  	EnableSelinuxSupport bool                      `json:"selinux-enabled,omitempty"`
    67  	RemappedRoot         string                    `json:"userns-remap,omitempty"`
    68  	Ulimits              map[string]*units.Ulimit  `json:"default-ulimits,omitempty"`
    69  	CPURealtimePeriod    int64                     `json:"cpu-rt-period,omitempty"`
    70  	CPURealtimeRuntime   int64                     `json:"cpu-rt-runtime,omitempty"`
    71  	OOMScoreAdjust       int                       `json:"oom-score-adjust,omitempty"` // Deprecated: configure the daemon's oom-score-adjust using a process manager instead.
    72  	Init                 bool                      `json:"init,omitempty"`
    73  	InitPath             string                    `json:"init-path,omitempty"`
    74  	SeccompProfile       string                    `json:"seccomp-profile,omitempty"`
    75  	ShmSize              opts.MemBytes             `json:"default-shm-size,omitempty"`
    76  	NoNewPrivileges      bool                      `json:"no-new-privileges,omitempty"`
    77  	IpcMode              string                    `json:"default-ipc-mode,omitempty"`
    78  	CgroupNamespaceMode  string                    `json:"default-cgroupns-mode,omitempty"`
    79  	// ResolvConf is the path to the configuration of the host resolver
    80  	ResolvConf string `json:"resolv-conf,omitempty"`
    81  	Rootless   bool   `json:"rootless,omitempty"`
    82  }
    83  
    84  // GetExecRoot returns the user configured Exec-root
    85  func (conf *Config) GetExecRoot() string {
    86  	return conf.ExecRoot
    87  }
    88  
    89  // GetInitPath returns the configured docker-init path
    90  func (conf *Config) GetInitPath() string {
    91  	if conf.InitPath != "" {
    92  		return conf.InitPath
    93  	}
    94  	if conf.DefaultInitBinary != "" {
    95  		return conf.DefaultInitBinary
    96  	}
    97  	return DefaultInitBinary
    98  }
    99  
   100  // LookupInitPath returns an absolute path to the "docker-init" binary by searching relevant "libexec" directories (per FHS 3.0 & 2.3) followed by PATH
   101  func (conf *Config) LookupInitPath() (string, error) {
   102  	binary := conf.GetInitPath()
   103  	if filepath.IsAbs(binary) {
   104  		return binary, nil
   105  	}
   106  
   107  	for _, dir := range []string{
   108  		// FHS 3.0: "/usr/libexec includes internal binaries that are not intended to be executed directly by users or shell scripts. Applications may use a single subdirectory under /usr/libexec."
   109  		// https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch04s07.html
   110  		"/usr/local/libexec/docker",
   111  		"/usr/libexec/docker",
   112  
   113  		// FHS 2.3: "/usr/lib includes object files, libraries, and internal binaries that are not intended to be executed directly by users or shell scripts."
   114  		// https://refspecs.linuxfoundation.org/FHS_2.3/fhs-2.3.html#USRLIBLIBRARIESFORPROGRAMMINGANDPA
   115  		"/usr/local/lib/docker",
   116  		"/usr/lib/docker",
   117  	} {
   118  		// exec.LookPath has a fast-path short-circuit for paths that contain "/" (skipping the PATH lookup) that then verifies whether the given path is likely to be an actual executable binary (so we invoke that instead of reimplementing the same checks)
   119  		if file, err := exec.LookPath(filepath.Join(dir, binary)); err == nil {
   120  			return file, nil
   121  		}
   122  	}
   123  
   124  	// if we checked all the "libexec" directories and found no matches, fall back to PATH
   125  	return exec.LookPath(binary)
   126  }
   127  
   128  // GetResolvConf returns the appropriate resolv.conf
   129  // Check setupResolvConf on how this is selected
   130  func (conf *Config) GetResolvConf() string {
   131  	return conf.ResolvConf
   132  }
   133  
   134  // IsSwarmCompatible defines if swarm mode can be enabled in this config
   135  func (conf *Config) IsSwarmCompatible() error {
   136  	if conf.LiveRestoreEnabled {
   137  		return fmt.Errorf("--live-restore daemon configuration is incompatible with swarm mode")
   138  	}
   139  	return nil
   140  }
   141  
   142  func verifyDefaultIpcMode(mode string) error {
   143  	const hint = `use "shareable" or "private"`
   144  
   145  	dm := container.IpcMode(mode)
   146  	if !dm.Valid() {
   147  		return fmt.Errorf("default IPC mode setting (%v) is invalid; "+hint, dm)
   148  	}
   149  	if dm != "" && !dm.IsPrivate() && !dm.IsShareable() {
   150  		return fmt.Errorf(`IPC mode "%v" is not supported as default value; `+hint, dm)
   151  	}
   152  	return nil
   153  }
   154  
   155  func verifyDefaultCgroupNsMode(mode string) error {
   156  	cm := container.CgroupnsMode(mode)
   157  	if !cm.Valid() {
   158  		return fmt.Errorf(`default cgroup namespace mode (%v) is invalid; use "host" or "private"`, cm)
   159  	}
   160  
   161  	return nil
   162  }
   163  
   164  // ValidatePlatformConfig checks if any platform-specific configuration settings are invalid.
   165  func (conf *Config) ValidatePlatformConfig() error {
   166  	if conf.OOMScoreAdjust != 0 {
   167  		return errors.New(`DEPRECATED: The "oom-score-adjust" config parameter and the dockerd "--oom-score-adjust" options have been removed.`)
   168  	}
   169  	if err := verifyDefaultIpcMode(conf.IpcMode); err != nil {
   170  		return err
   171  	}
   172  
   173  	return verifyDefaultCgroupNsMode(conf.CgroupNamespaceMode)
   174  }
   175  
   176  // IsRootless returns conf.Rootless on Linux but false on Windows
   177  func (conf *Config) IsRootless() bool {
   178  	return conf.Rootless
   179  }
   180  
   181  func setPlatformDefaults(cfg *Config) error {
   182  	cfg.Ulimits = make(map[string]*units.Ulimit)
   183  	cfg.ShmSize = opts.MemBytes(DefaultShmSize)
   184  	cfg.SeccompProfile = SeccompProfileDefault
   185  	cfg.IpcMode = string(DefaultIpcMode)
   186  	cfg.Runtimes = make(map[string]system.Runtime)
   187  
   188  	if cgroups.Mode() != cgroups.Unified {
   189  		cfg.CgroupNamespaceMode = string(DefaultCgroupV1NamespaceMode)
   190  	} else {
   191  		cfg.CgroupNamespaceMode = string(DefaultCgroupNamespaceMode)
   192  	}
   193  
   194  	if rootless.RunningWithRootlessKit() {
   195  		cfg.Rootless = true
   196  
   197  		var err error
   198  		// use rootlesskit-docker-proxy for exposing the ports in RootlessKit netns to the initial namespace.
   199  		cfg.BridgeConfig.UserlandProxyPath, err = exec.LookPath(rootless.RootlessKitDockerProxyBinary)
   200  		if err != nil {
   201  			return errors.Wrapf(err, "running with RootlessKit, but %s not installed", rootless.RootlessKitDockerProxyBinary)
   202  		}
   203  
   204  		dataHome, err := homedir.GetDataHome()
   205  		if err != nil {
   206  			return err
   207  		}
   208  		runtimeDir, err := homedir.GetRuntimeDir()
   209  		if err != nil {
   210  			return err
   211  		}
   212  
   213  		cfg.Root = filepath.Join(dataHome, "docker")
   214  		cfg.ExecRoot = filepath.Join(runtimeDir, "docker")
   215  		cfg.Pidfile = filepath.Join(runtimeDir, "docker.pid")
   216  	} else {
   217  		cfg.Root = "/var/lib/docker"
   218  		cfg.ExecRoot = "/var/run/docker"
   219  		cfg.Pidfile = "/var/run/docker.pid"
   220  	}
   221  
   222  	return nil
   223  }