From 61c32e19fc3c24f78c27705d55ce136175ffa265 Mon Sep 17 00:00:00 2001 From: Enderson Maia Date: Mon, 29 Apr 2024 14:44:30 +0100 Subject: [PATCH] feat(cli): add --drive-type to build command --- apps/cli/src/commands/build.ts | 90 +++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 12 deletions(-) diff --git a/apps/cli/src/commands/build.ts b/apps/cli/src/commands/build.ts index ffbdb82f..d9704400 100644 --- a/apps/cli/src/commands/build.ts +++ b/apps/cli/src/commands/build.ts @@ -24,6 +24,8 @@ export type ImageInfo = { workdir: string; }; +type DriveType = "ext2" | "sqfs"; + const CARTESI_LABEL_PREFIX = "io.cartesi.rollups"; const CARTESI_LABEL_RAM_SIZE = `${CARTESI_LABEL_PREFIX}.ram_size`; const CARTESI_LABEL_DATA_SIZE = `${CARTESI_LABEL_PREFIX}.data_size`; @@ -66,6 +68,13 @@ export default class BuildApplication extends BaseCommand< description: "if the developer is only interested in building the ext2 image to run sunodo shell and does not want to create the machine snapshot, this flag can be used to skip the last step of the process.", }), + "drive-type": Flags.string({ + summary: "cartesi-machine flash drive type", + description: + "defines which drive type will be used as cartesi amchine flash-drives", + options: ["ext2", "sqfs"], + default: "ext2", + }), }; /** @@ -238,7 +247,48 @@ Update your application Dockerfile using one of the templates at https://github. ]; } - private static createMachineSnapshotCommand(info: ImageInfo): string[] { + private static createSquashfsCommand(): string[] { + const cmd = [ + "cat", + "/tmp/input0", + "|", + "mksquashfs", + "-", + "/tmp/output", + "-tar", + "-mkfs-time", + "0", + "-all-time", + "0", + "-all-root", + "-noappend", + "-no-exports", + "-comp", + "lzo", + "-quiet", + "-no-progress", + ]; + return ["/usr/bin/env", "bash", "-c", cmd.join(" ")]; + } + + private static createDriveCommand( + type: DriveType, + extraBytes: number, + ): string[] { + switch (type) { + case "ext2": + return BuildApplication.createExt2Command(extraBytes); + case "sqfs": + return BuildApplication.createSquashfsCommand(); + } + } + + private async createMachineSnapshotCommand( + info: ImageInfo, + ): Promise { + const { flags } = await this.parse(BuildApplication); + const driveType = flags["drive-type"] as DriveType; + const ramSize = info.ramSize; const entrypoint = [ "rollup-init", @@ -270,6 +320,9 @@ Update your application Dockerfile using one of the templates at https://github. `--append-entrypoint=${entrypoint}`, ]; + if (driveType === "sqfs") + result.push("--append-bootargs=rootfstype=squashfs"); + return result; } @@ -278,9 +331,12 @@ Update your application Dockerfile using one of the templates at https://github. imageInfo: ImageInfo, sdkImage: string, ): Promise { + const { flags } = await this.parse(BuildApplication); + const driveType = flags["drive-type"] as DriveType; + const cruntimeTarPath = this.getContextPath("cruntime.tar"); const cruntimeGnutarPath = this.getContextPath("cruntime.gnutar"); - const cruntimeDrivePath = this.getContextPath("cruntime.ext2"); + const cruntimeDrivePath = this.getContextPath("cruntime.drive"); try { await this.createTarball(image, cruntimeTarPath); @@ -294,7 +350,8 @@ Update your application Dockerfile using one of the templates at https://github. await this.sdkRun( sdkImage, - BuildApplication.createExt2Command( + BuildApplication.createDriveCommand( + driveType, bytes.parse(imageInfo.dataSize), ), [cruntimeGnutarPath], @@ -310,8 +367,11 @@ Update your application Dockerfile using one of the templates at https://github. imageInfo: ImageInfo, sdkImage: string, ): Promise { + const { flags } = await this.parse(BuildApplication); + const driveType = flags["drive-type"] as DriveType; + const ociConfigTarPath = this.getContextPath("ociconfig.tar"); - const ociConfigDrivePath = this.getContextPath("ociconfig.ext2"); + const ociConfigDrivePath = this.getContextPath("ociconfig.drive"); try { const configTar = createTar([ @@ -324,7 +384,8 @@ Update your application Dockerfile using one of the templates at https://github. await this.sdkRun( sdkImage, - BuildApplication.createExt2Command( + BuildApplication.createDriveCommand( + driveType, bytes.parse(imageInfo.dataSize), ), [ociConfigTarPath], @@ -340,9 +401,13 @@ Update your application Dockerfile using one of the templates at https://github. imageInfo: ImageInfo, sdkImage: string, ): Promise { + const { flags } = await this.parse(BuildApplication); + const driveType = flags["drive-type"] as DriveType; + const appTarPath = this.getContextPath("app.tar"); const appGnutarPath = this.getContextPath("app.gnutar"); - const appDrivePath = this.getContextPath("app.ext2"); + const appDrivePath = this.getContextPath("app.drive"); + try { // create OCI Image tarball await this.createTarball(image, appTarPath); @@ -355,10 +420,11 @@ Update your application Dockerfile using one of the templates at https://github. appGnutarPath, ); - // create ext2 + // create drive await this.sdkRun( sdkImage, - BuildApplication.createExt2Command( + BuildApplication.createDriveCommand( + driveType, bytes.parse(imageInfo.dataSize), ), [appGnutarPath], @@ -374,9 +440,9 @@ Update your application Dockerfile using one of the templates at https://github. const { flags } = await this.parse(BuildApplication); const snapshotPath = this.getContextPath("image"); - const cruntimeDrivePath = this.getContextPath("cruntime.ext2"); - const ociConfigDrivePath = this.getContextPath("ociconfig.ext2"); - const appDrivePath = this.getContextPath("app.ext2"); + const cruntimeDrivePath = this.getContextPath("cruntime.drive"); + const ociConfigDrivePath = this.getContextPath("ociconfig.drive"); + const appDrivePath = this.getContextPath("app.drive"); // clean up temp files we create along the process tmp.setGracefulCleanup(); @@ -411,7 +477,7 @@ Update your application Dockerfile using one of the templates at https://github. if (!flags["skip-snapshot"]) { await this.sdkRun( sdkImage, - BuildApplication.createMachineSnapshotCommand(imageInfo), + await this.createMachineSnapshotCommand(imageInfo), [cruntimeDrivePath, ociConfigDrivePath, appDrivePath], snapshotPath, );