netdata 交叉编译

netdata 交叉编译

前言

netdata 是一个基本上由 C/C++ 开发的系统监控程序,其主要应用于服务器监控;由于使用其技术实现基本上是由 C/C++ ,因此是比较合适于嵌入式的设备监控的,其余的监控一体化方案往往会由于技术栈的问题导致无法移植到嵌入式设备上。

netdata 是可以运行在嵌入式设备上的,尽管某些方面不能尽善尽美,毕竟 netdata 并不是专门针对于嵌入式产品开发的,其主要还是服务于服务器;嵌入式上的移植一直是一个问题,受限于目标机本身的种种问题,如交叉编译工具链版本过低,缺少某些环境依赖库,32位编译链对64位编译链的不兼容问题,往往很容易导致交叉编译失败。

本文仅以 netdata 的交叉编译流程,即 netdata 的移植及部署做一个比较完整的记录。

netdata 的交叉编译过程十分复杂,需要谨慎地进行,建议提前对虚拟机进行快照保存,防止难以回溯,本安装只适用于ubunut系统,由于使用ubunut的apt包管理工具。

ubuntu 环境准备阶段

在安装过程中不要切换终端

切换为 root 权限

su

设置环境变量

# PATH 以及 CrossComplie 按照自己的情况填写
export PATH=/opt/hisi-linux/x86-arm/arm-hisiv600-linux/target/bin:${PATH}        # 导入交叉编译工具链安装路径
export WorkSpace=$(pwd)                                                          # 工作空间
export CrossComplie="arm-hisiv600-linux"                                         # 交叉编译工具链

为了方便执行,需要记录一下当前文件夹的路径,以及你的交叉编译工具链,下载的全部命令行都会使用这些临时打入的变量

安装ubunut工具

# 安装 automake 插件
apt install autoconf automake libtool pkg-config
# 安装 perl
apt install perl
# 安装 uuid
# Hint : 本质上是不用的,是 netdata 的 configure 没有写好
apt install uuid-dev
# 安装 32位 库相关内容
sudo apt install lib32z1-dev
sudo apt install lib32z1 lib32ncurses5
sudo apt install lib32stdc++6
sudo apt install gcc-multilib

配置交叉编译环境

# 配置交叉编译环境
export CC="${CrossComplie}-gcc"
export AR="${CrossComplie}-ar"
export CXX="${CrossComplie}-c++"
export RANLIB="${CrossComplie}-ranlib"

# 清除环境
rm -rf install
rm -rf netdata

# 构建环境
mkdir install
mkdir install/libuv
mkdir install/zlib
mkdir install/liblz4
mkdir install/openssl
mkdir install/libuuid
mkdir install/libuv
mkdir install/netdata
mkdir install/judy

依赖库获取

# 基本上一次是下不好的,毕竟国内很难访问,但是不允许更换,这些库都是测试过的
cd "${WorkSpace}" || exit
rm -rf depend
mkdir depend
cd "${WorkSpace}"/depend || exit
## 下载 uuid 源代码
wget https://nchc.dl.sourceforge.net/project/libuuid/libuuid-1.0.3.tar.gz
## 下载 zlib 源代码
git clone https://github.com/madler/zlib.git
## 下载 libuv 源代码
git clone https://github.com/libuv/libuv.git
## 下载 openssl 源代码
git clone https://github.com/openssl/openssl.git
## 下载 lz4 源代码
git clone https://github.com/lz4/lz4.git
## 下载 LibJudy 源代码
wget https://nchc.dl.sourceforge.net/project/judy/judy/Judy-1.0.5/Judy-1.0.5.tar.gz
## 下载 netdata 源代码
git clone https://github.com/netdata/netdata.git

交叉编译依赖

交叉编译 Judy

cd "${WorkSpace}"/depend || exit
tar zxvf Judy-1.0.5.tar.gz
cd "${WorkSpace}"/depend/judy-1.0.5 || exit
./configure --host="${CrossComplie}" --prefix="${WorkSpace}"/install/judy
### 修改 Judy Makefile : 修改其交叉编译的 bug
sed -i "563s/\$(CC)/gcc -m32/g" "${WorkSpace}"/depend/judy-1.0.5/src/JudyL/Makefile
sed -i "563s/\$(CC)/gcc -m32/g" "${WorkSpace}"/depend/judy-1.0.5/src/Judy1/Makefile
make
make install
rm -rf "${WorkSpace}"/depned/judy-1.0.5

交叉编译 zlib

cd "${WorkSpace}"/depend/zlib || exit
git checkout v1.2.9
./configure --prefix="${WorkSpace}"/install/zlib
make
make install
make clean

交叉编译 libuuid

cd "${WorkSpace}"/depend || exit
tar zxvf libuuid-1.0.3.tar.gz
cd "${WorkSpace}"/depend/libuuid-1.0.3 || exit
./configure --host="${CrossComplie}" --prefix="${WorkSpace}"/install/libuuid
make
make install
rm -rf "${WorkSpace}"/depned/libuuid-1.0.3

交叉编译 libuv

cd "${WorkSpace}"/depend/libuv || exit
git checkout v1.42.0
sh autogen.sh
./configure --host="${CrossComplie}" --prefix="${WorkSpace}"/install/libuv
make
make install
make clean

交叉编译 openssl

cd "${WorkSpace}"/depend/openssl || exit
git checkout OpenSSL_1_1_0-stable
./config no-asm no-shared --prefix="${WorkSpace}"/install/openssl
### 修改 oepnssl Makefile : 去除 "-m64"
sed -i "s/ -m64/  /g" "${WorkSpace}"/depend/openssl/Makefile
make
make install
make clean

交叉编译 liblz4

cd "${WorkSpace}"/depend/lz4 || exit
git checkout r131
make CC="${CrossComplie}-gcc"
make DESTDIR="${WorkSpace}"/install/liblz4 install

netdata 安装

拷贝库及头文件

cd "${WorkSpace}"/depend/netdata || exit
git checkout v1.31.0
autoreconf -ivf
## 拷贝依赖库以及头文件
rm -rf 3rdLibrary
mkdir 3rdLibrary
mkdir 3rdLibrary/lib
mkdir 3rdLibrary/include
### 拷贝 judy
cp "${WorkSpace}"/install/judy/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp "${WorkSpace}"/install/judy/include/* "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 libzl4
cp "${WorkSpace}"/install/liblz4/usr/local/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp "${WorkSpace}"/install/liblz4/usr/local/include/* "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 libuuid
cp "${WorkSpace}"/install/libuuid/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp -rf "${WorkSpace}"/install/libuuid/include/uuid "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 libuv
cp "${WorkSpace}"/install/libuv/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp -rf "${WorkSpace}"/install/libuv/include/* "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 oepnssl
cp "${WorkSpace}"/install/openssl/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp -rf "${WorkSpace}"/install/openssl/include/openssl "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 zlib
cp "${WorkSpace}"/install/zlib/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp "${WorkSpace}"/install/zlib/include/* "${WorkSpace}"/depend/netdata/3rdLibrary/include

交叉编译 netdata

cd "${WorkSpace}"/depend/netdata || exit
./configure --prefix="${WorkSpace}"/install/netdata --host=arm-hisiv600-linux \
    --disable-cloud --disable-ebpf \
    CFLAGS="-I${WorkSpace}/depend/netdata/3rdLibrary/include" \
    LDFLAGS="-L${WorkSpace}/depend/netdata/3rdLibrary/lib" \
    LIBS="-lcrypto -llz4 -luuid -lz -lJudy -lssl -luv -ldl"
make
make install

部署 netdata

# 配置netdata
cd "${WorkSpace}" || exit
cp -rf "${WorkSpace}"/install/netdata "${WorkSpace}"
mkdir -p "${WorkSpace}"/netdata/var/cache/netdata
mkdir -p "${WorkSpace}"/netdata/var/lib/netdata
mkdir -p "${WorkSpace}"/netdata/var/log/netdata
touch "${WorkSpace}"/netdata/etc/netdata/netdata.conf
## 写入默认配置文件
cat>"${WorkSpace}"/netdata/etc/netdata/netdata.conf<<EOF
# netdata configuration
# global netdata configuration
[global]
    run as user = root
    hostname = unknown
    update every = 1
    config directory = /var/netdata/etc/netdata
    stock config directory = /var/netdata/lib/netdata/netdata/conf.d
    log directory = /var/netdata/var/log/netdata
    web files directory = /var/netdata/share/netdata/web
    cache directory = /var/netdata/var/cache/netdata
    lib directory = /var/netdata/var/lib/netdata
    home directory = /var/netdata/var/lib/netdata
    lock directory = /var/netdata/var/lib/netdata/lock
    plugins directory = "/var/netdata/libexec/netdata/plugins.d" "/var/netdata/etc/netdata/custom-plugins.d"
[web]
    default port = 25000
EOF
chown root "${WorkSpace}"/netdata -R
chgrp root "${WorkSpace}"/netdata -R
chmod 0777 "${WorkSpace}"/netdata -R

netdata 程序运行说明

echo "程序运行说明"
echo "将当前目录下生成的 netdata 拷贝至目标机的 /var 目录下"
echo "进入到 netdata 目录下,使用 ./sbin/netdata -c ./etc/netdata.conf 运行程序"
echo "使用 killall netdata 关闭程序"
echo "使用 [目标机ip]:25000/netdata.conf 查看 netdata 配置并修改"
echo "程序的默认运行目录被配置在 /var 目录下"
echo ""

附录

一键化安装脚本

#! /bin/bash
#
# author : haorui
# date : 2021-10-27
# script : netdata_install.h
#
# 备注 : 需要运行在 特权模式(sudo) 下,即 root 下
# 脚本说明 : 自动安装 netdata
# 检查是否运行在 root 下

function help() {
    echo "netdata 交叉编译脚本"
    echo "此脚本记录了 netdata 中所有的步骤,建议按照脚本的步骤手动安装"
    echo "脚本存在执行失败的情况,主要的原因有以下两点:"
    echo "1 - 克隆远程仓库失败(建议手动下载源码)"
    echo "2 - 不同交叉编译工具链之间些许的差异"
    echo "Hint : 此脚本只能在 ubuntu 运行"
    echo "Hint : 此脚本针对于 32 位的交叉编译工具链"
    echo ""
    echo ""

    echo "脚本使用说明"
    echo "1 - 在 sudo 下运行脚本"
    echo "2 - 将 CrossComplie 修改为自己的交叉编译工具链"
    echo "3 - export PATH 自己的交叉编译工具链"
    echo "4 - ResourceLocation在local的情况下不会重新下载源码,采取本地构建"
    echo "建议手动下载源码,按照脚本中指定的路径"
    echo ""
    echo ""

    echo "程序运行说明"
    echo "将当前目录下生成的 netdata 拷贝至目标机的 /var 目录下"
    echo "进入到 netdata 目录下,使用 ./sbin/netdata -c ./etc/netdata.conf 运行程序"
    echo "使用 killall netdata 关闭程序"
    echo "使用 [目标机ip]:25000/netdata.conf 查看 netdata 配置并修改"
    echo "程序的默认运行目录被配置在 /var 目录下"
    echo ""
    echo ""

    echo "请仔细阅读,按下任意键后脚本开始执:"
    read -r
}

set-x

# 帮助文档
help

if [ "$(id -u)" != 0 ]; then
    echo "使用 sudo 运行此脚本";
    exit 255
fi

# 脚本环境变量相关
export PATH=/opt/hisi-linux/x86-arm/arm-hisiv600-linux/target/bin:${PATH} # 导入交叉编译工具链安装路径
WorkSpace=$(pwd)                                                          # 工作空间
ResourceLocation="local"                                                  # 资源获取位置
CrossComplie="arm-hisiv600-linux"                                         # 交叉编译工具链

# 安装 automake 插件
apt install autoconf automake libtool pkg-config

# 安装 perl
apt install perl

# 安装 uuid
# Hint : 本质上是不用的,是 netdata 的 configure 没有写好
apt install uuid-dev

# 安装 32位 库相关内容
sudo apt install lib32z1-dev
sudo apt install lib32z1 lib32ncurses5
sudo apt install lib32stdc++6
sudo apt install gcc-multilib


# 配置交叉编译环境
export CC="${CrossComplie}-gcc"
export AR="${CrossComplie}-ar"
export CXX="${CrossComplie}-c++"
export RANLIB="${CrossComplie}-ranlib"

# 清除环境
rm -rf install
rm -rf netdata

# 构建环境
mkdir install
mkdir install/libuv
mkdir install/zlib
mkdir install/liblz4
mkdir install/openssl
mkdir install/libuuid
mkdir install/libuv
mkdir install/netdata
mkdir install/judy

# 下载依赖 (depend)
if [ ${ResourceLocation} == "remote" ]; then
    cd "${WorkSpace}" || exit
    rm -rf depend
    mkdir depend
    cd "${WorkSpace}"/depend || exit
    ## 下载 uuid 源代码
    wget https://nchc.dl.sourceforge.net/project/libuuid/libuuid-1.0.3.tar.gz
    ## 下载 zlib 源代码
    git clone https://github.com/madler/zlib.git
    ## 下载 libuv 源代码
    git clone https://github.com/libuv/libuv.git
    ## 下载 openssl 源代码
    git clone https://github.com/openssl/openssl.git
    ## 下载 lz4 源代码
    git clone https://github.com/lz4/lz4.git
    ## 下载 LibJudy 源代码
    wget https://nchc.dl.sourceforge.net/project/judy/judy/Judy-1.0.5/Judy-1.0.5.tar.gz
    ## 下载 netdata 源代码
    git clone https://github.com/netdata/netdata.git
fi

# 交叉编译依赖

## 交叉编译 Judy
cd "${WorkSpace}"/depend || exit
tar zxvf Judy-1.0.5.tar.gz
cd "${WorkSpace}"/depend/judy-1.0.5 || exit
./configure --host="${CrossComplie}" --prefix="${WorkSpace}"/install/judy
### 修改 Judy Makefile : 修改其交叉编译的 bug
sed -i "563s/\$(CC)/gcc -m32/g" "${WorkSpace}"/depend/judy-1.0.5/src/JudyL/Makefile
sed -i "563s/\$(CC)/gcc -m32/g" "${WorkSpace}"/depend/judy-1.0.5/src/Judy1/Makefile
make
make install
rm -rf "${WorkSpace}"/depned/judy-1.0.5

## 交叉编译 zlib
cd "${WorkSpace}"/depend/zlib || exit
git checkout v1.2.9
./configure --prefix="${WorkSpace}"/install/zlib
make
make install
make clean

## 交叉编译 libuuid
cd "${WorkSpace}"/depend || exit
tar zxvf libuuid-1.0.3.tar.gz
cd "${WorkSpace}"/depend/libuuid-1.0.3 || exit
./configure --host="${CrossComplie}" --prefix="${WorkSpace}"/install/libuuid
make
make install
rm -rf "${WorkSpace}"/depned/libuuid-1.0.3

## 交叉编译 libuv
cd "${WorkSpace}"/depend/libuv || exit
git checkout v1.42.0
sh autogen.sh
./configure --host="${CrossComplie}" --prefix="${WorkSpace}"/install/libuv
make
make install
make clean

## 交叉编译 openssl
cd "${WorkSpace}"/depend/openssl || exit
git checkout OpenSSL_1_1_0-stable
./config no-asm no-shared --prefix="${WorkSpace}"/install/openssl
### 修改 oepnssl Makefile : 去除 "-m64"
sed -i "s/ -m64/  /g" "${WorkSpace}"/depend/openssl/Makefile
make
make install
make clean

## 交叉编译 liblz4
cd "${WorkSpace}"/depend/lz4 || exit
git checkout r131
make CC="${CrossComplie}-gcc"
make DESTDIR="${WorkSpace}"/install/liblz4 install

# 交叉编译 netdata
cd "${WorkSpace}"/depend/netdata || exit
git checkout v1.31.0
autoreconf -ivf
## 拷贝依赖库以及头文件
rm -rf 3rdLibrary
mkdir 3rdLibrary
mkdir 3rdLibrary/lib
mkdir 3rdLibrary/include
### 拷贝 judy
cp "${WorkSpace}"/install/judy/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp "${WorkSpace}"/install/judy/include/* "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 libzl4
cp "${WorkSpace}"/install/liblz4/usr/local/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp "${WorkSpace}"/install/liblz4/usr/local/include/* "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 libuuid
cp "${WorkSpace}"/install/libuuid/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp -rf "${WorkSpace}"/install/libuuid/include/uuid "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 libuv
cp "${WorkSpace}"/install/libuv/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp -rf "${WorkSpace}"/install/libuv/include/* "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 oepnssl
cp "${WorkSpace}"/install/openssl/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp -rf "${WorkSpace}"/install/openssl/include/openssl "${WorkSpace}"/depend/netdata/3rdLibrary/include
### 拷贝 zlib
cp "${WorkSpace}"/install/zlib/lib/*.a "${WorkSpace}"/depend/netdata/3rdLibrary/lib
cp "${WorkSpace}"/install/zlib/include/* "${WorkSpace}"/depend/netdata/3rdLibrary/include
## 正式开始交叉编译 netdata
cd "${WorkSpace}"/depend/netdata || exit
./configure --prefix="${WorkSpace}"/install/netdata --host=arm-hisiv600-linux \
    --disable-cloud --disable-ebpf \
    CFLAGS="-I${WorkSpace}/depend/netdata/3rdLibrary/include" \
    LDFLAGS="-L${WorkSpace}/depend/netdata/3rdLibrary/lib" \
    LIBS="-lcrypto -llz4 -luuid -lz -lJudy -lssl -luv -ldl"
make
make install

# 配置netdata
cd "${WorkSpace}" || exit
cp -rf "${WorkSpace}"/install/netdata "${WorkSpace}"
mkdir -p "${WorkSpace}"/netdata/var/cache/netdata
mkdir -p "${WorkSpace}"/netdata/var/lib/netdata
mkdir -p "${WorkSpace}"/netdata/var/log/netdata
touch "${WorkSpace}"/netdata/etc/netdata/netdata.conf
## 写入默认配置文件
cat>"${WorkSpace}"/netdata/etc/netdata/netdata.conf<<EOF
# netdata configuration
# global netdata configuration
[global]
    run as user = root
    hostname = unknown
    update every = 1
    config directory = /var/netdata/etc/netdata
    stock config directory = /var/netdata/lib/netdata/netdata/conf.d
    log directory = /var/netdata/var/log/netdata
    web files directory = /var/netdata/share/netdata/web
    cache directory = /var/netdata/var/cache/netdata
    lib directory = /var/netdata/var/lib/netdata
    home directory = /var/netdata/var/lib/netdata
    lock directory = /var/netdata/var/lib/netdata/lock
    plugins directory = "/var/netdata/libexec/netdata/plugins.d" "/var/netdata/etc/netdata/custom-plugins.d"
[web]
    default port = 25000

EOF
chown root "${WorkSpace}"/netdata -R
chgrp root "${WorkSpace}"/netdata -R
chmod 0777 "${WorkSpace}"/netdata -R


echo "程序运行说明"
echo "将当前目录下生成的 netdata 拷贝至目标机的 /var 目录下"
echo "进入到 netdata 目录下,使用 ./sbin/netdata -c ./etc/netdata.conf 运行程序"
echo "使用 killall netdata 关闭程序"
echo "使用 [目标机ip]:25000/netdata.conf 查看 netdata 配置并修改"
echo "程序的默认运行目录被配置在 /var 目录下"
echo ""
echo ""

其他

对于交叉编译 netdata 的过程中,可能最难编译的就是 Judy 了。

上一篇:xcode编译错误:The Legacy Build System will be removed in a future release.


下一篇:Python classmethod类方法修饰符