build-state.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env node
  2. "use strict";
  3. const path = require("path");
  4. // TODO:
  5. // - Timeout
  6. console.log("Don't forget to run `make all` before running this script");
  7. var fs = require("fs");
  8. var V86 = require("./../../../build/libv86.js").V86;
  9. const V86_ROOT = path.join(__dirname, "../../..");
  10. var OUTPUT_FILE = path.join(V86_ROOT, "images/debian-state-base.bin");
  11. process.stdin.setRawMode(true);
  12. process.stdin.resume();
  13. process.stdin.setEncoding("utf8");
  14. process.stdin.on("data", handle_key);
  15. var emulator = new V86({
  16. bios: { url: path.join(V86_ROOT, "/bios/seabios.bin") },
  17. vga_bios: { url: path.join(V86_ROOT, "/bios/vgabios.bin") },
  18. autostart: true,
  19. memory_size: 512 * 1024 * 1024,
  20. vga_memory_size: 8 * 1024 * 1024,
  21. network_relay_url: "<UNUSED>",
  22. bzimage_initrd_from_filesystem: true,
  23. cmdline: "rw init=/bin/systemd root=host9p console=ttyS0 spectre_v2=off pti=off",
  24. filesystem: {
  25. basefs: {
  26. url: path.join(V86_ROOT, "/images/debian-base-fs.json"),
  27. },
  28. baseurl: path.join(V86_ROOT, "/images/debian-9p-rootfs-flat/"),
  29. },
  30. screen_dummy: true,
  31. });
  32. console.log("Now booting, please stand by ...");
  33. var boot_start = Date.now();
  34. var serial_text = "";
  35. let booted = false;
  36. emulator.add_listener("serial0-output-char", function(c)
  37. {
  38. process.stdout.write(c);
  39. serial_text += c;
  40. if(!booted && serial_text.endsWith("root@localhost:~# "))
  41. {
  42. console.error("\nBooted in %d", (Date.now() - boot_start) / 1000);
  43. booted = true;
  44. // sync and drop caches: Makes it safer to change the filesystem as fewer files are rendered
  45. emulator.serial0_send("sync;echo 3 >/proc/sys/vm/drop_caches\n");
  46. setTimeout(function ()
  47. {
  48. emulator.save_state(function(err, s)
  49. {
  50. if(err)
  51. {
  52. throw err;
  53. }
  54. fs.writeFile(OUTPUT_FILE, new Uint8Array(s), function(e)
  55. {
  56. if(e) throw e;
  57. console.error("Saved as " + OUTPUT_FILE);
  58. stop();
  59. });
  60. });
  61. }, 10 * 1000);
  62. }
  63. });
  64. function handle_key(c)
  65. {
  66. if(c === "\u0003")
  67. {
  68. // ctrl c
  69. stop();
  70. }
  71. else
  72. {
  73. emulator.serial0_send(c);
  74. }
  75. }
  76. function stop()
  77. {
  78. emulator.stop();
  79. process.stdin.pause();
  80. }