Skip to content

Commit

Permalink
Move LoopDevice class to context manager
Browse files Browse the repository at this point in the history
Change the LoopDevice class to be a context manager.
All code using LoopDevice was updated to the following
with statement:

with LoopDevice(...) as loop_provider:
    loop_provider.some_member()

This is related to Issue #2412
  • Loading branch information
schaefi committed Jan 9, 2024
1 parent bd442f6 commit 08f76de
Show file tree
Hide file tree
Showing 14 changed files with 511 additions and 476 deletions.
22 changes: 11 additions & 11 deletions doc/source/contributing/kiwi_from_python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ which contains your host `tmp` directory.
from kiwi.storage.loop_device import LoopDevice
from kiwi.filesystem import FileSystem
loop_provider = LoopDevice(
with LoopDevice(
filename='my_tmp.ext4', filesize_mbytes=100
)
loop_provider.create()
filesystem = FileSystem.new(
'ext4', loop_provider, '/tmp/'
)
filesystem.create_on_device(
label='TMP'
)
filesystem.sync_data()
) as loop_provider:
loop_provider.create()
filesystem = FileSystem.new(
'ext4', loop_provider, '/tmp/'
)
filesystem.create_on_device(
label='TMP'
)
filesystem.sync_data()
86 changes: 43 additions & 43 deletions kiwi/bootloader/config/systemd_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,49 +104,49 @@ def _create_embedded_fat_efi_image(self, path: str):
Command.run(
['sgdisk', '-n', ':1.0', '-t', '1:EF00', path]
)
loop_provider = LoopDevice(path)
loop_provider.create(overwrite=False)
disk = Disk('gpt', loop_provider)
disk.map_partitions()
disk.partitioner.partition_id = 1
disk._add_to_map('efi')
Command.run(
['mkdosfs', '-n', 'BOOT', disk.partition_map['efi']]
)
Path.create(f'{self.root_dir}/boot/efi')
efi_mount = MountManager(
device=disk.partition_map['efi'],
mountpoint=f'{self.root_dir}/boot/efi'
)
device_mount = MountManager(
device='/dev',
mountpoint=f'{self.root_dir}/dev'
)
proc_mount = MountManager(
device='/proc',
mountpoint=f'{self.root_dir}/proc'
)
sys_mount = MountManager(
device='/sys',
mountpoint=f'{self.root_dir}/sys'
)
efi_mount.mount()
device_mount.bind_mount()
proc_mount.bind_mount()
sys_mount.bind_mount()
try:
self._run_bootctl(self.root_dir)
self.set_loader_entry(self.root_dir, self.target.live)
finally:
efi_mount.umount()
device_mount.umount()
proc_mount.umount()
sys_mount.umount()
Command.run(
['dd', f'if={disk.partition_map["efi"]}', f'of={path}.img']
)
del disk
del loop_provider
with LoopDevice(path) as loop_provider:
loop_provider.create(overwrite=False)
disk = Disk('gpt', loop_provider)
disk.map_partitions()
disk.partitioner.partition_id = 1
disk._add_to_map('efi')
Command.run(
['mkdosfs', '-n', 'BOOT', disk.partition_map['efi']]
)
Path.create(f'{self.root_dir}/boot/efi')
efi_mount = MountManager(
device=disk.partition_map['efi'],
mountpoint=f'{self.root_dir}/boot/efi'
)
device_mount = MountManager(
device='/dev',
mountpoint=f'{self.root_dir}/dev'
)
proc_mount = MountManager(
device='/proc',
mountpoint=f'{self.root_dir}/proc'
)
sys_mount = MountManager(
device='/sys',
mountpoint=f'{self.root_dir}/sys'
)
efi_mount.mount()
device_mount.bind_mount()
proc_mount.bind_mount()
sys_mount.bind_mount()
try:
self._run_bootctl(self.root_dir)
self.set_loader_entry(self.root_dir, self.target.live)
finally:
efi_mount.umount()
device_mount.umount()
proc_mount.umount()
sys_mount.umount()
Command.run(
['dd', f'if={disk.partition_map["efi"]}', f'of={path}.img']
)
del disk

Command.run(
['mv', f'{path}.img', path]
)
Expand Down
Loading

0 comments on commit 08f76de

Please sign in to comment.