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

Add --write-eclean-kernel-preserved-set option #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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(preserved_set)


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