# Class used to generate container image based on Belden CoreOS

IMAGE_FSTYPES = "container oci"
IMAGE_FEATURES = "read-only-rootfs"
IMAGE_LINGUAS = ""
NO_RECOMMENDATIONS = "1"

# We have choosen to use the same machine configuration for container and 
# the host, thus we can't use linux-dummy has the default kernel provider.
IMAGE_CONTAINER_NO_DUMMY = "1"

COREOS_CONTAINER_IMAGE_BASE_INSTALL = "\
    packagegroup-coreos-container \
"

COREOS_CONTAINER_IMAGE_EXTRA_INSTALL ?= ""
IMAGE_INSTALL ?= "${COREOS_CONTAINER_IMAGE_BASE_INSTALL} ${COREOS_CONTAINER_IMAGE_EXTRA_INSTALL}"

# OCI Parameters
# ==============================================================================

OCI_IMAGE_ENTRYPOINT ?= "${@bb.utils.contains('IMAGE_FEATURES', 'systemd', '/usr/sbin/init', '/usr/bin/sh', d)}"

inherit image
inherit image-oci

# Images features for containers
# ==============================================================================

# Install systemd inside the container
FEATURE_PACKAGES_systemd = "systemd"


# Image Post processings
# ==============================================================================

IMAGE_CMD:oci:append() {
    # meta-virtualization default IMAGE_CMD doesn't create a symlink on kirkstone
    image_link_name="${IMAGE_LINK_NAME}${IMAGE_NAME_SUFFIX}-oci"

    ln -sf "$image_name" "$image_link_name"

    if [ -n "${OCI_IMAGE_TAR_OUTPUT}" ]; then
        ln -sf "$image_name.tar" "$image_link_name.tar"
    fi
}

# Workaround /var/volatile for now
# See layers/meta-virtualization/recipes-extended/images/container-base.bb
ROOTFS_POSTPROCESS_COMMAND += "rootfs_fixup_var_volatile ; "
rootfs_fixup_var_volatile () {
    install -m 1777 -d ${IMAGE_ROOTFS}/${localstatedir}/volatile/tmp
    install -m 755 -d ${IMAGE_ROOTFS}/${localstatedir}/volatile/log

    # in rootfs-postcommands.bbclass, when using initscripts theses link
    # are created by running ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
    # When the distro is configured to systemd, this is not done, so we need to
    # do it here manually (As systemd is not always included in the container)
    ln -sf ${localstatedir}/volatile/tmp ${IMAGE_ROOTFS}/${localstatedir}/tmp
    ln -sf ${localstatedir}/volatile/log ${IMAGE_ROOTFS}/${localstatedir}/log
}

ROOTFS_POSTPROCESS_COMMAND += "${@bb.utils.contains('IMAGE_FEATURES', 'systemd', 'rootfs_fixup_systemd ; ', '', d)}"
rootfs_fixup_systemd () {
    # Mask systemd services that are not needed/doesn't work in a container
    # This ensure that the container doesn't boot in systemd emergency mode
    systemctl --root=${IMAGE_ROOTFS} mask systemd-remount-fs.service
    systemctl --root=${IMAGE_ROOTFS} mask var-volatile.mount
}

# Add support for plugin classes like in coreos-image.bbclass
COREOS_IMAGE_EXTRACLASSES ?= ""
inherit ${COREOS_IMAGE_EXTRACLASSES}
