In order to create a partition layout similar to real Android devices, I decided to try to use loop devices.
I've been searching for this on Google, but many point out that the mount
command in init.rc cannot handle loop (I think like losetup
or mount -o loop <file>
). Some even suggest adding a custom command to handle losetup
command.
Finally, I decided to look into the source code of init
. In the file system/core/init/builtins.c
I was surprised to see that in the function do_mount
(line 319) there is some code for handling loop device.
[...] } else if (!strncmp(source, "loop@", 5)) { int mode, loop, fd; struct loop_info info; mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; fd = open(source + 5, mode); if (fd < 0) { return -1; } for (n = 0; ; n++) { sprintf(tmp, "/dev/block/loop%d", n); loop = open(tmp, mode); if (loop < 0) { return -1; } /* if it is a blank loop device */ if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { /* if it becomes our loop device */ if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { close(fd); if (mount(tmp, target, system, flags, options) < 0) { ioctl(loop, LOOP_CLR_FD, 0); close(loop); return -1; } close(loop); return 0; } } close(loop); } close(fd); ERROR("out of loopback devices"); return -1; } else { [...]
It handles specifying loop devices like the way of mtd partitions.
So the solution is to use this:
mount ext4 loop@file.img /system
However the file is located at the SD card, so I have to mount it before that:
mkdir /mnt/sdcard2 0000 system system mount vfat /dev/block/mmcblk0p1 /mnt/sdcard2 mount ext4 loop@/mnt/sdcard2/file.img /system
Here I used /mnt/sdcard2
as a mountpoint to prevent affecting Android's internal SD card system (though it's not supposed to remove the SD card).