diff mbox series

runqemu: add QB_SETUP_CMD and QB_CLEANUP_CMD

Message ID 20221214085425.1980230-1-mikko.rapeli@linaro.org
State Accepted, archived
Commit d5c38964a4458aa31ec37810773ecc4f5d410dbe
Headers show
Series runqemu: add QB_SETUP_CMD and QB_CLEANUP_CMD | expand

Commit Message

Mikko Rapeli Dec. 14, 2022, 8:54 a.m. UTC
These enable running custom shell setup and cleanup commands
before and after qemu. Enables machine configurations to for
example run qemu with swtpm to emulate TPM devices.

Example config with meta-tpm2 based swtpm in machine config:

 * image recipe depens on swtpm-native to get the native tools to PATH,
   same handling as qemu itself

do_testimage[depends] += "swtpm-native:do_populate_sysroot"

 * startup commands for swtpm daemon, note that swtpm
   socket file has 107 character limit and absolute paths to build environment
   will not work

QB_SETUP_CMD = " \
   test -d '${IMAGE_BASENAME}_swtpm' || ( mkdir -p '${IMAGE_BASENAME}_swtpm' && \
       swtpm_setup --tpmstate '${IMAGE_BASENAME}_swtpm' --tpm2 --pcr-banks sha256 ); \
   swtpm socket --tpmstate dir='${IMAGE_BASENAME}_swtpm' \
         --ctrl type=unixio,path='${IMAGE_BASENAME}_swtpm/swtpm-sock' \
         --log level=40 --tpm2 -t -d \
"

 * qemu startup command in machine config is configured enable swtpm device

QB_OPT_APPEND += "-chardev socket,id=chrtpm,path='${IMAGE_BASENAME}_swtpm/swtpm-sock' -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis-device,tpmdev=tpm0"

 * in this case, swtpm daemon stops automatically with qemu machine, but
   QB_CLEANUP_CMD could be used to kill a specific process and wipe
   temporary files

Now runqemu and testimage.bbclass can be used with swtpm.

Signed-off-by: Mikko Rapeli <mikko.rapeli@linaro.org>
---
 scripts/runqemu | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
diff mbox series

Patch

diff --git a/scripts/runqemu b/scripts/runqemu
index 04728673de..ccc557f308 100755
--- a/scripts/runqemu
+++ b/scripts/runqemu
@@ -1058,6 +1058,13 @@  class BaseConfig(object):
 
         self.nfs_running = True
 
+    def setup_cmd(self):
+        cmd = self.get('QB_SETUP_CMD')
+        if cmd != '':
+            logger.info('Running setup command %s' % str(cmd))
+            if subprocess.call(cmd, shell=True) != 0:
+                raise RunQemuError('Failed to run %s' % cmd)
+
     def setup_net_bridge(self):
         self.set('NETWORK_CMD', '-netdev bridge,br=%s,id=net0,helper=%s -device virtio-net-pci,netdev=net0 ' % (
             self.net_bridge, os.path.join(self.bindir_native, 'qemu-oe-bridge-helper')))
@@ -1526,6 +1533,13 @@  class BaseConfig(object):
             else:
                 logger.error("Failed to run qemu: %s", process.stderr.read().decode())
 
+    def cleanup_cmd(self):
+        cmd = self.get('QB_CLEANUP_CMD')
+        if cmd != '':
+            logger.info('Running cleanup command %s' % str(cmd))
+            if subprocess.call(cmd, shell=True) != 0:
+                raise RunQemuError('Failed to run %s' % cmd)
+
     def cleanup(self):
         if self.cleaned:
             return
@@ -1654,6 +1668,7 @@  def main():
         config.setup_network()
         config.setup_rootfs()
         config.setup_final()
+        config.setup_cmd()
         config.start_qemu()
     except RunQemuError as err:
         logger.error(err)
@@ -1663,6 +1678,7 @@  def main():
         traceback.print_exc()
         return 1
     finally:
+        config.cleanup_cmd()
         config.cleanup()
         # Deliberately ignore the return code of 'tput smam'.
         subprocess.call(["tput", "smam"])