instalar docker, hacer booteable

This commit is contained in:
Cat /dev/Nulo 2023-03-28 14:00:55 -03:00
parent 9c87891f2b
commit 5502061f21

View file

@ -4,17 +4,23 @@ import {
copyFile, copyFile,
mkdir, mkdir,
opendir, opendir,
readdir,
rm, rm,
symlink, symlink,
writeFile, writeFile,
} from "node:fs/promises"; } from "node:fs/promises";
import { tmpdir } from "node:os";
import { basename, join } from "node:path"; import { basename, join } from "node:path";
import { promisify } from "node:util"; import { promisify } from "node:util";
const execFile = promisify(execFileCallback); const execFile = promisify(execFileCallback);
// TODO: configurar runsc
// TODO: cache de apk (robar de define-alpine-the-sequel)
const currRootPath = await setupRootfsDir(); const currRootPath = await setupRootfsDir();
await setupBasicApkEnvironment(currRootPath); await setupBasicApkEnvironment(currRootPath);
const packages = ["alpine-base", "fish", "docker", "linux-virt"];
await execFile("doas", [ await execFile("doas", [
"apk", "apk",
"--initdb", "--initdb",
@ -22,9 +28,25 @@ await execFile("doas", [
"--root", "--root",
currRootPath, currRootPath,
"add", "add",
...["alpine-baselayout", "busybox", "libc-utils", "alpine-keys"], ...packages,
"fish",
]); ]);
await chroot(currRootPath, ["rc-update", "add", "docker", "default"]);
await chroot(currRootPath, ["rc-update", "add", "localmount", "boot"]);
await writeFstab(currRootPath);
// make VM disk image
const ext4Path = "raw.ext4";
await execFile("qemu-img", ["create", "-f", "raw", ext4Path, "50G"]);
await execFile("mkfs.ext4", [ext4Path]);
const mntdir = join(tmpdir(), "woodpecker-in-a-vm-" + nanoid());
await mkdir(mntdir);
await execFile("doas", ["mount", ext4Path, mntdir]);
for (const path of await readdir(currRootPath))
await execFile("doas", ["cp", "-r", join(currRootPath, path), mntdir]);
await execFile("doas", ["umount", mntdir]);
await execFile("qemu-img", ["convert", "-O", "qcow2", ext4Path, "vm.qcow2"]);
async function setupRootfsDir() { async function setupRootfsDir() {
const artifactsPath = "./artifacts"; const artifactsPath = "./artifacts";
@ -58,3 +80,17 @@ async function setupBasicApkEnvironment(rootfs: string) {
].join("\n") ].join("\n")
); );
} }
async function writeFstab(rootfs: string) {
const virtualDiskName = "/dev/vda";
await writeFile(
"fstab",
`${virtualDiskName} / ext4 rw,relatime,discard 0 1
`
);
await execFile("doas", ["mv", "fstab", join(rootfs, "/etc/fstab")]);
}
async function chroot(rootfs: string, cmd: string[]) {
await execFile("doas", ["chroot", rootfs, ...cmd]);
}