1
0

build-state.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env node
  2. "use strict";
  3. console.log("Don't forget to run `make all` before running this script");
  4. const path = require("path");
  5. const fs = require("fs");
  6. const V86 = require("./../../../build/libv86.js").V86;
  7. const V86_ROOT = path.join(__dirname, "../../..");
  8. const OUTPUT_FILE = path.join(V86_ROOT, "images/alpine-state.bin");
  9. var emulator = new V86({
  10. bios: { url: path.join(V86_ROOT, "bios/seabios.bin") },
  11. vga_bios: { url: path.join(V86_ROOT, "bios/vgabios.bin") },
  12. autostart: true,
  13. memory_size: 512 * 1024 * 1024,
  14. vga_memory_size: 8 * 1024 * 1024,
  15. network_relay_url: "<UNUSED>",
  16. bzimage_initrd_from_filesystem: true,
  17. cmdline: "rw root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose modules=virtio_pci tsc=reliable init_on_free=on",
  18. filesystem: {
  19. baseurl: path.join(V86_ROOT, "images/alpine-rootfs-flat"),
  20. basefs: path.join(V86_ROOT, "images/alpine-fs.json"),
  21. },
  22. });
  23. console.log("Now booting, please stand by ...");
  24. let serial_text = "";
  25. let booted = false;
  26. emulator.add_listener("serial0-output-byte", function(byte)
  27. {
  28. const c = String.fromCharCode(byte);
  29. //process.stdout.write(c);
  30. serial_text += c;
  31. if(!booted && serial_text.endsWith("localhost:~# "))
  32. {
  33. booted = true;
  34. emulator.serial0_send("sync;echo 3 >/proc/sys/vm/drop_caches\n");
  35. setTimeout(async function ()
  36. {
  37. const s = await emulator.save_state();
  38. fs.writeFile(OUTPUT_FILE, new Uint8Array(s), function(e)
  39. {
  40. if(e) throw e;
  41. console.log("Saved as " + OUTPUT_FILE);
  42. emulator.stop();
  43. });
  44. }, 10 * 1000);
  45. }
  46. });