run-qemu.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env node
  2. "use strict";
  3. const QEMU = "qemu-system-x86_64";
  4. const assert = require("assert").strict;
  5. const fs = require("fs");
  6. const { spawn, spawnSync } = require("child_process");
  7. const path = require("path");
  8. const share_dir_9p = fs.mkdtempSync("/tmp/v86-test-qemu-9p");
  9. fs.copyFileSync(path.join(__dirname, "/test-i386"), path.join(share_dir_9p, "/test-i386"));
  10. const qemu_version = spawnSync(QEMU, ["--version"]);
  11. assert(qemu_version.status === 0, "QEMU not found, return code: " + qemu_version.status);
  12. console.error("Using QEMU:");
  13. console.error(qemu_version.stdout.toString("utf8"));
  14. const qemu = spawn(QEMU,
  15. [
  16. "-serial", "stdio",
  17. "-cdrom", path.join(__dirname, "/../../images/linux4.iso"),
  18. "-device", "virtio-9p-pci,fsdev=fs9p,mount_tag=host9p",
  19. "-fsdev", `local,id=fs9p,path=${share_dir_9p},security_model=none`,
  20. "-display", "none",
  21. "-cpu", "Westmere", // default doesn't support popcnt
  22. //"-monitor", "telnet:127.0.0.1:1235,server,nowait",
  23. ]
  24. );
  25. let qemu_output = "";
  26. let ran_command = false;
  27. let finished = false;
  28. qemu.stdout.on("data", data => {
  29. process.stderr.write(data);
  30. qemu_output += data.toString().replace(/\r/, "");
  31. if(!ran_command && qemu_output.endsWith("~% "))
  32. {
  33. ran_command = true;
  34. qemu.stdin.write("chmod +x /mnt/test-i386\n");
  35. qemu.stdin.write("/mnt/test-i386 > /mnt/result\n");
  36. qemu.stdin.write("echo test fini''shed\n");
  37. }
  38. if(ran_command && !finished && qemu_output.includes("test finished"))
  39. {
  40. const result_file = path.join(share_dir_9p, "result");
  41. finished = true;
  42. console.error("Finished");
  43. process.stdout.write(fs.readFileSync(result_file));
  44. fs.unlinkSync(result_file);
  45. fs.unlinkSync(path.join(share_dir_9p, "test-i386"));
  46. fs.rmdirSync(share_dir_9p);
  47. qemu.kill();
  48. }
  49. });
  50. qemu.stderr.on("data", data => {
  51. process.stderr.write(data);
  52. });