From 6fcea28b63938b902d4b348c411a0216c1b03da3 Mon Sep 17 00:00:00 2001
From: Enderson Maia <endersonmaia@gmail.com>
Date: Mon, 29 Apr 2024 18:26:24 +0100
Subject: [PATCH 1/2] feat(cli): bump to cartesi/sdk:0.6.0

---
 .changeset/chatty-boxes-ring.md | 5 +++++
 apps/cli/src/commands/build.ts  | 2 +-
 apps/cli/src/commands/shell.ts  | 2 +-
 3 files changed, 7 insertions(+), 2 deletions(-)
 create mode 100644 .changeset/chatty-boxes-ring.md

diff --git a/.changeset/chatty-boxes-ring.md b/.changeset/chatty-boxes-ring.md
new file mode 100644
index 00000000..3ca3b4db
--- /dev/null
+++ b/.changeset/chatty-boxes-ring.md
@@ -0,0 +1,5 @@
+---
+"@cartesi/cli": minor
+---
+
+bump to cartesi/sdk:0.6.0 in build and shell
diff --git a/apps/cli/src/commands/build.ts b/apps/cli/src/commands/build.ts
index 72bd2b96..d14e3ac7 100644
--- a/apps/cli/src/commands/build.ts
+++ b/apps/cli/src/commands/build.ts
@@ -28,7 +28,7 @@ const CARTESI_LABEL_DATA_SIZE = `${CARTESI_LABEL_PREFIX}.data_size`;
 const CARTESI_DEFAULT_RAM_SIZE = "128Mi";
 
 const CARTESI_LABEL_SDK_VERSION = `${CARTESI_LABEL_PREFIX}.sdk_version`;
-const CARTESI_DEFAULT_SDK_VERSION = "0.4.0";
+const CARTESI_DEFAULT_SDK_VERSION = "0.6.0";
 
 export default class BuildApplication extends BaseCommand<
     typeof BuildApplication
diff --git a/apps/cli/src/commands/shell.ts b/apps/cli/src/commands/shell.ts
index d7034897..ea5060f4 100644
--- a/apps/cli/src/commands/shell.ts
+++ b/apps/cli/src/commands/shell.ts
@@ -33,7 +33,7 @@ export default class Shell extends BaseCommand<typeof Shell> {
         const ext2 = path.join(containerDir, path.basename(ext2Path));
         const ramSize = "128Mi";
         const driveLabel = "root";
-        const sdkImage = "cartesi/sdk:0.4.0"; // XXX: how to resolve sdk version?
+        const sdkImage = "cartesi/sdk:0.6.0"; // XXX: how to resolve sdk version?
         const args = [
             "run",
             "--interactive",

From cef2d9f64b25151a1c86b718ee6e7ed1a9dc91fb Mon Sep 17 00:00:00 2001
From: Enderson Maia <endersonmaia@gmail.com>
Date: Mon, 15 Apr 2024 11:51:40 +0100
Subject: [PATCH 2/2] feat(cli): use crane to build rootfs tarball

---
 .changeset/tiny-cats-tickle.md |  5 +++++
 apps/cli/src/commands/build.ts | 34 +++++++++++++++-------------------
 2 files changed, 20 insertions(+), 19 deletions(-)
 create mode 100644 .changeset/tiny-cats-tickle.md

diff --git a/.changeset/tiny-cats-tickle.md b/.changeset/tiny-cats-tickle.md
new file mode 100644
index 00000000..f35badc9
--- /dev/null
+++ b/.changeset/tiny-cats-tickle.md
@@ -0,0 +1,5 @@
+---
+"@sunodo/cli": patch
+---
+
+use crane to build rootfs tarball
diff --git a/apps/cli/src/commands/build.ts b/apps/cli/src/commands/build.ts
index d14e3ac7..aa25675c 100644
--- a/apps/cli/src/commands/build.ts
+++ b/apps/cli/src/commands/build.ts
@@ -141,32 +141,19 @@ Update your application Dockerfile using one of the templates at https://github.
         return info;
     }
 
-    // creates a rootfs tarball from the image
-    // this process is not always fully reproducible
-    // FIXME: we could use the image and create a flat rootfs without
-    //        `docker container create` (use undocker, umoci, a native typescript implementation, etc.)
+    // saves the OCI Image to a tarball
     private async createTarball(
         image: string,
         outputFilePath: string,
     ): Promise<void> {
         // create docker tarball from app image
         const { stdout: appCid } = await execa("docker", [
-            "container",
-            "create",
-            "--platform",
-            "linux/riscv64",
+            "image",
+            "save",
             image,
-        ]);
-
-        await execa("docker", [
-            "container",
-            "export",
             "-o",
             outputFilePath,
-            appCid,
         ]);
-
-        await execa("docker", ["container", "rm", appCid]);
     }
 
     // this wraps the call to the sdk image with a one-shot approach
@@ -201,15 +188,24 @@ Update your application Dockerfile using one of the templates at https://github.
         await execa("docker", ["container", "rm", cid]);
     }
 
-    // returns the command to create rootfs from a tarball
+    // returns the command to create rootfs tarball from an OCI Image tarball
     private static createRootfsTarCommand(): string[] {
-        return [
+        const cmd = [
+            "cat",
+            "/tmp/input",
+            "|",
+            "crane",
+            "export",
+            "-", // OCI Image from stdin
+            "-", // rootfs tarball to stdout
+            "|",
             "bsdtar",
             "-cf",
             "/tmp/output",
             "--format=gnutar",
-            "@/tmp/input",
+            "@/dev/stdin", // rootfs tarball from stdin
         ];
+        return ["/usr/bin/env", "bash", "-c", cmd.join(" ")];
     }
 
     // returns the command to create ext2 from a rootfs