From 037afdc3d92118efb94cc87a8d92c298fa01768e Mon Sep 17 00:00:00 2001 From: Fabian Date: Sun, 26 May 2024 17:47:16 +0900 Subject: [PATCH] alpine: add script to automatically build state image using nodejs --- examples/alpine.html | 1 + tools/docker/alpine/Readme.md | 1 + tools/docker/alpine/build-state.js | 59 ++++++++++++++++++++++++++++++ tools/docker/alpine/build.sh | 2 - 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100755 tools/docker/alpine/build-state.js diff --git a/examples/alpine.html b/examples/alpine.html index a59047f7..3973947e 100644 --- a/examples/alpine.html +++ b/examples/alpine.html @@ -19,6 +19,7 @@ window.onload = function() autostart: true, bzimage_initrd_from_filesystem: true, cmdline: "rw root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose modules=virtio_pci tsc=reliable", + //initial_state: { url: "../images/alpine-state.bin" }, }); }; diff --git a/tools/docker/alpine/Readme.md b/tools/docker/alpine/Readme.md index 0cd4e4cf..1256f048 100644 --- a/tools/docker/alpine/Readme.md +++ b/tools/docker/alpine/Readme.md @@ -3,3 +3,4 @@ You can build a Alpine Linux 9p image using Docker: 1. As needed, kernel flavor (`virt` is smaller than `lts`, but don't have networking) and set of additional packages (community repo is enabled by default) can be edited in `Dockerfile` 2. Check and run `./build.sh` with started dockerd (podman works) 3. Run local webserver (e.g. `make run`) and open `examples/alpine.html` +4. (optional) Run `./build-state.js` and add `initial_state: { url: "../images/alpine-state.bin" }` to `alpine.html` diff --git a/tools/docker/alpine/build-state.js b/tools/docker/alpine/build-state.js new file mode 100755 index 00000000..17d35ef9 --- /dev/null +++ b/tools/docker/alpine/build-state.js @@ -0,0 +1,59 @@ +#!/usr/bin/env node +"use strict"; + +console.log("Don't forget to run `make all` before running this script"); + +const path = require("path"); +const fs = require("fs"); +const V86 = require("./../../../build/libv86.js").V86; + +const V86_ROOT = path.join(__dirname, "../../.."); +const OUTPUT_FILE = path.join(V86_ROOT, "images/alpine-state.bin"); + +var emulator = new V86({ + bios: { url: path.join(V86_ROOT, "bios/seabios.bin") }, + vga_bios: { url: path.join(V86_ROOT, "bios/vgabios.bin") }, + autostart: true, + memory_size: 512 * 1024 * 1024, + vga_memory_size: 8 * 1024 * 1024, + network_relay_url: "", + bzimage_initrd_from_filesystem: true, + cmdline: "rw root=host9p rootfstype=9p rootflags=trans=virtio,cache=loose modules=virtio_pci tsc=reliable init_on_free=on", + filesystem: { + baseurl: path.join(V86_ROOT, "images/alpine-rootfs-flat"), + basefs: path.join(V86_ROOT, "images/alpine-fs.json"), + }, + screen_dummy: true, +}); + +console.log("Now booting, please stand by ..."); + +let serial_text = ""; +let booted = false; + +emulator.add_listener("serial0-output-byte", function(byte) +{ + const c = String.fromCharCode(byte); + //process.stdout.write(c); + + serial_text += c; + + if(!booted && serial_text.endsWith("localhost:~# ")) + { + booted = true; + + emulator.serial0_send("sync;echo 3 >/proc/sys/vm/drop_caches\n"); + + setTimeout(async function () + { + const s = await emulator.save_state(); + + fs.writeFile(OUTPUT_FILE, new Uint8Array(s), function(e) + { + if(e) throw e; + console.log("Saved as " + OUTPUT_FILE); + emulator.stop(); + }); + }, 10 * 1000); + } +}); diff --git a/tools/docker/alpine/build.sh b/tools/docker/alpine/build.sh index 1bb20696..e6c4c7aa 100755 --- a/tools/docker/alpine/build.sh +++ b/tools/docker/alpine/build.sh @@ -27,5 +27,3 @@ mkdir -p "$OUT_ROOTFS_FLAT" ../../../tools/copy-to-sha256.py "$OUT_ROOTFS_TAR" "$OUT_ROOTFS_FLAT" echo "$OUT_ROOTFS_TAR", "$OUT_ROOTFS_FLAT" and "$OUT_FSJSON" created. - -# TODO: auto-build state, add examples/alpine.html