use registered qemu emulator filename if already registered.
authorDohyung Kim <dohyung2.kim@samsung.com>
Mon, 5 Jun 2017 05:49:23 +0000 (14:49 +0900)
committeryuhuan.yang <yuhuan.yang@samsung.com>
Tue, 30 Jan 2018 02:18:53 +0000 (10:18 +0800)
1. preserve-argv[0] flag is enabled
   copy both qemu-XXX and qemu-XXX-binfmt and use them.
2. flag is disabled and the copied qemu and the registered qemu are different
   create symlink as the registered qemu emulator filename and use symlink

Change-Id: I3f9050a01cc3bd0431b37a2c1e29729ef15ee765
Signed-off-by: Dohyung Kim <dohyung2.kim@samsung.com>
mic/chroot.py
mic/imager/baseimager.py
mic/utils/misc.py

index 85e8ba0..a8a41db 100644 (file)
@@ -304,11 +304,11 @@ def chroot(chrootdir, bindmounts = None, execute = "/bin/bash"):
 
     arch = ELF_arch(chrootdir)
     if arch == "arm":
-        qemu_emulator = misc.setup_qemu_emulator(chrootdir, "arm")
+        qemu_emulators = misc.setup_qemu_emulator(chrootdir, "arm")
     elif arch == "mipsel":
-        qemu_emulator = misc.setup_qemu_emulator(chrootdir, "mipsel")
+        qemu_emulators = misc.setup_qemu_emulator(chrootdir, "mipsel")
     else:
-        qemu_emulator = None
+        qemu_emulators = []
 
     savefs_before_chroot(chrootdir, None)
 
@@ -326,5 +326,5 @@ def chroot(chrootdir, bindmounts = None, execute = "/bin/bash"):
 
     finally:
         cleanup_chrootenv(chrootdir, bindmounts, globalmounts)
-        if qemu_emulator:
+        for qemu_emulator in qemu_emulators:
             os.unlink(chrootdir + qemu_emulator)
index b10f2ed..257af8e 100755 (executable)
@@ -144,8 +144,8 @@ class BaseImageCreator(object):
 
         self.image_format = None
 
-        # Save qemu emulator file name in order to clean up it finally
-        self.qemu_emulator = None
+        # Save qemu emulator file names in order to clean up it finally
+        self.qemu_emulators = []
 
         # No ks provided when called by convertor, so skip the dependency check
         if self.ks:
@@ -826,8 +826,8 @@ class BaseImageCreator(object):
             fs.makedirs(self._instroot + d)
 
         if self.target_arch and self.target_arch.startswith("arm") or \
-            self.target_arch == "aarch64" or self.target_arch == "mipsel" :
-            self.qemu_emulator = misc.setup_qemu_emulator(self._instroot,
+            self.target_arch == "aarch64":
+            self.qemu_emulators = misc.setup_qemu_emulator(self._instroot,
                                                           self.target_arch)
 
         self.get_cachedir(cachedir)
@@ -868,8 +868,8 @@ class BaseImageCreator(object):
             if not os.path.islink(mtab):
                 os.unlink(self._instroot + "/etc/mtab")
 
-            if self.qemu_emulator:
-                os.unlink(self._instroot + self.qemu_emulator)
+            for qemu_emulator in self.qemu_emulators:
+                os.unlink(self._instroot + qemu_emulator)
         except OSError:
             pass
 
index 873ebbe..5aa5b16 100755 (executable)
@@ -898,6 +898,7 @@ def is_statically_linked(binary):
     return ", statically linked, " in runner.outs(['file', binary])
 
 def setup_qemu_emulator(rootdir, arch):
+    qemu_emulators = []
     # mount binfmt_misc if it doesn't exist
     if not os.path.exists("/proc/sys/fs/binfmt_misc"):
         modprobecmd = find_binary_path("modprobe")
@@ -940,6 +941,7 @@ def setup_qemu_emulator(rootdir, arch):
     if not os.path.exists(rootdir + "/usr/bin"):
         makedirs(rootdir + "/usr/bin")
     shutil.copy(qemu_emulator, rootdir + qemu_emulator)
+    qemu_emulators.append(qemu_emulator)
 
     # disable selinux, selinux will block qemu emulator to run
     if os.path.exists("/usr/sbin/setenforce"):
@@ -957,8 +959,26 @@ def setup_qemu_emulator(rootdir, arch):
 
         with open("/proc/sys/fs/binfmt_misc/register", "w") as fd:
             fd.write(qemu_arm_string)
-
-    return qemu_emulator
+    else:
+        flags = ""
+        interpreter = ""
+        with open(node, "r") as fd:
+            for line in fd.readlines():
+                if line.startswith("flags:"):
+                    flags = line[len("flags:"):].strip()
+                elif line.startswith("interpreter"):
+                    interpreter = line[len("interpreter"):].strip()
+
+        if flags == "P" and interpreter.endswith("-binfmt"):
+            # copy binfmt wrapper when preserve-argv[0] flag is enabled
+            shutil.copy(os.path.realpath(interpreter), rootdir + interpreter)
+            qemu_emulators.append(interpreter)
+        elif not flags and interpreter != qemu_emulator:
+            # create symlink as registered qemu emulator
+            os.symlink(qemu_emulator, rootdir + interpreter)
+            qemu_emulators.append(interpreter)
+
+    return qemu_emulators
 
 def SrcpkgsDownload(pkgs, repometadata, instroot, cachedir):
     def get_source_repometadata(repometadata):