Skip to content

Commit

Permalink
Add --write-eclean-kernel-preserved-set option
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Flowdalic committed Mar 21, 2023
1 parent 2de12fa commit 404a2b5
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ecleankernel/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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(':')
Expand Down Expand Up @@ -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(new_content)


has_kernel_install = False
has_bootloader_postrm = False
if args.root == Path('/'):
Expand Down

0 comments on commit 404a2b5

Please sign in to comment.