github.com/containerd/nerdctl@v1.7.7/extras/rootless/containerd-rootless.sh (about)

     1  #!/bin/sh
     2  
     3  #   Copyright The containerd Authors.
     4  
     5  #   Licensed under the Apache License, Version 2.0 (the "License");
     6  #   you may not use this file except in compliance with the License.
     7  #   You may obtain a copy of the License at
     8  
     9  #       http://www.apache.org/licenses/LICENSE-2.0
    10  
    11  #   Unless required by applicable law or agreed to in writing, software
    12  #   distributed under the License is distributed on an "AS IS" BASIS,
    13  #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  #   See the License for the specific language governing permissions and
    15  #   limitations under the License.
    16  
    17  # -----------------------------------------------------------------------------
    18  # Forked from https://github.com/moby/moby/blob/v20.10.3/contrib/dockerd-rootless.sh
    19  # Copyright The Moby Authors.
    20  # Licensed under the Apache License, Version 2.0
    21  # NOTICE: https://github.com/moby/moby/blob/v20.10.3/NOTICE
    22  # -----------------------------------------------------------------------------
    23  
    24  # containerd-rootless.sh executes containerd in rootless mode.
    25  #
    26  # Usage: containerd-rootless.sh [CONTAINERD_OPTIONS]
    27  #
    28  # External dependencies:
    29  # * newuidmap and newgidmap needs to be installed.
    30  # * /etc/subuid and /etc/subgid needs to be configured for the current user.
    31  # * RootlessKit (>= v0.10.0) needs to be installed. RootlessKit >= v0.14.1 is recommended.
    32  # * Either one of slirp4netns (>= v0.4.0), VPNKit, lxc-user-nic needs to be installed. slirp4netns >= v1.1.7 is recommended.
    33  #
    34  # Recognized environment variables:
    35  # * CONTAINERD_ROOTLESS_ROOTLESSKIT_STATE_DIR=DIR: the rootlesskit state dir. Defaults to "$XDG_RUNTIME_DIR/containerd-rootless".
    36  # * CONTAINERD_ROOTLESS_ROOTLESSKIT_NET=(slirp4netns|vpnkit|lxc-user-nic): the rootlesskit network driver. Defaults to "slirp4netns" if slirp4netns (>= v0.4.0) is installed. Otherwise defaults to "vpnkit".
    37  # * CONTAINERD_ROOTLESS_ROOTLESSKIT_MTU=NUM: the MTU value for the rootlesskit network driver. Defaults to 65520 for slirp4netns, 1500 for other drivers.
    38  # * CONTAINERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER=(builtin|slirp4netns): the rootlesskit port driver. Defaults to "builtin".
    39  # * CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX=(auto|true|false): whether to protect slirp4netns with a dedicated mount namespace. Defaults to "auto".
    40  # * CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP=(auto|true|false): whether to protect slirp4netns with seccomp. Defaults to "auto".
    41  
    42  set -e
    43  if ! [ -w $XDG_RUNTIME_DIR ]; then
    44  	echo "XDG_RUNTIME_DIR needs to be set and writable"
    45  	exit 1
    46  fi
    47  if ! [ -w $HOME ]; then
    48  	echo "HOME needs to be set and writable"
    49  	exit 1
    50  fi
    51  : "${XDG_DATA_HOME:=$HOME/.local/share}"
    52  : "${XDG_CONFIG_HOME:=$HOME/.config}"
    53  
    54  if [ -z $_CONTAINERD_ROOTLESS_CHILD ]; then
    55  	if [ "$(id -u)" = "0" ]; then
    56  		echo "Must not run as root"
    57  		exit 1
    58  	fi
    59  	case "$1" in
    60  	"check" | "install" | "uninstall")
    61  		echo "Did you mean 'containerd-rootless-setuptool.sh $@' ?"
    62  		exit 1
    63  		;;
    64  	esac
    65  
    66  	: "${CONTAINERD_ROOTLESS_ROOTLESSKIT_STATE_DIR:=$XDG_RUNTIME_DIR/containerd-rootless}"
    67  	: "${CONTAINERD_ROOTLESS_ROOTLESSKIT_NET:=}"
    68  	: "${CONTAINERD_ROOTLESS_ROOTLESSKIT_MTU:=}"
    69  	: "${CONTAINERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER:=builtin}"
    70  	: "${CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX:=auto}"
    71  	: "${CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP:=auto}"
    72  	net=$CONTAINERD_ROOTLESS_ROOTLESSKIT_NET
    73  	mtu=$CONTAINERD_ROOTLESS_ROOTLESSKIT_MTU
    74  	if [ -z $net ]; then
    75  		if command -v slirp4netns >/dev/null 2>&1; then
    76  			# If --netns-type is present in --help, slirp4netns is >= v0.4.0.
    77  			if slirp4netns --help | grep -qw -- --netns-type; then
    78  				net=slirp4netns
    79  				if [ -z $mtu ]; then
    80  					mtu=65520
    81  				fi
    82  			else
    83  				echo "slirp4netns found but seems older than v0.4.0. Falling back to VPNKit."
    84  			fi
    85  		fi
    86  		if [ -z $net ]; then
    87  			if command -v vpnkit >/dev/null 2>&1; then
    88  				net=vpnkit
    89  			else
    90  				echo "Either slirp4netns (>= v0.4.0) or vpnkit needs to be installed"
    91  				exit 1
    92  			fi
    93  		fi
    94  	fi
    95  	if [ -z $mtu ]; then
    96  		mtu=1500
    97  	fi
    98  
    99  	_CONTAINERD_ROOTLESS_CHILD=1
   100  	export _CONTAINERD_ROOTLESS_CHILD
   101  
   102  	# `selinuxenabled` always returns false in RootlessKit child, so we execute `selinuxenabled` in the parent.
   103  	# https://github.com/rootless-containers/rootlesskit/issues/94
   104  	if command -v selinuxenabled >/dev/null 2>&1; then
   105  		if selinuxenabled; then
   106  			_CONTAINERD_ROOTLESS_SELINUX=1
   107  			export _CONTAINERD_ROOTLESS_SELINUX
   108  		fi
   109  	fi
   110  	# Re-exec the script via RootlessKit, so as to create unprivileged {user,mount,network} namespaces.
   111  	#
   112  	# --copy-up allows removing/creating files in the directories by creating tmpfs and symlinks
   113  	# * /etc:     copy-up is required so as to prevent `/etc/resolv.conf` in the
   114  	#             namespace from being unexpectedly unmounted when `/etc/resolv.conf` is recreated on the host
   115  	#             (by either systemd-networkd or NetworkManager)
   116  	# * /run:     copy-up is required so that we can create /run/containerd (hardcoded) in our namespace
   117  	# * /var/lib: copy-up is required so that we can create /var/lib/containerd in our namespace
   118  	exec rootlesskit \
   119  		--state-dir=$CONTAINERD_ROOTLESS_ROOTLESSKIT_STATE_DIR \
   120  		--net=$net --mtu=$mtu \
   121  		--slirp4netns-sandbox=$CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX \
   122  		--slirp4netns-seccomp=$CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP \
   123  		--disable-host-loopback --port-driver=$CONTAINERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER \
   124  		--copy-up=/etc --copy-up=/run --copy-up=/var/lib \
   125  		--propagation=rslave \
   126  		$CONTAINERD_ROOTLESS_ROOTLESSKIT_FLAGS \
   127  		$0 $@
   128  else
   129  	[ $_CONTAINERD_ROOTLESS_CHILD = 1 ]
   130  	# Remove the *symlinks* for the existing files in the parent namespace if any,
   131  	# so that we can create our own files in our mount namespace.
   132  	# The actual files in the parent namespace are *not removed* by this rm command.
   133  	rm -f /run/containerd /run/xtables.lock \
   134  		/var/lib/containerd /var/lib/cni /etc/containerd
   135  
   136  	# Bind-mount /etc/ssl.
   137  	# Workaround for "x509: certificate signed by unknown authority" on openSUSE Tumbleweed.
   138  	# https://github.com/rootless-containers/rootlesskit/issues/225
   139  	realpath_etc_ssl=$(realpath /etc/ssl)
   140  	rm -f /etc/ssl
   141  	mkdir /etc/ssl
   142  	mount --rbind "${realpath_etc_ssl}" /etc/ssl
   143  
   144  	# Bind-mount /run/containerd
   145  	mkdir -p "${XDG_RUNTIME_DIR}/containerd" "/run/containerd"
   146  	mount --bind "${XDG_RUNTIME_DIR}/containerd" "/run/containerd"
   147  
   148  	# Bind-mount /var/lib/containerd
   149  	mkdir -p "${XDG_DATA_HOME}/containerd" "/var/lib/containerd"
   150  	mount --bind "${XDG_DATA_HOME}/containerd" "/var/lib/containerd"
   151  
   152  	# Bind-mount /var/lib/cni
   153  	mkdir -p "${XDG_DATA_HOME}/cni" "/var/lib/cni"
   154  	mount --bind "${XDG_DATA_HOME}/cni" "/var/lib/cni"
   155  
   156  	# Bind-mount /etc/containerd
   157  	mkdir -p "${XDG_CONFIG_HOME}/containerd" "/etc/containerd"
   158  	mount --bind "${XDG_CONFIG_HOME}/containerd" "/etc/containerd"
   159  
   160  	if [ -n "$_CONTAINERD_ROOTLESS_SELINUX" ]; then
   161  		# iptables requires /run in the child to be relabeled. The actual /run in the parent is unaffected.
   162  		# https://github.com/containers/podman/blob/e6fc34b71aa9d876b1218efe90e14f8b912b0603/libpod/networking_linux.go#L396-L401
   163  		# https://github.com/moby/moby/issues/41230
   164  		chcon system_u:object_r:iptables_var_run_t:s0 /run
   165  	fi
   166  
   167  	exec containerd $@
   168  fi