runqemu: add QB_SETUP_CMD and QB_CLEANUP_CMD

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.

(From OE-Core rev: d5c38964a4458aa31ec37810773ecc4f5d410dbe)

Signed-off-by: Mikko Rapeli <mikko.rapeli@linaro.org>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Mikko Rapeli 2022-12-14 10:54:25 +02:00 committed by Richard Purdie
parent 80b0e933a0
commit 0d1023673b

View File

@ -1058,6 +1058,13 @@ class BaseConfig(object):
self.nfs_running = True 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): def setup_net_bridge(self):
self.set('NETWORK_CMD', '-netdev bridge,br=%s,id=net0,helper=%s -device virtio-net-pci,netdev=net0 ' % ( 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'))) self.net_bridge, os.path.join(self.bindir_native, 'qemu-oe-bridge-helper')))
@ -1526,6 +1533,13 @@ class BaseConfig(object):
else: else:
logger.error("Failed to run qemu: %s", process.stderr.read().decode()) 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): def cleanup(self):
if self.cleaned: if self.cleaned:
return return
@ -1654,6 +1668,7 @@ def main():
config.setup_network() config.setup_network()
config.setup_rootfs() config.setup_rootfs()
config.setup_final() config.setup_final()
config.setup_cmd()
config.start_qemu() config.start_qemu()
except RunQemuError as err: except RunQemuError as err:
logger.error(err) logger.error(err)
@ -1663,6 +1678,7 @@ def main():
traceback.print_exc() traceback.print_exc()
return 1 return 1
finally: finally:
config.cleanup_cmd()
config.cleanup() config.cleanup()
# Deliberately ignore the return code of 'tput smam'. # Deliberately ignore the return code of 'tput smam'.
subprocess.call(["tput", "smam"]) subprocess.call(["tput", "smam"])