交叉编译iproute2网络管理工具

交叉编译iproute2网络管理工具

busybox中自带的ip命令不支持netns和bridge,于是,参考网文指导,自己移植了一个。

参考: https://blog.csdn.net/u013401853/article/details/71126645

发布: 2017年05月03日 17:04:58 普朗克常量

  • 1)下载源码

  • 2)解压,修改Makefile

    • 修改Makefille中的CC为交叉编译器
      CC = /usr/arm/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-gcc
      
    • 普朗克常量/PS: 因为我只需要ip命令,所以修改第38行只编译ip工具 SUBDIRS=ip
    • 关于 4.19.0 版本
      // 修改 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
      
  • 3)make

    make LDFLAGS=-static  ## 静态连接,否则,运行时报缺少.so文件
    

    PS:

    • 该make过程不会完全成功,但所需的ip已编译完成,ip目录下的ip即为所需的ip可执行程序
    • 下载的4.19 版本要求编译器有selinux.h、capability.h(gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi不支持),故下载了4.13版本,通过编译
  • 4)编译错误解决

    • 由于gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabi编译器的原因,函数setns声明有冲突,如下:
      In 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;  
      
      解决:编辑./include/namespace.h,使用bits/sched.h的声明
      #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"
      

      查看lib/libnetlink.c源码,如果没有定义 HAVE_LIBMNL 宏,则有上述警告。可以从官网 http://ftp.netfilter.org/pub/libmnl/ 下载 libmnl 编译,这里未作此测试

    • 静态链接时,执行ip link add veth0a type veth peer name veth0b失败

      # 在 CentOS 7 中静态链接,出现同样问题,侧面佐证静态链接不可行;
      # 在 ./ip/iplink.c 中加入跟踪代码,发现问题出在 dlh = BODY = dlopen(NULL, RTLD_LAZY) 时为(明显地,必须是动态链接)
      

      动态编译,将libdl.so.2.23拷贝到目标机并ln -s为libdl.so.2,运行ldd,如下:

      /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)
      

      然后,执行ip link add veth0a type veth peer name veth0b成功

  • 5)将ip目录下的ip程序放进板子看能否运行

    直接就能运行

    普朗克常量/PS: 以前以为要下载内核对应的iproute2版本才能正常运行,但我编译了最新的iiproute版本放进板子一样可以运行。

    # 动态链接时,报告缺少.so文件
    /root # ./ip --help
    ./ip: error while loading shared libraries: libdl.so.2: cannot open shared object file: No such file or directory  
    
  • 参考:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章