image-container-extension.bbclass 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # This software is a part of ISAR.
  2. # Copyright (C) Siemens AG, 2021
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. # This class extends the image.bbclass for containerizing the root filesystem.
  7. CONTAINER_IMAGE_FORMATS ?= "docker-archive"
  8. CONTAINER_IMAGE_NAME ?= "${PN}-${DISTRO}-${DISTRO_ARCH}"
  9. CONTAINER_IMAGE_TAG ?= "${PV}-${PR}"
  10. containerize_rootfs() {
  11. local cmd="/bin/dash"
  12. local empty_tag="empty"
  13. local tag="${CONTAINER_IMAGE_TAG}"
  14. local oci_img_dir="${WORKDIR}/oci-image"
  15. local rootfs="$1"
  16. local container_formats="$2"
  17. local container_name_prefix="$3"
  18. # prepare OCI container image skeleton
  19. bbdebug 1 "prepare OCI container image skeleton"
  20. sudo rm -rf "${oci_img_dir}" "${oci_img_dir}_unpacked"
  21. sudo umoci init --layout "${oci_img_dir}"
  22. sudo umoci new --image "${oci_img_dir}:${empty_tag}"
  23. sudo umoci config --image "${oci_img_dir}:${empty_tag}" \
  24. --config.cmd="${cmd}"
  25. sudo umoci unpack --image "${oci_img_dir}:${empty_tag}" \
  26. "${oci_img_dir}_unpacked"
  27. # add root filesystem as the flesh of the skeleton
  28. sudo cp -a "${rootfs}"/* "${oci_img_dir}_unpacked/rootfs/"
  29. # clean-up temporary files
  30. sudo find "${oci_img_dir}_unpacked/rootfs/tmp" -mindepth 1 -delete
  31. # pack container image
  32. bbdebug 1 "pack container image"
  33. sudo umoci repack --image "${oci_img_dir}:${tag}" \
  34. "${oci_img_dir}_unpacked"
  35. sudo umoci remove --image "${oci_img_dir}:${empty_tag}"
  36. sudo rm -rf "${oci_img_dir}_unpacked"
  37. # no root needed anymore
  38. sudo chown --recursive $(id -u):$(id -g) "${oci_img_dir}"
  39. # convert the OCI container image to the desired format
  40. image_name="${container_name_prefix}${CONTAINER_IMAGE_NAME}"
  41. for image_type in ${CONTAINER_IMAGE_FORMATS} ; do
  42. image_archive="${DEPLOY_DIR_IMAGE}/${image_name}-${tag}-${image_type}.tar"
  43. bbdebug 1 "Creating container image type: ${image_type}"
  44. case "${image_type}" in
  45. "docker-archive" | "oci-archive")
  46. if [ "${image_type}" = "oci-archive" ] ; then
  47. target="${image_type}:${image_archive}:${tag}"
  48. else
  49. target="${image_type}:${image_archive}:${image_name}:${tag}"
  50. fi
  51. rm -f "${image_archive}" "${image_archive}.xz"
  52. bbdebug 2 "Converting OCI image to ${image_type}"
  53. skopeo --insecure-policy copy \
  54. "oci:${oci_img_dir}:${tag}" "${target}"
  55. bbdebug 2 "Compressing image"
  56. xz -T0 "${image_archive}"
  57. ;;
  58. "oci")
  59. tar --create --xz --directory "${oci_img_dir}" \
  60. --file "${image_archive}.xz" .
  61. ;;
  62. "docker-daemon" | "containers-storage")
  63. if [ -f /.dockerenv ] || [ -f /run/.containerenv ] ; then
  64. die "Adding the container image to a container runtime (${image_type}) not supported if running from a container (e.g. 'kas-container')"
  65. fi
  66. skopeo --insecure-policy copy \
  67. "oci:${oci_img_dir}:${tag}" \
  68. "${image_type}:${image_name}:${tag}"
  69. ;;
  70. *)
  71. die "Unsupported format for containerize_rootfs: ${image_type}"
  72. ;;
  73. esac
  74. done
  75. }