今天在編譯android-O(8.0)的時候,我本身新增長了一個ext4格式的分區,在編譯這個分區的時候,須要對應的生成文件系統的配置文件xxxx_filesystem_config.txt,發現了生成這個配置文件的的Makefile裏面,調用了sed命令來處理一些字符串:linux
build/core/Makefileandroid
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
剛開始看的有點暈,一句命令很長,咱們拆分開來一個個分析:app
cd $(1), 進入到指定的目錄ide
find . -type d | sed 's,$$,/,'; 查找當前目錄下全部的子目錄,若是以$結尾的,使用/替換$ui
find . \! -type d 查找當前目錄下全部的非目錄文件spa
cut -c 3- 去掉字符串的前面兩個字符code
Sort 排序排序
sed 's,^,$(2) 字符串開頭加上指定字符串ip
咱們來看看,這一系列的命令到底作了哪些動做:字符串
給出的調用是,用來生成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的配置文件。