run.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. #
  4. # Entry point script for netdata
  5. set -e
  6. if [ ! -w / ] && [ "${EUID}" -eq 0 ]; then
  7. echo >&2 "WARNING: This Docker host appears to not properly support newer stat system calls. This is known to cause issues with Netdata (most notably, nodes running on such hosts **cannot be claimed**)."
  8. echo >&2 "WARNING: For more information, see https://learn.netdata.cloud/docs/agent/claim#known-issues-on-older-hosts-with-seccomp-enabled"
  9. fi
  10. # Needed to read Proxmox VMs and (LXC) containers configuration files (name resolution + CPU and memory limits)
  11. function add_netdata_to_proxmox_conf_files_group() {
  12. group_guid="$(stat -c %g /host/etc/pve 2>/dev/null || true)"
  13. [ -z "${group_guid}" ] && return
  14. if ! getent group "${group_guid}" >/dev/null; then
  15. echo "Creating proxmox-etc-pve group with GID ${group_guid}"
  16. if ! addgroup -g "${group_guid}" "proxmox-etc-pve"; then
  17. echo >&2 "Failed to add group proxmox-etc-pve with GID ${group_guid}."
  18. return
  19. fi
  20. fi
  21. if ! getent group "${group_guid}" | grep -q netdata; then
  22. echo "Assign netdata user to group ${group_guid}"
  23. if ! usermod -a -G "${group_guid}" "${DOCKER_USR}"; then
  24. echo >&2 "Failed to add netdata user to group with GID ${group_guid}."
  25. return
  26. fi
  27. fi
  28. }
  29. if [ ! "${DISABLE_TELEMETRY:-0}" -eq 0 ] ||
  30. [ -n "$DISABLE_TELEMETRY" ] ||
  31. [ ! "${DO_NOT_TRACK:-0}" -eq 0 ] ||
  32. [ -n "$DO_NOT_TRACK" ]; then
  33. touch /etc/netdata/.opt-out-from-anonymous-statistics
  34. fi
  35. chmod o+rX / 2>/dev/null || echo "Unable to change permissions without errors."
  36. if [ "${EUID}" -eq 0 ]; then
  37. if [ -n "${NETDATA_EXTRA_APK_PACKAGES}" ]; then
  38. echo >&2 "WARNING: Netdata’s Docker images have switched from Alpine to Debian as a base platform. Supplementary package support is now handled through the NETDATA_EXTRA_DEB_PACKAGES variable instead of NETDATA_EXTRA_APK_PACKAGES."
  39. echo >&2 "WARNING: The container will still run, but supplementary packages listed in NETDATA_EXTRA_APK_PACKAGES will not be installed."
  40. echo >&2 "WARNING: To remove these messages, either undefine NETDATA_EXTRA_APK_PACKAGES, or define it to an empty string."
  41. fi
  42. if [ -n "${NETDATA_EXTRA_DEB_PACKAGES}" ]; then
  43. echo "Fetching APT repository metadata."
  44. if ! apt-get update; then
  45. echo "Failed to fetch APT repository metadata."
  46. else
  47. echo "Installing supplementary packages."
  48. export DEBIAN_FRONTEND="noninteractive"
  49. # shellcheck disable=SC2086
  50. if ! apt-get install -y --no-install-recommends ${NETDATA_EXTRA_DEB_PACKAGES}; then
  51. echo "Failed to install supplementary packages."
  52. fi
  53. fi
  54. fi
  55. BALENA_PGID=$(stat -c %g /var/run/balena.sock 2>/dev/null || true)
  56. DOCKER_PGID=$(stat -c %g /var/run/docker.sock 2>/dev/null || true)
  57. re='^[0-9]+$'
  58. if [[ $BALENA_PGID =~ $re ]]; then
  59. echo "Netdata detected balena-engine.sock"
  60. DOCKER_HOST='/var/run/balena-engine.sock'
  61. PGID="$BALENA_PGID"
  62. elif [[ $DOCKER_PGID =~ $re ]]; then
  63. echo "Netdata detected docker.sock"
  64. DOCKER_HOST="/var/run/docker.sock"
  65. PGID="$DOCKER_PGID"
  66. fi
  67. export PGID
  68. export DOCKER_HOST
  69. if [ -n "${PGID}" ]; then
  70. echo "Creating docker group ${PGID}"
  71. addgroup --gid "${PGID}" "docker" || echo >&2 "Could not add group docker with ID ${PGID}, its already there probably"
  72. echo "Assign netdata user to docker group ${PGID}"
  73. usermod --append --groups "docker" "${DOCKER_USR}" || echo >&2 "Could not add netdata user to group docker with ID ${PGID}"
  74. fi
  75. if [ -d "/host/etc/pve" ]; then
  76. add_netdata_to_proxmox_conf_files_group || true
  77. fi
  78. else
  79. echo >&2 "WARNING: Entrypoint started as non-root user. This is not officially supported and some features may not be available."
  80. fi
  81. if mountpoint -q /etc/netdata; then
  82. echo "Copying stock configuration to /etc/netdata"
  83. cp -an /etc/netdata.stock/* /etc/netdata
  84. cp -an /etc/netdata.stock/.[^.]* /etc/netdata
  85. fi
  86. if [ -w "/etc/netdata" ]; then
  87. if mountpoint -q /etc/netdata; then
  88. hostname >/etc/netdata/.container-hostname
  89. else
  90. rm -f /etc/netdata/.container-hostname
  91. fi
  92. fi
  93. exec /usr/sbin/netdata -u "${DOCKER_USR}" -D -s /host -p "${NETDATA_LISTENER_PORT}" "$@"