Skip to content

Commit

Permalink
Move Raid Luks and Integrity context manager
Browse files Browse the repository at this point in the history
Change the RaidDevice, LuksDevice and IntegrityDevice classes
to context manager. All code using this classes moved to:

    with RaidDevice(...) as raid:
        raid.some_member()

    with LuksDevice(...) as luks:
        luks.some_member()

    with IntegrityDevice(...) as integrity:
        integrity.some_member()

In addition to the context manager move, the use of them
has been refactored into a nested structure of supported
device layouts inside the disk builder.

This is related to Issue #2412
  • Loading branch information
schaefi committed Feb 4, 2024
1 parent bd37e2d commit e545e9a
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 90 deletions.
81 changes: 62 additions & 19 deletions kiwi/builder/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,26 +349,66 @@ def create_disk(self) -> Result:
disk.public_partition_id_map['kiwi_RootPart'] = \
disk.public_partition_id_map['kiwi_ROPart']

# create raid on current root device if requested
if self.mdraid:
raid_root = self._raid_instance(device_map)
device_map = self._map_raid(device_map, disk, raid_root)

# create integrity on current root device if requested
if self.integrity:
integrity_root = self._integrity_instance(device_map)
device_map = self._map_integrity(device_map, integrity_root)

# create luks on current root device if requested
if self.luks is not None:
luks_root = self._luks_instance(device_map)
device_map = self._map_luks(device_map, luks_root)

# create system layout for root system
self._create_system_instance(device_map)

# sync system data, configure system, setup loader and initrd
self._process_build(device_map, disk)
# create raid on root dev
with self._raid_instance(device_map) as raid_root:
device_map = self._map_raid(
device_map, disk, raid_root
)
if self.integrity:
# create integrity on raid root dev
with self._integrity_instance(
device_map
) as integrity_root:
device_map = self._map_integrity(
device_map, integrity_root
)
if self.luks is not None:
with self._luks_instance(
device_map
) as luks_root:
# create luks on raid+integrity root dev
device_map = self._map_luks(
device_map, luks_root
)
# build bootable raid+integrity+luks disk
self._process_build(device_map, disk)
else:
# build bootable raid+integrity disk
self._process_build(device_map, disk)
elif self.luks is not None:
with self._luks_instance(device_map) as luks_root:
# create luks on raid root dev
device_map = self._map_luks(device_map, luks_root)
# build bootable raid+luks disk
self._process_build(device_map, disk)
else:
# build bootable raid disk
self._process_build(device_map, disk)
elif self.integrity:
# create integrity on root dev
with self._integrity_instance(device_map) as integrity_root:
device_map = self._map_integrity(
device_map, integrity_root
)
if self.luks is not None:
with self._luks_instance(device_map) as luks_root:
# create luks on integrity root dev
device_map = self._map_luks(device_map, luks_root)
# build bootable integrity+luks disk
self._process_build(device_map, disk)
else:
# build bootable integrity disk
self._process_build(device_map, disk)
elif self.luks is not None:
with self._luks_instance(device_map) as luks_root:
# create luks on root dev
device_map = self._map_luks(device_map, luks_root)
# build bootable luks disk
self._process_build(device_map, disk)
else:
# build bootable disk
self._process_build(device_map, disk)

# store image bundle_format in result
if self.bundle_format:
Expand Down Expand Up @@ -703,6 +743,9 @@ def _map_root_filesystem(
self.storage_map['system'] = filesystem

def _process_build(self, device_map: Dict, disk: Disk) -> None:
# create system layout for root system
self._create_system_instance(device_map)

# representing the entire image system except for the boot/ area
# which could live on another part of the disk
system: Optional[Union[FileSystemBase, VolumeManagerBase]] = \
Expand Down
15 changes: 9 additions & 6 deletions kiwi/storage/integrity_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def __init__(
self.integrity_metadata_file: Optional[IO[bytes]] = None
self.credentials = credentials

def __enter__(self):
return self

def get_device(self) -> Optional[MappedDevice]:
"""
Instance of MappedDevice providing the dm_integrity device
Expand Down Expand Up @@ -298,15 +301,15 @@ def _get_integrity_superblock(self) -> Dict[str, Union[str, List[str]]]:
integrity[entry[0]] = entry[1]
return integrity

def __del__(self):
def __exit__(self, exc_type, exc_value, traceback):
if self.integrity_device:
log.info('Cleaning up %s instance', type(self).__name__)
try:
Command.run(
['integritysetup', 'close', self.integrity_name]
)
except Exception:
log.warning(
'Shutdown of integrity map %s failed, %s still busy',
self.integrity_name, self.integrity_device
except Exception as issue:
log.error(
'Shutdown of integrity map {0}:{1} failed with {2}'.format(
self.integrity_name, self.integrity_device, issue
)
)
15 changes: 9 additions & 6 deletions kiwi/storage/luks_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def __init__(self, storage_provider: DeviceProvider) -> None:
]
}

def __enter__(self):
return self

def get_device(self) -> Optional[MappedDevice]:
"""
Instance of MappedDevice providing the luks device
Expand Down Expand Up @@ -208,15 +211,15 @@ def create_random_keyfile(filename: str) -> None:
keyfile.write(os.urandom(Defaults.get_luks_key_length()))
os.chmod(filename, 0o600)

def __del__(self):
def __exit__(self, exc_type, exc_value, traceback):
if self.luks_device:
log.info('Cleaning up %s instance', type(self).__name__)
try:
Command.run(
['cryptsetup', 'luksClose', self.luks_name]
)
except Exception:
log.warning(
'Shutdown of luks map %s failed, %s still busy',
self.luks_name, self.luks_device
except Exception as issue:
log.error(
'Shutdown of luks map {0}:{1} failed with: {2}'.format(
self.luks_name, self.luks_device, issue
)
)
15 changes: 9 additions & 6 deletions kiwi/storage/raid_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def __init__(self, storage_provider: DeviceProvider):
}
self.raid_device = None

def __enter__(self):
return self

def get_device(self) -> Optional[MappedDevice]:
"""
Instance of MappedDevice providing the raid device
Expand Down Expand Up @@ -123,15 +126,15 @@ def is_loop(self) -> bool:
"""
return self.storage_provider.is_loop()

def __del__(self):
def __exit__(self, exc_type, exc_value, traceback):
if self.raid_device:
log.info('Cleaning up %s instance', type(self).__name__)
try:
Command.run(
['mdadm', '--stop', self.raid_device]
)
except Exception:
log.warning(
'Shutdown of raid device failed, %s still busy',
self.raid_device
except Exception as issue:
log.error(
'Shutdown of raid device {0} failed with: {1}'.format(
self.raid_device, issue
)
)
Loading

0 comments on commit e545e9a

Please sign in to comment.