Repacking system.img with make_ext4fs

I’ve compiled aosp and then tried to repack my system.img using the following steps:

simg2img system.img system.raw
mount -o loop -t ext4 system.raw ./systemdir
make_ext4fs -s -l SIZE -a system custom-system.img ./systemdir

The size is computed from tune2fs -l ./system.raw (blocks * block size).
Then I flash this image with fastboot:
fastboot flash system ./custom-system.img

The kernel starts booting and then I see the following over the serial console:

[ 7.404572] init: Switching root to ‘/system’
[ 7.584143] EXT4-fs (sdd11): mounted filesystem without journal. Opts:
[ 7.764133] init: Skipped setting INIT_AVB_VERSION (not in recovery mode)
[ 7.771193] init: execv("/system/bin/init") failed: Permission denied
[ 7.788993] init: #00 pc 00000000002e4860 /init (deleted)

The board then reboots into fastboot mode. What am I doing wrong? I haven’t even modified the system image, just tried to repack it.

Is this an academic exercise? Or is there an objective at the end of it? If there is an objective, there may be a better way to accomplish it.

I need to modify stock system.img (add a file to it) and flash it back to the device. If there’s another way, please let me know

Well first of all, it is inadvisable to make changes to the system image. It’s always better to make the changes to vendor.

And second, if you can build the images from source, you really should make your changes to the build tree and run a build with the changes in place.

What are you trying to change?

I want to add a binary file to /system/bin/myprog on the device without rebuilding the entire tree. Is there a simpler way? I’ve found several places on the forum describing the process for repacking I’ve shown above but it’s not working. Do you know why? If not, is there another way to repack the existing system.img without rebuilding?

adb root
adb remount
adb push myprog /vendor/bin/