2018-10-15 07:52:53 +00:00
|
|
|
#!/bin/sh
|
|
|
|
# dockerd-rootless.sh executes dockerd in rootless mode.
|
|
|
|
#
|
|
|
|
# Usage: dockerd-rootless.sh --experimental [DOCKERD_OPTIONS]
|
|
|
|
# Currently, specifying --experimental is mandatory.
|
|
|
|
#
|
|
|
|
# External dependencies:
|
|
|
|
# * newuidmap and newgidmap needs to be installed.
|
|
|
|
# * /etc/subuid and /etc/subgid needs to be configured for the current user.
|
2019-04-19 08:54:29 +00:00
|
|
|
# * Either one of slirp4netns (v0.3+), VPNKit, lxc-user-nic needs to be installed.
|
|
|
|
# slirp4netns is used by default if installed. Otherwise fallsback to VPNKit.
|
|
|
|
# The default value can be overridden with $DOCKERD_ROOTLESS_ROOTLESSKIT_NET=(slirp4netns|vpnkit|lxc-user-nic)
|
2018-10-15 07:52:53 +00:00
|
|
|
#
|
|
|
|
# See the documentation for the further information.
|
|
|
|
|
|
|
|
set -e -x
|
|
|
|
if ! [ -w $XDG_RUNTIME_DIR ]; then
|
|
|
|
echo "XDG_RUNTIME_DIR needs to be set and writable"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if ! [ -w $HOME ]; then
|
|
|
|
echo "HOME needs to be set and writable"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
rootlesskit=""
|
|
|
|
for f in docker-rootlesskit rootlesskit; do
|
2020-03-03 03:27:49 +00:00
|
|
|
if which $f > /dev/null 2>&1; then
|
2018-10-15 07:52:53 +00:00
|
|
|
rootlesskit=$f
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ -z $rootlesskit ]; then
|
|
|
|
echo "rootlesskit needs to be installed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-04-19 08:54:29 +00:00
|
|
|
: "${DOCKERD_ROOTLESS_ROOTLESSKIT_NET:=}"
|
|
|
|
: "${DOCKERD_ROOTLESS_ROOTLESSKIT_MTU:=}"
|
2019-09-02 05:58:58 +00:00
|
|
|
# if slirp4netns v0.4.0+ is installed, slirp4netns is hardened using sandbox (mount namespace) and seccomp
|
|
|
|
: "${DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX:=auto}"
|
|
|
|
: "${DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP:=auto}"
|
2019-04-19 08:54:29 +00:00
|
|
|
net=$DOCKERD_ROOTLESS_ROOTLESSKIT_NET
|
|
|
|
mtu=$DOCKERD_ROOTLESS_ROOTLESSKIT_MTU
|
2018-10-15 07:52:53 +00:00
|
|
|
if [ -z $net ]; then
|
2020-03-03 03:27:49 +00:00
|
|
|
if which slirp4netns > /dev/null 2>&1; then
|
2019-04-19 08:54:29 +00:00
|
|
|
if slirp4netns --help | grep -- --disable-host-loopback; then
|
|
|
|
net=slirp4netns
|
|
|
|
if [ -z $mtu ]; then
|
|
|
|
mtu=65520
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "slirp4netns does not support --disable-host-loopback. Falling back to VPNKit."
|
|
|
|
fi
|
2018-10-15 07:52:53 +00:00
|
|
|
fi
|
2019-04-19 08:54:29 +00:00
|
|
|
if [ -z $net ]; then
|
2020-03-03 03:27:49 +00:00
|
|
|
if which vpnkit > /dev/null 2>&1; then
|
2019-04-19 08:54:29 +00:00
|
|
|
net=vpnkit
|
|
|
|
else
|
|
|
|
echo "Either slirp4netns (v0.3+) or vpnkit needs to be installed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ -z $mtu ]; then
|
|
|
|
mtu=1500
|
2018-10-15 07:52:53 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z $_DOCKERD_ROOTLESS_CHILD ]; then
|
|
|
|
_DOCKERD_ROOTLESS_CHILD=1
|
|
|
|
export _DOCKERD_ROOTLESS_CHILD
|
|
|
|
# Re-exec the script via RootlessKit, so as to create unprivileged {user,mount,network} namespaces.
|
|
|
|
#
|
|
|
|
# --copy-up allows removing/creating files in the directories by creating tmpfs and symlinks
|
|
|
|
# * /etc: copy-up is required so as to prevent `/etc/resolv.conf` in the
|
|
|
|
# namespace from being unexpectedly unmounted when `/etc/resolv.conf` is recreated on the host
|
|
|
|
# (by either systemd-networkd or NetworkManager)
|
|
|
|
# * /run: copy-up is required so that we can create /run/docker (hardcoded for plugins) in our namespace
|
2019-05-15 11:52:59 +00:00
|
|
|
exec $rootlesskit \
|
2019-04-19 08:54:29 +00:00
|
|
|
--net=$net --mtu=$mtu \
|
2019-09-02 05:58:58 +00:00
|
|
|
--slirp4netns-sandbox=$DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX \
|
|
|
|
--slirp4netns-seccomp=$DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP \
|
2019-04-19 08:54:29 +00:00
|
|
|
--disable-host-loopback --port-driver=builtin \
|
2018-10-15 07:52:53 +00:00
|
|
|
--copy-up=/etc --copy-up=/run \
|
|
|
|
$DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS \
|
|
|
|
$0 $@
|
|
|
|
else
|
|
|
|
[ $_DOCKERD_ROOTLESS_CHILD = 1 ]
|
|
|
|
# remove the symlinks for the existing files in the parent namespace if any,
|
|
|
|
# so that we can create our own files in our mount namespace.
|
|
|
|
rm -f /run/docker /run/xtables.lock
|
2019-05-15 11:52:59 +00:00
|
|
|
exec dockerd $@
|
2018-10-15 07:52:53 +00:00
|
|
|
fi
|