run-qemu.js 1.9 KB

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