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

  1. gunzip -c $HOME/img/ramdisk.img | cpio -i

打包:在工作目录下,把该目录下的所有内容打包

[plain] view plaincopy

  1. 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

  1. simg2img system.img system.img.ext4
  2. mkdir mnt_dir
  3. sudo mount -t ext4 -o loop system.img.ext4 mnt_dir

打包:

[plain] view plaincopy

  1. 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 )

参考下面链接:

http://forum.xda-developers.com/galaxy-s2/general/ref-unpacking-repacking-stock-rom-img-t1081239/page23

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 指定源路径

更多相关文章

  1. android 基于百度地图api获取经纬度
  2. 解决:Failed to fectch URl https://dl-ssl.google.com/android/r
  3. Android(安卓)studio 使用心得(十)---android studio 多渠道打包
  4. Android(安卓)Permission denied 错误(附Android权限大全)
  5. 用Flex开发移动应用(1)---- 创建一个新的Mobile工程
  6. Android(安卓)图标设置 尺寸
  7. AndroidManifest.xml 配置文件
  8. android下面res目录使用
  9. NPM 和webpack 的基础使用

随机推荐

  1. android 系统架构介绍
  2. Ubuntu上搭建Android编译环境(1)
  3. Android程序结构
  4. Android免root查看数据库内容
  5. android frameworks 源码介绍
  6. Google解密携手宏达电:确保Android一炮打
  7. android中延迟执行某个任务
  8. Android(安卓)App如何接入Google Map(三)
  9. Android(java)学习笔记135:Android中assets
  10. Android 之 Project Butter 详细介绍