今天在编译android-O(8.0)的时候,我自己新增加了一个ext4格式的分区,在编译这个分区的时候,需要对应的生成文件系统的配置文件xxxx_filesystem_config.txt,发现了生成这个配置文件的的Makefile里面,调用了sed命令来处理一些字符串:
build/core/Makefile
define fs_config
(cd $(1); find . -type d | sed 's,$$,/,'; find . ! -type d) | cut -c 3- | sort | sed 's,^,$(2),' | $(HOST_OUT_EXECUTABLES)/fs_config -C -D $(TARGET_OUT) -S $(SELINUX_FC)
endef
$(call fs_config,$(zip_root)/VENDOR,vendor/) > $(zip_root)/META/vendor_filesystem_config.txt
刚开始看的有点晕,一句命令很长,我们拆分开来一个个分析:
cd $(1), 进入到指定的目录
find . -type d | sed 's,$$,/,'; 查找当前目录下所有的子目录,如果以$结尾的,使用/替换$
find . ! -type d 查找当前目录下所有的非目录文件
cut -c 3- 去掉字符串的前面两个字符
Sort 排序
sed 's,^,$(2) 字符串开头加上指定字符串
我们来看看,这一系列的命令到底做了哪些动作:
给出的调用是,用来生成vendor的系统配置文件:
具体流程如下:
cd $(1), 进入到源文件目录:
out/target/product/xxxx/obj/PACKAGING/target_files_intermediates/xxxx-target_files-20170714/VENDOR
find . -type d | sed 's,$$,/,' 查找目录文件:
.
./lib64
./lib64/mediadrm
./lib64/hw
./lib64/soundfx
./package_config
./etc
./etc/bluetooth
./etc/seccomp_policy
......
find . ! -type d 查找非目录文件:
./lib64/libwpa_client.so
./lib64/libsurfaceoverlay_jni.so
./lib64/mediadrm/libdrmclearkeyplugin.so
./lib64/libril.so
./lib64/libfbcnf.so
./lib64/libamavutils.so
......
cut -c 3- 取第三个字符开始的数据
lib64
lib64/mediadrm
lib64/hw
lib64/soundfx
package_config
etc
etc/bluetooth
etc/seccomp_policy
....
lib64/libwpa_client.so
lib64/libsurfaceoverlay_jni.so
lib64/mediadrm/libdrmclearkeyplugin.so
lib64/libril.so
lib64/libfbcnf.so
lib64/libamavutils.so
....
Sort 排序
etc
etc/bluetooth
etc/seccomp_policy
lib64
lib64/hw
lib64/libamavutils.so
lib64/libfbcnf.so
lib64/libril.so
lib64/libsurfaceoverlay_jni.so
lib64/libwpa_client.so
lib64/mediadrm
lib64/mediadrm/libdrmclearkeyplugin.so
lib64/soundfx
package_config
sed 's,^,$(2) 没一行前面加上 vendor/
vendor/
vendor/etc
vendor/etc/bluetooth
vendor/etc/seccomp_policy
vendor/lib64
vendor/lib64/hw
vendor/lib64/libamavutils.so
vendor/lib64/libfbcnf.so
vendor/lib64/libril.so
vendor/lib64/libsurfaceoverlay_jni.so
vendor/lib64/libwpa_client.so
vendor/lib64/mediadrm
vendor/lib64/mediadrm/libdrmclearkeyplugin.so
vendor/lib64/soundfx
vendor/package_config
至此,包含了对vendor下的所有文件以及目录的集合,然后执行:
out/host/linux-x86/obj/EXECUTABLES/fs_config_intermediates/fs_config -C -D out/target/product/xxxx/ -S target/product/xxxx/obj/ETC/file_contexts.bin_intermediates/file_contexts.bin
会根据file_contexts.bin中配置的selinux权限,对上述集合中所以的目录以及非目录文件,赋予相应的selinux权限,然后生成vendor_systemfile_config.txt:
vendor 0 2000 755 selabel=u:object_r:vendor_file:s0 capabilities=0x0
vendor/app 0 2000 755 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/AppInstaller 0 2000 755 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/AppInstaller/AppInstaller.apk 0 0 644 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/BluetoothRemote 0 2000 755 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/BluetoothRemote/BluetoothRemote.apk 0 0 644 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/DLNA 0 2000 755 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/DLNA/DLNA.apk 0 0 644 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/DLNA/lib 0 2000 755 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/DLNA/lib/arm 0 2000 755 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/DLNA/lib/arm/libhisivideo19.so 0 0 644 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
vendor/app/DLNA/lib/arm/libhisivideo_3798m.so 0 0 644 selabel=u:object_r:vendor_app_file:s0 capabilities=0x0
至此,Makefile中通过sed命令,生成分区文件系统的selinux的配置文件。