diff --git a/tests/legacy/unit/plugins/v2/test_initrd.py b/tests/legacy/unit/plugins/v2/test_initrd.py new file mode 100755 index 0000000000..4af4fdaab2 --- /dev/null +++ b/tests/legacy/unit/plugins/v2/test_initrd.py @@ -0,0 +1,771 @@ +# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- +# +# Copyright (C) 2022 Canonical Ltd +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import logging +import platform +import textwrap +from dataclasses import dataclass +from unittest import mock + +from testtools import TestCase + +from snapcraft_legacy.plugins.v2.initrd import InitrdPlugin + + +class Initrdv2PluginProperties: + initrd_modules: [str] = None + initrd_configured_modules: [str] = None + initrd_firmware: [str] = None + initrd_compression: [str] = None + initrd_compression_options: [str] = None + initrd_overlay: str = None + initrd_addons: [str] = None + initrd_add_ppa: bool = False + + +class TestPluginInitrd(TestCase): + """ + Legacy initrd plugin tests. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def _setup_test( + self, + initrdmodules=None, + initrdconfiguredmodules=None, + initrdstagefirmware=False, + initrdfirmware=None, + initrdcompression=None, + initrdcompressionoptions=None, + initrdoverlay=None, + initrdaddons=None, + arch=platform.machine(), + ): + @dataclass + class Options(Initrdv2PluginProperties): + initrd_modules = initrdmodules + initrd_configured_modules = initrdconfiguredmodules + initrd_stage_firmware = initrdstagefirmware + initrd_firmware = initrdfirmware + initrd_compression = initrdcompression + initrd_compression_options = initrdcompressionoptions + initrd_overlay = initrdoverlay + initrd_addons = initrdaddons + # Ensure that the PPA is not added so we don't cause side-effects + initrd_add_ppa = False + + target_arch = _DEB_ARCH_TRANSLATIONS[arch] + + with mock.patch( + "snapcraft_legacy.plugins.v2.initrd._get_target_architecture", + return_value=target_arch, + ): + plugin = InitrdPlugin(part_name="initrd", options=Options()) + + return plugin + + def test_schema(self): + self.assertEqual( + InitrdPlugin.get_schema(), + { + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "additionalProperties": False, + "properties": { + "initrd-modules": { + "type": "array", + "minitems": 1, + "uniqueItems": True, + "items": {"type": "string"}, + "default": [], + }, + "initrd-configured-modules": { + "type": "array", + "minitems": 1, + "uniqueItems": True, + "items": {"type": "string"}, + "default": [], + }, + "initrd-stage-firmware": { + "type": "boolean", + "default": False, + }, + "initrd-firmware": { + "type": "array", + "minitems": 1, + "uniqueItems": True, + "items": {"type": "string"}, + "default": [], + }, + "initrd-compression": { + "type": "string", + "enum": ["lz4", "xz", "gz", "zstd"], + }, + "initrd-compression-options": { + "type": "array", + "minitems": 1, + "uniqueItems": True, + "items": {"type": "string"}, + "default": [], + }, + "initrd-overlay": { + "type": "string", + "default": "", + }, + "initrd-addons": { + "type": "array", + "minitems": 1, + "uniqueItems": True, + "items": {"type": "string"}, + "default": [], + }, + "initrd-add-ppa": { + "type": "boolean", + "default": True, + }, + }, + }, + ) + + def test_get_base_build_packages(self): + plugin = self._setup_test() + # default initrd compression is zstd + self.assertEqual( + plugin.get_build_packages(), + { + "bc", + "binutils", + "fakeroot", + "dracut-core", + "kmod", + "kpartx", + "lz4", + "systemd", + }, + ) + + def test_get_base_build_packages_lz4(self): + plugin = self._setup_test(initrdcompression="lz4") + self.assertEqual( + plugin.get_build_packages(), + { + "bc", + "binutils", + "fakeroot", + "dracut-core", + "kmod", + "kpartx", + "lz4", + "systemd", + }, + ) + + def test_get_base_build_packages_xz(self): + plugin = self._setup_test(initrdcompression="xz") + self.assertEqual( + plugin.get_build_packages(), + { + "bc", + "binutils", + "fakeroot", + "dracut-core", + "kmod", + "kpartx", + "xz-utils", + "systemd", + }, + ) + + def test_get_build_snaps(self): + plugin = self._setup_test() + self.assertEqual(plugin.get_build_snaps(), set()) + + def test_check_configuration_simple(self): + plugin = self._setup_test() + opt = plugin.options + + self.assertEqual(opt.initrd_modules, None) + self.assertEqual(opt.initrd_configured_modules, None) + self.assertEqual(opt.initrd_firmware, None) + self.assertEqual(opt.initrd_compression, None) + self.assertEqual(opt.initrd_compression_options, None) + self.assertEqual(opt.initrd_overlay, None) + self.assertEqual(opt.initrd_addons, None) + self.assertFalse(opt.initrd_add_ppa) + + def test_check_configuration_zstd_compression(self): + plugin = self._setup_test( + initrdcompression="zstd", + ) + opt = plugin.options + + self.assertIs(opt.initrd_modules, None) + self.assertIs(opt.initrd_configured_modules, None) + self.assertIs(opt.initrd_firmware, None) + self.assertEqual(opt.initrd_compression, "zstd") + self.assertIs(opt.initrd_compression_options, None) + self.assertIs(opt.initrd_overlay, None) + self.assertIs(opt.initrd_addons, None) + self.assertFalse(opt.initrd_add_ppa) + + def test_check_configuration_lz4_custom_compression(self): + plugin = self._setup_test( + initrdcompression="lz4", + initrdcompressionoptions=["-9", "-l"], + ) + opt = plugin.options + + self.assertIs(opt.initrd_modules, None) + self.assertIs(opt.initrd_configured_modules, None) + self.assertIs(opt.initrd_firmware, None) + self.assertEqual(opt.initrd_compression, "lz4") + self.assertEqual(opt.initrd_compression_options, ["-9", "-l"]) + self.assertIs(opt.initrd_overlay, None) + self.assertIs(opt.initrd_addons, None) + self.assertFalse(opt.initrd_add_ppa) + + def test_check_get_build_environment(self): + plugin = self._setup_test() + + self.assertEqual( + plugin.get_build_environment(), + { + "UC_INITRD_DEB": "${SNAPCRAFT_PART_BUILD}/ubuntu-core-initramfs", + }, + ) + + def test_check_get_build_environment_compiler_paths(self): + plugin = self._setup_test() + + self.assertEqual( + plugin.get_build_environment(), + { + "UC_INITRD_DEB": "${SNAPCRAFT_PART_BUILD}/ubuntu-core-initramfs", + }, + ) + + def test_check_get_build_command(self): + plugin = self._setup_test() + + # we need to get build environment + plugin.get_build_environment() + build_commands = plugin.get_build_commands() + assert _is_sub_array(build_commands, _initrd_modules_empty_cmd) + assert _is_sub_array(build_commands, _initrd_configured_modules_empty_cmd) + assert _is_sub_array(build_commands, _link_files_fnc) + assert _is_sub_array(build_commands, _download_initrd_fnc) + assert _is_sub_array(build_commands, _get_initrd_cmd) + assert _is_sub_array(build_commands, _download_snapd_fnc) + assert _is_sub_array(build_commands, _get_snapd_cmd) + assert _is_sub_array(build_commands, _parse_kernel_release_cmd) + assert _is_sub_array(build_commands, _install_initrd_modules_cmd) + assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) + assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) + assert not _is_sub_array(build_commands, _install_initrd_firmware_cmd) + assert not _is_sub_array(build_commands, _install_initrd_addons_cmd) + assert not _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) + assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) + assert _is_sub_array(build_commands, _clean_old_initrd_cmd) + assert _is_sub_array(build_commands, _initrd_check_firmware_part) + assert _is_sub_array(build_commands, _initrd_tool_cmd) + assert not _is_sub_array(build_commands, _update_initrd_compression_cmd) + assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) + assert _is_sub_array(build_commands, _create_inird_cmd) + + def test_check_get_build_command_defconfig_configs_no_firmware_lz4(self): + plugin = self._setup_test( + initrdcompression="lz4", + initrdcompressionoptions=["-9", "-l"], + initrdmodules=["dm-crypt", "slimbus"], + initrdstagefirmware=True, + initrdfirmware=["firmware/for/wifi", "firmware/for/webcam"], + initrdaddons=[ + "usr/bin/cryptsetup", + "usr/lib/my-arch/libcrypto.so", + ], + ) + + # we need to get build environment + plugin.get_build_environment() + build_commands = plugin.get_build_commands() + assert _is_sub_array(build_commands, _initrd_modules_cmd) + assert _is_sub_array(build_commands, _initrd_configured_modules_empty_cmd) + assert _is_sub_array(build_commands, _link_files_fnc) + assert _is_sub_array(build_commands, _download_initrd_fnc) + assert _is_sub_array(build_commands, _get_initrd_cmd) + assert _is_sub_array(build_commands, _download_snapd_fnc) + assert _is_sub_array(build_commands, _get_snapd_cmd) + assert _is_sub_array(build_commands, _parse_kernel_release_cmd) + assert _is_sub_array(build_commands, _install_initrd_modules_cmd) + assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) + assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) + assert _is_sub_array(build_commands, _install_initrd_firmware_cmd) + assert _is_sub_array(build_commands, _install_initrd_addons_cmd) + assert not _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) + assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) + assert _is_sub_array(build_commands, _clean_old_initrd_cmd) + assert _is_sub_array(build_commands, _initrd_check_firmware_stage) + assert _is_sub_array(build_commands, _initrd_tool_cmd) + assert _is_sub_array(build_commands, _update_initrd_compression_cmd) + assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) + assert _is_sub_array(build_commands, _create_inird_stage_firmware_cmd) + + def test_check_get_build_command_unknown_compiler(self): + plugin = self._setup_test() + + # we need to get build environment + plugin.get_build_environment() + build_commands = plugin.get_build_commands() + assert _is_sub_array(build_commands, _initrd_modules_empty_cmd) + assert _is_sub_array(build_commands, _initrd_configured_modules_empty_cmd) + assert _is_sub_array(build_commands, _link_files_fnc) + assert _is_sub_array(build_commands, _download_initrd_fnc) + assert _is_sub_array(build_commands, _get_initrd_cmd) + assert _is_sub_array(build_commands, _download_snapd_fnc) + assert _is_sub_array(build_commands, _get_snapd_cmd) + assert _is_sub_array(build_commands, _parse_kernel_release_cmd) + assert _is_sub_array(build_commands, _install_initrd_modules_cmd) + assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) + assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) + assert not _is_sub_array(build_commands, _install_initrd_firmware_cmd) + assert not _is_sub_array(build_commands, _install_initrd_addons_cmd) + assert not _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) + assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) + assert _is_sub_array(build_commands, _clean_old_initrd_cmd) + assert _is_sub_array(build_commands, _initrd_check_firmware_part) + assert _is_sub_array(build_commands, _initrd_tool_cmd) + assert not _is_sub_array(build_commands, _update_initrd_compression_cmd) + assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) + assert _is_sub_array(build_commands, _create_inird_cmd) + + def test_check_get_build_command_config_flavour_configs(self): + plugin = self._setup_test( + initrdmodules=["dm-crypt", "slimbus"], + initrdconfiguredmodules=["libarc4"], + initrdoverlay="my-overlay", + initrdcompression="gz", + ) + + # we need to get build environment + plugin.get_build_environment() + build_commands = plugin.get_build_commands() + assert _is_sub_array(build_commands, _initrd_modules_cmd) + assert _is_sub_array(build_commands, _initrd_configured_modules_cmd) + assert _is_sub_array(build_commands, _link_files_fnc) + assert _is_sub_array(build_commands, _download_initrd_fnc) + assert _is_sub_array(build_commands, _get_initrd_cmd) + assert _is_sub_array(build_commands, _download_snapd_fnc) + assert _is_sub_array(build_commands, _get_snapd_cmd) + assert _is_sub_array(build_commands, _parse_kernel_release_cmd) + assert _is_sub_array(build_commands, _install_initrd_modules_cmd) + assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) + assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) + assert not _is_sub_array(build_commands, _install_initrd_firmware_cmd) + assert not _is_sub_array(build_commands, _install_initrd_addons_cmd) + assert _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) + assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) + assert _is_sub_array(build_commands, _clean_old_initrd_cmd) + assert _is_sub_array(build_commands, _initrd_check_firmware_part) + assert _is_sub_array(build_commands, _initrd_tool_cmd) + assert _is_sub_array(build_commands, _update_initrd_compression_gz_cmd) + assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) + assert _is_sub_array(build_commands, _create_inird_cmd) + + def test_check_get_build_command_cross(self): + plugin = self._setup_test( + initrdmodules=["dm-crypt", "slimbus"], + initrdconfiguredmodules=["libarc4"], + initrdoverlay="my-overlay", + arch="armv7l", + ) + + # we need to get build environment + plugin.get_build_environment() + build_commands = plugin.get_build_commands() + assert _is_sub_array(build_commands, _initrd_modules_cmd) + assert _is_sub_array(build_commands, _initrd_configured_modules_cmd) + assert _is_sub_array(build_commands, _link_files_fnc) + assert _is_sub_array(build_commands, _download_initrd_fnc) + assert _is_sub_array(build_commands, _get_initrd_armhf_cmd) + assert _is_sub_array(build_commands, _download_snapd_fnc) + assert _is_sub_array(build_commands, _get_snapd_armhf_cmd) + assert _is_sub_array(build_commands, _parse_kernel_release_cmd) + assert _is_sub_array(build_commands, _install_initrd_modules_cmd) + assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) + assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) + assert not _is_sub_array(build_commands, _install_initrd_firmware_cmd) + assert not _is_sub_array(build_commands, _install_initrd_addons_cmd) + assert _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) + assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) + assert _is_sub_array(build_commands, _clean_old_initrd_cmd) + assert _is_sub_array(build_commands, _initrd_check_firmware_part) + assert _is_sub_array(build_commands, _initrd_tool_cmd) + assert not _is_sub_array(build_commands, _update_initrd_compression_cmd) + assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) + assert _is_sub_array(build_commands, _create_inird_cmd) + + +def _is_sub_array(array, sub_array): + a = len(array) + s = len(sub_array) + # Two pointers to traverse the arrays + i = 0 + j = 0 + + # Traverse both arrays simultaneously + while i < a and j < s: + # If element matches + # increment both pointers + if array[i] == sub_array[j]: + i += 1 + j += 1 + + # If sub_array is completely traversed + if j == s: + return True + + # If not, increment i and reset j + else: + i = i - j + 1 + j = 0 + + return False + + +_DEB_ARCH_TRANSLATIONS = { + "aarch64": "arm64", + "armv7l": "armhf", + "i686": "i386", + "riscv64": "riscv64", + "x86_64": "amd64", +} + + +_initrd_modules_empty_cmd = ['initrd_installed_kernel_modules=""'] + +_initrd_modules_cmd = ['initrd_installed_kernel_modules="dm-crypt slimbus"'] + +_initrd_configured_modules_empty_cmd = ['initrd_configured_kernel_modules=""'] + +_initrd_configured_modules_cmd = ['initrd_configured_kernel_modules="libarc4"'] + +_link_files_fnc = [ + textwrap.dedent( + """ + # link files, accept wild cards + # 1: reference dir, 2: file(s) including wild cards, 3: dst dir + link_files() { + if [ "${2}" = "*" ]; then + for f in $(ls ${1}) + do + link_files "${1}" "${f}" "${3}" + done + return 0 + fi + if [ -d "${1}/${2}" ]; then + for f in $(ls ${1}/${2}) + do + link_files "${1}" "${2}/${f}" "${3}" + done + return 0 + fi + + local found="" + for f in $(ls ${1}/${2}) + do + if [[ -L "${f}" ]]; then + local rel_path=$( realpath --no-symlinks --relative-to=${1} ${f} ) + else + local rel_path=$( realpath -se --relative-to=${1} ${f} ) + fi + local dir_path=$(dirname ${rel_path}) + mkdir -p ${3}/${dir_path} + echo "installing ${f} to ${3}/${dir_path}" + ln -f ${f} ${3}/${dir_path} + found="yes" + done + if [ "yes" = "${found}" ]; then + return 0 + else + return 1 + fi + } + """ + ) +] + +_download_initrd_fnc = [ + textwrap.dedent( + """ + # Helper to download code initrd deb package + # 1: arch, 2: output dir + download_core_initrd() { + apt-get download ubuntu-core-initramfs:${1} + # unpack dep to the target dir + dpkg -x ubuntu-core-initramfs_*.deb ${2} + } + """ + ) +] + +_get_initrd_cmd = [ + textwrap.dedent( + """ + echo "Getting ubuntu-core-initrd...." + # only download u-c-initrd deb if needed + if [ ! -e ${{UC_INITRD_DEB}} ]; then + download_core_initrd {arch} ${{UC_INITRD_DEB}} + fi + """.format( + arch=_DEB_ARCH_TRANSLATIONS[platform.machine()] + ) + ) +] + + +_get_initrd_armhf_cmd = [ + textwrap.dedent( + """ + echo "Getting ubuntu-core-initrd...." + # only download u-c-initrd deb if needed + if [ ! -e ${{UC_INITRD_DEB}} ]; then + download_core_initrd {arch} ${{UC_INITRD_DEB}} + fi + """.format( + arch="armhf" + ) + ) +] + +_download_snapd_fnc = [ + textwrap.dedent( + """ + # Helper to download snap-bootstrap from snapd deb package + # 1: arch, 2: output dir + download_snap_bootstrap() { + apt-get download snapd:${1} + # unpack dep to the target dir + dpkg -x snapd_*.deb ${2} + } + """ + ) +] + +_get_snapd_cmd = [ + textwrap.dedent( + """ + echo "Getting snapd deb for snap bootstrap..." + # only download again if files does not exist, otherwise + # assume we are re-running build + if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then + download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} + fi + """.format( + arch=_DEB_ARCH_TRANSLATIONS[platform.machine()] + ) + ) +] + +_get_snapd_armhf_cmd = [ + textwrap.dedent( + """ + echo "Getting snapd deb for snap bootstrap..." + # only download again if files does not exist, otherwise + # assume we are re-running build + if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then + download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} + fi + """.format( + arch="armhf" + ) + ) +] + +_parse_kernel_release_cmd = [ + "KERNEL_RELEASE=$(ls ${SNAPCRAFT_STAGE}/modules)", +] + +_install_initrd_modules_cmd = [ + 'echo "Installing ko modules to initrd..."', + 'install_modules=""', + 'echo "Gathering module dependencies..."', + 'install_modules=""', + "uc_initrd_feature_kernel_modules=${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/kernel-modules", + "mkdir -p ${uc_initrd_feature_kernel_modules}", + "initramfs_ko_modules_conf=${uc_initrd_feature_kernel_modules}/extra-modules.conf", + "touch ${initramfs_ko_modules_conf}", + "for m in ${initrd_installed_kernel_modules} ${initrd_configured_kernel_modules}", + "do", + "\techo ${m} >> ${initramfs_ko_modules_conf}", + "done", +] + +_configure_initrd_modules_cmd = [ + " ".join( + [ + "[ -e ${initramfs_ko_modules_conf} ]", + "&&", + "sort -fu ${initramfs_ko_modules_conf} -o ${initramfs_ko_modules_conf}", + ], + ), + 'echo "Configuring ubuntu-core-initramfs.conf with supported modules"', + 'echo "If module is not included in initrd, do not include it"', + "initramfs_conf_dir=${uc_initrd_feature_kernel_modules}/usr/lib/modules-load.d", + "mkdir -p ${initramfs_conf_dir}", + "initramfs_conf=${initramfs_conf_dir}/ubuntu-core-initramfs.conf", + 'echo "# configures modules" > ${initramfs_conf}', + "for m in $(cat ${initramfs_ko_modules_conf})", + "do", + " ".join( + [ + "\tif", + "[ -n", + '"$(modprobe -n -q --show-depends', + "-d ${SNAPCRAFT_PART_INSTALL}", + '-S "${KERNEL_RELEASE}"', + '${m})" ]; then', + ], + ), + "\t\techo ${m} >> ${initramfs_conf}", + "\tfi", + "done", +] + +_initrd_overlay_features_cmd = [ + "uc_initrd_feature_firmware=${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/uc-firmware", + "mkdir -p ${uc_initrd_feature_firmware}", + "uc_initrd_feature_overlay=${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/uc-overlay", + "mkdir -p ${uc_initrd_feature_overlay}", +] + +_install_initrd_firmware_cmd = [ + 'echo "Installing initrd overlay firmware..."', + "for f in firmware/for/wifi firmware/for/webcam", + "do", + '\tif ! link_files "${SNAPCRAFT_PART_INSTALL}" "${f}" "${uc_initrd_feature_firmware}/lib" ; then', + '\t\tif ! link_files "${SNAPCRAFT_STAGE}" "${f}" "${uc_initrd_feature_firmware}/lib" ; then', + '\t\t\techo "Missing firmware [${f}], ignoring it"', + "\t\tfi", + "\tfi", + "done", +] + +_install_initrd_addons_cmd = [ + 'echo "Installing initrd addons..."', + "for a in usr/bin/cryptsetup usr/lib/my-arch/libcrypto.so", + "do", + '\techo "Copy overlay: ${a}"', + '\tlink_files "${SNAPCRAFT_STAGE}" "${a}" "${uc_initrd_feature_overlay}"', + "done", +] + +_intatll_initrd_overlay_cmd = [ + 'link_files "${SNAPCRAFT_STAGE}/my-overlay" "" "${uc_initrd_feature_overlay}"' +] + +_prepare_ininird_features_cmd = [ + 'echo "Preparing snap-boostrap initrd feature..."', + "uc_initrd_feature_snap_bootstratp=${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/snap-bootstrap", + "mkdir -p ${uc_initrd_feature_snap_bootstratp}", + " ".join( + [ + "link_files", + '"${UC_INITRD_DEB}" "usr/lib/snapd/snap-bootstrap"', + '"${uc_initrd_feature_snap_bootstratp}"', + ], + ), + 'link_files "${UC_INITRD_DEB}" "usr/lib/snapd/info" "${uc_initrd_feature_snap_bootstratp}"', + "cp ${UC_INITRD_DEB}/usr/lib/snapd/info ${SNAPCRAFT_PART_INSTALL}/snapd-info", +] + +_clean_old_initrd_cmd = [ + "if compgen -G ${SNAPCRAFT_PART_INSTALL}/initrd.img* > /dev/null; then", + "\trm -rf ${SNAPCRAFT_PART_INSTALL}/initrd.img*", + "fi", +] + +_initrd_check_firmware_stage = [ + '[ ! -d "${SNAPCRAFT_STAGE}/firmware" ] && echo -e "firmware directory ' + "${SNAPCRAFT_STAGE}/firmware does not exist, consider using " + 'initrd-stage-firmware: true/false option" && exit 1' +] + +_initrd_check_firmware_part = [ + '[ ! -d "${SNAPCRAFT_PART_INSTALL}/lib/firmware" ] && echo -e "firmware directory ' + "${SNAPCRAFT_PART_INSTALL}/lib/firmware does not exist, consider using " + 'initrd-stage-firmware: true/false option" && exit 1' +] + +_initrd_tool_cmd = [ + "ubuntu_core_initramfs=${UC_INITRD_DEB}/usr/bin/ubuntu-core-initramfs", +] + +_update_initrd_compression_cmd = [ + 'echo "Updating compression command to be used for initrd"', + "sed -i 's/lz4 -9 -l/lz4 -9 -l/g' ${ubuntu_core_initramfs}", +] + +_update_initrd_compression_gz_cmd = [ + 'echo "Updating compression command to be used for initrd"', + "sed -i 's/lz4 -9 -l/gzip -7/g' ${ubuntu_core_initramfs}", +] + +_initrd_tool_workaroud_cmd = [ + "for feature in kernel-modules snap-bootstrap uc-firmware uc-overlay", + "do", + " ".join( + [ + '\tlink_files "${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/${feature}"', + '"*"', + '"${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/main"', + ], + ), + "done", +] + +_create_inird_cmd = [ + " ".join( + [ + "${ubuntu_core_initramfs}", + "create-initrd", + "--root ${UC_INITRD_DEB}", + "--kernelver=${KERNEL_RELEASE}", + "--kerneldir ${SNAPCRAFT_STAGE}/modules/${KERNEL_RELEASE}", + "--firmwaredir ${SNAPCRAFT_PART_INSTALL}/lib/firmware", + "--skeleton ${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs", + "--output ${SNAPCRAFT_PART_INSTALL}/initrd.img", + ], + ), + "ln $(ls ${SNAPCRAFT_PART_INSTALL}/initrd.img*) ${SNAPCRAFT_PART_INSTALL}/initrd.img", +] + +_create_inird_stage_firmware_cmd = [ + " ".join( + [ + "${ubuntu_core_initramfs}", + "create-initrd", + "--root ${UC_INITRD_DEB}", + "--kernelver=${KERNEL_RELEASE}", + "--kerneldir ${SNAPCRAFT_STAGE}/modules/${KERNEL_RELEASE}", + "--firmwaredir ${SNAPCRAFT_STAGE}/firmware", + "--skeleton ${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs", + "--output ${SNAPCRAFT_PART_INSTALL}/initrd.img", + ], + ), + "ln $(ls ${SNAPCRAFT_PART_INSTALL}/initrd.img*) ${SNAPCRAFT_PART_INSTALL}/initrd.img", +] diff --git a/tests/legacy/unit/plugins/v2/test_kernel.py b/tests/legacy/unit/plugins/v2/test_kernel.py index 81dd38910f..04212a8736 100644 --- a/tests/legacy/unit/plugins/v2/test_kernel.py +++ b/tests/legacy/unit/plugins/v2/test_kernel.py @@ -42,13 +42,6 @@ class Kernelv2PluginProperties: kernel_compiler: str = None kernel_compiler_paths: [str] = None kernel_compiler_parameters: [str] = None - kernel_initrd_modules: [str] = None - kernel_initrd_configured_modules: [str] = None - kernel_initrd_firmware: [str] = None - kernel_initrd_compression: [str] = None - kernel_initrd_compression_options: [str] = None - kernel_initrd_overlay: str = None - kernel_initrd_addons: [str] = None kernel_enable_zfs_support: bool = False kernel_enable_perf: bool = False kernel_add_ppa: bool = False @@ -72,14 +65,6 @@ def _setup_test( kernelimagetarget="Image.gz", kernelwithfirmware=True, kerneldevicetrees=None, - kernelinitrdmodules=None, - kernelinitrdconfiguredmodules=None, - kernelinitrdstagefirmware=False, - kernelinitrdfirmware=None, - kernelinitrdcompression=None, - kernelinitrdcompressionoptions=None, - kernelinitrdoverlay=None, - kernelinitrdaddons=None, kernelcompiler=None, kernelcompilerpaths=None, kernelcompilerparameters=None, @@ -97,14 +82,6 @@ class Options(Kernelv2PluginProperties): kernel_image_target = kernelimagetarget kernel_with_firmware = kernelwithfirmware kernel_device_trees = kerneldevicetrees - kernel_initrd_modules = kernelinitrdmodules - kernel_initrd_configured_modules = kernelinitrdconfiguredmodules - kernel_initrd_stage_firmware = kernelinitrdstagefirmware - kernel_initrd_firmware = kernelinitrdfirmware - kernel_initrd_compression = kernelinitrdcompression - kernel_initrd_compression_options = kernelinitrdcompressionoptions - kernel_initrd_overlay = kernelinitrdoverlay - kernel_initrd_addons = kernelinitrdaddons kernel_compiler = kernelcompiler kernel_compiler_paths = kernelcompilerpaths kernel_compiler_parameters = kernelcompilerparameters @@ -173,53 +150,6 @@ def test_schema(self): "items": {"type": "string"}, "default": [], }, - "kernel-initrd-modules": { - "type": "array", - "minitems": 1, - "uniqueItems": True, - "items": {"type": "string"}, - "default": [], - }, - "kernel-initrd-configured-modules": { - "type": "array", - "minitems": 1, - "uniqueItems": True, - "items": {"type": "string"}, - "default": [], - }, - "kernel-initrd-stage-firmware": { - "type": "boolean", - "default": False, - }, - "kernel-initrd-firmware": { - "type": "array", - "minitems": 1, - "uniqueItems": True, - "items": {"type": "string"}, - "default": [], - }, - "kernel-initrd-compression": { - "type": "string", - "enum": ["lz4", "xz", "gz", "zstd"], - }, - "kernel-initrd-compression-options": { - "type": "array", - "minitems": 1, - "uniqueItems": True, - "items": {"type": "string"}, - "default": [], - }, - "kernel-initrd-overlay": { - "type": "string", - "default": "", - }, - "kernel-initrd-addons": { - "type": "array", - "minitems": 1, - "uniqueItems": True, - "items": {"type": "string"}, - "default": [], - }, "kernel-compiler": { "type": "string", "default": "", @@ -246,10 +176,6 @@ def test_schema(self): "type": "boolean", "default": False, }, - "kernel-add-ppa": { - "type": "boolean", - "default": True, - }, "kernel-use-llvm": { "oneOf": [{"type": "boolean"}, {"type": "string"}], "default": False, @@ -271,50 +197,8 @@ def test_get_base_build_packages(self): "gcc", "cmake", "cryptsetup", - "dracut-core", - "kmod", - "kpartx", - "lz4", - "systemd", - }, - ) - - def test_get_base_build_packages_lz4(self): - plugin = self._setup_test(kernelinitrdcompression="lz4") - self.assertEqual( - plugin.get_build_packages(), - { - "bc", - "binutils", - "debhelper", - "fakeroot", - "gcc", - "cmake", - "cryptsetup", - "dracut-core", - "kmod", - "kpartx", - "lz4", - "systemd", - }, - ) - - def test_get_base_build_packages_xz(self): - plugin = self._setup_test(kernelinitrdcompression="xz") - self.assertEqual( - plugin.get_build_packages(), - { - "bc", - "binutils", - "debhelper", - "fakeroot", - "gcc", - "cmake", - "cryptsetup", - "dracut-core", "kmod", "kpartx", - "xz-utils", "systemd", }, ) @@ -331,10 +215,8 @@ def test_get_base_build_packages_zfs(self): "gcc", "cmake", "cryptsetup", - "dracut-core", "kmod", "kpartx", - "lz4", "systemd", "autoconf", "automake", @@ -356,10 +238,8 @@ def test_get_base_build_packages_zfs_cross(self): "gcc", "cmake", "cryptsetup", - "dracut-core", "kmod", "kpartx", - "lz4", "systemd", "autoconf", "automake", @@ -388,17 +268,8 @@ def test_check_configuration_simple(self): self.assertEqual(opt.kernel_compiler, None) self.assertEqual(opt.kernel_compiler_paths, None) self.assertEqual(opt.kernel_compiler_parameters, None) - self.assertEqual(opt.kernel_initrd_modules, None) - self.assertEqual(opt.kernel_initrd_configured_modules, None) - self.assertEqual(opt.kernel_initrd_firmware, None) - self.assertEqual(opt.kernel_initrd_compression, None) - self.assertEqual(opt.kernel_initrd_compression_options, None) - self.assertEqual(opt.kernel_initrd_overlay, None) - self.assertEqual(opt.kernel_initrd_addons, None) self.assertFalse(opt.kernel_enable_zfs_support) self.assertFalse(opt.kernel_enable_perf) - self.assertFalse(opt.kernel_add_ppa) - self.assertFalse(opt.kernel_use_llvm) def test_check_configuration_kde_config(self): plugin = self._setup_test( @@ -418,16 +289,8 @@ def test_check_configuration_kde_config(self): self.assertIs(opt.kernel_compiler, None) self.assertIs(opt.kernel_compiler_paths, None) self.assertIs(opt.kernel_compiler_parameters, None) - self.assertIs(opt.kernel_initrd_modules, None) - self.assertIs(opt.kernel_initrd_configured_modules, None) - self.assertIs(opt.kernel_initrd_firmware, None) - self.assertIs(opt.kernel_initrd_compression, None) - self.assertIs(opt.kernel_initrd_compression_options, None) - self.assertIs(opt.kernel_initrd_overlay, None) - self.assertIs(opt.kernel_initrd_addons, None) self.assertFalse(opt.kernel_enable_zfs_support) self.assertFalse(opt.kernel_enable_perf) - self.assertFalse(opt.kernel_add_ppa) self.assertFalse(opt.kernel_use_llvm) def test_check_configuration_image_target(self): @@ -435,7 +298,6 @@ def test_check_configuration_image_target(self): kernelkdefconfig=["snappy_defconfig"], kernelimagetarget={"arm64": "Image", "armhf": "Image.gz"}, kernelwithfirmware=True, - kernelinitrdcompression="zstd", ) opt = plugin.options @@ -451,16 +313,8 @@ def test_check_configuration_image_target(self): self.assertIs(opt.kernel_compiler, None) self.assertIs(opt.kernel_compiler_paths, None) self.assertIs(opt.kernel_compiler_parameters, None) - self.assertIs(opt.kernel_initrd_modules, None) - self.assertIs(opt.kernel_initrd_configured_modules, None) - self.assertIs(opt.kernel_initrd_firmware, None) - self.assertEqual(opt.kernel_initrd_compression, "zstd") - self.assertIs(opt.kernel_initrd_compression_options, None) - self.assertIs(opt.kernel_initrd_overlay, None) - self.assertIs(opt.kernel_initrd_addons, None) self.assertFalse(opt.kernel_enable_zfs_support) self.assertFalse(opt.kernel_enable_perf) - self.assertFalse(opt.kernel_add_ppa) self.assertFalse(opt.kernel_use_llvm) def test_check_configuration_konfig_file(self): @@ -468,8 +322,6 @@ def test_check_configuration_konfig_file(self): kernelkconfigfile="arch/arm64/configs/snappy_defconfig", kernelwithfirmware=True, kernelkconfigs=["CONFIG_DEBUG_INFO=n", "CONFIG_DM_CRYPT=y"], - kernelinitrdcompression="lz4", - kernelinitrdcompressionoptions=["-9", "-l"], ) opt = plugin.options @@ -485,16 +337,8 @@ def test_check_configuration_konfig_file(self): self.assertIs(opt.kernel_compiler, None) self.assertIs(opt.kernel_compiler_paths, None) self.assertIs(opt.kernel_compiler_parameters, None) - self.assertIs(opt.kernel_initrd_modules, None) - self.assertIs(opt.kernel_initrd_configured_modules, None) - self.assertIs(opt.kernel_initrd_firmware, None) - self.assertEqual(opt.kernel_initrd_compression, "lz4") - self.assertEqual(opt.kernel_initrd_compression_options, ["-9", "-l"]) - self.assertIs(opt.kernel_initrd_overlay, None) - self.assertIs(opt.kernel_initrd_addons, None) self.assertFalse(opt.kernel_enable_zfs_support) self.assertFalse(opt.kernel_enable_perf) - self.assertFalse(opt.kernel_add_ppa) self.assertFalse(opt.kernel_use_llvm) def test_check_get_build_environment(self): @@ -507,7 +351,6 @@ def test_check_get_build_environment(self): "CROSS_COMPILE": "${SNAPCRAFT_ARCH_TRIPLET}-", "ARCH": plugin._kernel_arch, "DEB_ARCH": "${SNAPCRAFT_TARGET_ARCH}", - "UC_INITRD_DEB": "${SNAPCRAFT_PART_BUILD}/ubuntu-core-initramfs", "KERNEL_BUILD_ARCH_DIR": f"${{SNAPCRAFT_PART_BUILD}}/arch/{plugin._kernel_arch}/boot", "KERNEL_IMAGE_TARGET": plugin.kernel_image_target, }, @@ -527,7 +370,6 @@ def test_check_get_build_environment_compiler_paths_cross(self): "CROSS_COMPILE": "${SNAPCRAFT_ARCH_TRIPLET}-", "ARCH": "arm", "DEB_ARCH": "${SNAPCRAFT_TARGET_ARCH}", - "UC_INITRD_DEB": "${SNAPCRAFT_PART_BUILD}/ubuntu-core-initramfs", "KERNEL_BUILD_ARCH_DIR": "${SNAPCRAFT_PART_BUILD}/arch/arm/boot", "KERNEL_IMAGE_TARGET": "Image.gz", "PATH": "${SNAPCRAFT_STAGE}/gcc-11/bin:${PATH}", @@ -546,7 +388,6 @@ def test_check_get_build_environment_compiler_paths(self): "CROSS_COMPILE": "${SNAPCRAFT_ARCH_TRIPLET}-", "ARCH": plugin._kernel_arch, "DEB_ARCH": "${SNAPCRAFT_TARGET_ARCH}", - "UC_INITRD_DEB": "${SNAPCRAFT_PART_BUILD}/ubuntu-core-initramfs", "KERNEL_BUILD_ARCH_DIR": f"${{SNAPCRAFT_PART_BUILD}}/arch/{plugin._kernel_arch}/boot", "KERNEL_IMAGE_TARGET": plugin.kernel_image_target, "PATH": "${SNAPCRAFT_STAGE}/gcc-11/bin:${SNAPCRAFT_STAGE}/gcc-11/sbin:${PATH}", @@ -560,13 +401,6 @@ def test_check_get_build_command(self): plugin.get_build_environment() build_commands = plugin.get_build_commands() assert _is_sub_array(build_commands, _determine_kernel_src) - assert _is_sub_array(build_commands, _initrd_modules_empty_cmd) - assert _is_sub_array(build_commands, _initrd_configured_modules_empty_cmd) - assert _is_sub_array(build_commands, _link_files_fnc) - assert _is_sub_array(build_commands, _download_initrd_fnc) - assert _is_sub_array(build_commands, _get_initrd_cmd) - assert _is_sub_array(build_commands, _download_snapd_fnc) - assert _is_sub_array(build_commands, _get_snapd_cmd) assert not _is_sub_array(build_commands, _clone_zfs_cmd) assert _is_sub_array(build_commands, _clean_old_build_cmd) assert _is_sub_array(build_commands, _prepare_config_cmd) @@ -580,19 +414,6 @@ def test_check_get_build_command(self): assert _is_sub_array(build_commands, _install_kernel_cmd) assert _is_sub_array(build_commands, _parse_kernel_release_cmd) - assert _is_sub_array(build_commands, _install_initrd_modules_cmd) - assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) - assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) - assert not _is_sub_array(build_commands, _install_initrd_firmware_cmd) - assert not _is_sub_array(build_commands, _install_initrd_addons_cmd) - assert not _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) - assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) - assert _is_sub_array(build_commands, _clean_old_initrd_cmd) - assert _is_sub_array(build_commands, _initrd_check_firmware_part) - assert _is_sub_array(build_commands, _initrd_tool_cmd) - assert not _is_sub_array(build_commands, _update_initrd_compression_cmd) - assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) - assert _is_sub_array(build_commands, _create_inird_cmd) assert _is_sub_array(build_commands, _install_config_cmd) assert not _is_sub_array(build_commands, _build_zfs_cmd) assert not _is_sub_array(build_commands, _build_perf_cmd) @@ -606,28 +427,12 @@ def test_check_get_build_command_defconfig_configs_no_firmware_lz4(self): kernelimagetarget="Image", kernelwithfirmware=False, kernelkconfigs=["CONFIG_DEBUG_INFO=n", "CONFIG_DM_CRYPT=y"], - kernelinitrdcompression="lz4", - kernelinitrdcompressionoptions=["-9", "-l"], - kernelinitrdmodules=["dm-crypt", "slimbus"], - kernelinitrdstagefirmware=True, - kernelinitrdfirmware=["firmware/for/wifi", "firmware/for/webcam"], - kernelinitrdaddons=[ - "usr/bin/cryptsetup", - "usr/lib/my-arch/libcrypto.so", - ], ) # we need to get build environment plugin.get_build_environment() build_commands = plugin.get_build_commands() assert _is_sub_array(build_commands, _determine_kernel_src) - assert _is_sub_array(build_commands, _initrd_modules_cmd) - assert _is_sub_array(build_commands, _initrd_configured_modules_empty_cmd) - assert _is_sub_array(build_commands, _link_files_fnc) - assert _is_sub_array(build_commands, _download_initrd_fnc) - assert _is_sub_array(build_commands, _get_initrd_cmd) - assert _is_sub_array(build_commands, _download_snapd_fnc) - assert _is_sub_array(build_commands, _get_snapd_cmd) assert not _is_sub_array(build_commands, _clone_zfs_cmd) assert _is_sub_array(build_commands, _clean_old_build_cmd) assert _is_sub_array(build_commands, _prepare_config_defconfig_cmd) @@ -644,19 +449,6 @@ def test_check_get_build_command_defconfig_configs_no_firmware_lz4(self): assert _is_sub_array(build_commands, _install_kernel_no_firmware_clang_cmd) assert _is_sub_array(build_commands, _parse_kernel_release_cmd) - assert _is_sub_array(build_commands, _install_initrd_modules_cmd) - assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) - assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) - assert _is_sub_array(build_commands, _install_initrd_firmware_cmd) - assert _is_sub_array(build_commands, _install_initrd_addons_cmd) - assert not _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) - assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) - assert _is_sub_array(build_commands, _clean_old_initrd_cmd) - assert _is_sub_array(build_commands, _initrd_check_firmware_stage) - assert _is_sub_array(build_commands, _initrd_tool_cmd) - assert _is_sub_array(build_commands, _update_initrd_compression_cmd) - assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) - assert _is_sub_array(build_commands, _create_inird_stage_firmware_cmd) assert _is_sub_array(build_commands, _install_config_cmd) assert not _is_sub_array(build_commands, _build_zfs_cmd) assert not _is_sub_array(build_commands, _build_perf_cmd) @@ -677,13 +469,6 @@ def test_check_get_build_command_unknown_compiler(self): assert "Only other 'supported' compiler is clang" in cm.output[1] assert "hopefully you know what you are doing" in cm.output[2] assert _is_sub_array(build_commands, _determine_kernel_src) - assert _is_sub_array(build_commands, _initrd_modules_empty_cmd) - assert _is_sub_array(build_commands, _initrd_configured_modules_empty_cmd) - assert _is_sub_array(build_commands, _link_files_fnc) - assert _is_sub_array(build_commands, _download_initrd_fnc) - assert _is_sub_array(build_commands, _get_initrd_cmd) - assert _is_sub_array(build_commands, _download_snapd_fnc) - assert _is_sub_array(build_commands, _get_snapd_cmd) assert not _is_sub_array(build_commands, _clone_zfs_cmd) assert _is_sub_array(build_commands, _clean_old_build_cmd) assert _is_sub_array(build_commands, _prepare_config_custom_cc_cmd) @@ -697,19 +482,6 @@ def test_check_get_build_command_unknown_compiler(self): assert _is_sub_array(build_commands, _install_kernel_custom_cc_cmd) assert _is_sub_array(build_commands, _parse_kernel_release_cmd) - assert _is_sub_array(build_commands, _install_initrd_modules_cmd) - assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) - assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) - assert not _is_sub_array(build_commands, _install_initrd_firmware_cmd) - assert not _is_sub_array(build_commands, _install_initrd_addons_cmd) - assert not _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) - assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) - assert _is_sub_array(build_commands, _clean_old_initrd_cmd) - assert _is_sub_array(build_commands, _initrd_check_firmware_part) - assert _is_sub_array(build_commands, _initrd_tool_cmd) - assert not _is_sub_array(build_commands, _update_initrd_compression_cmd) - assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) - assert _is_sub_array(build_commands, _create_inird_cmd) assert _is_sub_array(build_commands, _install_config_cmd) assert not _is_sub_array(build_commands, _build_zfs_cmd) assert not _is_sub_array(build_commands, _build_perf_cmd) @@ -723,23 +495,12 @@ def test_check_get_build_command_config_flavour_configs(self): kernelwithfirmware=False, kernelenablezfssupport=True, kernelenableperf=True, - kernelinitrdmodules=["dm-crypt", "slimbus"], - kernelinitrdconfiguredmodules=["libarc4"], - kernelinitrdoverlay="my-overlay", - kernelinitrdcompression="gz", ) # we need to get build environment plugin.get_build_environment() build_commands = plugin.get_build_commands() assert _is_sub_array(build_commands, _determine_kernel_src) - assert _is_sub_array(build_commands, _initrd_modules_cmd) - assert _is_sub_array(build_commands, _initrd_configured_modules_cmd) - assert _is_sub_array(build_commands, _link_files_fnc) - assert _is_sub_array(build_commands, _download_initrd_fnc) - assert _is_sub_array(build_commands, _get_initrd_cmd) - assert _is_sub_array(build_commands, _download_snapd_fnc) - assert _is_sub_array(build_commands, _get_snapd_cmd) assert _is_sub_array(build_commands, _clone_zfs_cmd) assert _is_sub_array(build_commands, _clean_old_build_cmd) assert _is_sub_array(build_commands, _prepare_config_flavour_cmd) @@ -750,19 +511,6 @@ def test_check_get_build_command_config_flavour_configs(self): assert _is_sub_array(build_commands, _install_kernel_no_dtbs_no_firmware_cmd) assert _is_sub_array(build_commands, _parse_kernel_release_cmd) assert _is_sub_array(build_commands, _install_dtbs_cmd) - assert _is_sub_array(build_commands, _install_initrd_modules_cmd) - assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) - assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) - assert not _is_sub_array(build_commands, _install_initrd_firmware_cmd) - assert not _is_sub_array(build_commands, _install_initrd_addons_cmd) - assert _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) - assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) - assert _is_sub_array(build_commands, _clean_old_initrd_cmd) - assert _is_sub_array(build_commands, _initrd_check_firmware_part) - assert _is_sub_array(build_commands, _initrd_tool_cmd) - assert _is_sub_array(build_commands, _update_initrd_compression_gz_cmd) - assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) - assert _is_sub_array(build_commands, _create_inird_cmd) assert _is_sub_array(build_commands, _install_config_cmd) assert _is_sub_array(build_commands, _build_zfs_cmd) assert _is_sub_array(build_commands, _build_perf_cmd) @@ -775,9 +523,6 @@ def test_check_get_build_command_cross(self): kernelimagetarget={"arm64": "Image", "armhf": "Image.gz"}, kernelenablezfssupport=True, kernelenableperf=True, - kernelinitrdmodules=["dm-crypt", "slimbus"], - kernelinitrdconfiguredmodules=["libarc4"], - kernelinitrdoverlay="my-overlay", arch="armv7l", ) @@ -785,13 +530,6 @@ def test_check_get_build_command_cross(self): plugin.get_build_environment() build_commands = plugin.get_build_commands() assert _is_sub_array(build_commands, _determine_kernel_src) - assert _is_sub_array(build_commands, _initrd_modules_cmd) - assert _is_sub_array(build_commands, _initrd_configured_modules_cmd) - assert _is_sub_array(build_commands, _link_files_fnc) - assert _is_sub_array(build_commands, _download_initrd_fnc) - assert _is_sub_array(build_commands, _get_initrd_armhf_cmd) - assert _is_sub_array(build_commands, _download_snapd_fnc) - assert _is_sub_array(build_commands, _get_snapd_armhf_cmd) assert _is_sub_array(build_commands, _clone_zfs_cmd) assert _is_sub_array(build_commands, _clean_old_build_cmd) assert _is_sub_array(build_commands, _prepare_config_flavour_cmd) @@ -802,19 +540,6 @@ def test_check_get_build_command_cross(self): assert _is_sub_array(build_commands, _install_kernel_armhf_cmd) assert _is_sub_array(build_commands, _parse_kernel_release_cmd) assert not _is_sub_array(build_commands, _install_dtbs_cmd) - assert _is_sub_array(build_commands, _install_initrd_modules_cmd) - assert _is_sub_array(build_commands, _configure_initrd_modules_cmd) - assert _is_sub_array(build_commands, _initrd_overlay_features_cmd) - assert not _is_sub_array(build_commands, _install_initrd_firmware_cmd) - assert not _is_sub_array(build_commands, _install_initrd_addons_cmd) - assert _is_sub_array(build_commands, _intatll_initrd_overlay_cmd) - assert _is_sub_array(build_commands, _prepare_ininird_features_cmd) - assert _is_sub_array(build_commands, _clean_old_initrd_cmd) - assert _is_sub_array(build_commands, _initrd_check_firmware_part) - assert _is_sub_array(build_commands, _initrd_tool_cmd) - assert not _is_sub_array(build_commands, _update_initrd_compression_cmd) - assert _is_sub_array(build_commands, _initrd_tool_workaroud_cmd) - assert _is_sub_array(build_commands, _create_inird_cmd) assert _is_sub_array(build_commands, _install_config_cmd) assert _is_sub_array(build_commands, _build_zfs_cmd) assert _is_sub_array(build_commands, _build_perf_armhf_cmd) @@ -1012,12 +737,10 @@ def test_use_llvm(self): "gcc", "cmake", "cryptsetup", - "dracut-core", "kmod", "kpartx", "lld", "llvm", - "lz4", "systemd", }, ) @@ -1045,12 +768,10 @@ def test_use_llvm_specific_version(self): "gcc", "cmake", "cryptsetup", - "dracut-core", "kmod", "kpartx", f"lld{version}", f"llvm{version}", - "lz4", "systemd", }, ) @@ -1125,146 +846,6 @@ def _is_sub_array(array, sub_array): 'echo "KERNEL_SRC=${KERNEL_SRC}"', ] -_initrd_modules_empty_cmd = ['initrd_installed_kernel_modules=""'] - -_initrd_modules_cmd = ['initrd_installed_kernel_modules="dm-crypt slimbus"'] - -_initrd_configured_modules_empty_cmd = ['initrd_configured_kernel_modules=""'] - -_initrd_configured_modules_cmd = ['initrd_configured_kernel_modules="libarc4"'] - -_link_files_fnc = [ - textwrap.dedent( - """ - # link files, accept wild cards - # 1: reference dir, 2: file(s) including wild cards, 3: dst dir - link_files() { - if [ "${2}" = "*" ]; then - for f in $(ls ${1}) - do - link_files "${1}" "${f}" "${3}" - done - return 0 - fi - if [ -d "${1}/${2}" ]; then - for f in $(ls ${1}/${2}) - do - link_files "${1}" "${2}/${f}" "${3}" - done - return 0 - fi - - local found="" - for f in $(ls ${1}/${2}) - do - if [[ -L "${f}" ]]; then - local rel_path=$( realpath --no-symlinks --relative-to=${1} ${f} ) - else - local rel_path=$( realpath -se --relative-to=${1} ${f} ) - fi - local dir_path=$(dirname ${rel_path}) - mkdir -p ${3}/${dir_path} - echo "installing ${f} to ${3}/${dir_path}" - ln -f ${f} ${3}/${dir_path} - found="yes" - done - if [ "yes" = "${found}" ]; then - return 0 - else - return 1 - fi - } - """ - ) -] - -_download_initrd_fnc = [ - textwrap.dedent( - """ - # Helper to download code initrd deb package - # 1: arch, 2: output dir - download_core_initrd() { - apt-get download ubuntu-core-initramfs:${1} - # unpack dep to the target dir - dpkg -x ubuntu-core-initramfs_*.deb ${2} - } - """ - ) -] - -_get_initrd_cmd = [ - textwrap.dedent( - """ - echo "Getting ubuntu-core-initrd...." - # only download u-c-initrd deb if needed - if [ ! -e ${{UC_INITRD_DEB}} ]; then - download_core_initrd {arch} ${{UC_INITRD_DEB}} - fi - """.format( - arch=_DEB_ARCH_TRANSLATIONS[platform.machine()] - ) - ) -] - - -_get_initrd_armhf_cmd = [ - textwrap.dedent( - """ - echo "Getting ubuntu-core-initrd...." - # only download u-c-initrd deb if needed - if [ ! -e ${{UC_INITRD_DEB}} ]; then - download_core_initrd {arch} ${{UC_INITRD_DEB}} - fi - """.format( - arch="armhf" - ) - ) -] - -_download_snapd_fnc = [ - textwrap.dedent( - """ - # Helper to download snap-bootstrap from snapd deb package - # 1: arch, 2: output dir - download_snap_bootstrap() { - apt-get download snapd:${1} - # unpack dep to the target dir - dpkg -x snapd_*.deb ${2} - } - """ - ) -] - -_get_snapd_cmd = [ - textwrap.dedent( - """ - echo "Getting snapd deb for snap bootstrap..." - # only download again if files does not exist, otherwise - # assume we are re-running build - if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then - download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} - fi - """.format( - arch=_DEB_ARCH_TRANSLATIONS[platform.machine()] - ) - ) -] - -_get_snapd_armhf_cmd = [ - textwrap.dedent( - """ - echo "Getting snapd deb for snap bootstrap..." - # only download again if files does not exist, otherwise - # assume we are re-running build - if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then - download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} - fi - """.format( - arch="armhf" - ) - ) -] - _clean_old_build_cmd = [ textwrap.dedent( """ @@ -1369,8 +950,6 @@ def _is_sub_array(array, sub_array): inspect.getfile(check_new_config), "check_new_config", "${SNAPCRAFT_PART_BUILD}/.config", - "${initrd_installed_kernel_modules}", - "${initrd_configured_kernel_modules}", ], ) @@ -1581,176 +1160,6 @@ def _is_sub_array(array, sub_array): "ln -f ${KERNEL_BUILD_ARCH_DIR}/dts/pi/cm4.dtb ${SNAPCRAFT_PART_INSTALL}/dtbs/cm4.dtb", ] -_install_initrd_modules_cmd = [ - 'echo "Installing ko modules to initrd..."', - 'install_modules=""', - 'echo "Gathering module dependencies..."', - 'install_modules=""', - "uc_initrd_feature_kernel_modules=${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/kernel-modules", - "mkdir -p ${uc_initrd_feature_kernel_modules}", - "initramfs_ko_modules_conf=${uc_initrd_feature_kernel_modules}/extra-modules.conf", - "touch ${initramfs_ko_modules_conf}", - "for m in ${initrd_installed_kernel_modules} ${initrd_configured_kernel_modules}", - "do", - "\techo ${m} >> ${initramfs_ko_modules_conf}", - "done", -] - -_configure_initrd_modules_cmd = [ - " ".join( - [ - "[ -e ${initramfs_ko_modules_conf} ]", - "&&", - "sort -fu ${initramfs_ko_modules_conf} -o ${initramfs_ko_modules_conf}", - ], - ), - 'echo "Configuring ubuntu-core-initramfs.conf with supported modules"', - 'echo "If module is not included in initrd, do not include it"', - "initramfs_conf_dir=${uc_initrd_feature_kernel_modules}/usr/lib/modules-load.d", - "mkdir -p ${initramfs_conf_dir}", - "initramfs_conf=${initramfs_conf_dir}/ubuntu-core-initramfs.conf", - 'echo "# configures modules" > ${initramfs_conf}', - "for m in $(cat ${initramfs_ko_modules_conf})", - "do", - " ".join( - [ - "\tif", - "[ -n", - '"$(modprobe -n -q --show-depends', - "-d ${SNAPCRAFT_PART_INSTALL}", - '-S "${KERNEL_RELEASE}"', - '${m})" ]; then', - ], - ), - "\t\techo ${m} >> ${initramfs_conf}", - "\tfi", - "done", -] - -_initrd_overlay_features_cmd = [ - "uc_initrd_feature_firmware=${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/uc-firmware", - "mkdir -p ${uc_initrd_feature_firmware}", - "uc_initrd_feature_overlay=${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/uc-overlay", - "mkdir -p ${uc_initrd_feature_overlay}", -] - -_install_initrd_firmware_cmd = [ - 'echo "Installing initrd overlay firmware..."', - "for f in firmware/for/wifi firmware/for/webcam", - "do", - '\tif ! link_files "${SNAPCRAFT_PART_INSTALL}" "${f}" "${uc_initrd_feature_firmware}/lib" ; then', - '\t\tif ! link_files "${SNAPCRAFT_STAGE}" "${f}" "${uc_initrd_feature_firmware}/lib" ; then', - '\t\t\techo "Missing firmware [${f}], ignoring it"', - "\t\tfi", - "\tfi", - "done", -] - -_install_initrd_addons_cmd = [ - 'echo "Installing initrd addons..."', - "for a in usr/bin/cryptsetup usr/lib/my-arch/libcrypto.so", - "do", - '\techo "Copy overlay: ${a}"', - '\tlink_files "${SNAPCRAFT_STAGE}" "${a}" "${uc_initrd_feature_overlay}"', - "done", -] - -_intatll_initrd_overlay_cmd = [ - 'link_files "${SNAPCRAFT_STAGE}/my-overlay" "" "${uc_initrd_feature_overlay}"' -] - -_prepare_ininird_features_cmd = [ - 'echo "Preparing snap-boostrap initrd feature..."', - "uc_initrd_feature_snap_bootstratp=${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/snap-bootstrap", - "mkdir -p ${uc_initrd_feature_snap_bootstratp}", - " ".join( - [ - "link_files", - '"${UC_INITRD_DEB}" "usr/lib/snapd/snap-bootstrap"', - '"${uc_initrd_feature_snap_bootstratp}"', - ], - ), - 'link_files "${UC_INITRD_DEB}" "usr/lib/snapd/info" "${uc_initrd_feature_snap_bootstratp}"', - "cp ${UC_INITRD_DEB}/usr/lib/snapd/info ${SNAPCRAFT_PART_INSTALL}/snapd-info", -] - -_clean_old_initrd_cmd = [ - "if compgen -G ${SNAPCRAFT_PART_INSTALL}/initrd.img* > /dev/null; then", - "\trm -rf ${SNAPCRAFT_PART_INSTALL}/initrd.img*", - "fi", -] - -_initrd_check_firmware_stage = [ - '[ ! -d "${SNAPCRAFT_STAGE}/firmware" ] && echo -e "firmware directory ' - "${SNAPCRAFT_STAGE}/firmware does not exist, consider using " - 'kernel-initrd-stage-firmware: true/false option" && exit 1' -] - -_initrd_check_firmware_part = [ - '[ ! -d "${SNAPCRAFT_PART_INSTALL}/lib/firmware" ] && echo -e "firmware directory ' - "${SNAPCRAFT_PART_INSTALL}/lib/firmware does not exist, consider using " - 'kernel-initrd-stage-firmware: true/false option" && exit 1' -] - -_initrd_tool_cmd = [ - "ubuntu_core_initramfs=${UC_INITRD_DEB}/usr/bin/ubuntu-core-initramfs", -] - -_update_initrd_compression_cmd = [ - 'echo "Updating compression command to be used for initrd"', - "sed -i 's/lz4 -9 -l/lz4 -9 -l/g' ${ubuntu_core_initramfs}", -] - -_update_initrd_compression_gz_cmd = [ - 'echo "Updating compression command to be used for initrd"', - "sed -i 's/lz4 -9 -l/gzip -7/g' ${ubuntu_core_initramfs}", -] - -_initrd_tool_workaroud_cmd = [ - "for feature in kernel-modules snap-bootstrap uc-firmware uc-overlay", - "do", - " ".join( - [ - '\tlink_files "${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/${feature}"', - '"*"', - '"${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs/main"', - ], - ), - "done", -] - -_create_inird_cmd = [ - " ".join( - [ - "${ubuntu_core_initramfs}", - "create-initrd", - "--root ${UC_INITRD_DEB}", - "--kernelver=${KERNEL_RELEASE}", - "--kerneldir ${SNAPCRAFT_PART_INSTALL}/lib/modules/${KERNEL_RELEASE}", - "--firmwaredir ${SNAPCRAFT_PART_INSTALL}/lib/firmware", - "--skeleton ${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs", - "--output ${SNAPCRAFT_PART_INSTALL}/initrd.img", - ], - ), - "ln $(ls ${SNAPCRAFT_PART_INSTALL}/initrd.img*) ${SNAPCRAFT_PART_INSTALL}/initrd.img", -] - -_create_inird_stage_firmware_cmd = [ - " ".join( - [ - "${ubuntu_core_initramfs}", - "create-initrd", - "--root ${UC_INITRD_DEB}", - "--kernelver=${KERNEL_RELEASE}", - "--kerneldir ${SNAPCRAFT_PART_INSTALL}/lib/modules/${KERNEL_RELEASE}", - "--firmwaredir ${SNAPCRAFT_STAGE}/firmware", - "--skeleton ${UC_INITRD_DEB}/usr/lib/ubuntu-core-initramfs", - "--output ${SNAPCRAFT_PART_INSTALL}/initrd.img", - ], - ), - "ln $(ls ${SNAPCRAFT_PART_INSTALL}/initrd.img*) ${SNAPCRAFT_PART_INSTALL}/initrd.img", -] - _install_config_cmd = [ 'echo "Installing kernel config..."', "ln -f ${SNAPCRAFT_PART_BUILD}/.config ${SNAPCRAFT_PART_INSTALL}/config-${KERNEL_RELEASE}",