原文來自:https://blog.csdn.net/z1026544682/article/details/100137317,有小部分理解修改。
Ext4有兩種鏡像方式:一種是裸鏡像(raw image)和 稀疏鏡像(sparse image)。
1.1.1 raw image
1. 描述
這種是raw ext4 image(即raw image),使用file觀察:其特點是完整的ext4分區鏡像(如果未使用滿則使用0進行填充),可以直接使用mount進行掛載,也可以直接燒寫,因此比較大。
好處:升級時設備進行簡單的順序數據寫入。通過file可以查看其類型:
file rootfs.ext4:
rootfs.ext4: Linux rev 1.0 ext4 filesystem data, UUID=db0ca30c-e2c8-4b9a-b48a-a1620d43aa3a (extents) (large files) (huge files)
2. 制作方法
(a)方式一:使用 mkfs.ext4:
l 創建掛載目錄:mkdir mnt
l 生成一個128M全0的roofs.ext4文件:dd if=/dev/zero of=rootfs.ext4 bs=1M count=128
l 將新文件格式化為ext4格式:mkfs.ext4 rootfs.ext4
l 將文件掛載到mnt目錄,注意這里我們要使用mount –o loop的屬性,表示我們要把rootfs.ext4當作硬盤分區掛載到mnt:echo 123456 | sudo -S mount -o loop rootfs.ext4 mnt
l 將rootfs目錄下的文件拷貝到mnt目錄下:sudo cp rootfs/* mnt/ -rf
l 卸載mnt就完成了ext4文件系統制作:sudo umount mnt
(b)方式二:使用make_ext4fs(不帶-s參數):
make_ext4fs -l 128M roofs.ext4 rootfs
-l 128M:ext4文件系統分區大小
rootfs.ext4:生成ext4文件系統的目標文件
rootfs:生成ext4文件系統的源目錄
3. 燒寫(以mmc為例)
uboot下使用 mmc write ,linux下使用 dd,直接燒寫。
1.1.2 simg: sparse image
1. 描述
另外一種是sparse ext4 image,它是將raw ext4進行稀疏描述,因此尺寸比較小(制作目錄有多少文件就計算多少,沒有全零填充)。不能直接掛載。在linux下也不能直接燒寫,需要還原為 raw image格式。通過file來查看:
file alg.ext4:
alg.ext4: Android sparse image, version: 1.0, Total of 262144 4096-byte output blocks in 35 input chunks
2. 制作方法
同樣使用make_ext4fs,只需加上-s選項,即可生成sparse image。
make_ext4fs -l 128M -s roofs.simg rootfs
-s:生成ext4的S模式鏡像(simg)
3. 燒寫(linux)
使用simg2img將 simg (sparse ext4 image)還原為 raw ext4 image,再像raw image 那樣燒寫。
simg2img roofs.simg rootfs.ext4