From 712aef971c90be9b90535e2f89ac3a4204770169 Mon Sep 17 00:00:00 2001 From: Ondrej Kubik Date: Wed, 8 Nov 2023 13:28:06 +0000 Subject: [PATCH] plugin: inird: download debs to source dir Signed-off-by: Ondrej Kubik --- snapcraft/parts/plugins/initrd_plugin.py | 13 ++++-- snapcraft/parts/plugins/kernel_plugin.py | 3 +- snapcraft_legacy/plugins/v2/_initrd_build.py | 43 ++++++++++++------- snapcraft_legacy/plugins/v2/initrd.py | 2 + tests/legacy/unit/plugins/v2/test_initrd.py | 44 +++++++++++++------- tests/unit/parts/plugins/test_initrd.py | 43 +++++++++++-------- 6 files changed, 93 insertions(+), 55 deletions(-) diff --git a/snapcraft/parts/plugins/initrd_plugin.py b/snapcraft/parts/plugins/initrd_plugin.py index 0d3c7023d88..fd858e0a325 100644 --- a/snapcraft/parts/plugins/initrd_plugin.py +++ b/snapcraft/parts/plugins/initrd_plugin.py @@ -114,11 +114,11 @@ from pydantic import root_validator from snapcraft.parts.plugins import kernel_plugin -from snapcraft_legacy.plugins.v2 import _initrd_build -from snapcraft_legacy.plugins.v2 import _kernel_build +from snapcraft_legacy.plugins.v2 import _initrd_build, _kernel_build logger = logging.getLogger(__name__) + class InitrdPluginProperties(plugins.PluginProperties, plugins.PluginModel): """The part properties used by the Initrd plugin.""" @@ -187,11 +187,15 @@ def __init__( target_arch = self._part_info.target_arch self._deb_arch = _kernel_build.get_deb_architecture(target_arch) if not self.options.initrd_kernel_image_target: - self.kernel_image_target = kernel_plugin.default_kernel_image_target[self._deb_arch] + self.kernel_image_target = kernel_plugin.default_kernel_image_target[ + self._deb_arch + ] elif isinstance(self.options.initrd_kernel_image_target, str): self.kernel_image_target = self.options.initrd_kernel_image_target elif self._deb_arch in self.options.initrd_kernel_image_target: - self.kernel_image_target = self.options.initrd_kernel_image_target[self._deb_arch] + self.kernel_image_target = self.options.initrd_kernel_image_target[ + self._deb_arch + ] @overrides def get_build_snaps(self) -> Set[str]: @@ -254,6 +258,7 @@ def get_build_commands(self) -> List[str]: initrd_default_compression="zstd -1 -T0", initrd_include_extra_modules_conf=True, initrd_tool_pass_root=False, + source_dir="${CRAFT_PART_SRC}", install_dir="${CRAFT_PART_INSTALL}", stage_dir="${CRAFT_STAGE}", ) diff --git a/snapcraft/parts/plugins/kernel_plugin.py b/snapcraft/parts/plugins/kernel_plugin.py index 66ba5fe02eb..e5cd3cef4e5 100644 --- a/snapcraft/parts/plugins/kernel_plugin.py +++ b/snapcraft/parts/plugins/kernel_plugin.py @@ -100,9 +100,8 @@ default_kernel_image_target = { "amd64": "bzImage", - "i386": "bzImage", "armhf": "zImage", - "arm64": "Image.gz", + "arm64": "Image", "powerpc": "uImage", "ppc64el": "vmlinux.strip", "s390x": "bzImage", diff --git a/snapcraft_legacy/plugins/v2/_initrd_build.py b/snapcraft_legacy/plugins/v2/_initrd_build.py index ed9011e22b8..d7e11655875 100644 --- a/snapcraft_legacy/plugins/v2/_initrd_build.py +++ b/snapcraft_legacy/plugins/v2/_initrd_build.py @@ -103,28 +103,33 @@ def _download_core_initrd_fnc_cmd() -> List[str]: cmd = textwrap.dedent( """ # Helper to download code initrd deb package - # 1: arch, 2: output dir + # 1: arch, 2: output dir 3: source dir download_core_initrd() { - apt-get download ubuntu-core-initramfs:${1} - # unpack dep to the target dir - dpkg -x ubuntu-core-initramfs_*.deb ${2} + # skip download if file already exist + if ! ls ${3}/ubuntu-core-initramfs_*.deb 1> /dev/null 2>&1; then + apt-get download ubuntu-core-initramfs:${1} + mv ubuntu-core-initramfs_*.deb ${3} + fi + # unpack dep to the target dir + dpkg -x ${3}/ubuntu-core-initramfs_*.deb ${2} } """ ) return [cmd] -def _download_generic_initrd_cmd(target_arch: str) -> List[str]: +def _download_generic_initrd_cmd(target_arch: str, source_dir: str) -> List[str]: """Download Ubuntu Core initrd deb.""" 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}} + download_core_initrd {arch} ${{UC_INITRD_DEB}} {source_dir} fi """.format( - arch=target_arch + arch=target_arch, + source_dir=source_dir ) ) return [cmd] @@ -135,18 +140,22 @@ def _download_snap_bootstrap_fnc_cmd() -> List[str]: cmd = textwrap.dedent( """ # Helper to download snap-bootstrap from snapd deb package - # 1: arch, 2: output dir + # 1: arch, 2: output dir 3: source dir download_snap_bootstrap() { - apt-get download snapd:${1} - # unpack dep to the target dir - dpkg -x snapd_*.deb ${2} + # skip download if file already exist + if ! ls ${3}/snapd_*.deb 1> /dev/null 2>&1; then + apt-get download snapd:${1} + mv snapd_*.deb ${3} + fi + # unpack dep to the target dir + dpkg -x ${3}/snapd_*.deb ${2} } """ ) return [cmd] -def _download_snap_bootstrap_cmd(target_arch: str) -> List[str]: +def _download_snap_bootstrap_cmd(target_arch: str, source_dir: str) -> List[str]: """Download snap-bootstrap deb.""" cmd = textwrap.dedent( """ @@ -154,10 +163,11 @@ def _download_snap_bootstrap_cmd(target_arch: str) -> List[str]: # 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}} + download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} {source_dir} fi """.format( - arch=target_arch + arch=target_arch, + source_dir=source_dir ) ) return [cmd] @@ -574,6 +584,7 @@ def get_build_commands( initrd_default_compression: str, initrd_include_extra_modules_conf: bool, initrd_tool_pass_root: bool, + source_dir: str, install_dir: str, stage_dir: str, ) -> List[str]: @@ -587,11 +598,11 @@ def get_build_commands( "", *_download_core_initrd_fnc_cmd(), "", - *_download_generic_initrd_cmd(target_arch=target_arch), + *_download_generic_initrd_cmd(target_arch=target_arch, source_dir=source_dir), "", *_download_snap_bootstrap_fnc_cmd(), "", - *_download_snap_bootstrap_cmd(target_arch=target_arch), + *_download_snap_bootstrap_cmd(target_arch=target_arch, source_dir=source_dir), "", *_get_initrd_install_command( initrd_compression=initrd_compression, diff --git a/snapcraft_legacy/plugins/v2/initrd.py b/snapcraft_legacy/plugins/v2/initrd.py index 76d53b817bd..409bcf0d265 100644 --- a/snapcraft_legacy/plugins/v2/initrd.py +++ b/snapcraft_legacy/plugins/v2/initrd.py @@ -228,10 +228,12 @@ def get_build_commands(self) -> List[str]: initrd_overlay=self.options.initrd_overlay, initrd_stage_firmware=self.options.initrd_stage_firmware, build_efi_image=False, + kernel_image_target="", initrd_ko_use_workaround=True, initrd_default_compression="lz4 -9 -l", initrd_include_extra_modules_conf=False, initrd_tool_pass_root=True, + source_dir="${SNAPCRAFT_PART_SRC}", install_dir="${SNAPCRAFT_PART_INSTALL}", stage_dir="${SNAPCRAFT_STAGE}", ) diff --git a/tests/legacy/unit/plugins/v2/test_initrd.py b/tests/legacy/unit/plugins/v2/test_initrd.py index 1a28df7c8ca..01d74b6629e 100755 --- a/tests/legacy/unit/plugins/v2/test_initrd.py +++ b/tests/legacy/unit/plugins/v2/test_initrd.py @@ -508,15 +508,21 @@ def _is_sub_array(array, sub_array): ) ] +_parts_source_dir = "${SNAPCRAFT_PART_SRC}" + _download_initrd_fnc = [ textwrap.dedent( """ # Helper to download code initrd deb package - # 1: arch, 2: output dir + # 1: arch, 2: output dir 3: source dir download_core_initrd() { - apt-get download ubuntu-core-initramfs:${1} - # unpack dep to the target dir - dpkg -x ubuntu-core-initramfs_*.deb ${2} + # skip download if file already exist + if ! ls ${3}/ubuntu-core-initramfs_*.deb 1> /dev/null 2>&1; then + apt-get download ubuntu-core-initramfs:${1} + mv ubuntu-core-initramfs_*.deb ${3} + fi + # unpack dep to the target dir + dpkg -x ${3}/ubuntu-core-initramfs_*.deb ${2} } """ ) @@ -528,10 +534,11 @@ def _is_sub_array(array, sub_array): 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}} + download_core_initrd {arch} ${{UC_INITRD_DEB}} {parts_source_dir} fi """.format( - arch=_DEB_ARCH_TRANSLATIONS[platform.machine()] + arch=_DEB_ARCH_TRANSLATIONS[platform.machine()], + parts_source_dir=_parts_source_dir, ) ) ] @@ -543,10 +550,10 @@ def _is_sub_array(array, sub_array): 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}} + download_core_initrd {arch} ${{UC_INITRD_DEB}} {parts_source_dir} fi """.format( - arch="armhf" + arch="armhf", parts_source_dir=_parts_source_dir ) ) ] @@ -555,11 +562,15 @@ def _is_sub_array(array, sub_array): textwrap.dedent( """ # Helper to download snap-bootstrap from snapd deb package - # 1: arch, 2: output dir + # 1: arch, 2: output dir 3: source dir download_snap_bootstrap() { - apt-get download snapd:${1} - # unpack dep to the target dir - dpkg -x snapd_*.deb ${2} + # skip download if file already exist + if ! ls ${3}/snapd_*.deb 1> /dev/null 2>&1; then + apt-get download snapd:${1} + mv snapd_*.deb ${3} + fi + # unpack dep to the target dir + dpkg -x ${3}/snapd_*.deb ${2} } """ ) @@ -572,10 +583,11 @@ def _is_sub_array(array, sub_array): # 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}} + download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} {parts_source_dir} fi """.format( - arch=_DEB_ARCH_TRANSLATIONS[platform.machine()] + arch=_DEB_ARCH_TRANSLATIONS[platform.machine()], + parts_source_dir=_parts_source_dir, ) ) ] @@ -587,10 +599,10 @@ def _is_sub_array(array, sub_array): # 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}} + download_snap_bootstrap {arch} ${{UC_INITRD_DEB}} {parts_source_dir} fi """.format( - arch="armhf" + arch="armhf", parts_source_dir=_parts_source_dir ) ) ] diff --git a/tests/unit/parts/plugins/test_initrd.py b/tests/unit/parts/plugins/test_initrd.py index 2127a62566a..bf41b87eb6d 100644 --- a/tests/unit/parts/plugins/test_initrd.py +++ b/tests/unit/parts/plugins/test_initrd.py @@ -335,7 +335,7 @@ def test_check_get_build_command_config_flavour_configs( "initrd-overlay": "my-overlay", "initrd-compression": "gz", "initrd-build-efi-image": "true", - "initrd-kernel-image-target": "Image" + "initrd-kernel-image-target": "Image", }, ) @@ -698,17 +698,22 @@ def _is_sub_array(array, sub_array): textwrap.dedent( """ # Helper to download code initrd deb package - # 1: arch, 2: output dir + # 1: arch, 2: output dir 3: source dir download_core_initrd() { - apt-get download ubuntu-core-initramfs:${1} - # unpack dep to the target dir - dpkg -x ubuntu-core-initramfs_*.deb ${2} + # skip download if file already exist + if ! ls ${3}/ubuntu-core-initramfs_*.deb 1> /dev/null 2>&1; then + apt-get download ubuntu-core-initramfs:${1} + mv ubuntu-core-initramfs_*.deb ${3} + fi + # unpack dep to the target dir + dpkg -x ${3}/ubuntu-core-initramfs_*.deb ${2} } """ ) ] _machine_arch = _DEB_ARCH_TRANSLATIONS[platform.machine()] +_parts_source_dir = "${CRAFT_PART_SRC}" _get_initrd_cmd = [ textwrap.dedent( @@ -716,7 +721,7 @@ def _is_sub_array(array, sub_array): echo "Getting ubuntu-core-initrd...." # only download u-c-initrd deb if needed if [ ! -e ${{UC_INITRD_DEB}} ]; then - download_core_initrd {_machine_arch} ${{UC_INITRD_DEB}} + download_core_initrd {_machine_arch} ${{UC_INITRD_DEB}} {_parts_source_dir} fi """ ) @@ -724,11 +729,11 @@ def _is_sub_array(array, sub_array): _get_initrd_armhf_cmd = [ textwrap.dedent( - """ + f""" echo "Getting ubuntu-core-initrd...." # only download u-c-initrd deb if needed - if [ ! -e ${UC_INITRD_DEB} ]; then - download_core_initrd armhf ${UC_INITRD_DEB} + if [ ! -e ${{UC_INITRD_DEB}} ]; then + download_core_initrd armhf ${{UC_INITRD_DEB}} {_parts_source_dir} fi """ ) @@ -738,11 +743,15 @@ def _is_sub_array(array, sub_array): textwrap.dedent( """ # Helper to download snap-bootstrap from snapd deb package - # 1: arch, 2: output dir + # 1: arch, 2: output dir 3: source dir download_snap_bootstrap() { - apt-get download snapd:${1} - # unpack dep to the target dir - dpkg -x snapd_*.deb ${2} + # skip download if file already exist + if ! ls ${3}/snapd_*.deb 1> /dev/null 2>&1; then + apt-get download snapd:${1} + mv snapd_*.deb ${3} + fi + # unpack dep to the target dir + dpkg -x ${3}/snapd_*.deb ${2} } """ ) @@ -755,7 +764,7 @@ def _is_sub_array(array, sub_array): # 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 {_machine_arch} ${{UC_INITRD_DEB}} + download_snap_bootstrap {_machine_arch} ${{UC_INITRD_DEB}} {_parts_source_dir} fi """ ) @@ -763,12 +772,12 @@ def _is_sub_array(array, sub_array): _get_snapd_armhf_cmd = [ textwrap.dedent( - """ + f""" 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 armhf ${UC_INITRD_DEB} + if [ ! -e ${{UC_INITRD_DEB}}/usr/lib/snapd ]; then + download_snap_bootstrap armhf ${{UC_INITRD_DEB}} {_parts_source_dir} fi """ )