[V2,1/2] images: add support for container images

Message ID 20210209141005.45491-2-silvano.cirujano-cuesta@siemens.com
State Superseded, archived
Headers show
Series Add support for containerized root filesystems | expand

Commit Message

Silvano Cirujano Cuesta Feb. 9, 2021, 4:10 a.m. UTC
Add support for creation of container images with the build root
filesystems.

Extend also task "populate_sdk" to support the creation of a container image
containing the SDK.

Signed-off-by: Silvano Cirujano Cuesta <silvano.cirujano-cuesta@siemens.com>
---
 meta/classes/container-img.bbclass | 99 ++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
 create mode 100644 meta/classes/container-img.bbclass

Patch

diff --git a/meta/classes/container-img.bbclass b/meta/classes/container-img.bbclass
new file mode 100644
index 0000000..85d9fb4
--- /dev/null
+++ b/meta/classes/container-img.bbclass
@@ -0,0 +1,99 @@ 
+# This software is a part of ISAR.
+# Copyright (C) Siemens AG, 2021
+#
+# SPDX-License-Identifier: MIT
+#
+# This class provides the tasks 'containerize_rootfs' and 'containerize_sdk'
+# to create container images containing the target rootfs and the SDK
+# respectively.
+
+CONTAINER_FORMAT ?= "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"
+
+    # 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/"
+
+    # 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_FORMAT} ; 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")
+                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
+}
+
+do_container_image[stamp-extra-info] = "${DISTRO}-${MACHINE}"
+do_container_image[vardeps] += "CONTAINER_FORMAT"
+do_container_image(){
+    rootfs_id="${DISTRO}-${DISTRO_ARCH}"
+
+    bbnote "Generate container image in these formats: ${CONTAINER_FORMAT}"
+    containerize_rootfs "${IMAGE_ROOTFS}" "${rootfs_id}"
+}
+
+addtask container_image before do_image after do_image_tools
+
+do_container_sdk[stamp-extra-info] = "${DISTRO}-${MACHINE}"
+do_container_sdk[vardeps] += "CONTAINER_FORMAT"
+do_container_sdk(){
+    rootfs_id="sdk-${DISTRO}-${DISTRO_ARCH}"
+
+    bbnote "Generate containerized SDK in these formats: ${CONTAINER_FORMAT}"
+    containerize_rootfs "${SDKCHROOT_DIR}" "${rootfs_id}"
+}
+
+addtask container_sdk after do_populate_sdk
+