From 0678fb869f62e3dbe5d9651a672978230283f06b Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 20 Mar 2023 15:39:55 +0100 Subject: [PATCH] Add --write-eclean-kernel-preserved-set option This options makes eclean-kernel create (or update) a portage user set called 'eclean-kernel-preserved'. Since user sets are automatically part of the world set, this prevents kernels that a remained by eclean-kernel to be uninstalled by portage via the depclean mechanism. Signed-off-by: Florian Schmaus --- ecleankernel/__main__.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/ecleankernel/__main__.py b/ecleankernel/__main__.py index 307e2a6..404c364 100644 --- a/ecleankernel/__main__.py +++ b/ecleankernel/__main__.py @@ -29,6 +29,8 @@ from ecleankernel.layout.std import StdLayout from ecleankernel.sort import MTimeSort, VersionSort +from gentoolkit.helpers import FileOwner + ecleankern_desc = ''' Remove old kernel versions, keeping either N newest kernels (with -n) or only those which are referenced by a bootloader (with -a). @@ -145,6 +147,9 @@ def main(argv: typing.List[str]) -> int: help=f'Exclude kernel parts from being removed ' f'(comma-separated, supported parts: ' f'{", ".join(kernel_parts)})') + group.add_argument('--write-eclean-kernel-preserved-set', + action='store_true', + help='Write /etc/portage/sets/eclean-kernel-preserved.') all_args = [] config_dirs = os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg').split(':') @@ -256,6 +261,31 @@ def main(argv: typing.List[str]) -> int: bootloader=bootloader, destructive=args.destructive) + if args.write_eclean_kernel_preserved_set or os.getenv('ECLEAN_KERNEL_PRESERVED_SET', 'false') == 'true': + preserved_pkgs = [] + remaining_kernels = [k for k in kernels + if k not in removals] + file_owner = FileOwner(early_out=True) + for k in remaining_kernels: + module_dir = Path(f"/lib/modules/{k.real_kv}/") + if not module_dir.is_dir: + continue + + owners = file_owner((str(module_dir),)) + if owners: + pkg = owners[0][0] + preserved_pkgs.append(pkg) + if preserved_pkgs: + set_dir = Path("/etc/portage/sets/") + if not set_dir.exists: + set_dir.mkdir() + set_file = set_dir / "eclean-kernel-preserved" + + preserved_set = ''.join([f"={pkg}\n" for pkg in preserved_pkgs]) + if not set_file.exists() or set_file.read_text() != preserved_set: + set_file.write_text(preserved_set) + + has_kernel_install = False has_bootloader_postrm = False if args.root == Path('/'):