Before knowing about binfmt, I always wondered how wine is able is able to execute .exe files directly, i.e. ./prog.exe instead of wine ./prog.exe. Turns out the wine package (at least on Arch) comes with a handler for them and the Arch wiki mentions that you may want to remove it for security reasons.
thayne|3 months ago
[1]: https://wiki.archlinux.org/title/Binfmt_misc_for_Java
mbreese|3 months ago
FYI - Because JAR files are specially formatted ZIP files, you can also prepend a shell script stub to the front of the file. Java reads JAR files and doesn't start processing them until it sees the ZIP magic bytes (PK\x03\x04). So long as your shell script doesn't contain those bytes, you can add whatever you want.
This is about the minimal stub script you can get away with.
Using this, you don't even need binfmt to execute JARs. IMHO, the better example for binfmt and Java is executing class files directly... which is also covered in your linked Arch docs.WhyNotHugo|3 months ago
mamikk|3 months ago
But inside a container (with its own mount namespace) or inside a chroot then the qemu binaries does not necessarily exist. But the binfmt_misc handler will still work in this case because of two features.
1. The kernel will open the qemu binaries in the original mount namespace when the binfmt_misc handler is registered with the F-flag (Fix binary) so the kernel will always have an open file reference to the qemu binary independent of mount namespace.
2. Distributions (at least Debian) ships statically linked qemu binaries so that qemu does not need to load any shared libraries inside the target namespace/chroot.
nicman23|3 months ago
ktm5j|3 months ago