Skip to content

Commit

Permalink
Add support for RHEL/CentOS 7/8 Kernels (#96)
Browse files Browse the repository at this point in the history
* CentOS compilation

This PR will bring support to CentOS kernels

* centos: Kernel compilation

This commit brings the necessary changes to compile the kerne,
but there are steps yet with the library

* centos: New docker files

* centos: Centos7 adjusts

* centos: centos7 adjust to docker file

* centos: centos7 missing \

* Add support for patching broken CentOS 7 shitty kernels and apply strstr patch

* centos: Fix compilation on CentOS 7 with docker

* centos: Fixes to CentOS 8

* Minor tweaks to make CentOS 7 & 8 more similar

Intended goal here is to eventually figure out how to merge the two
CentOS 7 & 8 build environments.

They are now fairly similar with some minor exceptions:

```sh
prologic@Jamess-iMac
Thu Mar 12 09:33:40
~/NetData/kernel-collector
 (centos) 2
$ diff -Ndry Dockerfile.glibc.centos7 Dockerfile.glibc.centos8
FROM centos:7.7.1908 AS build				      |	FROM centos:8.1.1911 AS build

ARG ARCH=x86							ARG ARCH=x86
ENV ARCH=$ARCH							ENV ARCH=$ARCH

ARG KERNEL_VERSION=3.10.0-1062.12.1.el7			      |	ARG KERNEL_VERSION=4.18.0-147.5.1.el8_1
ENV KERNEL_VERSION=$KERNEL_VERSION				ENV KERNEL_VERSION=$KERNEL_VERSION

ENV _LIBC=glibc							ENV _LIBC=glibc

RUN yum update -y && \						RUN yum update -y && \
    yum groupinstall -y "Development tools" && \		    yum groupinstall -y "Development tools" && \
    yum install -y asciidoc audit-libs-devel bash bc binutils	    yum install -y asciidoc audit-libs-devel bash bc binutils
                   bison diffutils elfutils elfutils-devel \	                   bison diffutils elfutils elfutils-devel \
                   elfutils-libelf-devel findutils flex gawk 	                   elfutils-libelf-devel findutils flex gawk
                   gzip hmaccalc hostname java-devel m4 make 	                   gzip hmaccalc hostname java-devel m4 make
                   module-init-tools ncurses-devel net-tools 	                   module-init-tools ncurses-devel net-tools
                   numactl-devel openssl openssl-devel patch 	                   numactl-devel openssl openssl-devel patch
                   perl perl-ExtUtils-Embed pesign python-dev |	                   perl perl-ExtUtils-Embed pesign redhat-rpm
                   python-docutils redhat-rpm-config rpm-buil |	                   rpm-build sh-utils tar xmlto xz zlib-devel
                   xmlto xz zlib-devel glibc-headers	      <
							      <
RUN yum install -y centos-release-scl && \		      <
    yum-config-manager --enable rhel-server-rhscl-7-rpms && \ <
    yum install -y llvm-toolset-7.0*			      <

RUN cd ~/ && mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCE	RUN cd ~/ && mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCE
    echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros && 	    echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros &&
    rpm -i http://vault.centos.org/7.7.1908/updates/Source/SP |	    rpm -i http://vault.centos.org/8.1.1911/BaseOS/Source/SPa

RUN cd ~/rpmbuild/SOURCES && \					RUN cd ~/rpmbuild/SOURCES && \
    tar -xf linux-${KERNEL_VERSION}.tar.xz && \			    tar -xf linux-${KERNEL_VERSION}.tar.xz && \
    mkdir -p /usr/src/kernels/ && \				    mkdir -p /usr/src/kernels/ && \
    ln -s linux-${KERNEL_VERSION} /usr/src/kernels/$(uname -r	    ln -s linux-${KERNEL_VERSION} /usr/src/kernels/$(uname -r
    cp -f kernel-3.10.0-x86_64.config linux-${KERNEL_VERSION} <
    cd linux-${KERNEL_VERSION} && \				    cd linux-${KERNEL_VERSION} && \
    make oldconfig && \					      |	    make defconfig && \
    make prepare && \						    make prepare && \
    make scripts && \						    make scripts && \
    make headers_install					    make headers_install

WORKDIR /kernel-collector					WORKDIR /kernel-collector

COPY .dockerfiles/build.sh /build.sh				COPY .dockerfiles/build.sh /build.sh
COPY . .							COPY . .

ENTRYPOINT ["scl"]					      |	CMD ["/build.sh"]
CMD ["enable", "llvm-toolset-7.0", "./build.sh"]	      <
```

* Trigger CI

* Fix execution bit on patch_kernel.sh

* Import CI checks from netdata/netdata for shell and yaml

* Add build-special job for RHEL/CentOS OS(es) / Kernels

* Trigger CI

* Fixed CMD of centos7 buidl env

* Fix a bunch of build errors related to the kernels we're building against

* Fixed clang version detection

* centos: this brings the missing ifdef

* Fixed missing/wrong deps for llvm/clang on CentOS8 and wrong include path for clang/llvm libs

* Fixed artifacts for CentOS/RHEL

Co-authored-by: James Mills <prologic@shortcircuit.net.au>
  • Loading branch information
thiagoftsm and prologic authored Mar 16, 2020
1 parent 9693fec commit ddb1c2d
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .dockerfiles/patch_kernel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

set -e

cd /usr/src/linux || exit 1
srcdir="${1:-/usr/src/linux}"

cd "${srcdir}" || exit

for patch in /usr/src/linux-"${KERNEL_VERSION}"-patches/*.diff.gz; do
printf >&2 " Patching linux-%s with %s ... " "${KERNEL_VERSION}" "${patch}"
Expand Down
37 changes: 33 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- '4.19.6'
- '4.18.20'
- '4.18.16'
- '4.16.18'
- '4.16.18'
- '4.16.3'
- '4.15'
- '4.14.171'
Expand All @@ -33,13 +33,13 @@ jobs:
libc: musl
# excludes musl on 4.18.20
- kernel_version: 4.18.20
libc: musl
libc: musl
# excludes musl on 4.18.16
- kernel_version: 4.18.16
libc: musl
libc: musl
# excludes musl on 4.16.18
- kernel_version: 4.16.18
libc: musl
libc: musl
# excludes musl on 4.16.3
- kernel_version: 4.16.3
libc: musl
Expand All @@ -66,3 +66,32 @@ jobs:
with:
name: artifacts-${{ matrix.kernel_version }}-${{ matrix.libc }}
path: artifacts

build-special:
name: Build Special
strategy:
fail-fast: false
matrix:
os:
- 'centos7'
- 'centos8'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Prepare Build Image
run: |
docker build -f Dockerfile.glibc.${{ matrix.os }} -t kernel-collector:glibc_${{ matrix.os }} .
- name: Build It
run: |
docker run --rm -v $PWD:/kernel-collector -e DEBUG=1 kernel-collector:glibc_${{ matrix.os }}
- name: List Artifacts
run: |
ls -lah artifacts
test -f artifacts/netdata_ebpf-*.tar.xz
- name: Upload Artifacts
uses: actions/upload-artifact@v1
if: success()
with:
name: artifacts-glibc_${{ matrix.os }}
path: artifacts
32 changes: 32 additions & 0 deletions .github/workflows/review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# Runs various ReviewDog based checks against PR with suggested changes to improve quality
name: Review
on:
pull_request:
jobs:
shellcheck:
name: shellcheck
runs-on: ubuntu-latest
steps:
- name: Git clone repository
uses: actions/checkout@v2
- name: Run shellcheck
uses: reviewdog/action-shellcheck@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
reporter: github-pr-check
path: "."
pattern: "*.sh*"
exclude: "./.git/*"

yamllint:
name: yamllint
runs-on: ubuntu-latest
steps:
- name: Git clone repository
uses: actions/checkout@v2
- name: Run yamllint
uses: reviewdog/action-yamllint@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
reporter: github-pr-check
29 changes: 29 additions & 0 deletions .yamllint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
yaml-files:
- '*.yaml'
- '*.yml'
- '.yamllint'

rules:
braces: enable
brackets: enable
colons: enable
commas: enable
comments: disable
comments-indentation: disable
document-end: disable
document-start:
level: warning
empty-lines: enable
empty-values: disable
hyphens: enable
indentation: enable
key-duplicates: enable
key-ordering: disable
line-length: disable
new-line-at-end-of-file: enable
new-lines: enable
octal-values: enable
quoted-strings: disable
trailing-spaces: enable
truthy: disable
50 changes: 50 additions & 0 deletions Dockerfile.glibc.centos7
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
FROM centos:7.7.1908 AS build

ARG ARCH=x86
ENV ARCH=$ARCH

ARG KERNEL_VERSION=3.10.0-1062.12.1.el7
ENV KERNEL_VERSION=$KERNEL_VERSION

ENV _LIBC=glibc

RUN yum update -y && \
yum groupinstall -y "Development tools" && \
yum install -y asciidoc audit-libs-devel bash bc binutils binutils-devel \
bison diffutils elfutils elfutils-devel \
elfutils-libelf-devel findutils flex gawk gcc gettext \
gzip hmaccalc hostname java-devel m4 make \
module-init-tools ncurses-devel net-tools newt-devel \
numactl-devel openssl openssl-devel patch pciutils-devel \
perl perl-ExtUtils-Embed pesign python-devel \
python-docutils redhat-rpm-config rpm-build sh-utils tar \
xmlto xz zlib-devel glibc-headers

RUN yum install -y centos-release-scl && \
yum-config-manager --enable rhel-server-rhscl-7-rpms && \
yum install -y llvm-toolset-7.0*

RUN mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} && \
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros && \
rpm -i http://vault.centos.org/7.7.1908/updates/Source/SPackages/kernel-${KERNEL_VERSION}.src.rpm 2>&1

RUN cd ~/rpmbuild/SOURCES && \
tar -xf linux-${KERNEL_VERSION}.tar.xz && \
cp -f kernel-3.10.0-x86_64.config linux-${KERNEL_VERSION}/.config

RUN cd /usr/src && \
ln -s ~/rpmbuild/SOURCES/linux-${KERNEL_VERSION} linux

RUN cd /usr/src/linux && \
make oldconfig && \
make prepare && \
make scripts && \
make headers_install

WORKDIR /kernel-collector

COPY .dockerfiles/build.sh /build.sh
COPY . .

ENTRYPOINT ["scl"]
CMD ["enable", "llvm-toolset-7.0", "/build.sh"]
44 changes: 44 additions & 0 deletions Dockerfile.glibc.centos8
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM centos:8.1.1911 AS build

ARG ARCH=x86
ENV ARCH=$ARCH

ARG KERNEL_VERSION=4.18.0-147.5.1.el8_1
ENV KERNEL_VERSION=$KERNEL_VERSION

ENV _LIBC=glibc

RUN yum update -y && \
yum groupinstall -y "Development tools" && \
yum install -y asciidoc audit-libs-devel bash bc binutils binutils-devel \
bison diffutils elfutils elfutils-devel \
elfutils-libelf-devel findutils flex gawk gcc gettext \
gzip hmaccalc hostname java-devel m4 make \
module-init-tools ncurses-devel net-tools newt-devel \
numactl-devel openssl openssl-devel patch pciutils-devel \
perl perl-ExtUtils-Embed pesign redhat-rpm-config \
rpm-build sh-utils tar xmlto xz zlib-devel clang llvm

RUN mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} && \
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros && \
rpm -i http://vault.centos.org/8.1.1911/BaseOS/Source/SPackages/kernel-${KERNEL_VERSION}.src.rpm 2>&1

RUN cd ~/rpmbuild/SOURCES && \
tar -xf linux-${KERNEL_VERSION}.tar.xz && \
mkdir -p /usr/src/kernels

RUN cd /usr/src && \
ln -s ~/rpmbuild/SOURCES/linux-${KERNEL_VERSION} linux

RUN cd /usr/src/linux && \
make defconfig && \
make prepare && \
make scripts && \
make headers_install

WORKDIR /kernel-collector

COPY .dockerfiles/build.sh /build.sh
COPY . .

CMD ["/build.sh"]
Empty file.
Binary file added kernel-patches/3.10.0-1062.12.1.el7/strstr.diff.gz
Binary file not shown.
11 changes: 9 additions & 2 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ LINUXINCLUDE += -I$(KERNELSOURCE)/include/uapi
LINUXINCLUDE += -include $(KERNELSOURCE)/include/linux/kconfig.h
LINUXINCLUDE += -I../includes

CLANG_VERSION := $(shell clang --version | grep -o -E '[0-9]+\.[0-9]+\.[0-9]' | uniq)
LLVM_INCLUDES = -I/usr/lib/clang/$(CLANG_VERSION)/include
CLANG_VERSION := $(shell clang --version | grep -o -E 'clang version [0-9]+\.[0-9]+\.[0-9]' | cut -f 3 -d ' ')
LLVM_INCLUDES = $(shell [ -d /usr/lib/clang ] && echo "-I/usr/lib/clang/$(CLANG_VERSION)/include" || echo "-I/usr/lib64/clang/$(CLANG_VERSION)/include")
LLVM_INCLUDES += -I/opt/rh/llvm-toolset-7.0/root/usr/lib64/clang/$(CLANG_VERSION)/include

#KERNEL_VERSION="$(shell basename $(realpath /usr/src/linux) | cut -f 2 -d '-')"
KERNEL_VERSION="$(shell cat /usr/src/linux/include/config/kernel.release)"
Expand All @@ -44,6 +45,8 @@ all: process_kern.o latency_process_kern.o
-Wno-gnu-variable-sized-type-not-at-end \
-Wno-tautological-compare \
-DNETDATASEL=0 \
-D__BPF_TRACING__ \
-include netdata_asm_goto.h \
-O2 -emit-llvm -c $<
$(LLC) -march=bpf -filetype=obj -o r$@ $(<:.c=.ll)
$(CLANG) $(EXTRA_CFLAGS) -S -nostdinc $(LINUXINCLUDE) $(LLVM_INCLUDES) \
Expand All @@ -52,6 +55,8 @@ all: process_kern.o latency_process_kern.o
-Wno-gnu-variable-sized-type-not-at-end \
-Wno-tautological-compare \
-DNETDATASEL=1 \
-D__BPF_TRACING__ \
-include netdata_asm_goto.h \
-O2 -emit-llvm -c $<
$(LLC) -march=bpf -filetype=obj -o d$@ $(<:.c=.ll)
$(CLANG) $(EXTRA_CFLAGS) -S -nostdinc $(LINUXINCLUDE) $(LLVM_INCLUDES) \
Expand All @@ -60,6 +65,8 @@ all: process_kern.o latency_process_kern.o
-Wno-gnu-variable-sized-type-not-at-end \
-Wno-tautological-compare \
-DNETDATASEL=2 \
-D__BPF_TRACING__ \
-include netdata_asm_goto.h \
-O2 -emit-llvm -c $<
$(LLC) -march=bpf -filetype=obj -o p$@ $(<:.c=.ll)

Expand Down
19 changes: 19 additions & 0 deletions kernel/netdata_asm_goto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _NETDATA_ASM_GOTO_H_
# define _NETDATA_ASM_GOTO_H_ 1

# include <linux/types.h>
# include <linux/version.h>

# ifndef RHEL_RELEASE_VERSION
# define RHEL_RELEASE_VERSION(x,y) ((x << 8) + (y))
# endif

# if RHEL_RELEASE_CODE && \
RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(8,0)
# ifdef asm_volatile_goto
# undef asm_volatile_goto
# define asm_volatile_goto(x...) asm volatile("invalid use of asm_volatile_goto")
# endif
# endif

#endif
21 changes: 13 additions & 8 deletions kernel/process_kern.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#define KBUILD_MODNAME "process_kern"
#include <linux/bpf.h>
#include <linux/version.h>
#include <linux/ptrace.h>
#include <linux/sched.h>
#include <linux/sched/task.h>
# include <linux/ptrace.h>
# include <linux/sched.h>
# include <linux/sched/task.h>

#include <linux/threads.h>
#include <linux/version.h>
Expand Down Expand Up @@ -560,18 +560,23 @@ int netdata_release_task(struct pt_regs* ctx)
return 0;
}

#if NETDATASEL < 2
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0))
# if NETDATASEL < 2
SEC("kretprobe/_do_fork")
#else
# else
SEC("kprobe/_do_fork")
# endif
#else
# if NETDATASEL < 2
SEC("kretprobe/do_fork")
# else
SEC("kprobe/do_fork")
# endif
#endif
int netdata_fork(struct pt_regs* ctx)
{
#if NETDATASEL < 2
int ret = (int)PT_REGS_RC(ctx);
# if NETDATASEL == 1
struct netdata_error_report_t ner;
# endif
#endif
struct netdata_pid_stat_t data = { };
struct netdata_pid_stat_t *fill;
Expand Down
12 changes: 12 additions & 0 deletions libbpf_0_0_1/src/libbpf.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)

prefix=/usr
libdir=/usr/lib64
includedir=${prefix}/include

Name: libbpf
Description: BPF library
Version: 0.0.3
Libs: -L${libdir} -lbpf
Requires.private: libelf
Cflags: -I${includedir}
2 changes: 2 additions & 0 deletions library/bpf_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size, i
} else if (is_sockops) {
prog_type = BPF_PROG_TYPE_SOCK_OPS;
}
#if LINUX_VERSION_CODE > KERNEL_VERSION(4,10,0)
else if (is_sk_skb) {
prog_type = BPF_PROG_TYPE_SK_SKB;
}
#endif
#if LINUX_VERSION_CODE > KERNEL_VERSION(4,17,0)
else if (is_sk_msg) {
prog_type = BPF_PROG_TYPE_SK_MSG;
Expand Down

0 comments on commit ddb1c2d

Please sign in to comment.