Skip to content

Commit

Permalink
DiskBuilder: use ExitStack to unmount partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
dcermak committed Jan 18, 2024
1 parent 06165c4 commit 2e3d190
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions kiwi/builder/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with kiwi. If not, see <http://www.gnu.org/licenses/>
#
from contextlib import ExitStack
import os
import logging
from typing import (
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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]
Expand Down

0 comments on commit 2e3d190

Please sign in to comment.