libzypp 17.38.17
PurgeKernels.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
17#include <zypp/PurgeKernels.h>
18#include <zypp/PoolQuery.h>
19#include <zypp/ResPool.h>
20#include <zypp/Resolver.h>
21#include <zypp/Filter.h>
22#include <zypp/ZConfig.h>
23
24#include <iostream>
25#include <fstream>
26#include <map>
27#include <unordered_map>
28#include <sys/utsname.h>
29#include <functional>
30#include <array>
31#include <climits>
32
33#undef ZYPP_BASE_LOGGER_LOGGROUP
34#define ZYPP_BASE_LOGGER_LOGGROUP "PurgeKernels"
35
36namespace zypp {
37
38 using Flavour = std::string;
39 using SolvableList = std::list<sat::Solvable>;
40 using EditionToSolvableMap = std::map<Edition, SolvableList >;
41 using ArchToEditionMap = std::map<Arch, EditionToSolvableMap >;
42
43 struct GroupInfo {
44
45 enum GroupType {
46 None, //<< Just here to support default construction
47 Kernels, //<< Map contains kernel packages, so need to receive special handling and flavour matching
48 RelatedBinaries, //<< Map contains related binary packages, so need to receive special handling and flavour matching
49 Sources //<< Map contains source packages, so when matching those against running we ignore the flavour
51
52 GroupInfo( const GroupType type = None, std::string flav = "") : groupType(type), groupFlavour( std::move(flav) ) { }
53
54 ArchToEditionMap archToEdMap; //<< Map of actual packages
55 std::string groupFlavour; //<< This would contain a specific flavour if there is one calculated
56 };
57 using GroupMap = std::unordered_map<std::string, GroupInfo>;
58
60
61 Impl() {
62 struct utsname unameData;
63 if ( uname( &unameData) == 0 ) {
64
65 const auto archStr = str::regex_substitute( unameData.machine, str::regex( "^i.86$", str::regex::match_extended ), "i586" );
66
67 _kernelArch = Arch( archStr );
68 setUnameR( std::string( unameData.release ) );
69
70 _detectedRunning = true;
71
72 MIL << "Detected running kernel: Flavour: " << _runningKernelFlavour << " Arch: " << _kernelArch << "\n";
73 for ( const auto &edVar : _runningKernelEditionVariants )
74 MIL << "Edition variant: " << edVar << "\n";
75 MIL << std::endl;
76
77 } else {
78 MIL << "Failed to detect running kernel: " << errno << std::endl;
79 }
80 }
81
82 void setUnameR ( const std::string &uname ) {
83
84 _uname_r = uname;
85
86 MIL << "Set uname " << uname << std::endl;
87
88 const std::string flavour = str::regex_substitute( _uname_r, str::regex( ".*-", str::regex::match_extended ), "", true );
89 std::string version = str::regex_substitute( _uname_r, str::regex( "-[^-]*$", str::regex::match_extended | str::regex::newline ), "", true );
90
91 const std::string release = str::regex_substitute( version, str::regex( ".*-", str::regex::match_extended ), "", true );
92
93 version = str::regex_substitute( version, str::regex( "-[^-]*$", str::regex::match_extended | str::regex::newline ), "", true );
94
95
96 auto makeRcVariant = [ &release ]( std::string myVersion, const std::string &replace ){
97 // from purge-kernels script, was copied from kernel-source/rpm/mkspec
98 myVersion = str::regex_substitute( myVersion, str::regex( "\\.0-rc", str::regex::match_extended ), replace, true );
99 myVersion = str::regex_substitute( myVersion, str::regex( "-rc[0-9]+", str::regex::match_extended ), "", true );
100 myVersion = str::regex_substitute( myVersion, str::regex( "-", str::regex::match_extended ), ".", true );
101 return Edition( myVersion, release );
102 };
103
105 _runningKernelEditionVariants.insert( makeRcVariant( version, "~rc") );
106 _runningKernelEditionVariants.insert( makeRcVariant( version, ".rc") );
107
108 _runningKernelFlavour = flavour;
109
110 MIL << "Parsed info from uname: " << std::endl;
111 MIL << "Kernel Flavour: " << _runningKernelFlavour << std::endl;
112 MIL << "Kernel Edition Variants: \n";
113 for ( const auto &var : _runningKernelEditionVariants )
114 MIL << " " << var << "\n";
115 MIL << std::endl;
116 }
117
118 bool removePackageAndCheck( const sat::Solvable slv, const std::set<sat::Solvable> &keepList , const std::set<sat::Solvable> &removeList ) const;
119 static bool versionMatch ( const Edition &a, const Edition &b );
120 void parseKeepSpec();
121 void fillKeepList(const GroupMap &installedKernels, std::set<sat::Solvable> &keepList , std::set<sat::Solvable> &removeList ) const;
122
123 std::set<size_t> _keepLatestOffsets = { 0 };
124 std::set<size_t> _keepOldestOffsets;
125 std::set<Edition> _keepSpecificEditions;
126 std::string _uname_r;
136 bool _keepRunning = true;
137 bool _detectedRunning = false;
138 };
139
144 bool PurgeKernels::Impl::removePackageAndCheck( const sat::Solvable slv, const std::set<sat::Solvable> &keepList , const std::set<sat::Solvable> &removeList ) const
145 {
146 const filter::ByStatus toBeUninstalledFilter( &ResStatus::isToBeUninstalled );
147
148 PoolItem pi ( slv );
149
150 auto pool = ResPool::instance();
151
152 // make sure the pool is clean
153 if ( !pool.resolver().resolvePool() ) {
154 MIL << "Pool failed to resolve, not doing anything" << std::endl;
155 return false;
156 }
157
158 MIL << "Request to remove package: " << pi << std::endl;
159
160 //list of packages that are allowed to be removed automatically.
161 //Unified Kernel Images, their initrds and addons are only allowed in the subpackages named after the
162 //kernel they were built for (e.g. kernel-default-uki-virt-6_12_0-1, the tag always starts with a digit
163 //but can contain letters for rc or .g<sha> kernels): the packages without the kernel in the name are
164 //meta packages (the user's choice of UKI or addon), and if one still requires the
165 //subpackage of this kernel, the kernel has to be kept instead of silently removing the meta package.
166 const str::regex validRemovals("(kernel-syms(-.*)?|kgraft-patch(-.*)?|kernel-(.*)-livepatch(-.*)?|kernel-livepatch(-.*)?|kernel-.+-uki-.+-[0-9].*|.*-kmp(-.*)?)");
167
168 if ( pi.status().isLocked() ) {
169 MIL << "Package " << pi << " is locked by the user, not removing." << std::endl;
170 return false;
171 }
172
173 //remember which packages are already marked for removal, we do not need to check them again
174 std::set<sat::Solvable> currentSetOfRemovals;
175 for ( const PoolItem & p : pool.byStatus( toBeUninstalledFilter ) ) {
176 currentSetOfRemovals.insert( p.satSolvable() );
177 }
178
180
181 if ( !pool.resolver().resolvePool() ) {
182 MIL << "Failed to resolve pool, skipping " << pi << std::endl;
183 pool.resolver().problems();
184 pi.statusReset();
185
186 return false;
187 }
188
189 std::set<sat::Solvable> removedInThisRun;
190 removedInThisRun.insert( slv );
191
192 for ( const PoolItem & p : pool.byStatus( toBeUninstalledFilter ) ) {
193
194 //check if that package is removeable
195 if ( p.status().isByUser() //this was set by us, ignore it
196 || (currentSetOfRemovals.find( p.satSolvable() ) != currentSetOfRemovals.end()) //this was marked by a previous removal, ignore them
197 )
198 continue;
199
200 // remember for later we need remove the debugsource and debuginfo packages as well
201 removedInThisRun.insert( p.satSolvable() );
202
203 MIL << "Package " << p << " was marked by the solver for removal." << std::endl;
204
205 // if we do not plan to remove that package anyway, we need to check if its allowed to be removed ( package in removelist can never be in keep list )
206 if ( removeList.find( p.satSolvable() ) != removeList.end() )
207 continue;
208
209 if ( keepList.find( p.satSolvable() ) != keepList.end() ) {
210 MIL << "Package " << p << " is in keep spec, skipping" << pi << std::endl;
211 pi.statusReset();
212 return false;
213 }
214
215 /*
216 * bsc#1185325 We can not solely rely on name matching to figure out
217 * which packages are kmod's, in SLES from Leap 15.3 forward we have the
218 * kernel-flavour-extra packages ( and others similarly named ) that are basically
219 * a collection of kmod's. So checking the name for .*-kmp(-.*)? is not enough.
220 * We first check if the package provides kmod(*) or ksym(*) and only fall back to name
221 * checking if that is not the case.
222 * Just to be safe I'll leave the regex in the fallback case as well, but it should be completely
223 * redundant now.
224 */
225 bool mostLikelyKmod = false;
226 StrMatcher matchMod( "kmod(*)", Match::GLOB );
227 StrMatcher matchSym( "ksym(*)", Match::GLOB );
228 for ( const auto &prov : p.dep_provides() ) {
229 if ( matchMod.doMatch( prov.detail().name().c_str()) || matchSym.doMatch( prov.detail().name().c_str() ) ) {
230 mostLikelyKmod = true;
231 break;
232 }
233 }
234
235 if ( mostLikelyKmod ) {
236 MIL << "Package " << p << " is most likely a kmod " << std::endl;
237 } else {
238 str::smatch what;
239 if ( !str::regex_match( p.name(), what, validRemovals) ) {
240 MIL << "Package " << p << " should not be removed, skipping " << pi << std::endl;
241 pi.statusReset();
242 return false;
243 }
244 }
245 }
246
247 MIL << "Successfully marked package: " << pi << " for removal."<<std::endl;
248
249 //now check and mark the -debugsource and -debuginfo packages for this package and all the packages that were removed. Maybe collect it before and just remove here
250 MIL << "Trying to remove debuginfo for: " << pi <<"."<<std::endl;
251 for ( sat::Solvable solvable : removedInThisRun ) {
252
253 if ( solvable.arch() == Arch_noarch ||
254 solvable.arch() == Arch_empty )
255 continue;
256
257 for ( const char * suffix : { "-debugsource", "-debuginfo" } ) {
258 PoolQuery q;
260 q.addDependency( sat::SolvAttr::dep_provides, Capability( solvable.name()+suffix, Rel::EQ, solvable.edition() ) );
262 q.setMatchExact();
263
264 for ( sat::Solvable debugPackage : q ) {
265
266 if ( debugPackage.arch() != solvable.arch() )
267 continue;
268
269 MIL << "Found debug package for " << solvable << " : " << debugPackage << std::endl;
270 //if removing the package fails it will not stop us from going on , so no need to check
271 removePackageAndCheck( debugPackage, keepList, removeList );
272 }
273 }
274 }
275 MIL << "Finished removing debuginfo for: " << pi <<"."<<std::endl;
276
277 return true;
278 }
279
284 {
285 if ( a == b )
286 return true;
287
288 // the build counter should not be considered here, so if there is one we cut it off
289 const str::regex buildCntRegex( "\\.[0-9]+($|\\.g[0-9a-f]{7}$)", str::regex::match_extended );
290
291 std::string versionStr = b.asString();
292 str::smatch matches;
293 if ( buildCntRegex.matches( versionStr.data(), matches ) ) {
294 if ( matches.size() >= 2 ) {
295 versionStr.replace( matches.begin(0), (matches.end(0) - matches.begin(0))+1, matches[1] );
296 return a == Edition(versionStr);
297 }
298 }
299 return false;
300 }
301
306 {
307 //keep spec parse regex, make sure to edit the group offsets if changing this regex
308 const str::regex specRegex( "^(latest|oldest)([+-][0-9]+)?$", str::regex::match_extended );
309
310 const unsigned tokenGrp = 1; //index of the group matching the token
311 const unsigned modifierGrp = 2; //index of the group matching the offset modifier
312
313
314 MIL << "Parsing keep spec: " << _keepSpec << std::endl;
315
316 std::vector<std::string> words;
317 str::split( _keepSpec, std::back_inserter(words), ",", str::TRIM );
318 if ( words.empty() ) {
319 WAR << "Invalid keep spec: " << _keepSpec << " using default latest,running." << std::endl;
320 return;
321 }
322
323 _keepRunning = false;
324 _keepLatestOffsets.clear();
325 _keepOldestOffsets.clear();
326
327 for ( const std::string &word : words ) {
328 if ( word == "running" ) {
329 _keepRunning = true;
330 } else {
331 str::smatch what;
332 if ( !str::regex_match( word, what, specRegex ) ) {
333 // Allow uname_r version numbers by cutting off any postfix that comes after a second dash, including the second dash.
334 // Github-Issue openSUSE/zypper#418
335 std::string_view edition(word);
336 const auto firstDash = word.find_first_of ('-');
337 if ( firstDash != std::string::npos ) {
338 const auto secondDash = word.find_first_of ('-', firstDash+1 );
339 if ( secondDash != std::string::npos ) {
340 WAR << "Ignoring possible flavor postfix:'"<< word.substr (secondDash) <<"' in keep spec: " << word << std::endl;
341 edition = std::string_view( word.c_str (), secondDash );
342 }
343 }
344 _keepSpecificEditions.insert( Edition(IdString(edition)) );
345 continue;
346 }
347
348 auto addKeepOff = []( const auto &off, auto &set, const auto &constraint ){
349 const off_t num = off.empty() ? 0 : str::strtonum<off_t>( off );
350 if ( !constraint(num) ) return false;
351 set.insert( static_cast<size_t>(std::abs(num)) );
352 return true;
353 };
354
355 if ( what[tokenGrp] == "oldest" ) {
356 addKeepOff( what[modifierGrp], _keepOldestOffsets, [ &word ]( off_t num ) {
357 if ( num < 0 ) {
358 WAR << "Ignoring invalid modifier in keep spec: " << word << ", oldest supports only positive modifiers." << std::endl;
359 return false;
360 }
361 return true;
362 });
363 } else {
364 addKeepOff( what[modifierGrp], _keepLatestOffsets, [ &word ]( off_t num ) {
365 if ( num > 0 ) {
366 WAR << "Ignoring invalid modifier in keep spec: " << word << ", latest supports only negative modifiers." << std::endl;
367 return false;
368 }
369 return true;
370 });
371 }
372 }
373 }
374 }
375
385 void PurgeKernels::Impl::fillKeepList( const GroupMap &installedKernels, std::set<sat::Solvable> &keepList, std::set<sat::Solvable> &removeList ) const
386 {
387
388 const auto markAsKeep = [ &keepList, &removeList ]( sat::Solvable pck ) {
389 MIL << "Marking package " << pck << " as to keep." << std::endl;
390 keepList.insert( pck ) ;
391 removeList.erase( pck );
392 };
393
394 const auto versionPredicate = []( const auto &editionVariants ){
395 return [ &editionVariants ]( const auto &elem ) {
396 const auto &f = std::bind( versionMatch, _1, elem.first );
397 return std::any_of( editionVariants.begin(), editionVariants.end(), f );
398 };
399 };
400
401 for ( const auto &groupInfo : installedKernels ) {
402
403 MIL << "Starting with group " << groupInfo.first << std::endl;
404
405 for ( const auto &archMap : groupInfo.second.archToEdMap ) {
406
407 MIL << "Starting with arch " << archMap.first << std::endl;
408
409 size_t currOff = 0; //the current "oldest" offset ( runs from map start to end )
410 size_t currROff = archMap.second.size() - 1; // the current "latest" offset ( runs from map end to start )
411
412
413 const EditionToSolvableMap &map = archMap.second;
414
415 if ( _keepRunning
416 && ( ( archMap.first == _kernelArch && groupInfo.second.groupFlavour == _runningKernelFlavour )
417 || groupInfo.second.groupType == GroupInfo::Sources ) ) {
418
419 MIL << "Matching packages against running kernel "<< _runningKernelFlavour << "-" <<_kernelArch << "\nVariants:\n";
420 for ( const auto &var : _runningKernelEditionVariants )
421 MIL << var << "\n";
422 MIL << std::endl;
423
424 const auto &editionPredicate = versionPredicate( _runningKernelEditionVariants );
425 auto it = std::find_if( map.begin(), map.end(), editionPredicate );
426 if ( it == map.end() ) {
427
428 // If we look at Sources we cannot match the flavour but we still want to keep on checking the rest of the keep spec
429 if ( groupInfo.second.groupType != GroupInfo::Sources ) {
430 MIL << "Running kernel " << _runningKernelFlavour << "-" <<_kernelArch << "\n";
431 for ( const auto &var : _runningKernelEditionVariants )
432 MIL << " Possible Variant:" << var << "\n";
433 MIL << "Not installed! \n";
434 MIL << "NOT removing any packages for flavor "<<_runningKernelFlavour<<"-"<<_kernelArch<<" ."<<std::endl;
435
436 for ( const auto &kernelMap : map ) {
437 for( sat::Solvable pck : kernelMap.second )
438 markAsKeep(pck);
439 }
440 continue;
441 }
442
443 } else {
444 // there could be multiple matches here because of rebuild counter, lets try to find the last one
445 MIL << "Found possible running candidate edition: " << it->first << std::endl;
446 auto nit = it;
447 for ( nit++ ; nit != map.end() && editionPredicate( *nit ) ; nit++ ) {
448 MIL << "Found possible more recent running candidate edition: " << nit->first << std::endl;
449 it = nit;
450 }
451 }
452
453 // mark all packages of the running version as keep
454 if ( it != map.end() ) {
455 for( sat::Solvable pck : it->second ) {
456 markAsKeep(pck);
457 }
458 }
459 }
460
461 for ( const auto &kernelMap : map ) {
462 //if we find one of the running offsets in the keepspec, we add the kernel id the the list of packages to keep
463 if ( _keepOldestOffsets.find( currOff ) != _keepOldestOffsets.end() || _keepLatestOffsets.find( currROff ) != _keepLatestOffsets.end() ) {
464 std::for_each( kernelMap.second.begin(), kernelMap.second.end(), markAsKeep );
465 }
466 currOff++;
467 currROff--;
468
469 // a kernel package might be explicitly locked by version
470 // We need to go over all package name provides ( provides is named like the package ) and match
471 // them against the specified version to know which ones to keep. (bsc#1176740 bsc#1176192)
472 std::for_each( kernelMap.second.begin(), kernelMap.second.end(), [ & ]( sat::Solvable solv ){
473 for ( Capability prov : solv.dep_provides() ) {
474 if ( prov.detail().name() == solv.name() && _keepSpecificEditions.count( prov.detail().ed() ) ) {
475 markAsKeep( solv );
476 break;
477 }
478 }
479 });
480 }
481 }
482 }
483 }
484
486 : _pimpl( new Impl() )
487 {
488
489 }
490
492 {
493 MIL << std::endl << "--------------------- Starting to mark obsolete kernels ---------------------"<<std::endl;
494
495 if ( _pimpl->_keepSpec.empty() ) {
496 WAR << "Keep spec is empty, removing nothing." << std::endl;
497 return;
498 }
499
501
503 WAR << "Unable to detect running kernel, but keeping the running kernel was requested. Not removing any packages." << std::endl;
504 return;
505 }
506
507 auto pool = ResPool::instance();
508 pool.resolver().setForceResolve( true ); // set allow uninstall flag
509
510 const filter::ByStatus toBeUninstalledFilter( &ResStatus::isToBeUninstalled );
511
512 // kernel flavour regex
513 const str::regex kernelFlavourRegex("^kernel-(.*)$");
514
515 // the map of all installed kernel packages, grouped by Flavour -> Arch -> Version -> (List of all packages in that category)
516 // devel and source packages are grouped together
517 GroupMap installedKrnlPackages;
518
519
520 // packages that we plan to remove
521 std::set<sat::Solvable> packagesToRemove;
522
523 const auto addPackageToMap = [&installedKrnlPackages, &packagesToRemove] ( const GroupInfo::GroupType type, const std::string &ident, const std::string &flavour, const sat::Solvable &installedKrnlPck ) {
524
525 if ( !installedKrnlPackages.count( ident ) )
526 installedKrnlPackages.insert( std::make_pair( ident, GroupInfo(type, flavour) ) );
527
528 auto &groupInfo = installedKrnlPackages[ ident ];
529 if ( groupInfo.groupType != type || groupInfo.groupFlavour != flavour ) {
530 ERR << "Got inconsistent type and flavour for ident this is a BUG: " << ident << std::endl
531 << "Original Flavour-Type: "<<groupInfo.groupFlavour<<"-"<<groupInfo.groupType << std::endl
532 << "Competing Flavour-Type: "<< flavour << "-" << type << std::endl;
533 }
534
535 const auto currArch = installedKrnlPck.arch();
536 if ( !groupInfo.archToEdMap.count( currArch ) )
537 groupInfo.archToEdMap.insert( std::make_pair( currArch , EditionToSolvableMap {} ) );
538
539 auto &editionToSolvableMap = groupInfo.archToEdMap[ currArch ];
540
541 // calculate the "shortest" or most generic edition of all the package name provides
542 // ( the key of the provides is the package name ). This generic edition is used to
543 // group the packages together. This should get us around the issue that uname -r does
544 // not represent the actual rpm package version anymore. ( bsc#1176740 )
545 auto currCount = INT_MAX;
546 Edition edToUse;
547 for ( Capability prov : installedKrnlPck.dep_provides() ) {
548 if ( prov.detail().name() == installedKrnlPck.name() ) {
549 if ( edToUse == Edition::noedition ) {
550 edToUse = installedKrnlPck.edition();
551 const auto &relStr = edToUse.release();
552 currCount = std::count( relStr.begin(), relStr.end(), '.');
553 } else {
554 const auto &pckEd = prov.detail().ed();
555 const auto &relStr = pckEd.release();
556 if ( const auto pntCnt = std::count( relStr.begin(), relStr.end(), '.'); pntCnt < currCount ) {
557 currCount = pntCnt;
558 edToUse = pckEd;
559 }
560 }
561 }
562 }
563
564 if ( !editionToSolvableMap.count( edToUse ) )
565 editionToSolvableMap.insert( std::make_pair( edToUse, SolvableList{} ) );
566
567 editionToSolvableMap[edToUse].push_back( installedKrnlPck );
568
569 //in the first step we collect all packages in this list, then later we will remove the packages we want to explicitly keep
570 packagesToRemove.insert( installedKrnlPck );
571 };
572
573 // the set of satSolvables that have to be kept always
574 std::set<sat::Solvable> packagesToKeep;
575
576 //collect the list of installed kernel packages
577 PoolQuery q;
579 q.addAttribute( sat::SolvAttr::dep_provides, "multiversion(kernel)" );
581 q.setMatchExact();
582
583 MIL << "Searching for obsolete multiversion kernel packages." << std::endl;
584
585 for ( sat::Solvable installedKrnlPck : q ) {
586
587 MIL << "Found installed multiversion kernel package " << installedKrnlPck << std::endl;
588
589 if ( installedKrnlPck.dep_provides().matches(Capability("kernel-uname-r")) ) {
590 MIL << "Identified as a kernel package " << std::endl;
591
592 // we group kernel packages by flavour
593 str::smatch what;
594 str::regex_match( installedKrnlPck.name(), what, kernelFlavourRegex );
595 if ( what[1].empty() ) {
596 WAR << "Could not detect flavour for: " << installedKrnlPck << " ...skipping" << std::endl;
597 continue;
598 }
599
600 std::string flavour = what[1];
601
602 // XXX: No dashes in flavor names
603 const auto dash = flavour.find_first_of('-');
604 if ( dash != std::string::npos ) {
605 flavour = flavour.substr( 0, dash );
606 }
607
608 // the ident for kernels is the flavour, to also handle cases like kernel-base and kernel which should be in the same group handled together
609 addPackageToMap( GroupInfo::Kernels, flavour, flavour, installedKrnlPck );
610
611 } else {
612
613 // if adapting the groups do not forget to explicitly handle the group when querying the matches
614 const str::regex explicitlyHandled("kernel-syms(-.*)?|kernel(-.*)?-devel");
615
616 MIL << "Not a kernel package, inspecting more closely " << std::endl;
617
618 // we directly handle all noarch packages that export multiversion(kernel)
619 if ( installedKrnlPck.arch() == Arch_noarch ) {
620
621 MIL << "Handling package explicitly due to architecture (noarch)."<< std::endl;
622 addPackageToMap( GroupInfo::Sources, installedKrnlPck.name(), "", installedKrnlPck );
623
624 } else if ( str::smatch match; str::regex_match( installedKrnlPck.name(), match, explicitlyHandled ) ) {
625
626 // try to get the flavour from the name
627 // if we have a kernel-syms getting no flavour means we have the "default" one, otherwise we use the flavour
628 // getting no flavour for a kernel(-*)?-devel means we have the kernel-devel package otherwise the flavour specific one
629 // ...yes this is horrible
630 std::string flav;
631
632 // first group match is a kernel-syms
633 if ( match.size() > 1 && match[1].size() )
634 flav = match[1].substr(1);
635 // second group match is a kernel-flavour-devel
636 else if ( match.size() > 2 && match[2].size() )
637 flav = match[2].substr(1);
638 else if ( installedKrnlPck.name() == "kernel-syms" )
639 flav = "default";
640
641 MIL << "Handling package explicitly due to name match."<< std::endl;
642 addPackageToMap ( GroupInfo::RelatedBinaries, installedKrnlPck.name(), flav, installedKrnlPck );
643 } else {
644 MIL << "Package not explicitly handled" << std::endl;
645 }
646 }
647
648 }
649
650 MIL << "Grouped packages: " << std::endl;
651 std::for_each( installedKrnlPackages.begin(), installedKrnlPackages.end(),[]( const auto &ident ){
652 MIL << "\tGroup ident: "<<ident.first<<std::endl;
653 MIL << "\t Group type: "<<ident.second.groupType<<std::endl;
654 MIL << "\t Group flav: "<<ident.second.groupFlavour<<std::endl;
655 std::for_each( ident.second.archToEdMap.begin(), ident.second.archToEdMap.end(), []( const auto &arch) {
656 MIL << "\t\tArch: "<<arch.first<<std::endl;
657 std::for_each( arch.second.begin(), arch.second.end(), []( const auto &edition) {
658 MIL << "\t\t\tEdition: "<<edition.first<<std::endl;
659 std::for_each( edition.second.begin(), edition.second.end(), []( const auto &packageId) {
660 MIL << "\t\t\t\t "<<sat::Solvable(packageId)<<std::endl;
661 });
662 });
663 });
664 });
665
666 _pimpl->fillKeepList( installedKrnlPackages, packagesToKeep, packagesToRemove );
667
668 for ( sat::Solvable slv : packagesToRemove )
669 _pimpl->removePackageAndCheck( slv, packagesToKeep, packagesToRemove );
670 }
671
672 void PurgeKernels::setUnameR( const std::string &val )
673 {
674 _pimpl->setUnameR( val );
675 }
676
677 std::string PurgeKernels::unameR() const
678 {
679 return _pimpl->_uname_r;
680 }
681
683 {
684 _pimpl->_kernelArch = arch;
685 }
686
688 {
689 return _pimpl->_kernelArch;
690 }
691
692 void PurgeKernels::setKeepSpec( const std::string &val )
693 {
694 _pimpl->_keepSpec = val;
695 }
696
697 std::string PurgeKernels::keepSpec() const
698 {
699 return _pimpl->_keepSpec;
700 }
701
702}
#define MIL
Definition Logger.h:130
#define ERR
Definition Logger.h:132
#define WAR
Definition Logger.h:131
Architecture.
Definition Arch.h:37
Arch()
Default ctor Arc_noarch.
Definition Arch.cc:681
A sat capability.
Definition Capability.h:63
Capability()
Default ctor, Empty capability.
Definition Capability.h:69
Edition represents [epoch:]version[-release].
Definition Edition.h:60
Edition()
Default ctor: noedition.
Definition Edition.h:76
std::string release() const
Release.
Definition Edition.cc:112
static const Edition noedition
Value representing noedition ("") This is in fact a valid Edition.
Definition Edition.h:72
constexpr IdString()
Default ctor, empty string.
Definition IdString.h:61
@ GLOB
Glob.
Definition StrMatcher.h:47
Combining sat::Solvable and ResStatus.
Definition PoolItem.h:51
ResStatus & status() const
Returns the current status.
Definition PoolItem.cc:212
ResStatus & statusReset() const
Resets status to the default state (KEEP_STATE bySOLVER; clears any lock!).
Definition PoolItem.cc:213
Meta-data query API.
Definition PoolQuery.h:91
void setMatchExact()
Set to match exact string instead of substring.
Definition PoolQuery.cc:964
void addKind(const ResKind &kind)
Filter by selectable kind.
Definition PoolQuery.cc:876
void addAttribute(const sat::SolvAttr &attr, const std::string &value="")
Filter by the value of the specified attr attribute.
Definition PoolQuery.cc:885
void setInstalledOnly()
Return only @System repo packages.
Definition PoolQuery.cc:975
void addDependency(const sat::SolvAttr &attr, const std::string &name, const Rel &op, const Edition &edition)
Query "name|global op edition".
Definition PoolQuery.cc:888
std::string unameR() const
Arch kernelArch() const
void setKeepSpec(const std::string &val)
std::string keepSpec() const
void setUnameR(const std::string &val)
RW_pointer< Impl > _pimpl
void setKernelArch(const zypp::Arch &arch)
static const ResKind package
Definition ResKind.h:40
static ResPool instance()
Singleton ctor.
Definition ResPool.cc:38
bool setToBeUninstalled(TransactByValue causer)
Definition ResStatus.h:550
bool isLocked() const
Definition ResStatus.h:270
bool isToBeUninstalled() const
Definition ResStatus.h:267
String matching (STRING|SUBSTRING|GLOB|REGEX).
Definition StrMatcher.h:298
bool doMatch(const char *string_r) const
Return whether string matches.
static ZConfig & instance()
Singleton ctor.
Definition ZConfig.cc:794
std::string multiversionKernels() const
Definition ZConfig.cc:1218
Filter solvables according to their status.
Definition Filter.h:143
static const SolvAttr dep_provides
Definition SolvAttr.h:79
A Solvable object within the sat Pool.
Definition Solvable.h:54
Regular expression.
Definition Regex.h:95
@ match_extended
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition Regex.h:101
@ newline
Match newline.
Definition Regex.h:102
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition Regex.cc:57
Regular expression match result.
Definition Regex.h:168
unsigned size() const
Definition Regex.cc:106
std::string::size_type end(unsigned i) const
End index of subexpression i in match_str (or std::string::npos).
Definition Regex.cc:100
std::string::size_type begin(unsigned i) const
Begin index of subexpression i in match_str (or std::string::npos).
Definition Regex.cc:97
bool regex_match(const char *s, smatch &matches, const regex &regex) ZYPP_API
Regular expression matching.
Definition Regex.cc:80
unsigned short a
unsigned short b
Definition ansi.h:855
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition String.h:602
TInt strtonum(const C_Str &str)
Parsing numbers from string.
std::string regex_substitute(const std::string &s, const regex &regex, const std::string &replacement, bool global=true) ZYPP_API
Replaces the matched regex with the string passed in replacement.
Definition Regex.cc:120
Easy-to use interface to the ZYPP dependency resolver.
std::map< Arch, EditionToSolvableMap > ArchToEditionMap
const Arch Arch_empty(IdString::Empty)
std::unordered_map< std::string, GroupInfo > GroupMap
std::list< sat::Solvable > SolvableList
std::string Flavour
std::map< Edition, SolvableList > EditionToSolvableMap
enum zypp::GroupInfo::GroupType groupType
ArchToEditionMap archToEdMap
std::string groupFlavour
GroupInfo(const GroupType type=None, std::string flav="")
static bool versionMatch(const Edition &a, const Edition &b)
std::set< Edition > _keepSpecificEditions
bool removePackageAndCheck(const sat::Solvable slv, const std::set< sat::Solvable > &keepList, const std::set< sat::Solvable > &removeList) const
std::set< Edition > _runningKernelEditionVariants
std::set< size_t > _keepLatestOffsets
void setUnameR(const std::string &uname)
std::set< size_t > _keepOldestOffsets
void fillKeepList(const GroupMap &installedKernels, std::set< sat::Solvable > &keepList, std::set< sat::Solvable > &removeList) const
static const Rel EQ
Definition Rel.h:52