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

Fixes for UKI without sd-boot, UKI with legacy installkernel, and rEFInd #51

Closed
wants to merge 6 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
27 changes: 20 additions & 7 deletions ecleankernel/layout/blspec.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# vim:fileencoding=utf-8
# (c) 2020-2023 Michał Górny <[email protected]>
# (c) 2024 Andrew Ammerlaan <[email protected]>
# Released under the terms of the 2-clause BSD license.

import distro
import logging
import os
import typing
Expand Down Expand Up @@ -53,14 +55,17 @@ def __init__(self,
raise LayoutNotFound("/etc/machine-id not found")

for d in self.potential_dirs:
# Present if type 1
bootloaderdir = root / d / "loader"
if bootloaderdir.is_dir():
# Present if type 2
ukidir = root / d / "EFI" / "Linux"
if bootloaderdir.is_dir() or ukidir.is_dir():
# Type 1 entries (linux+initrd) are in
# $BOOT/ENTRY-TOKEN/KERNEL-VERSION/
self.blsdir = root / d / self.kernel_id
# Type 2 entries (uki's) are in
# $BOOT/EFI/Linux/ENTRY-TOKEN-KERNEL-VERSION.efi
self.ukidir = root / d / "EFI" / "Linux"
self.ukidir = ukidir
return
else:
raise LayoutNotFound("/boot/[EFI/]loader not found")
Expand Down Expand Up @@ -128,16 +133,18 @@ def find_kernels(self,
# collect from ESP/Linux
if self.ukidir.is_dir():
for file in os.listdir(self.ukidir):
if not file.endswith(".efi"):
basename = file.removesuffix(".efi")
if file == basename:
# Not an UKI
continue

ver = file.removeprefix(f"{self.kernel_id}-"
).removeprefix("gentoo-")
if file == ver:
distro_id = distro.id() or "linux"

ver = basename.removeprefix(f"{self.kernel_id}-"
).removeprefix(f"{distro_id}-")
if basename == ver:
# Not our UKI
continue
ver = ver.removesuffix(".efi")

kernels[(ver, "uki")] = self.append_kernel_files(
KernelFileType.KERNEL,
Expand All @@ -146,6 +153,12 @@ def find_kernels(self,
ver, module_dict,
exclusions)

uki_icon = self.ukidir / f"{basename}.png"
if (KernelFileType.MISC not in exclusions and
os.path.isfile(uki_icon)):
kernels[(ver, "uki")].all_files.append(GenericFile(
uki_icon, KernelFileType.MISC))

# merge unassociated modules into kernel groups
for mkv, fobjs in module_dict.items():
if any(mkv == k.real_kv for k in kernels.values()):
Expand Down
18 changes: 14 additions & 4 deletions ecleankernel/layout/std.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# vim:fileencoding=utf-8
# (c) 2011-2020 Michał Górny <[email protected]>
# (c) 2024 Andrew Ammerlaan <[email protected]>
# Released under the terms of the 2-clause BSD license.

import distro
import itertools
import os
import os.path
Expand Down Expand Up @@ -48,6 +50,9 @@ class StdLayout(ModuleDirLayout):
'.lz',
'.xz',

# refind
".png",

# efistub
'.efi',
]
Expand All @@ -67,12 +72,14 @@ def find_kernels(self,
kernels: typing.Dict[str, typing.Dict[str, Kernel]] = {}
other_files: typing.List[typing.Tuple[GenericFile, str]] = []

distro_name = distro.name() or "Linux"

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'):
self.root / f"boot/EFI/EFI/{distro_name}",
self.root / f"boot/efi/EFI/{distro_name}",
self.root / f"boot/EFI/{distro_name}",
self.root / f"efi/EFI/{distro_name}"):
try:
for file in os.listdir(directory):
yield directory / file
Expand Down Expand Up @@ -105,6 +112,9 @@ def find_std_files() -> typing.Iterator:
except UnrecognizedKernelError:
# fall back to filename
for ftype, prefix in self.prefixes:
# kernel refind icon has same name as kernel +'.png'
if fn.endswith(".png"):
ftype = KernelFileType.MISC
if ftype not in exclusions:
if fn.startswith(prefix):
other_files.append(
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ classifiers = [
"Topic :: System :: Installation/Setup"
]
requires-python = ">=3.9"
dependencies = [
"distro"
]

[project.optional-dependencies]
test = ["pytest"]
Expand Down
30 changes: 22 additions & 8 deletions test/test_layout_blspec.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# vim:fileencoding=utf-8
# (c) 2020 Michał Górny <[email protected]>
# (c) 2024 Andrew Ammerlaan <[email protected]>
# Released under the terms of the 2-clause BSD license.

import distro
import os
import tempfile
import unittest
Expand All @@ -22,6 +24,8 @@
from test.test_file import write_bzImage
from test.test_layout_std import make_test_files, kernel_paths

distro_id = distro.id() or "linux"


class BlSpecLayoutTests(unittest.TestCase):
maxDiff = None
Expand All @@ -42,7 +46,8 @@ def create_layout(self,
entry = self.entry_token if entry_token else self.machine_id
test_spec = [
f"boot/{subdir}/EFI/Linux/{entry}-1.2.6.efi",
f"boot/{subdir}/EFI/Linux/{entry}-1.2.5.efi",
f"boot/{subdir}/EFI/Linux/{entry}-1.2.6.png",
f"boot/{subdir}/EFI/Linux/{distro_id}-1.2.5.efi",
f"boot/{subdir}/{entry}/1.2.5/initrd",
f"boot/{subdir}/{entry}/1.2.5/linux",
f"boot/{subdir}/{entry}/1.2.4/initrd",
Expand Down Expand Up @@ -79,7 +84,8 @@ def create_layout(self,
with open(path / "etc/kernel/entry-token", "w") as f:
f.write(f"{self.entry_token}\n")
write_bzImage(bootsub / f"EFI/Linux/{entry}-1.2.6.efi", b'1.2.6 test')
write_bzImage(bootsub / f"EFI/Linux/{entry}-1.2.5.efi", b'1.2.5 test')
write_bzImage(bootsub / f"EFI/Linux/{distro_id}-1.2.5.efi",
b'1.2.5 test')
write_bzImage(bootsub / f"{entry}/1.2.5/linux", b'1.2.5 test')
write_bzImage(bootsub / f"{entry}/1.2.3/linux", b'1.2.3 test')
write_bzImage(bootsub / f"{entry}/1.2.2/linux", b'1.2.2 test')
Expand All @@ -105,7 +111,7 @@ def assert_kernels(self,
subdir = 'EFI/' if efi_subdir else ''
files = {
f'boot/{subdir}/EFI/Linux/{self.machine_id}-1.2.6.efi': k126,
f'boot/{subdir}/EFI/Linux/{self.machine_id}-1.2.5.efi': k125,
f'boot/{subdir}/EFI/Linux/{distro_id}-1.2.5.efi': k125,
f'boot/{subdir}{self.machine_id}/1.2.5/initrd': k125,
f'boot/{subdir}{self.machine_id}/1.2.5/linux': k125,
f'boot/{subdir}{self.machine_id}/1.2.5': k125,
Expand Down Expand Up @@ -234,14 +240,16 @@ def test_find_modules(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
ModuleDirectory(modules / '1.2.5'),
GenericFile(modules / '1.2.5/../../../usr/src/linux',
KFT.BUILD),
],
'1.2.5'),
('1.2.6',
[KernelImage(ukipath / f"{self.machine_id}-1.2.6.efi"),
GenericFile(ukipath / f"{self.machine_id}-1.2.6.png",
KFT.MISC),
ModuleDirectory(modules / '1.2.6'),
GenericFile(modules / '1.2.6/../../../usr/src/linux',
KFT.BUILD),
Expand Down Expand Up @@ -302,7 +310,7 @@ def test_exclude_misc(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
ModuleDirectory(modules / '1.2.5'),
GenericFile(modules / '1.2.5/../../../usr/src/linux',
KFT.BUILD),
Expand Down Expand Up @@ -366,13 +374,15 @@ def test_exclude_modules(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
GenericFile(modules / '1.2.5/../../../usr/src/linux',
KFT.BUILD),
],
'1.2.5'),
('1.2.6',
[KernelImage(ukipath / f"{self.machine_id}-1.2.6.efi"),
GenericFile(ukipath / f"{self.machine_id}-1.2.6.png",
KFT.MISC),
GenericFile(modules / '1.2.6/../../../usr/src/linux',
KFT.BUILD),
],
Expand Down Expand Up @@ -427,12 +437,14 @@ def test_exclude_build(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
ModuleDirectory(modules / '1.2.5'),
],
'1.2.5'),
('1.2.6',
[KernelImage(ukipath / f"{self.machine_id}-1.2.6.efi"),
GenericFile(ukipath / f"{self.machine_id}-1.2.6.png",
KFT.MISC),
ModuleDirectory(modules / '1.2.6'),
],
'1.2.6'),
Expand Down Expand Up @@ -491,14 +503,16 @@ def test_find_modules_EFI(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
ModuleDirectory(modules / '1.2.5'),
GenericFile(modules / '1.2.5/../../../usr/src/linux',
KFT.BUILD),
],
'1.2.5'),
('1.2.6',
[KernelImage(ukipath / f"{self.machine_id}-1.2.6.efi"),
GenericFile(ukipath / f"{self.machine_id}-1.2.6.png",
KFT.MISC),
ModuleDirectory(modules / '1.2.6'),
GenericFile(modules / '1.2.6/../../../usr/src/linux',
KFT.BUILD),
Expand Down
44 changes: 27 additions & 17 deletions test/test_layout_std.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# vim:fileencoding=utf-8
# (c) 2020 Michał Górny <[email protected]>
# (c) 2024 Andrew Ammerlaan <[email protected]>
# Released under the terms of the 2-clause BSD license.
Nowa-Ammerlaan marked this conversation as resolved.
Show resolved Hide resolved

import io
import distro
import os
import tempfile
import typing
Expand All @@ -22,6 +24,8 @@

TEST_DATA_DIR = Path(__file__).parent / 'data'

distro_name = distro.name() or "Linux"


def kernel_paths(kd: typing.List[Kernel]
) -> typing.Iterable[typing.Tuple[
Expand Down Expand Up @@ -68,10 +72,11 @@ def create_layout(self) -> tempfile.TemporaryDirectory:

"""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',
f"efi/EFI/{distro_name}/vmlinuz-1.2.1.efi",
f"efi/EFI/{distro_name}/vmlinuz-1.2.1.png",
f"efi/EFI/{distro_name}/System.map-1.2.1",
f"efi/EFI/{distro_name}/config-1.2.1",
f"efi/EFI/{distro_name}/initramfs-1.2.1.img",
]

test_spec += [
Expand All @@ -92,7 +97,7 @@ def create_layout(self) -> tempfile.TemporaryDirectory:
td = make_test_files(test_spec)
path = Path(td.name)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
efistub = path / f"efi/EFI/{distro_name}/"
modules = path / 'lib/modules'

write_bzImage(efistub / 'vmlinuz-1.2.1.efi', b'1.2.1 test')
Expand Down Expand Up @@ -126,10 +131,10 @@ 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,
f"efi/EFI/{distro_name}/vmlinuz-1.2.1.efi": k121,
f"efi/EFI/{distro_name}/System.map-1.2.1": k121,
f"efi/EFI/{distro_name}/config-1.2.1": k121,
f"efi/EFI/{distro_name}/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,
Expand All @@ -154,7 +159,7 @@ def test_find_modules(self) -> None:
with self.create_layout() as td:
path = Path(td)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
efistub = path / f"efi/EFI/{distro_name}/"
modules = path / 'lib/modules'

self.assertEqual(
Expand All @@ -165,6 +170,7 @@ def test_find_modules(self) -> None:
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(efistub / "vmlinuz-1.2.1.png", KFT.MISC),
ModuleDirectory(modules / '1.2.1'),
GenericFile(modules / '1.2.1/../../../usr/src/linux',
KFT.BUILD),
Expand Down Expand Up @@ -208,7 +214,7 @@ def test_exclude_config(self) -> None:
with self.create_layout() as td:
path = Path(td)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
efistub = path / f"efi/EFI/{distro_name}/"
modules = path / 'lib/modules'

self.assertEqual(
Expand All @@ -219,6 +225,7 @@ def test_exclude_config(self) -> None:
[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'),
GenericFile(efistub / "vmlinuz-1.2.1.png", KFT.MISC),
ModuleDirectory(modules / '1.2.1'),
GenericFile(modules / '1.2.1/../../../usr/src/linux',
KFT.BUILD),
Expand Down Expand Up @@ -259,7 +266,7 @@ def test_exclude_modules(self) -> None:
with self.create_layout() as td:
path = Path(td)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
efistub = path / f"efi/EFI/{distro_name}/"
modules = path / 'lib/modules'

self.assertEqual(
Expand All @@ -271,6 +278,7 @@ def test_exclude_modules(self) -> None:
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(efistub / "vmlinuz-1.2.1.png", KFT.MISC),
GenericFile(modules / '1.2.1/../../../usr/src/linux',
KFT.BUILD),
],
Expand Down Expand Up @@ -309,7 +317,7 @@ def test_exclude_build(self) -> None:
with self.create_layout() as td:
path = Path(td)
boot = path / 'boot'
efistub = path / 'efi/EFI/Gentoo/'
efistub = path / f"efi/EFI/{distro_name}/"
modules = path / 'lib/modules'

self.assertEqual(
Expand All @@ -321,6 +329,7 @@ def test_exclude_build(self) -> None:
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(efistub / "vmlinuz-1.2.1.png", KFT.MISC),
ModuleDirectory(modules / '1.2.1'),
],
'1.2.1'),
Expand Down Expand Up @@ -412,10 +421,11 @@ def test_main_list_kernels(self,
- modules: {td}/lib/modules/1.2.2
- 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
- systemmap: {td}/efi/EFI/{distro_name}/System.map-1.2.1
- config: {td}/efi/EFI/{distro_name}/config-1.2.1
- initramfs: {td}/efi/EFI/{distro_name}/initramfs-1.2.1.img
- vmlinuz: {td}/efi/EFI/{distro_name}/vmlinuz-1.2.1.efi
- misc: {td}/efi/EFI/{distro_name}/vmlinuz-1.2.1.png
- modules: {td}/lib/modules/1.2.1
- build: {td}/lib/modules/1.2.1/../../../usr/src/linux'''.lstrip())
self.assert_kernels(Path(td))
Expand Down
Loading
Loading