commit:     175bcba82218df8e7ad40919b5d45dbcc3d2ac30
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Fri Jan 20 19:21:20 2017 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Fri Jan 20 19:26:19 2017 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=175bcba8

app-emulation/qemu: generate init.d script from upstream settings

 app-emulation/qemu/files/qemu-binfmt.initd.head | 64 +++++++++++++++++++++++++
 app-emulation/qemu/files/qemu-binfmt.initd.tail | 14 ++++++
 app-emulation/qemu/qemu-9999.ebuild             | 49 ++++++++++++++++++-
 3 files changed, 125 insertions(+), 2 deletions(-)

diff --git a/app-emulation/qemu/files/qemu-binfmt.initd.head 
b/app-emulation/qemu/files/qemu-binfmt.initd.head
new file mode 100644
index 00000000..858d5d7
--- /dev/null
+++ b/app-emulation/qemu/files/qemu-binfmt.initd.head
@@ -0,0 +1,64 @@
+#!/sbin/openrc-run
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+# Enable automatic non-native program execution by the kernel.
+
+# Defaulting to OC should be safe because it comes down to:
+#  - do we trust the interp itself to not be malicious?  yes; we built it.
+#  - do we trust the programs we're running?  ish; same permission as native
+#    binaries apply.  so if user can do bad stuff natively, cross isn't worse.
+: ${QEMU_BINFMT_FLAGS:=OC}
+
+depend() {
+       after procfs
+}
+
+start() {
+       ebegin "Registering qemu-user binaries (flags: ${QEMU_BINFMT_FLAGS})"
+
+       if [ ! -d /proc/sys/fs/binfmt_misc ] ; then
+               modprobe -q binfmt_misc
+       fi
+
+       if [ ! -d /proc/sys/fs/binfmt_misc ] ; then
+               eend 1 "You need support for 'misc binaries' in your kernel!"
+               return
+       fi
+
+       if [ ! -f /proc/sys/fs/binfmt_misc/register ] ; then
+               mount -t binfmt_misc -o nodev,noexec,nosuid \
+                       binfmt_misc /proc/sys/fs/binfmt_misc >/dev/null 2>&1
+               eend $? || return
+       fi
+
+       # Probe the native cpu type so we don't try registering them.
+       local cpu="$(uname -m)"
+       case "${cpu}" in
+       armv[4-9]*)
+               cpu="arm"
+               ;;
+       i386|i486|i586|i686|i86pc|BePC|x86_64)
+               cpu="i386"
+               ;;
+       m68k)
+               cpu="m68k"
+               ;;
+       mips*)
+               cpu="mips"
+               ;;
+       "Power Macintosh"|ppc|ppc64)
+               cpu="ppc"
+               ;;
+       s390*)
+               cpu="s390"
+               ;;
+       sh*)
+               cpu="sh"
+               ;;
+       sparc*)
+               cpu="sparc"
+               ;;
+       esac
+
+       # Register the interpreter for each cpu except for the native one.

diff --git a/app-emulation/qemu/files/qemu-binfmt.initd.tail 
b/app-emulation/qemu/files/qemu-binfmt.initd.tail
new file mode 100644
index 00000000..7679481
--- /dev/null
+++ b/app-emulation/qemu/files/qemu-binfmt.initd.tail
@@ -0,0 +1,14 @@
+       eend 0
+}
+
+stop() {
+       # We unregister everything in the "qemu-xxx" namespace.
+       ebegin "Unregistering qemu-user binaries"
+       local f
+       for f in /proc/sys/fs/binfmt_misc/qemu-* ; do
+               if [ -f "${f}" ] ; then
+                       echo '-1' > "${f}"
+               fi
+       done
+       eend 0
+}

diff --git a/app-emulation/qemu/qemu-9999.ebuild 
b/app-emulation/qemu/qemu-9999.ebuild
index 28b1867..6a78bbb 100644
--- a/app-emulation/qemu/qemu-9999.ebuild
+++ b/app-emulation/qemu/qemu-9999.ebuild
@@ -560,13 +560,58 @@ qemu_python_install() {
        python_doscript "${S}/scripts/qmp/qemu-ga-client"
 }
 
+# Generate the /etc/init.d/qemu-binfmt script which registers the user 
handlers.
+generate_initd() {
+       local out="${T}/qemu-binfmt"
+       local d="${T}/binfmt.d"
+
+       einfo "Generating qemu init.d script"
+
+       # Generate the debian fragments first.
+       mkdir -p "${d}"
+       "${S}"/scripts/qemu-binfmt-conf.sh \
+               --debian \
+               --exportdir "${d}" \
+               --qemu-path "${EPREFIX}/usr/bin" \
+               || die
+       # Then turn the fragments into a shell script we can source.
+       sed -E -i \
+               -e 's:^([^ ]+) (.*)$:\1="\2":' \
+               "${d}"/* || die
+
+       # Generate the init.d script by assembling the fragments from above.
+       local f qcpu package interpreter magic mask
+       cat "${FILESDIR}"/qemu-binfmt.initd.head >"${out}" || die
+       for f in "${d}"/qemu-* ; do
+               source "${f}"
+
+               # Normalize the cpu logic like we do in the init.d for the 
native cpu.
+               qcpu=${package#qemu-}
+               case ${qcpu} in
+               arm*)   qcpu="arm";;
+               mips*)  qcpu="mips";;
+               ppc*)   qcpu="ppc";;
+               s390*)  qcpu="s390";;
+               sh*)    qcpu="sh";;
+               sparc*) qcpu="sparc";;
+               esac
+
+               cat <<EOF >>"${out}"
+       if [ "\${cpu}" != "${qcpu}" -a -x "${interpreter}" ] ; then
+               echo 
':${package}:M::${magic}:${mask}:${interpreter}:'"\${QEMU_BINFMT_FLAGS}" 
>/proc/sys/fs/binfmt_misc/register
+       fi
+EOF
+       done
+       cat "${FILESDIR}"/qemu-binfmt.initd.tail >>"${out}" || die
+}
+
 src_install() {
        if [[ -n ${user_targets} ]]; then
                cd "${S}/user-build"
                emake DESTDIR="${ED}" install
 
-               # Install binfmt handler init script for user targets
-               newinitd "${FILESDIR}/qemu-binfmt.initd-r1" qemu-binfmt
+               # Install binfmt handler init script for user targets.
+               doinitd "${T}/qemu-binfmt"
        fi
 
        if [[ -n ${softmmu_targets} ]]; then

Reply via email to