59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
|
import { mkdtemp, rm } from "node:fs/promises";
|
||
|
import { tmpdir } from "node:os";
|
||
|
import path from "node:path";
|
||
|
import { execFile } from "./helpers.js";
|
||
|
import { Kernel } from "./kernel.js";
|
||
|
|
||
|
export async function runQemu(
|
||
|
squashfs: string,
|
||
|
kernel: Kernel,
|
||
|
{ graphic }: { graphic: boolean } = { graphic: true }
|
||
|
) {
|
||
|
const tmp = await mkdtemp(path.join(tmpdir(), "define-alpine-qemu-"));
|
||
|
try {
|
||
|
const disk = path.join(tmp, "tmp.ext4");
|
||
|
await execFile("fallocate", ["--length", "1G", disk]);
|
||
|
await execFile("mkfs.ext4", ["-F", disk]);
|
||
|
|
||
|
let kernelAppend = [
|
||
|
"root=/dev/sda",
|
||
|
"rootfstype=squashfs",
|
||
|
"modules=ext4",
|
||
|
"quiet",
|
||
|
"init=/sbin/runit-init",
|
||
|
];
|
||
|
let qemuAppend: string[] = [];
|
||
|
|
||
|
if (!graphic) {
|
||
|
kernelAppend.push("console=ttyS0");
|
||
|
qemuAppend.push("-nographic");
|
||
|
}
|
||
|
|
||
|
// sudo chown root:$(id -u) -R boot/ && sudo chmod g+rw -R boot/
|
||
|
await execFile("qemu-system-x86_64", [
|
||
|
"-enable-kvm",
|
||
|
"-m",
|
||
|
"2048",
|
||
|
"-drive",
|
||
|
`file=${squashfs},media=disk,format=raw`,
|
||
|
"-drive",
|
||
|
`file=${disk},media=disk,format=raw`,
|
||
|
"-net",
|
||
|
"nic,model=virtio-net-pci",
|
||
|
"-net",
|
||
|
"user,hostfwd=tcp:127.0.0.1:8080-:80",
|
||
|
"-kernel",
|
||
|
kernel.vmlinuz,
|
||
|
"-initrd",
|
||
|
kernel.initramfs,
|
||
|
"-append",
|
||
|
kernelAppend.join(" "),
|
||
|
...qemuAppend,
|
||
|
"-no-shutdown",
|
||
|
]);
|
||
|
// -append "root=/dev/sda rootfstype=squashfs modules=ext4 quiet init=/sbin/runit-init $append" $qemuappend
|
||
|
} finally {
|
||
|
await rm(tmp, { recursive: true, force: true });
|
||
|
}
|
||
|
}
|