Skip to content

Instantly share code, notes, and snippets.

@ktprograms
Last active October 27, 2021 08:29
Show Gist options
  • Save ktprograms/c8ea850abde717376ff7a6f83dbabb2f to your computer and use it in GitHub Desktop.
Save ktprograms/c8ea850abde717376ff7a6f83dbabb2f to your computer and use it in GitHub Desktop.
OpenRC init script that adds qemu binfmt_misc entries. Modified version of https://github.com/jirutka/qemu-openrc/blob/master/qemu-binfmt.initd
#!/sbin/openrc-run
i386_magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03\x00"
i386_mask="\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
x86_64_magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00"
x86_64_mask="\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
arm_magic='\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00'
arm_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'
ppc64le_magic='\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x15\x00'
ppc64le_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\x00'
s390x_magic='\x7fELF\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x16'
s390x_mask='\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff'
describe="Configure binfmt misc emulation via QEMU (x86, x86_64, arm, ppc64le and s390x only)"
depend() {
after procfs
}
start() {
ebegin "Registering QEMU binaries in binfmt misc"
if [ ! -d /proc/sys/fs/binfmt_misc ]; then
modprobe binfmt_misc || eend $? || return 1
fi
if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc >/dev/null || eend $? || return 1
fi
local retval=0
for arch in i386 x86_64 arm ppc64le s390x; do
local interpreter="/usr/bin/qemu-$arch"
local magic=$(eval echo \$${arch}_magic)
local mask=$(eval echo \$${arch}_mask)
if [ -x $interpreter ]; then
echo ":qemu-$arch:M::$magic:$mask:$interpreter:OCF" > /proc/sys/fs/binfmt_misc/register || {
eerror "Failed to register binfmt qemu-$arch"
retval=1
}
fi
done
eend $retval
}
stop() {
ebegin "Unregistering QEMU binaries in binfmt misc"
find /proc/sys/fs/binfmt_misc -name "qemu-*" | while read path; do
echo -1 > "$path"
done
eend $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment