Browse code

Merge pull request #208 from thaJeztah/19.03_backport_rootless_fixes

[19.03 backport] backport rootless fixes

Tibor Vass authored on 2019/05/14 10:38:17
Showing 10 changed files
... ...
@@ -9,12 +9,11 @@ import (
9 9
 	"github.com/docker/docker/daemon/config"
10 10
 	"github.com/docker/docker/opts"
11 11
 	"github.com/docker/docker/pkg/homedir"
12
-	"github.com/docker/docker/rootless"
13 12
 	"github.com/spf13/pflag"
14 13
 )
15 14
 
16 15
 func getDefaultPidFile() (string, error) {
17
-	if !rootless.RunningWithNonRootUsername() {
16
+	if !honorXDG {
18 17
 		return "/var/run/docker.pid", nil
19 18
 	}
20 19
 	runtimeDir, err := homedir.GetRuntimeDir()
... ...
@@ -25,7 +24,7 @@ func getDefaultPidFile() (string, error) {
25 25
 }
26 26
 
27 27
 func getDefaultDataRoot() (string, error) {
28
-	if !rootless.RunningWithNonRootUsername() {
28
+	if !honorXDG {
29 29
 		return "/var/lib/docker", nil
30 30
 	}
31 31
 	dataHome, err := homedir.GetDataHome()
... ...
@@ -36,7 +35,7 @@ func getDefaultDataRoot() (string, error) {
36 36
 }
37 37
 
38 38
 func getDefaultExecRoot() (string, error) {
39
-	if !rootless.RunningWithNonRootUsername() {
39
+	if !honorXDG {
40 40
 		return "/var/run/docker", nil
41 41
 	}
42 42
 	runtimeDir, err := homedir.GetRuntimeDir()
... ...
@@ -3,10 +3,13 @@
3 3
 package main
4 4
 
5 5
 import (
6
+	"os/exec"
7
+
6 8
 	"github.com/docker/docker/daemon/config"
7 9
 	"github.com/docker/docker/opts"
8 10
 	"github.com/docker/docker/rootless"
9 11
 	"github.com/docker/go-units"
12
+	"github.com/pkg/errors"
10 13
 	"github.com/spf13/pflag"
11 14
 )
12 15
 
... ...
@@ -35,7 +38,16 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error {
35 35
 	flags.BoolVar(&conf.BridgeConfig.EnableIPv6, "ipv6", false, "Enable IPv6 networking")
36 36
 	flags.StringVar(&conf.BridgeConfig.FixedCIDRv6, "fixed-cidr-v6", "", "IPv6 subnet for fixed IPs")
37 37
 	flags.BoolVar(&conf.BridgeConfig.EnableUserlandProxy, "userland-proxy", true, "Use userland proxy for loopback traffic")
38
-	flags.StringVar(&conf.BridgeConfig.UserlandProxyPath, "userland-proxy-path", "", "Path to the userland proxy binary")
38
+	defaultUserlandProxyPath := ""
39
+	if rootless.RunningWithRootlessKit() {
40
+		var err error
41
+		// use rootlesskit-docker-proxy for exposing the ports in RootlessKit netns to the initial namespace.
42
+		defaultUserlandProxyPath, err = exec.LookPath(rootless.RootlessKitDockerProxyBinary)
43
+		if err != nil {
44
+			return errors.Wrapf(err, "running with RootlessKit, but %s not installed", rootless.RootlessKitDockerProxyBinary)
45
+		}
46
+	}
47
+	flags.StringVar(&conf.BridgeConfig.UserlandProxyPath, "userland-proxy-path", defaultUserlandProxyPath, "Path to the userland proxy binary")
39 48
 	flags.StringVar(&conf.CgroupParent, "cgroup-parent", "", "Set parent cgroup for all containers")
40 49
 	flags.StringVar(&conf.RemappedRoot, "userns-remap", "", "User/Group setting for user namespaces")
41 50
 	flags.BoolVar(&conf.LiveRestoreEnabled, "live-restore", false, "Enable live restore of docker when containers are still running")
... ...
@@ -49,7 +61,8 @@ func installConfigFlags(conf *config.Config, flags *pflag.FlagSet) error {
49 49
 	flags.BoolVar(&conf.NoNewPrivileges, "no-new-privileges", false, "Set no-new-privileges by default for new containers")
50 50
 	flags.StringVar(&conf.IpcMode, "default-ipc-mode", config.DefaultIpcMode, `Default mode for containers ipc ("shareable" | "private")`)
51 51
 	flags.Var(&conf.NetworkConfig.DefaultAddressPools, "default-address-pool", "Default address pools for node specific local networks")
52
-	// Mostly users don't need to set this flag explicitly.
53
-	flags.BoolVar(&conf.Rootless, "rootless", rootless.RunningWithNonRootUsername(), "Enable rootless mode (experimental)")
52
+	// rootless needs to be explicitly specified for running "rootful" dockerd in rootless dockerd (#38702)
53
+	// Note that defaultUserlandProxyPath and honorXDG are configured according to the value of rootless.RunningWithRootlessKit, not the value of --rootless.
54
+	flags.BoolVar(&conf.Rootless, "rootless", rootless.RunningWithRootlessKit(), "Enable rootless mode; typically used with RootlessKit (experimental)")
54 55
 	return nil
55 56
 }
... ...
@@ -103,6 +103,12 @@ func (cli *DaemonCli) start(opts *daemonOptions) (err error) {
103 103
 		if cli.Config.IsRootless() {
104 104
 			logrus.Warn("Running in rootless mode. Cgroups, AppArmor, and CRIU are disabled.")
105 105
 		}
106
+		if rootless.RunningWithRootlessKit() {
107
+			logrus.Info("Running with RootlessKit integration")
108
+			if !cli.Config.IsRootless() {
109
+				return fmt.Errorf("rootless mode needs to be enabled for running with RootlessKit")
110
+			}
111
+		}
106 112
 	} else {
107 113
 		if cli.Config.IsRootless() {
108 114
 			return fmt.Errorf("rootless mode is supported only when running in experimental mode")
... ...
@@ -591,7 +597,7 @@ func loadListeners(cli *DaemonCli, serverConfig *apiserver.Config) ([]string, er
591 591
 	var hosts []string
592 592
 	for i := 0; i < len(cli.Config.Hosts); i++ {
593 593
 		var err error
594
-		if cli.Config.Hosts[i], err = dopts.ParseHost(cli.Config.TLS, rootless.RunningWithNonRootUsername(), cli.Config.Hosts[i]); err != nil {
594
+		if cli.Config.Hosts[i], err = dopts.ParseHost(cli.Config.TLS, honorXDG, cli.Config.Hosts[i]); err != nil {
595 595
 			return nil, errors.Wrapf(err, "error parsing -H %s", cli.Config.Hosts[i])
596 596
 		}
597 597
 
... ...
@@ -668,9 +674,9 @@ func validateAuthzPlugins(requestedPlugins []string, pg plugingetter.PluginGette
668 668
 	return nil
669 669
 }
670 670
 
671
-func systemContainerdRunning(isRootless bool) (string, bool, error) {
671
+func systemContainerdRunning(honorXDG bool) (string, bool, error) {
672 672
 	addr := containerddefaults.DefaultAddress
673
-	if isRootless {
673
+	if honorXDG {
674 674
 		runtimeDir, err := homedir.GetRuntimeDir()
675 675
 		if err != nil {
676 676
 			return "", false, err
... ...
@@ -18,14 +18,13 @@ import (
18 18
 	"github.com/docker/docker/daemon/config"
19 19
 	"github.com/docker/docker/libcontainerd/supervisor"
20 20
 	"github.com/docker/docker/pkg/homedir"
21
-	"github.com/docker/docker/rootless"
22 21
 	"github.com/docker/libnetwork/portallocator"
23 22
 	"github.com/pkg/errors"
24 23
 	"golang.org/x/sys/unix"
25 24
 )
26 25
 
27 26
 func getDefaultDaemonConfigDir() (string, error) {
28
-	if !rootless.RunningWithNonRootUsername() {
27
+	if !honorXDG {
29 28
 		return "/etc/docker", nil
30 29
 	}
31 30
 	// NOTE: CLI uses ~/.docker while the daemon uses ~/.config/docker, because
... ...
@@ -148,7 +147,7 @@ func newCgroupParent(config *config.Config) string {
148 148
 func (cli *DaemonCli) initContainerD(ctx context.Context) (func(time.Duration) error, error) {
149 149
 	var waitForShutdown func(time.Duration) error
150 150
 	if cli.Config.ContainerdAddr == "" {
151
-		systemContainerdAddr, ok, err := systemContainerdRunning(cli.Config.IsRootless())
151
+		systemContainerdAddr, ok, err := systemContainerdRunning(honorXDG)
152 152
 		if err != nil {
153 153
 			return nil, errors.Wrap(err, "could not determine whether the system containerd is running")
154 154
 		}
... ...
@@ -10,11 +10,16 @@ import (
10 10
 	"github.com/docker/docker/pkg/jsonmessage"
11 11
 	"github.com/docker/docker/pkg/reexec"
12 12
 	"github.com/docker/docker/pkg/term"
13
+	"github.com/docker/docker/rootless"
13 14
 	"github.com/moby/buildkit/util/apicaps"
14 15
 	"github.com/sirupsen/logrus"
15 16
 	"github.com/spf13/cobra"
16 17
 )
17 18
 
19
+var (
20
+	honorXDG bool
21
+)
22
+
18 23
 func newDaemonCommand() (*cobra.Command, error) {
19 24
 	opts := newDaemonOptions(config.New())
20 25
 
... ...
@@ -53,6 +58,14 @@ func init() {
53 53
 	if dockerversion.ProductName != "" {
54 54
 		apicaps.ExportedProduct = dockerversion.ProductName
55 55
 	}
56
+	// When running with RootlessKit, $XDG_RUNTIME_DIR, $XDG_DATA_HOME, and $XDG_CONFIG_HOME needs to be
57
+	// honored as the default dirs, because we are unlikely to have permissions to access the system-wide
58
+	// directories.
59
+	//
60
+	// Note that even running with --rootless, when not running with RootlessKit, honorXDG needs to be kept false,
61
+	// because the system-wide directories in the current mount namespace are expected to be accessible.
62
+	// ("rootful" dockerd in rootless dockerd, #38702)
63
+	honorXDG = rootless.RunningWithRootlessKit()
56 64
 }
57 65
 
58 66
 func main() {
... ...
@@ -9,7 +9,9 @@
9 9
 # External dependencies:
10 10
 # * newuidmap and newgidmap needs to be installed.
11 11
 # * /etc/subuid and /etc/subgid needs to be configured for the current user.
12
-# * Either slirp4netns (v0.3+) or VPNKit needs to be installed.
12
+# * Either one of slirp4netns (v0.3+), VPNKit, lxc-user-nic needs to be installed.
13
+#   slirp4netns is used by default if installed. Otherwise fallsback to VPNKit.
14
+#   The default value can be overridden with $DOCKERD_ROOTLESS_ROOTLESSKIT_NET=(slirp4netns|vpnkit|lxc-user-nic)
13 15
 #
14 16
 # See the documentation for the further information.
15 17
 
... ...
@@ -35,24 +37,32 @@ if [ -z $rootlesskit ]; then
35 35
 	exit 1
36 36
 fi
37 37
 
38
-net=""
39
-mtu=""
40
-if which slirp4netns >/dev/null 2>&1; then
41
-	if slirp4netns --help | grep -- --disable-host-loopback; then
42
-		net=slirp4netns
43
-		mtu=65520
44
-	else
45
-		echo "slirp4netns does not support --disable-host-loopback. Falling back to VPNKit."
46
-	fi
47
-fi
38
+: "${DOCKERD_ROOTLESS_ROOTLESSKIT_NET:=}"
39
+: "${DOCKERD_ROOTLESS_ROOTLESSKIT_MTU:=}"
40
+net=$DOCKERD_ROOTLESS_ROOTLESSKIT_NET
41
+mtu=$DOCKERD_ROOTLESS_ROOTLESSKIT_MTU
48 42
 if [ -z $net ]; then
49
-	if which vpnkit >/dev/null 2>&1; then
50
-		net=vpnkit
51
-		mtu=1500
52
-	else
53
-		echo "Either slirp4netns (v0.3+) or vpnkit needs to be installed"
54
-		exit 1
43
+	if which slirp4netns >/dev/null 2>&1; then
44
+		if slirp4netns --help | grep -- --disable-host-loopback; then
45
+			net=slirp4netns
46
+			if [ -z $mtu ]; then
47
+				mtu=65520
48
+			fi
49
+		else
50
+			echo "slirp4netns does not support --disable-host-loopback. Falling back to VPNKit."
51
+		fi
55 52
 	fi
53
+	if [ -z $net ]; then
54
+		if which vpnkit >/dev/null 2>&1; then
55
+			net=vpnkit
56
+		else
57
+			echo "Either slirp4netns (v0.3+) or vpnkit needs to be installed"
58
+			exit 1
59
+		fi
60
+	fi
61
+fi
62
+if [ -z $mtu ]; then
63
+	mtu=1500
56 64
 fi
57 65
 
58 66
 if [ -z $_DOCKERD_ROOTLESS_CHILD ]; then
... ...
@@ -66,7 +76,8 @@ if [ -z $_DOCKERD_ROOTLESS_CHILD ]; then
66 66
 	#         (by either systemd-networkd or NetworkManager)
67 67
 	# * /run: copy-up is required so that we can create /run/docker (hardcoded for plugins) in our namespace
68 68
 	$rootlesskit \
69
-		--net=$net --mtu=$mtu --disable-host-loopback --port-driver=builtin \
69
+		--net=$net --mtu=$mtu \
70
+		--disable-host-loopback --port-driver=builtin \
70 71
 		--copy-up=/etc --copy-up=/run \
71 72
 		$DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS \
72 73
 		$0 $@
... ...
@@ -20,7 +20,6 @@ $ grep ^$(whoami): /etc/subgid
20 20
 penguin:231072:65536
21 21
 ```
22 22
 
23
-* Either [slirp4netns](https://github.com/rootless-containers/slirp4netns) (v0.3+) or [VPNKit](https://github.com/moby/vpnkit) needs to be installed. slirp4netns is preferred for the best performance.
24 23
 
25 24
 ### Distribution-specific hint
26 25
 
... ...
@@ -55,10 +54,9 @@ penguin:231072:65536
55 55
 You need to run `dockerd-rootless.sh` instead of `dockerd`.
56 56
 
57 57
 ```console
58
-$ dockerd-rootless.sh --experimental --userland-proxy --userland-proxy-path=$(which rootlesskit-docker-proxy)"
58
+$ dockerd-rootless.sh --experimental
59 59
 ```
60 60
 As Rootless mode is experimental per se, currently you always need to run `dockerd-rootless.sh` with `--experimental`.
61
-Also, to expose ports, you need to set `--userland-proxy-path` to the path of `rootlesskit-docker-proxy` binary.
62 61
 
63 62
 Remarks:
64 63
 * The socket path is set to `$XDG_RUNTIME_DIR/docker.sock` by default. `$XDG_RUNTIME_DIR` is typically set to `/run/user/$UID`.
... ...
@@ -82,3 +80,12 @@ To route ping packets, you need to set up `net.ipv4.ping_group_range` properly a
82 82
 ```console
83 83
 $ sudo sh -c "echo 0   2147483647  > /proc/sys/net/ipv4/ping_group_range"
84 84
 ```
85
+
86
+### Changing network stack
87
+
88
+`dockerd-rootless.sh` uses [slirp4netns](https://github.com/rootless-containers/slirp4netns) (if installed) or [VPNKit](https://github.com/moby/vpnkit) as the network stack by default.
89
+These network stacks run in userspace and might have performance overhead. See [RootlessKit documentation](https://github.com/rootless-containers/rootlesskit/tree/v0.4.0#network-drivers) for further information.
90
+
91
+Optionally, you can use `lxc-user-nic` instead for the best performance.
92
+To use `lxc-user-nic`, you need to edit [`/etc/lxc/lxc-usernet`](https://github.com/rootless-containers/rootlesskit/tree/v0.4.0#--netlxc-user-nic-experimental) and set `$DOCKERD_ROOTLESS_ROOTLESSKIT_NET=lxc-user-nic`.
93
+
... ...
@@ -1,7 +1,7 @@
1 1
 #!/bin/sh
2 2
 
3
-# v0.3.0
4
-ROOTLESSKIT_COMMIT=70e0502f328bc5ffb14692a7ea41abb77196043b
3
+# v0.4.0
4
+ROOTLESSKIT_COMMIT=e92d5e772ee7e103aecf380c5874a40c52876ff0
5 5
 
6 6
 install_rootlesskit() {
7 7
 	case "$1" in
... ...
@@ -45,13 +45,13 @@ func ValidateHost(val string) (string, error) {
45 45
 }
46 46
 
47 47
 // ParseHost and set defaults for a Daemon host string.
48
-// defaultToTLS is preferred over defaultToUnixRootless.
49
-func ParseHost(defaultToTLS, defaultToUnixRootless bool, val string) (string, error) {
48
+// defaultToTLS is preferred over defaultToUnixXDG.
49
+func ParseHost(defaultToTLS, defaultToUnixXDG bool, val string) (string, error) {
50 50
 	host := strings.TrimSpace(val)
51 51
 	if host == "" {
52 52
 		if defaultToTLS {
53 53
 			host = DefaultTLSHost
54
-		} else if defaultToUnixRootless {
54
+		} else if defaultToUnixXDG {
55 55
 			runtimeDir, err := homedir.GetRuntimeDir()
56 56
 			if err != nil {
57 57
 				return "", err
... ...
@@ -5,22 +5,21 @@ import (
5 5
 	"sync"
6 6
 )
7 7
 
8
+const (
9
+	// RootlessKitDockerProxyBinary is the binary name of rootlesskit-docker-proxy
10
+	RootlessKitDockerProxyBinary = "rootlesskit-docker-proxy"
11
+)
12
+
8 13
 var (
9
-	runningWithNonRootUsername     bool
10
-	runningWithNonRootUsernameOnce sync.Once
14
+	runningWithRootlessKit     bool
15
+	runningWithRootlessKitOnce sync.Once
11 16
 )
12 17
 
13
-// RunningWithNonRootUsername returns true if we $USER is set to a non-root value,
14
-// regardless to the UID/EUID value.
15
-//
16
-// The value of this variable is mostly used for configuring default paths.
17
-// If the value is true, $HOME and $XDG_RUNTIME_DIR should be honored for setting up the default paths.
18
-// If false (not only EUID==0 but also $USER==root), $HOME and $XDG_RUNTIME_DIR should be ignored
19
-// even if we are in a user namespace.
20
-func RunningWithNonRootUsername() bool {
21
-	runningWithNonRootUsernameOnce.Do(func() {
22
-		u := os.Getenv("USER")
23
-		runningWithNonRootUsername = u != "" && u != "root"
18
+// RunningWithRootlessKit returns true if running under RootlessKit namespaces.
19
+func RunningWithRootlessKit() bool {
20
+	runningWithRootlessKitOnce.Do(func() {
21
+		u := os.Getenv("ROOTLESSKIT_STATE_DIR")
22
+		runningWithRootlessKit = u != ""
24 23
 	})
25
-	return runningWithNonRootUsername
24
+	return runningWithRootlessKit
26 25
 }