Alpine: First version

This commit is contained in:
Alessandro Pignotti 2024-09-22 18:24:17 +02:00
parent 878effcfb1
commit 91b5d5462f

View file

@ -109,7 +109,7 @@ __ __ _ __ ____ __
<main style="display: flex; flex-direction: row; justify-content: space-between; margin: 5px; height: 100%;">
<div style="flex-grow:1; height:100%;display:inline-block;margin:0;" class="scrollbar" id="console">
</div>
<canvas id="canvas"></canvas>
</main>
<script>
@ -119,37 +119,6 @@ __ __ _ __ ____ __
const underline= "\x1b[94;4m";
const normal= "\x1b[0m";
var printOnTerm = {
getAsciiText: function ()
{
var text = [
"+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+",
"| |",
"| WebVM is a server-less virtual Linux environment running fully client-side |",
"| in HTML5/WebAssembly. |",
"| |",
"| WebVM is powered by the CheerpX virtualization engine, which enables safe, |",
"| sandboxed client-side execution of x86 binaries on any browser. |",
"| |",
"| CheerpX includes an x86-to-WebAssembly JIT compiler, a virtual block-based |",
"| file system, and a Linux syscall emulator. |",
"| |",
"| [NEW!] Your own WebVM with custom images via Dockerfile: |",
"| |",
"| " + underline + "https://leaningtech.com/mini-webvm-your-linux-box-from-dockerfile-via-wasm" + normal +" |",
"| |",
"+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+",
"",
" Welcome to WebVM. If unsure, try these examples:",
"",
" python3 examples/python3/fibonacci.py ",
" gcc -o helloworld examples/c/helloworld.c && ./helloworld",
" objdump -d ./helloworld | less -M",
" vim examples/c/helloworld.c",
" curl --max-time 15 parrot.live # requires networking",
"",
];
return text;
},
getSharedArrayBufferMissingMessage: function ()
{
const isCustom = window.location.hostname !== "webvm.io";
@ -226,8 +195,11 @@ __ __ _ __ ____ __
window.addEventListener("resize", function(ev){fitAddon.fit();}, false);
term.focus();
var cxReadFunc = null;
function writeData(buf)
function writeData(buf, vt)
{
// Unsure why the output is not sent to vt 0
if(vt != 1)
return;
term.write(new Uint8Array(buf));
}
function readData(str)
@ -256,20 +228,9 @@ __ __ _ __ ____ __
h.textContent = "\u{1F7E0}";
}
//Actual CheerpX and bash specific logic
function runBash()
//Actual CheerpX and init specific logic
function runInit()
{
// Reasonable defaults for local deployments
var cmd = "/bin/bash";
var args = ["--login"];
var env = ["HOME=/home/user", "TERM=xterm", "USER=user", "SHELL=/bin/bash", "EDITOR=vim", "LANG=en_US.UTF-8", "LC_ALL=C"];
var cwd = "/home/user";
const structure = {
cmd: cmd,
args: args,
env: env,
cwd: cwd
}
if (typeof SharedArrayBuffer === "undefined")
{
printOnTerm.printError(printOnTerm.getSharedArrayBufferMissingMessage());
@ -280,29 +241,12 @@ __ __ _ __ ____ __
async function runTest(cx)
{
var processCount = 0;
function handleProcessCreated()
{
processCount++;
if(processCount == 5)
{
// Make sure no further event is reported
cx.unregisterCallback("processCreated", handleProcessCreated);
}
}
cx.registerCallback("processCreated", handleProcessCreated);
cx.registerCallback("cpuActivity", cpuCallback);
cx.registerCallback("diskActivity", hddCallback);
registerNetworkLogin(cx, networkInterface);
term.scrollToBottom();
async function cxLogAndRun(cheerpx, cmd, args, env)
{
await cheerpx.run(cmd, args, env);
printOnTerm.printMessage(" ");
}
cxReadFunc = cx.setCustomConsole(writeData, term.cols, term.rows);
function preventDefaults (e) {
@ -313,36 +257,33 @@ __ __ _ __ ____ __
consoleDiv.addEventListener("dragenter", preventDefaults, false);
consoleDiv.addEventListener("dragleave", preventDefaults, false);
consoleDiv.addEventListener("drop", preventDefaults, false);
const canvas = document.getElementById("canvas");
cx.setKmsCanvas(canvas, 1024, 768);
var opts = {env:structure.env, cwd:structure.cwd, uid: 1000, gid: 1000};
while (true)
{
await cxLogAndRun(cx, structure.cmd, structure.args, opts);
}
var opts = {uid: 0, git: 0};
cx.run("/sbin/init", [], opts);
}
function failCallback(err)
{
printOnTerm.printError(printOnTerm.getErrorMessage(err));
}
// The device url and type are replaced by Github Actions.
CheerpXApp.create({devices:[{type:"block",url:"https://disks.webvm.io/debian_large_20230522_5044875331.ext2",name:"block2"}],mounts:[{type:"ext2",dev:"block2",path:"/"},{type:"cheerpOS",dev:"/app",path:"/app"},{type:"cheerpOS",dev:"/str",path:"/data"},{type:"cheerpOS",dev:"/files",path:"/mnt"},{type:"devs",dev:"",path:"/dev"},{type:"proc",dev:"",path:"/proc"}], networkInterface: networkInterface}).then(runTest, failCallback);
//CheerpXApp.create({devices:[{type:"bytes",url:"/ghc_20240716_9952612194.ext2",name:"block1"}],mounts:[{type:"ext2",dev:"block1",path:"/"},{type:"cheerpOS",dev:"/app",path:"/app"},{type:"cheerpOS",dev:"/str",path:"/data"},{type:"cheerpOS",dev:"/files",path:"/files"},{type:"devs",dev:"",path:"/dev"},{type:"proc",dev:"",path:"/proc"}], networkInterface: networkInterface}).then(runTest, failCallback);
CheerpXApp.create({devices:[{type:"block",url:"https://disks.webvm.io/alpine_20240307_i3_slimmed.ext2",name:"block_alpine"}],mounts:[{type:"ext2",dev:"block_alpine",path:"/"},{type:"cheerpOS",dev:"/app",path:"/app"},{type:"proc",dev:"",path:"/proc"},{type:"devs",dev:"",path:"/dev"}], networkInterface}).then(runTest, failCallback);
}
function initialMessage()
{
printOnTerm.printMessage(printOnTerm.getAsciiText());
console.log("Welcome. We appreciate curiosity, but be warned that keeping the DevTools open causes significant performance degradation and crashes.");
}
initialMessage();
async function loadCX()
{
// Find the latest build
var url = "https://127.0.0.1:8080/cx.js";
var r = await fetch("https://cheerpxdemos.leaningtech.com/publicdeploy/LATEST.txt");
var url = await r.text();
url = url.trim();
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.addEventListener("load", runBash, false);
script.addEventListener("load", runInit, false);
document.head.appendChild(script);
}
loadCX();