test_kvmtool.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import os
  2. import infra.basetest
  3. class TestKvmTool(infra.basetest.BRTest):
  4. # This test passes the Buildroot image directory to the emulated
  5. # guest using the 9p virtiofs. This guest enables the
  6. # virtualization support. It will start a nested KVM virtual
  7. # machine, using the same Kernel/initramfs images. For these
  8. # reasons, we need a specific kernel configuration that enables
  9. # KVM and 9P.
  10. kern_frag = \
  11. infra.filepath("tests/package/test_kvmtool/linux-kvm.fragment")
  12. rootfs_overlay = \
  13. infra.filepath("tests/package/test_kvmtool/rootfs-overlay")
  14. config = \
  15. f"""
  16. BR2_aarch64=y
  17. BR2_TOOLCHAIN_EXTERNAL=y
  18. BR2_LINUX_KERNEL=y
  19. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  20. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.18.1"
  21. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  22. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  23. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{kern_frag}"
  24. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  25. BR2_PACKAGE_KVMTOOL=y
  26. BR2_ROOTFS_OVERLAY="{rootfs_overlay}"
  27. BR2_TARGET_ROOTFS_CPIO=y
  28. # BR2_TARGET_ROOTFS_TAR is not set
  29. """
  30. def test_run(self):
  31. img = os.path.join(self.builddir, "images", "rootfs.cpio")
  32. kern = os.path.join(self.builddir, "images", "Image")
  33. img_dir = os.path.join(self.builddir, "images")
  34. virtfs_tag = "br-imgs"
  35. virtfs_opts = [
  36. f"local,path={img_dir}",
  37. f"mount_tag={virtfs_tag}",
  38. "security_model=mapped-xattr",
  39. "readonly"
  40. ]
  41. qemu_opts = ["-M", "virt,gic-version=3,virtualization=on",
  42. "-cpu", "cortex-a57",
  43. "-smp", "4",
  44. "-m", "1G",
  45. "-virtfs", ','.join(virtfs_opts),
  46. "-initrd", img]
  47. self.emulator.boot(arch="aarch64",
  48. kernel=kern,
  49. options=qemu_opts)
  50. self.emulator.login()
  51. # Test the program can execute.
  52. self.assertRunOk("lkvm --version")
  53. # We mount the virtfs to get the Kernel and initramfs from the
  54. # test host.
  55. self.assertRunOk(f"mount -t 9p {virtfs_tag} /mnt/")
  56. # We define an arbitrary kernel command line argument that we
  57. # will use to detect we are in our virtual machine.
  58. kvm_karg = "br-kvm"
  59. # The number of CPU of our virtual machine
  60. kvm_nproc = 2
  61. # We run the KVM virtual machine
  62. cmd = "lkvm run"
  63. cmd += " --kernel /mnt/Image"
  64. cmd += " --initrd /mnt/rootfs.cpio"
  65. cmd += f" --cpus {kvm_nproc}"
  66. cmd += " --mem 320M"
  67. cmd += " --console virtio"
  68. cmd += " --irqchip gicv3"
  69. cmd += f" --params {kvm_karg}"
  70. # We use qemu.sendline() to run the command because the
  71. # program will not return an exit code. The command "lkvm run"
  72. # will spawn the new virtual machine console. The login is
  73. # expected to be reached after the command is issued.
  74. self.emulator.qemu.sendline(cmd)
  75. # We login in the virtual machine.
  76. self.emulator.login()
  77. # We check we can see our kernel argument.
  78. out, ret = self.emulator.run("cat /proc/cmdline")
  79. self.assertEqual(ret, 0)
  80. self.assertIn(kvm_karg, out[0])
  81. # We check the virtual machine has the expected number of CPU.
  82. out, ret = self.emulator.run("nproc")
  83. self.assertEqual(ret, 0)
  84. self.assertEqual(int(out[0]), kvm_nproc)