dockerd-rootless.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/sh
  2. # dockerd-rootless.sh executes dockerd in rootless mode.
  3. #
  4. # Usage: dockerd-rootless.sh [DOCKERD_OPTIONS]
  5. #
  6. # External dependencies:
  7. # * newuidmap and newgidmap needs to be installed.
  8. # * /etc/subuid and /etc/subgid needs to be configured for the current user.
  9. # * Either one of slirp4netns (>= v0.4.0), VPNKit, lxc-user-nic needs to be installed.
  10. #
  11. # Recognized environment variables:
  12. # * DOCKERD_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".
  13. # * DOCKERD_ROOTLESS_ROOTLESSKIT_MTU=NUM: the MTU value for the rootlesskit network driver. Defaults to 65520 for slirp4netns, 1500 for other drivers.
  14. # * DOCKERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER=(builtin|slirp4netns): the rootlesskit port driver. Defaults to "builtin".
  15. # * DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX=(auto|true|false): whether to protect slirp4netns with a dedicated mount namespace. Defaults to "auto".
  16. # * DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP=(auto|true|false): whether to protect slirp4netns with seccomp. Defaults to "auto".
  17. #
  18. # See the documentation for the further information: https://docs.docker.com/go/rootless/
  19. set -e -x
  20. case "$1" in
  21. "check" | "install" | "uninstall")
  22. echo "Did you mean 'dockerd-rootless-setuptool.sh $@' ?"
  23. exit 1
  24. ;;
  25. esac
  26. if ! [ -w "$XDG_RUNTIME_DIR" ]; then
  27. echo "XDG_RUNTIME_DIR needs to be set and writable"
  28. exit 1
  29. fi
  30. if ! [ -d "$HOME" ]; then
  31. echo "HOME needs to be set and exist."
  32. exit 1
  33. fi
  34. rootlesskit=""
  35. for f in docker-rootlesskit rootlesskit; do
  36. if command -v $f > /dev/null 2>&1; then
  37. rootlesskit=$f
  38. break
  39. fi
  40. done
  41. if [ -z "$rootlesskit" ]; then
  42. echo "rootlesskit needs to be installed"
  43. exit 1
  44. fi
  45. : "${DOCKERD_ROOTLESS_ROOTLESSKIT_NET:=}"
  46. : "${DOCKERD_ROOTLESS_ROOTLESSKIT_MTU:=}"
  47. : "${DOCKERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER:=builtin}"
  48. : "${DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX:=auto}"
  49. : "${DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP:=auto}"
  50. net=$DOCKERD_ROOTLESS_ROOTLESSKIT_NET
  51. mtu=$DOCKERD_ROOTLESS_ROOTLESSKIT_MTU
  52. if [ -z "$net" ]; then
  53. if command -v slirp4netns > /dev/null 2>&1; then
  54. # If --netns-type is present in --help, slirp4netns is >= v0.4.0.
  55. if slirp4netns --help | grep -qw -- --netns-type; then
  56. net=slirp4netns
  57. if [ -z "$mtu" ]; then
  58. mtu=65520
  59. fi
  60. else
  61. echo "slirp4netns found but seems older than v0.4.0. Falling back to VPNKit."
  62. fi
  63. fi
  64. if [ -z "$net" ]; then
  65. if command -v vpnkit > /dev/null 2>&1; then
  66. net=vpnkit
  67. else
  68. echo "Either slirp4netns (>= v0.4.0) or vpnkit needs to be installed"
  69. exit 1
  70. fi
  71. fi
  72. fi
  73. if [ -z "$mtu" ]; then
  74. mtu=1500
  75. fi
  76. dockerd="${DOCKERD:-dockerd}"
  77. if [ -z "$_DOCKERD_ROOTLESS_CHILD" ]; then
  78. _DOCKERD_ROOTLESS_CHILD=1
  79. export _DOCKERD_ROOTLESS_CHILD
  80. if [ "$(id -u)" = "0" ]; then
  81. echo "This script must be executed as a non-privileged user"
  82. exit 1
  83. fi
  84. # `selinuxenabled` always returns false in RootlessKit child, so we execute `selinuxenabled` in the parent.
  85. # https://github.com/rootless-containers/rootlesskit/issues/94
  86. if command -v selinuxenabled > /dev/null 2>&1 && selinuxenabled; then
  87. _DOCKERD_ROOTLESS_SELINUX=1
  88. export _DOCKERD_ROOTLESS_SELINUX
  89. fi
  90. # Re-exec the script via RootlessKit, so as to create unprivileged {user,mount,network} namespaces.
  91. #
  92. # --copy-up allows removing/creating files in the directories by creating tmpfs and symlinks
  93. # * /etc: copy-up is required so as to prevent `/etc/resolv.conf` in the
  94. # namespace from being unexpectedly unmounted when `/etc/resolv.conf` is recreated on the host
  95. # (by either systemd-networkd or NetworkManager)
  96. # * /run: copy-up is required so that we can create /run/docker (hardcoded for plugins) in our namespace
  97. exec $rootlesskit \
  98. --net=$net --mtu=$mtu \
  99. --slirp4netns-sandbox=$DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SANDBOX \
  100. --slirp4netns-seccomp=$DOCKERD_ROOTLESS_ROOTLESSKIT_SLIRP4NETNS_SECCOMP \
  101. --disable-host-loopback --port-driver=$DOCKERD_ROOTLESS_ROOTLESSKIT_PORT_DRIVER \
  102. --copy-up=/etc --copy-up=/run \
  103. --propagation=rslave \
  104. $DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS \
  105. "$0" "$@"
  106. else
  107. [ "$_DOCKERD_ROOTLESS_CHILD" = 1 ]
  108. # remove the symlinks for the existing files in the parent namespace if any,
  109. # so that we can create our own files in our mount namespace.
  110. rm -f /run/docker /run/containerd /run/xtables.lock
  111. if [ -n "$_DOCKERD_ROOTLESS_SELINUX" ]; then
  112. # iptables requires /run in the child to be relabeled. The actual /run in the parent is unaffected.
  113. # https://github.com/containers/podman/blob/e6fc34b71aa9d876b1218efe90e14f8b912b0603/libpod/networking_linux.go#L396-L401
  114. # https://github.com/moby/moby/issues/41230
  115. chcon system_u:object_r:iptables_var_run_t:s0 /run
  116. fi
  117. if [ "$(stat -c %T -f /etc)" = "tmpfs" ] && [ -L "/etc/ssl" ]; then
  118. # Workaround for "x509: certificate signed by unknown authority" on openSUSE Tumbleweed.
  119. # https://github.com/rootless-containers/rootlesskit/issues/225
  120. realpath_etc_ssl=$(realpath /etc/ssl)
  121. rm -f /etc/ssl
  122. mkdir /etc/ssl
  123. mount --rbind ${realpath_etc_ssl} /etc/ssl
  124. fi
  125. exec "$dockerd" "$@"
  126. fi