【干货分享】优化rootfs的大小

作者:付汉杰,hankf@xilinx.com,文章转载自:赛灵思中文社区论坛

1. 介绍
Xilinx VCU TRD 集成了图像和视频编解码功能,生成的根文件系统比较大,2020.1的rootfs.cpio.gz大约有114MB。工程师可以根据自己需要裁剪,减少根文件系统大小。
在Ubuntu下,解压rootfs.cpio.gz,得到rootfs.cpio,再解压,得到就是文件了。

2. 检查目录大小
使用du命令,可以检查各个文件夹的大小。

rootfs@rootfs$ du --max-depth=1 -h
4.0K	./run
8.5M	./lib
20K	./home
4.0K	./boot
4.0K	./tmp
4.0K	./sys
2.9M	./bin
4.0K	./mnt
312M	./usr
112K	./var
2.8M	./opt
2.0M	./etc
8.0K	./media
4.0K	./dev
4.0K	./proc
2.0M	./sbin
330M	

也可以看更深的目录,排除占用空间小的目录。

rootfs@rootfs$ du --max-depth=2 -h | grep [0-9]M
3.1M	./lib/modules
8.5M	./lib
2.9M	./bin
206M	./usr/lib
22M	./usr/bin
1.3M	./usr/libexec
80M	./usr/share
3.8M	./usr/sbin
312M	./usr
2.8M	./opt/vcu_qt
2.8M	./opt
2.0M	./etc
2.0M	./sbin
330M	.
rootfs$ du --max-depth=2 -h | grep [0-9]M
5.9M	./lib
2.9M	./bin
18M	./usr/lib
5.1M	./usr/bin
6.2M	./usr/share
3.3M	./usr/sbin
33M	./usr
2.0M	./sbin
45M	.

3. 检查文件大小
目录大小有助于粗略的分析根文件系统大小。要找到真正的原因,并找到减小根文件系统大小,需要分析文件大小。
使用find命令,找到文件,再使用ls命令,查看文件大小;最后使用grep命令过滤出大文件。

rootfs$ find | xargs -n 1 ls -l -h | grep [0-9]M
total 4.3M
-rwxr-xr-x 1 hankf hankf 1.4M May 27 13:58 libc-2.30.so
-rwxr-xr-x 1 hankf hankf 1.4M May 27 13:58 ./lib/libc-2.30.so
total 2.9M
-rwxr-xr-x 1 hankf hankf 1.2M May 27 14:07 bash.bash
-rwsr-xr-x 1 hankf hankf 1.6M May 27 14:09 busybox
-rwsr-xr-x 1 hankf hankf 1.6M May 27 14:09 ./bin/busybox
-rwxr-xr-x 1 hankf hankf 1.2M May 27 14:07 ./bin/bash.bash
total 105M
-rwxr-xr-x  1 hankf hankf 1.1M May 27 14:07 libasound.so.2.0.0
-rwxr-xr-x  1 hankf hankf 1.2M May 27 05:01 libcairo.so.2.11600.0
-rwxr-xr-x  1 hankf hankf 2.5M May 27 14:04 libcrypto.so.1.1
-rwxr-xr-x  1 hankf hankf 1.3M May 27 05:00 libepoxy.so.0.0.0
-rwxr-xr-x  1 hankf hankf 1.9M May 27 14:11 libgio-2.0.so.0.6000.7
-rwxr-xr-x  1 hankf hankf 1.2M May 27 14:11 libglib-2.0.so.0.6000.7
-rwxr-xr-x  1 hankf hankf 1.8M May 27 14:13 libgnutls.so.30.24.0
-rwxr-xr-x  1 hankf hankf 1.3M May 27 14:20 libgstreamer-1.0.so.0.1601.0
-rwxr-xr-x  1 hankf hankf 7.2M May 27 05:06 libgtk-3.so.0.2404.4
-r-xr-xr-x  1 hankf hankf 3.0M May 27 14:11 libpython3.7m.so.1.0
-rwxr-xr-x  1 hankf hankf 1.9M May 27 05:27 libQt5Charts.so.5.13.2
-rwxr-xr-x  1 hankf hankf 6.3M May 27 05:18 libQt5Core.so.5.13.2
-rwxr-xr-x  1 hankf hankf 5.8M May 27 05:18 libQt5Gui.so.5.13.2
-rwxr-xr-x  1 hankf hankf 1.9M May 27 05:18 libQt5Network.so.5.13.2
-rwxr-xr-x  1 hankf hankf 4.9M May 27 05:23 libQt5Qml.so.5.13.2
-rwxr-xr-x  1 hankf hankf 4.9M May 27 05:23 libQt5Quick.so.5.13.2
-rwxr-xr-x  1 hankf hankf 1.1M May 27 05:26 libQt5WaylandClient.so.5.13.2
-rwxr-xr-x  1 hankf hankf 1.6M May 27 05:26 libQt5WaylandCompositor.so.5.13.2
-rwxr-xr-x  1 hankf hankf 6.8M May 27 05:18 libQt5Widgets.so.5.13.2
-rwxr-xr-x  1 hankf hankf 1.3M May 27 05:18 libQt5XcbQpa.so.5.13.2
-rwxr-xr-x  1 hankf hankf 1.5M May 27 14:10 libsamplerate.so.0.1.8
-rwxr-xr-x  1 hankf hankf 1.1M May 27 14:05 libsqlite3.so.0.8.6
-rwxr-xr-x  1 hankf hankf 1.8M May 27 14:02 libstdc++.so.6.0.27
-rwxr-xr-x  1 hankf hankf 1.5M May 27 14:08 libunistring.so.2.1.0
-rwxr-xr-x  1 hankf hankf 1.5M May 27 14:03 libvpx.so.6.1.0
-rwxr-xr-x  1 hankf hankf 1.3M May 27 14:07 libX11.so.6.3.0
-rwxr-xr-x  1 hankf hankf 1.4M May 27 14:14 libxml2.so.2.9.9
-r-xr-xr-x 1 hankf hankf 3.0M May 27 14:11 ./usr/lib/libpython3.7m.so.1.0
-rwxr-xr-x 1 hankf hankf 1.9M May 27 05:18 ./usr/lib/libQt5Network.so.5.13.2
-rwxr-xr-x 1 hankf hankf 1.5M May 27 14:10 ./usr/lib/libsamplerate.so.0.1.8
-rwxr-xr-x 1 hankf hankf 6.3M May 27 05:18 ./usr/lib/libQt5Core.so.5.13.2
-rwxr-xr-x 1 hankf hankf 1.5M May 27 14:08 ./usr/lib/libunistring.so.2.1.0
-rwxr-xr-x 1 hankf hankf 4.9M May 27 05:23 ./usr/lib/libQt5Quick.so.5.13.2
total 1.7M
-rw-r--r-- 1 hankf hankf 1.7M Jul 15 17:32 libMali.so.9.0
-rw-r--r-- 1 hankf hankf 1.7M Jul 15 17:32 ./usr/lib/x11/libMali.so.9.0
-rwxr-xr-x 1 hankf hankf 1.1M May 27 05:26 ./usr/lib/libQt5WaylandClient.so.5.13.2
-rwxr-xr-x 1 hankf hankf 6.8M May 27 05:18 ./usr/lib/libQt5Widgets.so.5.13.2
-rwxr-xr-x 1 hankf hankf 1.2M May 27 05:01 ./usr/lib/libcairo.so.2.11600.0
total 1.7M
-rw-r--r-- 1 hankf hankf 1.7M Jul 15 17:32 libMali.so.9.0
-rw-r--r-- 1 hankf hankf 1.7M Jul 15 17:32 ./usr/lib/headless/libMali.so.9.0
-rwxr-xr-x 1 hankf hankf 1.8M May 27 14:13 ./usr/lib/libgnutls.so.30.24.0
-rwxr-xr-x 1 hankf hankf 1.9M May 27 05:27 ./usr/lib/libQt5Charts.so.5.13.2
total 4.7M
total 4.6M
-rwxr-xr-x 1 hankf hankf 1.1M May 27 14:11 unicodedata.cpython-37m-aarch64-linux-gnu.so
-rwxr-xr-x 1 hankf hankf 1.1M May 27 14:11 ./usr/lib/python3.7/lib-dynload/unicodedata.cpython-37m-aarch64-linux-gnu.so
total 1.2M
total 2.0M
total 2.0M
-rw-r--r-- 1 hankf hankf 1.4M May 27 14:11 pip-19.2.3-py2.py3-none-any.whl
-rw-r--r-- 1 hankf hankf 1.4M May 27 14:11 ./usr/lib/python3.7/ensurepip/_bundled/pip-19.2.3-py2.py3-none-any.whl
total 1.8M
total 1.8M
total 1.3M
total 9.6M
ls: cannot access './usr/lib/python3.7/site-packages/setuptools/script': No such file or directory
ls: cannot access '(dev).tmpl': No such file or directory
ls: cannot access './usr/lib/python3.7/site-packages/setuptools/command/launcher': No such file or directory
ls: cannot access 'manifest.xml': No such file or directory
total 2.3M
total 1.1M
total 1.1M
-rwxr-xr-x 1 hankf hankf 1.3M May 27 05:18 ./usr/lib/libQt5XcbQpa.so.5.13.2
-rwxr-xr-x 1 hankf hankf 1.2M May 27 14:11 ./usr/lib/libglib-2.0.so.0.6000.7
-rwxr-xr-x 1 hankf hankf 1.9M May 27 14:11 ./usr/lib/libgio-2.0.so.0.6000.7
total 2.9M
-rw-r--r-- 1 hankf hankf 2.9M Jul 15 17:38 locale-archive
-rw-r--r-- 1 hankf hankf 2.9M Jul 15 17:38 ./usr/lib/locale/locale-archive
-rwxr-xr-x 1 hankf hankf 4.9M May 27 05:23 ./usr/lib/libQt5Qml.so.5.13.2
total 1.4M
total 1.1M
-rwxr-xr-x 1 hankf hankf 1.1M May 27 14:05 ./usr/lib/libsqlite3.so.0.8.6
-rwxr-xr-x 1 hankf hankf 1.5M May 27 14:03 ./usr/lib/libvpx.so.6.1.0
-rwxr-xr-x 1 hankf hankf 1.8M May 27 14:02 ./usr/lib/libstdc++.so.6.0.27
-rwxr-xr-x 1 hankf hankf 2.5M May 27 14:04 ./usr/lib/libcrypto.so.1.1
-rwxr-xr-x 1 hankf hankf 1.6M May 27 05:26 ./usr/lib/libQt5WaylandCompositor.so.5.13.2
-rwxr-xr-x 1 hankf hankf 1.3M May 27 14:07 ./usr/lib/libX11.so.6.3.0
-rwxr-xr-x 1 hankf hankf 1.1M May 27 14:07 ./usr/lib/libasound.so.2.0.0
-rwxr-xr-x 1 hankf hankf 1.4M May 27 14:14 ./usr/lib/libxml2.so.2.9.9
-rwxr-xr-x 1 hankf hankf 1.3M May 27 05:00 ./usr/lib/libepoxy.so.0.0.0
-rwxr-xr-x 1 hankf hankf 1.3M May 27 14:20 ./usr/lib/libgstreamer-1.0.so.0.1601.0
total 1.7M
-rw-r--r-- 1 hankf hankf 1.7M Jul 15 17:32 libMali.so.9.0
-rw-r--r-- 1 hankf hankf 1.7M Jul 15 17:32 ./usr/lib/fbdev/libMali.so.9.0
total 1.5M
total 1.7M
-rw-r--r-- 1 hankf hankf 1.7M Jul 15 17:32 libMali.so.9.0
-rw-r--r-- 1 hankf hankf 1.7M Jul 15 17:32 ./usr/lib/wayland/libMali.so.9.0
-rwxr-xr-x 1 hankf hankf 7.2M May 27 05:06 ./usr/lib/libgtk-3.so.0.2404.4
-rwxr-xr-x 1 hankf hankf 5.8M May 27 05:18 ./usr/lib/libQt5Gui.so.5.13.2
total 13M
total 1.2M
total 2.2M
total 22M
-rwxr-xr-x 1 hankf hankf 1.1M Jul 15 17:32 ctrlsw_encoder
-rwxr-xr-x 1 hankf hankf 7.7M May 27 14:28 gdb
-rwxr-xr-x 1 hankf hankf 2.2M May 27 05:03 Xorg
-rwxr-xr-x 1 hankf hankf 2.2M May 27 05:03 ./usr/bin/Xorg
-rwxr-xr-x 1 hankf hankf 1.1M Jul 15 17:32 ./usr/bin/ctrlsw_encoder
-rwxr-xr-x 1 hankf hankf 7.7M May 27 14:28 ./usr/bin/gdb
total 1.8M
total 1.2M
total 4.0M
total 1.3M
total 1.2M
total 1.3M
total 1.3M
total 1.4M
total 5.4M
-rw-r--r-- 1 hankf hankf 5.4M May 27 14:08 magic.mgc
-rw-r--r-- 1 hankf hankf 5.4M May 27 14:08 ./usr/share/misc/magic.mgc
total 1.9M
total 2.2M
-rw-r--r-- 1 hankf hankf 2.2M May 27 05:17 libpnp_basictools.a
-rw-r--r-- 1 hankf hankf 2.2M May 27 05:17 ./usr/share/examples/widgets/tools/plugandpaint/plugins/libpnp_basictools.a
total 3.8M
total 2.8M
-rwxr-xr-x 1 hankf hankf 2.7M Jul 15 17:34 vcu_qt
-rwxr-xr-x 1 hankf hankf 2.7M Jul 15 17:34 ./opt/vcu_qt/bin/vcu_qt
total 2.0M

4. 去掉不必要文件
根据上述找到的大文件,禁止不使用的软件包,就能减小根文件系统大小。

比如,在petalinux工程中,禁止下列软件包后,rootfs.cpio.gz从114MB,减小到16.4MB。下列内容是从project-spec/configs/rootfs_config提取的。

# CONFIG_gdb is not set
# CONFIG_gdbserver is not set
# CONFIG_kernel-module-hdmi is not set
# CONFIG_packagegroup-core-x11 is not set
# CONFIG_packagegroup-petalinux-audio is not set
# CONFIG_packagegroup-petalinux-gstreamer is not set
# CONFIG_packagegroup-petalinux-matchbox is not set
# CONFIG_packagegroup-petalinux-qt is not set
# packagegroup-petalinux-v4lutils 
# CONFIG_packagegroup-petalinux-v4lutils is not set
# CONFIG_packagegroup-petalinux-x11 is not set
# CONFIG_trd-init is not set
# CONFIG_vcu-gst-app is not set
# CONFIG_custom-edid is not set
# CONFIG_gst-shark is not set
# CONFIG_gstreamer-vcu-examples is not set
# CONFIG_vcu-qt is not set

推荐阅读