Linux下 解包/打包 Android 映像文件 system.img, boot.img, ramdisk.img, userdata.img.
2014年10月20日 ⁄ 計算機視覺 ⁄ 共 1372字⁄ ⁄ 暫無評論
轉自: http://blog.csdn.net/yulix/article/details/12968705
Android源碼編譯成功后會輸出映像文件:system.img,boot.img, ramdisk.img,userdata.img等等。有時我們需要修改里面的內容,下面列出在Linux下如何解包/打包這些映像文件。
ramdisk.img
ramdisk.img是經cpio打包、並用gzip壓縮的文件。
解包: 新建一個工作目錄,把當前目錄更改為該工作目錄,執行下面命令(注意: img文件位置可能不同).
[plain] view plaincopy
- gunzip -c $HOME/img/ramdisk.img | cpio -i
打包:在工作目錄下,把該目錄下的所有內容打包
[plain] view plaincopy
- find . | cpio -o -H newc | gzip > ../newramdisk.img
參考文檔: http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images
boot.img
boot.img包含2K字節頭部,后面跟着的是zImage格式內核和和ramdisk格式的根文件系統。
解包工具: Android自帶的unpackbootimg,以及一些腳本工具比如split_bootimg.pl
打包工具: Android自帶的mkbootimg。
參考資料 :
中文請看: http://blog.csdn.net/wh_19910525/article/details/8200372
英文請看: http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images
system.img (EXT4)
system.img 是 sparse image格式文件,現有的mount命令無法直接處理。
我們得把sparse image格式轉變為普通的img格式,Android源碼中帶的ext4_utils可以做這個,沒有Android源碼也不用擔心,該工具的源代碼已被剝離出來,可以自行下載編譯,地址是:http://forum.xda-developers.com/showthread.php?t=1081239
我們得到工具有: simg2img,make_ext4fs等等:
解包:
[plain] view plaincopy
- simg2img system.img system.img.ext4
- mkdir mnt_dir
- sudo mount -t ext4 -o loop system.img.ext4 mnt_dir
打包:
[plain] view plaincopy
- sudo make_ext4fs -s -l 512M -a system system_new.img mnt_dir
注意:在我的機器上必須用root權限執行make_ext4fs,否則新生成的image文件無法使用。
userdata.img (EXT4)
和system.img(EXT4) 一樣處理
( file_context_open: Error getting file context handle (No such file or directory)
No such file or directory )
參考下面鏈接:
the problem is that this version requires a file_contexts file. This can be extracted from boot.img. The filename is file_contexts
1. extract boot.img (i used AndroidImageKitchen for this)
2. call make_ext4fs with -S parameter
i.e.
Code:
make_ext4fs -l -s 2690M -a system -S <PATH_TO_FILE_CONTEXTS_FILE> system.img.ext4 <WHEREEVER_YOU_MOUNTED_SYSTEM_TO>
btw: the name of the mountpoint doesnt matter. So calling your mountpoint sys was no problem
If you have extracted boot.img the file_contexts-file should be located in the ramdisk folder.
Don't panic if there is no such file. In this case you just have to use an earlier version of make_ext4fs.
The file_contexts file is used by versions of android which use SELinux. I only found this on android versions >= 4.3.
所以最終的命令是:
./make_ext4fs -s -l 550M -a system -S ./file_contexts system_new.img mnt_dir
make_ext4fs [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]
[ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]
[ -L <label> ] [ -f ] [ -a <android mountpoint> ]
[ -S file_contexts ]
[ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ]
<filename> [<directory>]
參數解析
#make_ext4fs -s -l 512M -a root -L linux ./rootfs_qt.img ./root
執行之后即會將root文件打包成 rootfs_qt.img 文件系統鏡像。
-l 512M"是分區大小,i9100的system分區是512M;
-s就是生成ext4的S模式制作;
-s 就是生成ext4的S模式制作;
-l 512M 是分區大小;
-a root 是指這個img用於Linux系統(若為-a system即表示為android系統,掛載點即是/system。使用這個參數,make_ext4fs會根據private/android_filesystem_config.h里定義好的權限來給文件夾里的所有文件重新設置權限,如果你刷機以后發現有文件權限不對,可以手工修改android_filesystem_config.h來添加權限,重新編譯make_ext4fs,也可以不使用 “-a system”參數,這樣就會使用文件的默認權限)。
./rootfs_qt.img 表示在當前目錄下生成鏡像文件。
./root 指定源路徑