public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: Silvano Cirujano Cuesta <silvano.cirujano-cuesta@siemens.com>
To: isar-users@googlegroups.com
Subject: [PATCH v6 1/5] classes: add root filesystem containerizing class
Date: Tue,  9 Mar 2021 21:52:35 +0100	[thread overview]
Message-ID: <20210309205239.652677-2-silvano.cirujano-cuesta@siemens.com> (raw)
In-Reply-To: <20210309205239.652677-1-silvano.cirujano-cuesta@siemens.com>

This class can be used to create container images which root filesystem
is that generated by the do_rootfs task.

Containerized root filesystems have following possible use-cases:
 - Using ISAR as a container image builder.
 - Simplify distribution of runtime rootfs (binaries, libraries,
   configurations, ...) for application development or testing.
 - Distributing SDKs.

Signed-off-by: Silvano Cirujano Cuesta <silvano.cirujano-cuesta@siemens.com>
---
 .../classes/image-container-extension.bbclass | 81 +++++++++++++++++++
 meta/classes/image.bbclass                    |  1 +
 2 files changed, 82 insertions(+)
 create mode 100644 meta/classes/image-container-extension.bbclass

diff --git a/meta/classes/image-container-extension.bbclass b/meta/classes/image-container-extension.bbclass
new file mode 100644
index 0000000..e26604a
--- /dev/null
+++ b/meta/classes/image-container-extension.bbclass
@@ -0,0 +1,81 @@
+# This software is a part of ISAR.
+# Copyright (C) Siemens AG, 2021
+#
+# SPDX-License-Identifier: MIT
+#
+# This class extends the image.bbclass for containerizing the root filesystem.
+
+CONTAINER_FORMATS ?= "docker-archive"
+
+containerize_rootfs() {
+    local cmd="/bin/dash"
+    local empty_tag="empty"
+    local full_tag="latest"
+    local oci_img_dir="${WORKDIR}/oci-image"
+    local rootfs="$1"
+    local rootfs_id="$2"
+    local container_formats="$3"
+
+    # prepare OCI container image skeleton
+    bbdebug 1 "prepare OCI container image skeleton"
+    rm -rf "${oci_img_dir}"
+    sudo umoci init --layout "${oci_img_dir}"
+    sudo umoci new --image "${oci_img_dir}:${empty_tag}"
+    sudo umoci config --image "${oci_img_dir}:${empty_tag}" \
+        --config.cmd="${cmd}"
+    sudo umoci unpack --image "${oci_img_dir}:${empty_tag}" \
+        "${oci_img_dir}_unpacked"
+
+    # add root filesystem as the flesh of the skeleton
+    sudo cp -a "${rootfs}"/* "${oci_img_dir}_unpacked/rootfs/"
+    # clean-up temporary files
+    sudo find "${oci_img_dir}_unpacked/rootfs/tmp" -mindepth 1 -delete
+
+    # pack container image
+    bbdebug 1 "pack container image"
+    sudo umoci repack --image "${oci_img_dir}:${full_tag}" \
+        "${oci_img_dir}_unpacked"
+    sudo umoci remove --image "${oci_img_dir}:${empty_tag}"
+    sudo rm -rf "${oci_img_dir}_unpacked"
+
+    # no root needed anymore
+    sudo chown --recursive $(id -u):$(id -g) "${oci_img_dir}"
+
+    # convert the OCI container image to the desired format
+    image_name="isar-${rootfs_id}"
+    for image_type in ${CONTAINER_FORMATS} ; do
+        image_archive="${DEPLOY_DIR_IMAGE}/${rootfs_id}-${image_type}.tar"
+        bbdebug 1 "Creating container image type: ${image_type}"
+        case "${image_type}" in
+            "docker-archive" | "oci-archive")
+                if [ "${image_type}" = "oci-archive" ] ; then
+                    target="${image_type}:${image_archive}:latest"
+                else
+                    target="${image_type}:${image_archive}:${image_name}:latest"
+                fi
+                rm -f "${image_archive}" "${image_archive}.xz"
+                bbdebug 2 "Converting OCI image to ${image_type}"
+                skopeo --insecure-policy copy \
+                    "oci:${oci_img_dir}:${full_tag}" "${target}"
+                bbdebug 2 "Compressing image"
+                xz -T0 "${image_archive}"
+                ;;
+            "oci")
+                tar --create --xz --directory "${oci_img_dir}" \
+                    --file "${image_archive}.xz" .
+                ;;
+            "docker-daemon" | "containers-storage")
+                if [ -f /.dockerenv ] || [ -f /run/.containerenv ] ; then
+                    die "Adding the container image to a container runtime (${image_type}) not supported if running from a container (e.g. 'kas-container')"
+                fi
+                skopeo --insecure-policy copy \
+                    "oci:${oci_img_dir}:${full_tag}" \
+                    "${image_type}:${image_name}:latest"
+                ;;
+            *)
+                die "Unsupported format for containerize_rootfs: ${image_type}"
+                ;;
+        esac
+    done
+}
+
diff --git a/meta/classes/image.bbclass b/meta/classes/image.bbclass
index eddc444..ec93cab 100644
--- a/meta/classes/image.bbclass
+++ b/meta/classes/image.bbclass
@@ -76,6 +76,7 @@ inherit image-tools-extension
 inherit image-postproc-extension
 inherit image-locales-extension
 inherit image-account-extension
+inherit image-container-extension
 
 # Extra space for rootfs in MB
 ROOTFS_EXTRA ?= "64"
-- 
2.30.1


  reply	other threads:[~2021-03-09 20:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-09 20:52 [PATCH v6 0/5] Add support for containerized root filesystems Silvano Cirujano Cuesta
2021-03-09 20:52 ` Silvano Cirujano Cuesta [this message]
2021-03-09 20:52 ` [PATCH v6 2/5] classes: add new image type 'container-img' Silvano Cirujano Cuesta
2021-03-09 20:52 ` [PATCH v6 3/5] sdk: add support for containerized sdk Silvano Cirujano Cuesta
2021-03-09 20:52 ` [PATCH v6 4/5] docs: document creation of container images Silvano Cirujano Cuesta
2021-03-09 20:52 ` [PATCH v6 5/5] ci: add container image testing configurations Silvano Cirujano Cuesta
2021-03-10 16:18 ` [PATCH v6 0/5] Add support for containerized root filesystems Anton Mikanovich
2021-03-10 17:18   ` Silvano Cirujano Cuesta
2021-03-10 17:25     ` Henning Schild
2021-03-10 17:30       ` Silvano Cirujano Cuesta
2021-03-10 16:28 ` Henning Schild

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210309205239.652677-2-silvano.cirujano-cuesta@siemens.com \
    --to=silvano.cirujano-cuesta@siemens.com \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox