Cgroup学习之——Ubuntu下交叉编译ARM平台libcgroup工具

cgroup系列文章:https://blog.csdn.net/Vince_/article/details/89070001

libcgroup包含cgroup用户空间工具套件(如lscgroup,lssubsys等)以及静态或者动态库,以供其他程序调用,并且包含debug套件。在学习cgroup以及Docker等内容的过程中非常有用,其编译过程总结如下

1. 下载libcgroup-0.41.tar.xz软件包

下载路径如下:

2. 安装交叉编译工具

平台

Ubuntu-18.04,用了MacBook Pro2017 Touch Bar版本的硬件平台,安装了最新稳定发布版Ubuntu

Host

aarch64架构,用Qemu虚拟环境,自己交叉编译的Linux运行,将编译的工具打包到文件系统即可

博主使用了aarch64架构的交叉变异工具进行试验,对应其他架构如arm或者powerpc等,可以同样下载安装对应的交叉编译工具。编译工具binutils-aarch64-linux-gnu,gcc-aarch64-linux-gnu和g++-aarch64-linux-gnu,在Ubuntu-18.04上默认会安装gcc-7版本,注意此处必须要另外安装g++-aarch64-linux-gnu软件包,编译过程中需要用到g++,否则会出错。

  •     sudo apt install binutils-aarch64-linux-gnu
  •     sudo apt install gcc-aarch64-linux-gnu
  •     sudo apt install g++-aarch64-linux-gnu

3. 将软件包解压并进入目录

  •     xz -d libcgroup-0.41.tar.xz
  •     tar -xf libcgroup-0.41.tar
  •     cd libcgroup-0.41

4. 配置并进行编译

主要有四个步骤:

4.1 安装依赖包

  • sudo apt install automake m4 libtool bison flex

4.2 执行configure文件生成Makefile

  • $ CC=aarch64-linux-gnu-gcc CROSS_COMPILE=aarch64-linux-gnu- CXX=aarch64-linux-gnu-g++ ./configure --prefix /media/hugo/kston/source/libcgroup-0.41/_install --disable-pam --host aarch64-linux-gnu

4.3 编译

  • make

4.4 安装

  • make install

其中configure的时候需要配置相应的选项,可以根据环境以及需要配置,有问题可以直接查看help信息。执行完上面的步骤之后就可以在前面的--prefix指定的安装路径找到所有安装文件。博主指定了/media/hugo/kston/source/libcgroup-0.41/_install,安装大家根据自己的环境可以指定对应的路径并在其中找到编译结果安装文件

 

编译可能遇到的问题

1. configure时功能不支持问题

log如下:

checking for pam_syslog in -lpam... no
configure: error: Cannot compile PAM module without libpam!

通过查看help可以看到该功能相关信息:

--enable-pam-module-dir=PATH
                          specify location of libcgroup PAM module
                          (default=$libdir/security)

查看help的方式如下:

  •     ./configure --help

解决方式:./configure 添加 --disable-pam 选项注释掉该功能

2. 修改了configure.in或者configure.am

直接编译会出错:

CDPATH="${ZSH_VERSION+.}:" && cd . && /bin/bash /media/hugo/kston/source/libcgroup-0.41/missing aclocal-1.13
/media/hugo/kston/source/libcgroup-0.41/missing: line 81: aclocal-1.13: command not found
WARNING: 'aclocal-1.13' is missing on your system.
         You should only need it if you modified 'acinclude.m4' or
         'configure.ac' or m4 files included by 'configure.ac'.
         The 'aclocal' program is part of the GNU Automake package:
         <http://www.gnu.org/software/automake>
         It also requires GNU Autoconf, GNU m4 and Perl in order to run:
         <http://www.gnu.org/software/autoconf>
         <http://www.gnu.org/software/m4/>
         <http://www.perl.org/>
Makefile:397: recipe for target 'aclocal.m4' failed
make: *** [aclocal.m4] Error 127

需要在修改之后重新生成配置信息:

  •     autoreconf -ivf

3. 如果autoreconf出错

需要安装libtool工具:

  •     sudo apt install libtool

4. 出现函数未定义的问题

log如下:

  CCLD     cgrulesengd
cgrulesengd.o: In function `cgre_store_unchanged_process':
/media/hugo/kston/source/libcgroup-0.41/src/daemon/cgrulesengd.c:310: undefined reference to `rpl_realloc'
cgrulesengd.o: In function `cgre_store_parent_info':
/media/hugo/kston/source/libcgroup-0.41/src/daemon/cgrulesengd.c:223: undefined reference to `rpl_realloc'
../../src/.libs/libcgroup.a(api.o): In function `cg_test_mounted_fs':
/media/hugo/kston/source/libcgroup-0.41/src/api.c:1085: undefined reference to `rpl_malloc'
../../src/.libs/libcgroup.a(api.o): In function `cg_set_control_value':
/media/hugo/kston/source/libcgroup-0.41/src/api.c:1387: undefined reference to `rpl_malloc'

这是因为在配置中对相应的malloc和realloc函数进行了重定义AC_FUNC_MALLOC和AC_FUNC_REALLOC注释掉,执行autoreconf操作之后再次编译即可

5. 编译出现File format wrong问题

log如下:

  CCLD     libcgrouptest01
  CXX      libcg_ba.o
  CXXLD    libcg_ba
/usr/bin/ld: ../src/.libs/libcgroup.a(api.o): Relocations in generic ELF (EM: 183)
/usr/bin/ld: ../src/.libs/libcgroup.a(api.o): Relocations in generic ELF (EM: 183)
/usr/bin/ld: ../src/.libs/libcgroup.a(api.o): Relocations in generic ELF (EM: 183)
/usr/bin/ld: ../src/.libs/libcgroup.a(api.o): Relocations in generic ELF (EM: 183)
/usr/bin/ld: ../src/.libs/libcgroup.a(api.o): Relocations in generic ELF (EM: 183)
/usr/bin/ld: ../src/.libs/libcgroup.a(api.o): Relocations in generic ELF (EM: 183)
/usr/bin/ld: ../src/.libs/libcgroup.a(api.o): Relocations in generic ELF (EM: 183)
../src/.libs/libcgroup.a: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
Makefile:733: recipe for target 'libcg_ba' failed
make[3]: *** [libcg_ba] Error 1
make[3]: Leaving directory '/media/hugo/kston/source/libcgroup-0.41/tests'
Makefile:850: recipe for target 'all-recursive' failed
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory '/media/hugo/kston/source/libcgroup-0.41/tests'
Makefile:471: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/media/hugo/kston/source/libcgroup-0.41'
Makefile:376: recipe for target 'all' failed
make: *** [all] Error 2

通过./configure --help可以看到有一些交叉编译符号需要设置,此处CXXLD识别不了文件格式,打印文件格式错误,所以应该设置该符号重新编译,即在./configure过程中指定

  •     CXX=aarch64-linux-gnu-g++

重新执行configure过程后make编译

6. 此时出现新的问题

log如下

checking whether we are cross compiling... configure: error: in `/media/hugo/kston/source/libcgroup-0.41':
configure: error: cannot run C++ compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details

看起来在交叉编译时需要指定其他参数,如--host,表示目标文件需要执行的平台,而通过--help信息也可以看到configure对相关交叉编译选项的说明,指定平台信息:

  •     --host aarch64-linux-gnu

重新编译通过

7. 通过--prefix指定文件安装路径

在进行make install将编译目标安装到对应路径,如果不存在路径,则会自动创建

$ ls -l _install/
total 20
drwxr-xr-x 2 hugo hugo 4096 3月  30 22:34 bin
drwxr-xr-x 3 hugo hugo 4096 3月  30 22:34 include
drwxr-xr-x 3 hugo hugo 4096 3月  30 22:34 lib
drwxr-xr-x 2 hugo hugo 4096 3月  30 22:34 sbin
drwxr-xr-x 3 hugo hugo 4096 3月  30 22:34 share

文件信息如下:

$ tree _install/{bin,lib,sbin}
_install/bin
├── cgclassify
├── cgcreate
├── cgdelete
├── cgexec
├── cgget
├── cgset
├── cgsnapshot
├── lscgroup
└── lssubsys
_install/lib
├── libcgroup.la
├── libcgroup.so -> libcgroup.so.1.0.41
├── libcgroup.so.1 -> libcgroup.so.1.0.41
├── libcgroup.so.1.0.41
└── pkgconfig
    └── libcgroup.pc
_install/sbin
├── cgclear
├── cgconfigparser
└── cgrulesengd

1 directory, 17 files

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值