| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | # |
| 4 | # Entry point script for netdata |
| 5 | |
| 6 | set -e |
| 7 | |
| 8 | DOCKER_USR="${DOCKER_USR:-netdata}" |
| 9 | |
| 10 | if [ ! -w / ] && [ "${EUID}" -eq 0 ]; then |
| 11 | 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**)." |
| 12 | echo >&2 "WARNING: For more information, see https://learn.netdata.cloud/docs/agent/claim#known-issues-on-older-hosts-with-seccomp-enabled" |
| 13 | fi |
| 14 | |
| 15 | # Check if user is a member of a group by GID |
| 16 | # Arguments: $1 = GID, $2 = username |
| 17 | is_user_in_group() { |
| 18 | local gid="$1" |
| 19 | local user="$2" |
| 20 | getent group "${gid}" 2>/dev/null | awk -F: '{print $4}' | tr ',' '\n' | grep -qx "${user}" |
| 21 | } |
| 22 | |
| 23 | # Add user to a group by GID, creating the group if necessary |
| 24 | # Arguments: $1 = GID, $2 = group name (for creation) |
| 25 | add_user_to_gid() { |
| 26 | local gid="$1" |
| 27 | local group_name="$2" |
| 28 | |
| 29 | [ -z "${gid}" ] && return 1 |
| 30 | |
| 31 | if ! getent group "${gid}" > /dev/null; then |
| 32 | echo "Creating ${group_name} group with GID ${gid}" |
| 33 | if ! addgroup --gid "${gid}" "${group_name}"; then |
| 34 | echo >&2 "Failed to add group ${group_name} with GID ${gid}." |
| 35 | return 1 |
| 36 | fi |
| 37 | fi |
| 38 | |
| 39 | if ! is_user_in_group "${gid}" "${DOCKER_USR}"; then |
| 40 | echo "Assigning ${DOCKER_USR} user to group ${gid}" |
| 41 | if ! usermod --append --groups "${gid}" "${DOCKER_USR}"; then |
| 42 | echo >&2 "Failed to add ${DOCKER_USR} user to group with GID ${gid}." |
| 43 | return 1 |
| 44 | fi |
| 45 | fi |
| 46 | } |
| 47 | |
| 48 | # Needed to read Proxmox VMs and (LXC) containers configuration files |
| 49 | add_netdata_to_proxmox_conf_files_group() { |
| 50 | [ "${DOCKER_USR}" = "root" ] && return 0 |
| 51 | |
| 52 | local group_gid |
| 53 | group_gid="$(stat -c %g /host/etc/pve 2> /dev/null || true)" |
| 54 | [ -z "${group_gid}" ] && return 0 |
| 55 | |
| 56 | add_user_to_gid "${group_gid}" "proxmox-etc-pve" |
| 57 | } |
| 58 | |
| 59 | # Needed to access NVIDIA GPU monitoring |
| 60 | add_netdata_to_nvidia_group() { |
| 61 | [ "${DOCKER_USR}" = "root" ] && return 0 |
| 62 | |
| 63 | local group_gid |
| 64 | group_gid="$(stat -c %g /dev/nvidiactl 2> /dev/null || true)" |
| 65 | [ -z "${group_gid}" ] && return 0 |
| 66 | |
| 67 | # Skip if the device is owned by root group |
| 68 | [ "${group_gid}" -eq 0 ] && return 0 |
| 69 | |
| 70 | add_user_to_gid "${group_gid}" "nvidia-dev" |
| 71 | } |
| 72 | |
| 73 | if [ "${DISABLE_TELEMETRY:-0}" != "0" ] || |
| 74 | [ "${DO_NOT_TRACK:-0}" != "0" ]; then |
| 75 | touch /etc/netdata/.opt-out-from-anonymous-statistics |
| 76 | fi |
| 77 | |
| 78 | chmod o+rX / 2> /dev/null || echo "Unable to change permissions without errors." |
| 79 | |
| 80 | if [ "${EUID}" -eq 0 ]; then |
| 81 | if [ -n "${NETDATA_EXTRA_APK_PACKAGES}" ]; then |
| 82 | 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." |
| 83 | echo >&2 "WARNING: The container will still run, but supplementary packages listed in NETDATA_EXTRA_APK_PACKAGES will not be installed." |
| 84 | echo >&2 "WARNING: To remove these messages, either undefine NETDATA_EXTRA_APK_PACKAGES, or define it to an empty string." |
| 85 | fi |
| 86 | |
| 87 | if [ -n "${NETDATA_EXTRA_DEB_PACKAGES}" ]; then |
| 88 | echo "Fetching APT repository metadata." |
| 89 | if ! apt-get update; then |
| 90 | echo "Failed to fetch APT repository metadata." |
| 91 | else |
| 92 | echo "Installing supplementary packages." |
| 93 | export DEBIAN_FRONTEND="noninteractive" |
| 94 | # shellcheck disable=SC2086 |
| 95 | if ! apt-get install -y --no-install-recommends ${NETDATA_EXTRA_DEB_PACKAGES}; then |
| 96 | echo "Failed to install supplementary packages." |
| 97 | fi |
| 98 | fi |
| 99 | fi |
| 100 | |
| 101 | BALENA_PGID=$(stat -c %g /var/run/balena.sock 2> /dev/null || true) |
| 102 | DOCKER_PGID=$(stat -c %g /var/run/docker.sock 2> /dev/null || true) |
| 103 | |
| 104 | re='^[0-9]+$' |
| 105 | if [[ $BALENA_PGID =~ $re ]]; then |
| 106 | echo "Netdata detected balena-engine.sock" |
| 107 | DOCKER_HOST='unix:///var/run/balena-engine.sock' |
| 108 | PGID="$BALENA_PGID" |
| 109 | elif [[ $DOCKER_PGID =~ $re ]]; then |
| 110 | echo "Netdata detected docker.sock" |
| 111 | DOCKER_HOST="unix:///var/run/docker.sock" |
| 112 | PGID="$DOCKER_PGID" |
| 113 | fi |
| 114 | |
| 115 | if [ -n "${PGID}" ]; then |
| 116 | export PGID |
| 117 | fi |
| 118 | if [ -n "${DOCKER_HOST}" ]; then |
| 119 | export DOCKER_HOST |
| 120 | fi |
| 121 | |
| 122 | if [ -n "${PGID}" ]; then |
| 123 | echo "Configuring docker group (GID ${PGID}) for ${DOCKER_USR}" |
| 124 | add_user_to_gid "${PGID}" "docker" || true |
| 125 | fi |
| 126 | |
| 127 | if [ -d "/host/etc/pve" ]; then |
| 128 | add_netdata_to_proxmox_conf_files_group || true |
| 129 | fi |
| 130 | |
| 131 | if [ -e "/dev/nvidiactl" ]; then |
| 132 | add_netdata_to_nvidia_group || true |
| 133 | fi |
| 134 | else |
| 135 | echo >&2 "WARNING: Entrypoint started as non-root user. This is not officially supported and some features may not be available." |
| 136 | fi |
| 137 | |
| 138 | if mountpoint -q /etc/netdata; then |
| 139 | echo "Copying stock configuration to /etc/netdata" |
| 140 | cp -an /etc/netdata.stock/* /etc/netdata |
| 141 | cp -an /etc/netdata.stock/.[^.]* /etc/netdata |
| 142 | fi |
| 143 | |
| 144 | if [ -w "/etc/netdata" ]; then |
| 145 | if mountpoint -q /etc/netdata; then |
| 146 | hostname > /etc/netdata/.container-hostname |
| 147 | else |
| 148 | rm -f /etc/netdata/.container-hostname |
| 149 | fi |
| 150 | fi |
| 151 | |
| 152 | exec /usr/sbin/netdata -u "${DOCKER_USR}" -D -s /host -p "${NETDATA_LISTENER_PORT:-19999}" "$@" |