github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/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 >= v2.0.0 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 # * CONTAINERD_ROOTLESS_ROOTLESSKIT_DETACH_NETNS=(auto|true|false): whether to launch rootlesskit with the "detach-netns" mode. 42 # Defaults to "auto", which is resolved to "true" if RootlessKit >= 2.0 is installed. 43 # The "detached-netns" mode accelerates `nerdctl (pull|push|build)` and enables `nerdctl run --net=host`, 44 # however, there is a relatively minor drawback with BuildKit prior to v0.13: 45 # the host loopback IP address (127.0.0.1) and abstract sockets are exposed to Dockerfile's "RUN" instructions during `nerdctl build` (not `nerdctl run`). 46 # The drawback is fixed in BuildKit v0.13. Upgrading from a prior version of BuildKit needs removing the old systemd unit: 47 # `containerd-rootless-setuptool.sh uninstall-buildkit && rm -f ~/.config/buildkit/buildkitd.toml` 48 49 # See also: https://github.com/containerd/nerdctl/blob/main/docs/rootless.md#configuring-rootlesskit 50 51 set -e 52 if ! [ -w $XDG_RUNTIME_DIR ]; then 53 echo "XDG_RUNTIME_DIR needs to be set and writable" 54 exit 1 55 fi 56 if ! [ -w $HOME ]; then 57 echo "HOME needs to be set and writable" 58 exit 1 59 fi 60 : "${XDG_DATA_HOME:=$HOME/.local/share}" 61 : "${XDG_CONFIG_HOME:=$HOME/.config}" 62 63 if [ -z $_CONTAINERD_ROOTLESS_CHILD ]; then 64 if [ "$(id -u)" = "0" ]; then 65 echo "Must not run as root" 66 exit 1 67 fi 68 case "$1" in 69 "check" | "install" | "uninstall") 70 echo "Did you mean 'containerd-rootless-setuptool.sh $@' ?" 71 exit 1 72 ;; 73 esac 74 75 : "${CONTAINERD_ROOTLESS_ROOTLESSKIT_STATE_DIR:=$XDG_RUNTIME_DIR/containerd-rootless}" 76 : "${CONTAINERD_ROOTLESS_ROOTLESSKIT_NET:=}" 77 : "${CONTAINERD_ROOTLESS_ROOTLESSKIT_MTU:=}" 78 : "${CONTAINERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER:=builtin}" 79 : "${CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX:=auto}" 80 : "${CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP:=auto}" 81 : "${CONTAINERD_ROOTLESS_ROOTLESSKIT_DETACH_NETNS:=auto}" 82 net=$CONTAINERD_ROOTLESS_ROOTLESSKIT_NET 83 mtu=$CONTAINERD_ROOTLESS_ROOTLESSKIT_MTU 84 if [ -z $net ]; then 85 if command -v slirp4netns >/dev/null 2>&1; then 86 # If --netns-type is present in --help, slirp4netns is >= v0.4.0. 87 if slirp4netns --help | grep -qw -- --netns-type; then 88 net=slirp4netns 89 if [ -z $mtu ]; then 90 mtu=65520 91 fi 92 else 93 echo "slirp4netns found but seems older than v0.4.0. Falling back to VPNKit." 94 fi 95 fi 96 if [ -z $net ]; then 97 if command -v vpnkit >/dev/null 2>&1; then 98 net=vpnkit 99 else 100 echo "Either slirp4netns (>= v0.4.0) or vpnkit needs to be installed" 101 exit 1 102 fi 103 fi 104 fi 105 if [ -z $mtu ]; then 106 mtu=1500 107 fi 108 109 _CONTAINERD_ROOTLESS_CHILD=1 110 export _CONTAINERD_ROOTLESS_CHILD 111 112 # `selinuxenabled` always returns false in RootlessKit child, so we execute `selinuxenabled` in the parent. 113 # https://github.com/rootless-containers/rootlesskit/issues/94 114 if command -v selinuxenabled >/dev/null 2>&1; then 115 if selinuxenabled; then 116 _CONTAINERD_ROOTLESS_SELINUX=1 117 export _CONTAINERD_ROOTLESS_SELINUX 118 fi 119 fi 120 121 case "$CONTAINERD_ROOTLESS_ROOTLESSKIT_DETACH_NETNS" in 122 auto) 123 if rootlesskit --help | grep -qw -- "--detach-netns"; then 124 CONTAINERD_ROOTLESS_ROOTLESSKIT_FLAGS=--detach-netns $CONTAINERD_ROOTLESS_ROOTLESSKIT_FLAGS 125 fi 126 ;; 127 1 | true) 128 CONTAINERD_ROOTLESS_ROOTLESSKIT_FLAGS=--detach-netns $CONTAINERD_ROOTLESS_ROOTLESSKIT_FLAGS 129 ;; 130 0 | false) 131 # NOP 132 ;; 133 *) 134 echo "Unknown CONTAINERD_ROOTLESS_ROOTLESSKIT_DETACH_NETNS value: $CONTAINERD_ROOTLESS_ROOTLESSKIT_DETACH_NETNS" 135 exit 1 136 ;; 137 esac 138 139 # Re-exec the script via RootlessKit, so as to create unprivileged {user,mount,network} namespaces. 140 # 141 # --copy-up allows removing/creating files in the directories by creating tmpfs and symlinks 142 # * /etc: copy-up is required so as to prevent `/etc/resolv.conf` in the 143 # namespace from being unexpectedly unmounted when `/etc/resolv.conf` is recreated on the host 144 # (by either systemd-networkd or NetworkManager) 145 # * /run: copy-up is required so that we can create /run/containerd (hardcoded) in our namespace 146 # * /var/lib: copy-up is required so that we can create /var/lib/containerd in our namespace 147 exec rootlesskit \ 148 --state-dir=$CONTAINERD_ROOTLESS_ROOTLESSKIT_STATE_DIR \ 149 --net=$net --mtu=$mtu \ 150 --slirp4netns-sandbox=$CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX \ 151 --slirp4netns-seccomp=$CONTAINERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP \ 152 --disable-host-loopback --port-driver=$CONTAINERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER \ 153 --copy-up=/etc --copy-up=/run --copy-up=/var/lib \ 154 --propagation=rslave \ 155 $CONTAINERD_ROOTLESS_ROOTLESSKIT_FLAGS \ 156 $0 $@ 157 else 158 [ $_CONTAINERD_ROOTLESS_CHILD = 1 ] 159 # Remove the *symlinks* for the existing files in the parent namespace if any, 160 # so that we can create our own files in our mount namespace. 161 # The actual files in the parent namespace are *not removed* by this rm command. 162 rm -f /run/containerd /run/xtables.lock \ 163 /var/lib/containerd /var/lib/cni /etc/containerd 164 165 # Bind-mount /etc/ssl. 166 # Workaround for "x509: certificate signed by unknown authority" on openSUSE Tumbleweed. 167 # https://github.com/rootless-containers/rootlesskit/issues/225 168 realpath_etc_ssl=$(realpath /etc/ssl) 169 rm -f /etc/ssl 170 mkdir /etc/ssl 171 mount --rbind "${realpath_etc_ssl}" /etc/ssl 172 173 # Bind-mount /run/containerd 174 mkdir -p "${XDG_RUNTIME_DIR}/containerd" "/run/containerd" 175 mount --bind "${XDG_RUNTIME_DIR}/containerd" "/run/containerd" 176 177 # Bind-mount /var/lib/containerd 178 mkdir -p "${XDG_DATA_HOME}/containerd" "/var/lib/containerd" 179 mount --bind "${XDG_DATA_HOME}/containerd" "/var/lib/containerd" 180 181 # Bind-mount /var/lib/cni 182 mkdir -p "${XDG_DATA_HOME}/cni" "/var/lib/cni" 183 mount --bind "${XDG_DATA_HOME}/cni" "/var/lib/cni" 184 185 # Bind-mount /etc/containerd 186 mkdir -p "${XDG_CONFIG_HOME}/containerd" "/etc/containerd" 187 mount --bind "${XDG_CONFIG_HOME}/containerd" "/etc/containerd" 188 189 if [ -n "$_CONTAINERD_ROOTLESS_SELINUX" ]; then 190 # iptables requires /run in the child to be relabeled. The actual /run in the parent is unaffected. 191 # https://github.com/containers/podman/blob/e6fc34b71aa9d876b1218efe90e14f8b912b0603/libpod/networking_linux.go#L396-L401 192 # https://github.com/moby/moby/issues/41230 193 chcon system_u:object_r:iptables_var_run_t:s0 /run 194 fi 195 196 exec containerd $@ 197 fi