linux制作ext4格式image有兩種方法:1、raw image,這種img會把文件系統填充0,因此可以直接掛載,但是占用空間比較大;2、sparse image,這種img是稀疏描述,不能直接掛載,大小就是文件系統中文件的大小
raw img
先dd創建一個空的img文件,然后給他格式化成ext4,掛載這個img,最后把需要的文件放進去。
dd if=/dev/zero of=rootfs.img bs=1M seek=0 count=256
(或dd if=/dev/null of=rootfs.img bs=1M seek=256)
mkfs.ext4 -F -L "rootfs" root.img
mkdir mountpoint
sudo mount root.img mountpoint
cp <fs file> mountpoint
sync
sudo umount mountpoint
sparse img
需要用到一個Android的工具make_ext4fs,命令看起來很簡單,但是有幾個坑。
make_ext4fs <-s> -l 256M <-a root> -L linux roofs.img rootfs_source_dir
-s 參數是選擇是否創建sparse格式,不加即為raw
-l 肯定是大小唄
-L 是label
-a root 是給linux做的,但是如果加了這個參數,那么最后出來的img中,權限會改變,與源目錄文件不再一樣,變成rw-r--r--
-a system 是給Android做的
這個工具最后出來的用戶和組是root,而不是源目錄文件的用戶,這點要注意
fakeroot
fakeroot command
This is useful for allowing users to create archives (tar, ar, .deb
etc.) with files in them with root permissions/ownership. Without fakeroot one would need
to have root privileges to create the constituent files of the archives with the correct
permissions and ownership, and then pack them up, or one would have to construct the
archives directly, without using the archiver.archives directly, without using the archiver.
有時候我們想制作用戶和組是root的img鏡像,但是由於沒有sudo,最終出來的卻是源目錄的用戶,也就是編譯者,此時就可以用這個東西欺騙一下虛擬出來一個root環境。