Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

also find std kernels on ESP/EFI/Gentoo #50

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 57 additions & 47 deletions ecleankernel/layout/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class StdLayout(ModuleDirLayout):
'.gz',
'.lz',
'.xz',

# efistub
'.efi',
]

def find_kernels(self,
Expand All @@ -63,54 +66,61 @@ def find_kernels(self,
# collect from /boot
kernels: typing.Dict[str, typing.Dict[str, Kernel]] = {}
other_files: typing.List[typing.Tuple[GenericFile, str]] = []
boot_directory = self.root / 'boot'
try:
diter = os.listdir(boot_directory)
except FileNotFoundError:
pass
else:
for fn in diter:
# skip hidden and GRUB signature files
if fn.startswith('.') or fn.endswith('.sig'):
continue
path = boot_directory / fn
if path.is_symlink() or not path.is_file():
continue
# skip unversioned files
ver = fn.partition('-')[2]
if not ver:
continue

# strip suffix from filename to get the correct version
for suffix in self.suffixes:
if ver.endswith(suffix):
ver = ver[:-len(suffix)]
break
elif ver.endswith(suffix + '.old'):
ver = ver[:-len(suffix)-4] + '.old'

# try recognizing kernel image via magic

def find_std_files() -> typing.Iterator:
for directory in (self.root / 'boot',
self.root / 'boot/EFI/EFI/Gentoo',
self.root / 'boot/efi/EFI/Gentoo',
self.root / 'boot/EFI/Gentoo',
self.root / 'efi/EFI/Gentoo'):
try:
kobj = KernelImage(path)
except UnrecognizedKernelError:
# fall back to filename
for ftype, prefix in self.prefixes:
if ftype not in exclusions:
if fn.startswith(prefix):
other_files.append(
(GenericFile(path, ftype), ver))
break
continue

# the following is done only for kernel images
assert isinstance(kobj, KernelImage)
kg = kernels.setdefault(ver, {})
k = kg.setdefault(kobj.internal_version, Kernel(ver))
k.all_files.append(kobj)

# associate the module directory
k.all_files.extend(
module_dict.get(kobj.internal_version, []))
for file in os.listdir(directory):
yield directory / file
except FileNotFoundError:
pass

for path in find_std_files():
fn = path.name
# skip hidden and GRUB signature files
if fn.startswith('.') or fn.endswith('.sig'):
continue
if path.is_symlink() or not path.is_file():
continue
# skip unversioned files
ver = fn.partition('-')[2]
if not ver:
continue

# strip suffix from filename to get the correct version
for suffix in self.suffixes:
if ver.endswith(suffix):
ver = ver[:-len(suffix)]
break
elif ver.endswith(suffix + '.old'):
ver = ver[:-len(suffix)-4] + '.old'

# try recognizing kernel image via magic
try:
kobj = KernelImage(path)
except UnrecognizedKernelError:
# fall back to filename
for ftype, prefix in self.prefixes:
if ftype not in exclusions:
if fn.startswith(prefix):
other_files.append(
(GenericFile(path, ftype), ver))
break
continue

# the following is done only for kernel images
assert isinstance(kobj, KernelImage)
kg = kernels.setdefault(ver, {})
k = kg.setdefault(kobj.internal_version, Kernel(ver))
k.all_files.append(kobj)

# associate the module directory
k.all_files.extend(
module_dict.get(kobj.internal_version, []))

# merge other files into kernel groups
for fobj, ver in other_files:
Expand Down
80 changes: 75 additions & 5 deletions test/test_layout_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,21 @@ def create_layout(self) -> tempfile.TemporaryDirectory:
'boot/initrd-1.2.3.img',
]
test_spec += [f'{x}.old' for x in test_spec]

"""EFI Stub"""
test_spec += [
'efi/EFI/Gentoo/vmlinuz-1.2.1.efi',
'efi/EFI/Gentoo/System.map-1.2.1',
'efi/EFI/Gentoo/config-1.2.1',
'efi/EFI/Gentoo/initramfs-1.2.1.img',
]

test_spec += [
'boot/config-1.2.4',
'boot/vmlinuz-1.2.2',
'boot/vmlinuz-1.2.2.sig',
'boot/System.map-1.2.2',
'lib/modules/1.2.1/test.ko',
'lib/modules/1.2.2/test.ko',
'lib/modules/1.2.3/test.ko',
'lib/modules/1.2.4/test.ko',
Expand All @@ -82,11 +92,14 @@ def create_layout(self) -> tempfile.TemporaryDirectory:
td = make_test_files(test_spec)
path = Path(td.name)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
modules = path / 'lib/modules'

write_bzImage(efistub / 'vmlinuz-1.2.1.efi', b'1.2.1 test')
write_bzImage(boot / 'vmlinuz-1.2.2', b'1.2.2 test')
write_bzImage(boot / 'vmlinuz-1.2.3', b'1.2.3 test')
write_bzImage(boot / 'vmlinuz-1.2.3.old', b'1.2.3 test')
os.symlink('../../../usr/src/linux', modules / '1.2.1/build')
os.symlink('../../../usr/src/linux', modules / '1.2.2/build')
os.symlink('../../../usr/src/linux', modules / '1.2.3/build')

Expand All @@ -98,6 +111,7 @@ def assert_kernels(self,
k123: bool = True,
k123old: bool = True,
k122: bool = True,
k121: bool = True,
stray: bool = True
) -> None:
"""Assert whether specified kernels were removed or kept"""
Expand All @@ -112,7 +126,12 @@ def assert_kernels(self,
'boot/initrd-1.2.3.img.old': k123old,
'boot/vmlinuz-1.2.2': k122,
'boot/System.map-1.2.2': k122,
'efi/EFI/Gentoo/vmlinuz-1.2.1.efi': k121,
'efi/EFI/Gentoo/System.map-1.2.1': k121,
'efi/EFI/Gentoo/config-1.2.1': k121,
'efi/EFI/Gentoo/initramfs-1.2.1.img': k121,
'boot/config-1.2.4': k124,
'lib/modules/1.2.1/test.ko': k121,
'lib/modules/1.2.2/test.ko': k122,
'lib/modules/1.2.2': k122,
'lib/modules/1.2.3/test.ko': k123 or k123old,
Expand All @@ -135,12 +154,23 @@ def test_find_modules(self) -> None:
with self.create_layout() as td:
path = Path(td)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
modules = path / 'lib/modules'

self.assertEqual(
sorted(kernel_paths(
StdLayout(root=path).find_kernels())),
[('1.2.2',
[('1.2.1',
[GenericFile(efistub / 'System.map-1.2.1', KFT.SYSTEM_MAP),
GenericFile(efistub / 'config-1.2.1', KFT.CONFIG),
GenericFile(efistub / 'initramfs-1.2.1.img', KFT.INITRAMFS),
KernelImage(efistub / 'vmlinuz-1.2.1.efi'),
ModuleDirectory(modules / '1.2.1'),
GenericFile(modules / '1.2.1/../../../usr/src/linux',
KFT.BUILD),
],
'1.2.1'),
('1.2.2',
[GenericFile(boot / 'System.map-1.2.2', KFT.SYSTEM_MAP),
KernelImage(boot / 'vmlinuz-1.2.2'),
ModuleDirectory(modules / '1.2.2'),
Expand Down Expand Up @@ -178,13 +208,23 @@ def test_exclude_config(self) -> None:
with self.create_layout() as td:
path = Path(td)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
modules = path / 'lib/modules'

self.assertEqual(
sorted(kernel_paths(
StdLayout(root=path).find_kernels(
exclusions=[KFT.CONFIG]))),
[('1.2.2',
[('1.2.1',
[GenericFile(efistub / 'System.map-1.2.1', KFT.SYSTEM_MAP),
GenericFile(efistub / 'initramfs-1.2.1.img', KFT.INITRAMFS),
KernelImage(efistub / 'vmlinuz-1.2.1.efi'),
ModuleDirectory(modules / '1.2.1'),
GenericFile(modules / '1.2.1/../../../usr/src/linux',
KFT.BUILD),
],
'1.2.1'),
('1.2.2',
[GenericFile(boot / 'System.map-1.2.2', KFT.SYSTEM_MAP),
KernelImage(boot / 'vmlinuz-1.2.2'),
ModuleDirectory(modules / '1.2.2'),
Expand Down Expand Up @@ -219,13 +259,23 @@ def test_exclude_modules(self) -> None:
with self.create_layout() as td:
path = Path(td)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
modules = path / 'lib/modules'

self.assertEqual(
sorted(kernel_paths(
StdLayout(root=path).find_kernels(
exclusions=[KFT.MODULES]))),
[('1.2.2',
[('1.2.1',
[GenericFile(efistub / 'System.map-1.2.1', KFT.SYSTEM_MAP),
GenericFile(efistub / 'config-1.2.1', KFT.CONFIG),
GenericFile(efistub / 'initramfs-1.2.1.img', KFT.INITRAMFS),
KernelImage(efistub / 'vmlinuz-1.2.1.efi'),
GenericFile(modules / '1.2.1/../../../usr/src/linux',
KFT.BUILD),
],
'1.2.1'),
('1.2.2',
[GenericFile(boot / 'System.map-1.2.2', KFT.SYSTEM_MAP),
KernelImage(boot / 'vmlinuz-1.2.2'),
GenericFile(modules / '1.2.2/../../../usr/src/linux',
Expand Down Expand Up @@ -259,13 +309,22 @@ def test_exclude_build(self) -> None:
with self.create_layout() as td:
path = Path(td)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
modules = path / 'lib/modules'

self.assertEqual(
sorted(kernel_paths(
StdLayout(root=path).find_kernels(
exclusions=[KFT.BUILD]))),
[('1.2.2',
[('1.2.1',
[GenericFile(efistub / 'System.map-1.2.1', KFT.SYSTEM_MAP),
GenericFile(efistub / 'config-1.2.1', KFT.CONFIG),
GenericFile(efistub / 'initramfs-1.2.1.img', KFT.INITRAMFS),
KernelImage(efistub / 'vmlinuz-1.2.1.efi'),
ModuleDirectory(modules / '1.2.1'),
],
'1.2.1'),
('1.2.2',
[GenericFile(boot / 'System.map-1.2.2', KFT.SYSTEM_MAP),
KernelImage(boot / 'vmlinuz-1.2.2'),
ModuleDirectory(modules / '1.2.2'),
Expand Down Expand Up @@ -351,7 +410,14 @@ def test_main_list_kernels(self,
- systemmap: {td}/boot/System.map-1.2.2
- vmlinuz: {td}/boot/vmlinuz-1.2.2
- modules: {td}/lib/modules/1.2.2
- build: {td}/lib/modules/1.2.2/../../../usr/src/linux'''.lstrip())
- build: {td}/lib/modules/1.2.2/../../../usr/src/linux
other 1.2.1 [1.2.1]
- systemmap: {td}/efi/EFI/Gentoo/System.map-1.2.1
- config: {td}/efi/EFI/Gentoo/config-1.2.1
- initramfs: {td}/efi/EFI/Gentoo/initramfs-1.2.1.img
- vmlinuz: {td}/efi/EFI/Gentoo/vmlinuz-1.2.1.efi
- modules: {td}/lib/modules/1.2.1
- build: {td}/lib/modules/1.2.1/../../../usr/src/linux'''.lstrip())
self.assert_kernels(Path(td))

def test_main_remove(self) -> None:
Expand All @@ -378,6 +444,7 @@ def test_main_remove_all(self) -> None:
'--root', td, '--debug', '--no-mount']),
0)
self.assert_kernels(Path(td),
k121=False,
k122=False,
k123=False,
k123old=False,
Expand All @@ -398,6 +465,7 @@ def test_main_remove_n2(self) -> None:
'--root', td, '--debug', '--no-mount']),
0)
self.assert_kernels(Path(td),
k121=False,
k122=False,
k124=False)

Expand Down Expand Up @@ -428,6 +496,7 @@ def test_config_file_system(self) -> None:
'--root', td, '--debug', '--no-mount']),
0)
self.assert_kernels(Path(td),
k121=False,
k122=False,
k124=False)

Expand All @@ -447,5 +516,6 @@ def test_config_file_user(self) -> None:
'--root', td, '--debug', '--no-mount']),
0)
self.assert_kernels(Path(td),
k121=False,
k122=False,
k124=False)
Loading