setup qemu env for arm build
authorZhang Qiang <qiang.z.zhang@intel.com>
Mon, 9 Apr 2012 15:50:16 +0000 (23:50 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Mon, 9 Apr 2012 15:50:16 +0000 (23:50 +0800)
gitbuildsys/cmd_localbuild.py
gitbuildsys/errors.py
gitbuildsys/utils.py

index 7536289..2608082 100644 (file)
@@ -31,6 +31,7 @@ import urlparse
 import msger
 import runner
 import utils
+import errors
 from conf import configmgr
 import git
 import buildservice
@@ -206,6 +207,12 @@ def do(opts, args):
 
     msger.info(' '.join(cmd))
 
+    if buildarch.startswith('arm'):
+        try:
+            utils.setup_qemu_emulator()
+        except errors.QemuError, e:
+            msger.error('%s' % e)
+
     name = utils.parse_spec(specfile, 'name')
     version = utils.parse_spec(specfile, 'version')
     if not name or not version:
index 5d56014..4995984 100644 (file)
@@ -52,6 +52,9 @@ class UnpackError(CmdError):
 class FormatError(CmdError):
     keyword = '<format>'
 
+class QemuError(CmdError):
+    keyword = '<qemu>'
+
 class Abort(CmdError):
     keyword = ''
 
index 94c6f40..eb1f439 100644 (file)
@@ -276,3 +276,47 @@ class UpstreamTarball(object):
             if m:
                 return (m.group('package'), m.group('version'))
 
+def is_statically_linked(binary):
+    return ", statically linked, " in runner.outs(['file', binary])
+
+def setup_qemu_emulator():
+    # mount binfmt_misc if it doesn't exist
+    if not os.path.exists("/proc/sys/fs/binfmt_misc"):
+        modprobecmd = find_binary_path("modprobe")
+        runner.show([modprobecmd, "binfmt_misc"])
+    if not os.path.exists("/proc/sys/fs/binfmt_misc/register"):
+        mountcmd = find_binary_path("mount")
+        runner.show([mountcmd, "-t", "binfmt_misc", "none", "/proc/sys/fs/binfmt_misc"])
+
+    # qemu_emulator is a special case, we can't use find_binary_path
+    # qemu emulator should be a statically-linked executable file
+    qemu_emulator = "/usr/bin/qemu-arm"
+    if not os.path.exists(qemu_emulator) or not is_statically_linked(qemu_emulator):
+        qemu_emulator = "/usr/bin/qemu-arm-static"
+    if not os.path.exists(qemu_emulator):
+        raise errors.QemuError("Please install a statically-linked qemu-arm")
+
+    # disable selinux, selinux will block qemu emulator to run
+    if os.path.exists("/usr/sbin/setenforce"):
+        msger.info('Try to disable selinux')
+        runner.show(["/usr/sbin/setenforce", "0"])
+
+    node = "/proc/sys/fs/binfmt_misc/arm"
+    if is_statically_linked(qemu_emulator) and os.path.exists(node):
+        return qemu_emulator
+
+    # unregister it if it has been registered and is a dynamically-linked executable
+    if not is_statically_linked(qemu_emulator) and os.path.exists(node):
+        qemu_unregister_string = "-1\n"
+        fd = open("/proc/sys/fs/binfmt_misc/arm", "w")
+        fd.write(qemu_unregister_string)
+        fd.close()
+
+    # register qemu emulator for interpreting other arch executable file
+    if not os.path.exists(node):
+        qemu_arm_string = ":arm:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x28\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfa\\xff\\xff\\xff:%s:\n" % qemu_emulator
+        fd = open("/proc/sys/fs/binfmt_misc/register", "w")
+        fd.write(qemu_arm_string)
+        fd.close()
+
+    return qemu_emulator