From 2e3d19013a1c506004035e9096de3ece23e1f8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20=C4=8Cerm=C3=A1k?= Date: Thu, 18 Jan 2024 11:08:55 +0100 Subject: [PATCH] DiskBuilder: use ExitStack to unmount partitions --- kiwi/builder/disk.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/kiwi/builder/disk.py b/kiwi/builder/disk.py index 96dfc8370d7..24b3cb69974 100644 --- a/kiwi/builder/disk.py +++ b/kiwi/builder/disk.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # +from contextlib import ExitStack import os import logging from typing import ( @@ -482,7 +483,7 @@ def create_disk(self) -> Result: # sync system data, configure system, setup loader and initrd try: - self._build_main_system( + with self._build_main_system( device_map, disk, system, @@ -492,20 +493,11 @@ def create_disk(self) -> Result: system_custom_parts, luks_root, raid_root - ) + ): + pass finally: - for map_name in sorted(system_custom_parts.keys()): - system_custom_parts[map_name].umount() - if system_efi: - system_efi.umount() - if system_spare: - system_spare.umount() - if system_boot: - system_boot.umount() if self.volume_manager_name: system.umount_volumes() - elif system: - system.umount() # store image bundle_format in result if self.bundle_format: @@ -653,7 +645,7 @@ def _build_main_system( system_custom_parts: Dict[str, FileSystemBase], luks_root: Optional[LuksDevice] = None, raid_root: Optional[RaidDevice] = None - ) -> None: + ) -> ExitStack: # create swap on current root device if requested if self.swap_mbytes: with FileSystem.new( @@ -729,7 +721,7 @@ def _build_main_system( disk_system.call_pre_disk_script() # syncing system data to disk image - self._sync_system_to_image( + stack = self._sync_system_to_image( device_map, system, system_boot, system_efi, system_spare, system_custom_parts ) @@ -757,6 +749,8 @@ def _build_main_system( # set root filesystem properties self._setup_property_root_is_readonly_snapshot(system) + return stack + def _install_image_requested(self) -> bool: return bool( self.install_iso or self.install_stick or self.install_pxe @@ -1364,17 +1358,18 @@ def _sync_system_to_image( system_efi: Optional[FileSystemBase], system_spare: Optional[FileSystemBase], system_custom_parts: Dict[str, FileSystemBase] - ) -> None: + ) -> ExitStack: + stack = ExitStack() log.info('Syncing system to image') if system_spare: log.info('--> Syncing spare partition data') - system_spare.sync_data() + stack.push(system_spare.sync_data()) for map_name in sorted(system_custom_parts.keys()): system_custom_part = system_custom_parts[map_name] log.info('--> Syncing custom partition(s) data') if not system_custom_part.filename: - system_custom_part.sync_data() + stack.push(system_custom_part.sync_data()) if device_map.get(f'{map_name}clone1'): log.info( f'--> Dumping {map_name!r} clone data at extra partition' @@ -1390,13 +1385,13 @@ def _sync_system_to_image( if system_efi: log.info('--> Syncing EFI boot data to EFI partition') - system_efi.sync_data() + stack.push(system_efi.sync_data()) if system_boot: log.info('--> Syncing boot data at extra partition') - system_boot.sync_data( + stack.push(system_boot.sync_data( self._get_exclude_list_for_boot_data_sync() - ) + )) if device_map.get('bootclone1'): log.info( '--> Dumping boot clone data at extra partition' @@ -1529,9 +1524,11 @@ def _sync_system_to_image( self._get_clone_devices('rootclone', device_map) ) else: - system.sync_data( + umount = system.sync_data( self._get_exclude_list_for_root_data_sync(device_map) ) + if umount: + stack.push(umount) if device_map.get('rootclone1'): log.info( '--> Dumping root clone data at extra partition' @@ -1556,6 +1553,8 @@ def _sync_system_to_image( self.integrity_root.sign_integrity_metadata() self.integrity_root.write_integrity_metadata() + return stack + def _install_bootloader( self, device_map: Dict, disk, system: Union[FileSystemBase, VolumeManagerBase]