Cross compile iproute2 network management tool
The built-in ip command in busybox does not support netns and bridge, so I transplanted one by myself referring to the web document guide.
Reference: https://blog.csdn.net/u013401853/article/details/71126645
Release: May 03, 2017 17:04:58 Planck constant
-
1) Download the source code
-
Official website:
- https://www.kernel.org/pub/linux/utils/net/iproute2/ from version 3.1.0
- iproute2-3.1.0.tar.bz2 23-Nov-2011 19:11 395K
- iproute2-3.19.0.tar.xz 10-Feb-2015 23:15 444K
- iproute2-4.11.0.tar.xz 01-May-2017 16:33 642K
- iproute2-4.13.0.tar.xz 05-Sep-2017 16:41 655K
- iproute2-4.19.0.tar.gz 23-Oct-2018 17:19 939K
- iproute2-4.20.0.tar.xz 07-Jan-2019 18:28 690K
-
Lower version
-
-
2) Unzip and modify Makefile
- Modify CC in Makefille as a cross compiler
CC = /usr/arm/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc
- Planck constants / PS : Because I only need the ip command, modify line 38 to compile only the ip tool SUBDIRS=ip
- About 4.19.0 version
// 修改 Makefile --- 交叉编译器 CC = /usr/arm/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc // 修改config.mk --- 交叉编译器 AR:=/usr/arm/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-ar CC:=/usr/arm/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc // 修改config.mk --- 去除对SELinux和CAP的支持 HAVE_SELINUX:= #LDLIBS += -lselinux #CFLAGS += -DHAVE_SELINUX CFLAGS += -DNEED_STRLCPY HAVE_CAP:= #CFLAGS += -DHAVE_LIBCAP #LDLIBS += -lcap
- Modify CC in Makefille as a cross compiler
-
3)make
make LDFLAGS=-static ## 静态连接,否则,运行时报缺少.so文件
PS:
- The make process will not be completely successful, but the required ip has been compiled, and the ip in the ip directory is the required ip executable program
- The downloaded version 4.19 requires the compiler to have selinux.h and capability.h (gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi does not support), so I downloaded version 4.13 and compiled it
-
4) Compile error resolution
- Due to the gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi compiler, the function setns declaration conflicts, as follows:
Solution: Edit ./include/namespace.h and use the declaration of bits/sched.hIn file included from utils.c:36:0: ../include/namespace.h:35:19: error: static declaration of ‘setns’ follows non-static declaration static inline int setns(int fd, int nstype) ^~~~~ In file included from /usr/arm/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc/usr/include/sched.h:43:0, from ../include/namespace.h:4, from utils.c:36: /usr/arm/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi/arm-linux-gnueabi/libc/usr/include/bits/sched.h:91:12: note: previous declaration of ‘setns’ was here extern int setns (int __fd, int __nstype) __THROW;
#ifndef HAVE_SETNS extern int setns (int __fd, int __nstype) __THROW; /* static inline int setns(int fd, int nstype) { ... } */ #endif /* HAVE_SETNS */
-
libmnl required for error support
lib ## 编译 iproute2-4.19/lib目录 libnetlink.c:120:2: warning: #warning "libmnl required for error support" [-Wcpp] #warning "libmnl required for error support"
Check the source code of lib/libnetlink.c. If the HAVE_LIBMNL macro is not defined, there will be the above warning. You can download libmnl from the official website http://ftp.netfilter.org/pub/libmnl/ and compile it. This test is not done here
-
When statically linked, execution
ip link add veth0a type veth peer name veth0b
fails# 在 CentOS 7 中静态链接,出现同样问题,侧面佐证静态链接不可行; # 在 ./ip/iplink.c 中加入跟踪代码,发现问题出在 dlh = BODY = dlopen(NULL, RTLD_LAZY) 时为(明显地,必须是动态链接)
Dynamically compile, copy libdl.so.2.23 to the target machine and ln -s as libdl.so.2, run ldd, as follows:
/root # ldd ./ip linux-vdso.so.1 (0x7ee1e000) libdl.so.2 => /lib/libdl.so.2 (0x76fa0000) libc.so.6 => /lib/libc.so.6 (0x76eb3000) /lib/ld-linux.so.3 (0x76fb3000)
Then, the execution is
ip link add veth0a type veth peer name veth0b
successful
- Due to the gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi compiler, the function setns declaration conflicts, as follows:
-
5) Put the ip program in the ip directory into the board to see if it can run
Can run directly
Planck Constants/ PS : I thought that I had to download the iproute2 version corresponding to the kernel to run normally, but I compiled the latest iiproute version and put it on the board to run it.
# 动态链接时,报告缺少.so文件 /root # ./ip --help ./ip: error while loading shared libraries: libdl.so.2: cannot open shared object file: No such file or directory
-
reference: