Skip to contentSkip to conversation

runqemu: restore support to run inside SDK

PATCH New [email protected] 1 participant 2 messages Feb 27

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 runqemu: restore support to run inside SDK

From: Chen Qi <[email protected]>

Using runqemu from SDK has been supported for a long time[1].

Below are example steps for using runqemu inside SDK.
1. mkdir destdir
2. cp -r /path/to/build/tmp/deploy/image/qemux86-64 destdir
3. Install SDK to destdir
4. Source SDK
5. runqemu qemux86-64 nographic slirp

Recently the related code path was deleted by accident during an effort to make codes cleaner and more consistent.

We need to restore support for it.

What actually matters is the STAGING_BINDIR_NATIVE, which we use to locate the qemu binary. So in case of SDK, we set it from OECORE_NATIVE_SYSROOT. The STAGING_DIR_NATIVE checking and setting are meaningless, thus deleting it.

[1] https://git.openembedded.org/openembedded-core/commit/?id=93649edc034f2540ff55dc9b41638797209cfb9c

---
 scripts/runqemu | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
scripts/runqemu +8 -4
index 32a3d6296a..5587d47865 100755
@@ -997,17 +997,21 @@ to your build configuration.
                 logger.info('Setting DEPLOY_DIR_IMAGE to folder containing %s (%s)' % (self.qemuboot, imgdir))
                 self.set('DEPLOY_DIR_IMAGE', imgdir)
 
-        # If the STAGING_*_NATIVE directories from the config file don't exist
+        oecore_native_sysroot = self.get('OECORE_NATIVE_SYSROOT')
+        if oecore_native_sysroot:
+            logger.info('In SDK environment, setting STAGING_BINDIR_NATIVE from OECORE_NATIVE_SYSROOT (%s)' % oecore_native_sysroot)
+            self.set('STAGING_BINDIR_NATIVE', '%s/usr/bin' % oecore_native_sysroot)
+
+        # If the STAGING_BINDIR_NATIVE directories from the config file don't exist
         # and we're in a sourced OE build directory try to extract the paths
         # from `bitbake -e`
-        havenative = os.path.exists(self.get('STAGING_DIR_NATIVE')) and \
-            os.path.exists(self.get('STAGING_BINDIR_NATIVE'))
+        havenative = os.path.exists(os.path.exists(self.get('STAGING_BINDIR_NATIVE')))
 
         if not havenative:
             if not self.bitbake_e:
                 self.load_bitbake_env()
 
-            native_vars = ['STAGING_DIR_NATIVE']
+            native_vars = ['STAGING_BINDIR_NATIVE']
             for nv in native_vars:
                 s = re.search('^%s="(.*)"' % nv, self.bitbake_e, re.M)
                 if s and s.group(1) != self.get(nv):
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 2/2 oeqa/selftest/sdk: add test_sdk_runqemu

From: Chen Qi <[email protected]>

Add test case to ensure runqemu works in SDK.

Using runqemu from SDK has been supported for many years. Add a test case to ensure we have no regression.

---
 meta/lib/oeqa/selftest/cases/sdk.py | 88 ++++++++++++++++++++++++++++-
 1 file changed, 87 insertions(+), 1 deletion(-)
meta/lib/oeqa/selftest/cases/sdk.py +87 -1
index 3971365029..ee1e4ca420 100644
@@ -4,7 +4,13 @@
 # SPDX-License-Identifier: MIT
 #
 
-import os.path
+import os
+import shutil
+import subprocess
+import glob
+import time
+import select
+from tempfile import TemporaryDirectory
 
 from oeqa.selftest.case import OESelftestTestCase
 from oeqa.utils.commands import bitbake, get_bb_vars
@@ -37,3 +43,83 @@ IMAGE_INSTALL:append = " selftest-hello"
         path = os.path.join(vars["SDK_DEPLOY"], vars["TOOLCHAIN_OUTPUTNAME"] + ".target.manifest")
         self.assertNotEqual(os.path.getsize(path), 0, msg="Target manifest is empty")
         self.assertIn("selftest-hello", self.load_manifest(path))
+
+    def test_sdk_runqemu(self):
+        """Test using runqemu from SDK which has no bitbake available"""
+
+        def path_with_no_bitbake():
+            orig_paths = os.environ["PATH"].split(":")
+            new_paths = []
+            for orig_path in orig_paths:
+                if not os.access(os.path.join(orig_path, "bitbake"), os.X_OK):
+                    new_paths.append(orig_path)
+            return ":".join(new_paths)
+
+        def runqemu_works_from_sdk(sdk_dir, sdk_env, machine, path, timeout=360):
+            env = os.environ.copy()
+            env["PATH"] = path
+
+            cmd = f'cd {sdk_dir}; . {sdk_env}; runqemu {machine} snapshot slirp nographic'
+            self.logger.info(f"Running {cmd}")
+            proc = subprocess.Popen(
+                ['bash', '-c', cmd],
+                stdout=subprocess.PIPE,
+                stderr=subprocess.STDOUT,
+                text=True,
+                bufsize=1,
+                env=env
+            )
+            buffer = ""
+            start = time.time()
+            while time.time() - start < timeout:
+                if proc.poll() is not None:
+                    chunk = proc.stdout.read()
+                    if chunk:
+                        buffer += chunk
+                    return ('Linux version' in buffer, "\n".join(buffer.split('\n')[-10:]))
+                ready, _, _ = select.select([proc.stdout], [], [], 0.1)
+                if ready:
+                    chunk = proc.stdout.read(4096)
+                    if chunk:
+                        buffer += chunk
+                        if "Linux version" in buffer:
+                            proc.terminate()
+                            return (True, "\n".join(buffer.split('\n')[-10:]))
+                time.sleep(0.1)
+            proc.terminate()
+            time.sleep(5)
+            if proc.poll() is None:
+                proc.kill()
+            return (False, "\n".join(buffer.split('\n')[-10:]))
+
+        image = "core-image-minimal"
+
+        self.write_config('QEMU_USE_KVM = "1"')
+
+        # generate image and SDK
+        bitbake(f"{image}")
+        bitbake(f"{image} -c populate_sdk")
+
+        # create a temporary destdir to test runqemu from SDK
+        with TemporaryDirectory() as tmpdir:
+            self.logger.debug(f"Created tempdir: {tmpdir}")
+            vars = get_bb_vars(["DEPLOY_DIR_IMAGE", "SDK_DEPLOY", "TOOLCHAIN_OUTPUTNAME", "MACHINE"], image)
+            machine = vars["MACHINE"]
+            image_dir = vars["DEPLOY_DIR_IMAGE"]
+            sdk_path = os.path.join(vars["SDK_DEPLOY"], vars["TOOLCHAIN_OUTPUTNAME"] + ".sh")
+            self.logger.debug(f"machine: {machine}")
+            self.logger.debug(f"image_dir: {image_dir}")
+            self.logger.debug(f"sdk_path: {sdk_path}")
+            dst_image_dir = os.path.join(tmpdir, os.path.basename(image_dir))
+            shutil.copytree(image_dir, dst_image_dir)
+            subprocess.check_call(f"{sdk_path} -d {tmpdir} -y", shell=True)
+            sdk_env = glob.glob(tmpdir + '/environment-setup-*')[0]
+            self.logger.debug(f"sdk_env: {sdk_env}")
+
+            path = path_with_no_bitbake()
+            self.logger.debug(f"path: {path}")
+
+            runqemu_works, output = runqemu_works_from_sdk(tmpdir, sdk_env, machine, path)
+            self.logger.debug(f"runqemu works in SDK: {runqemu_works}\n{output}")
+
+            self.assertTrue(runqemu_works, f"runqemu does not work in SDK\n{output}")
Signature · 1 line
2.43.0