1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PurgeKernels.cc
13 #include <zypp/base/String.h>
14 #include <zypp/base/Logger.h>
15 #include <zypp/base/Regex.h>
16 #include <zypp/base/Iterator.h>
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>
27 #include <unordered_map>
28 #include <sys/utsname.h>
32 #undef ZYPP_BASE_LOGGER_LOGGROUP
33 #define ZYPP_BASE_LOGGER_LOGGROUP "PurgeKernels"
37 using Flavour = std::string;
38 using SolvableList = std::list<sat::Solvable>;
39 using EditionToSolvableMap = std::map<Edition, SolvableList >;
40 using ArchToEditionMap = std::map<Arch, EditionToSolvableMap >;
45 None, //<< Just here to support default construction
46 Kernels, //<< Map contains kernel packages, so need to receive special handling and flavour matching
47 RelatedBinaries, //<< Map contains related binary packages, so need to receive special handling and flavour matching
48 Sources //<< Map contains source packages, so when matching those against running we ignore the flavour
51 GroupInfo( const GroupType type = None, std::string flav = "") : groupType(type), groupFlavour( std::move(flav) ) { }
53 ArchToEditionMap archToEdMap; //<< Map of actual packages
54 std::string groupFlavour; //<< This would contain a specific flavour if there is one calculated
56 using GroupMap = std::unordered_map<std::string, GroupInfo>;
58 struct PurgeKernels::Impl {
61 struct utsname unameData;
62 if ( uname( &unameData) == 0 ) {
64 const auto archStr = str::regex_substitute( unameData.machine, str::regex( "^i.86$", str::regex::match_extended ), "i586" );
66 _kernelArch = Arch( archStr );
67 setUnameR( std::string( unameData.release ) );
69 _detectedRunning = true;
71 MIL << "Detected running kernel: " << _runningKernelEdition << " " << _runningKernelFlavour << " " << _kernelArch << std::endl;
74 MIL << "Failed to detect running kernel: " << errno << std::endl;
78 void setUnameR ( const std::string &uname ) {
82 MIL << "Set uname " << uname << std::endl;
84 const std::string flavour = str::regex_substitute( _uname_r, str::regex( ".*-", str::regex::match_extended ), "", true );
85 std::string version = str::regex_substitute( _uname_r, str::regex( "-[^-]*$", str::regex::match_extended | str::regex::newline ), "", true );
87 const std::string release = str::regex_substitute( version, str::regex( ".*-", str::regex::match_extended ), "", true );
89 version = str::regex_substitute( version, str::regex( "-[^-]*$", str::regex::match_extended | str::regex::newline ), "", true );
91 // from purge-kernels script, was copied from kernel-source/rpm/mkspec
92 version = str::regex_substitute( version, str::regex( "\\.0-rc", str::regex::match_extended ), ".rc", true );
93 version = str::regex_substitute( version, str::regex( "-rc\\d+", str::regex::match_extended ), "", true );
94 version = str::regex_substitute( version, str::regex( "-", str::regex::match_extended ), ".", true );
96 _runningKernelEdition = Edition( version, release );
97 _runningKernelFlavour = flavour;
99 MIL << "Parsed info from uname: " << std::endl;
100 MIL << "Kernel Flavour: " << _runningKernelFlavour << std::endl;
101 MIL << "Kernel Edition: " << _runningKernelEdition << std::endl;
104 bool removePackageAndCheck( const sat::Solvable slv, const std::set<sat::Solvable> &keepList , const std::set<sat::Solvable> &removeList ) const;
105 static bool versionMatch ( const Edition &a, const Edition &b );
106 void parseKeepSpec();
107 void fillKeepList(const GroupMap &installedKernels, std::set<sat::Solvable> &keepList , std::set<sat::Solvable> &removeList ) const;
109 std::set<size_t> _keepLatestOffsets = { 0 };
110 std::set<size_t> _keepOldestOffsets;
111 std::set<Edition> _keepSpecificEditions;
112 std::string _uname_r;
113 Edition _runningKernelEdition;
114 Flavour _runningKernelFlavour;
116 std::string _keepSpec = ZConfig::instance().multiversionKernels();
117 bool _keepRunning = true;
118 bool _detectedRunning = false;
122 * tries to remove a the \ref PoolItem \a pi from the pool, solves and checks if no unexpected packages are removed due to the \a validRemovals regex.
123 * If the constraint fails the changes are reverted and \a false is returned.
125 bool PurgeKernels::Impl::removePackageAndCheck( const sat::Solvable slv, const std::set<sat::Solvable> &keepList , const std::set<sat::Solvable> &removeList ) const
127 const filter::ByStatus toBeUninstalledFilter( &ResStatus::isToBeUninstalled );
131 auto pool = ResPool::instance();
133 // make sure the pool is clean
134 if ( !pool.resolver().resolvePool() ) {
135 MIL << "Pool failed to resolve, not doing anything" << std::endl;
139 MIL << "Request to remove package: " << pi << std::endl;
141 //list of packages that are allowed to be removed automatically.
142 const str::regex validRemovals("(kernel-syms(-.*)?|kgraft-patch(-.*)?|kernel-livepatch(-.*)?|.*-kmp(-.*)?)");
144 if ( pi.status().isLocked() ) {
145 MIL << "Package " << pi << " is locked by the user, not removing." << std::endl;
149 //remember which packages are already marked for removal, we do not need to check them again
150 std::set<sat::Solvable> currentSetOfRemovals;
151 for ( const PoolItem & p : pool.byStatus( toBeUninstalledFilter ) ) {
152 currentSetOfRemovals.insert( p.satSolvable() );
155 pi.status().setToBeUninstalled( ResStatus::USER );
157 if ( !pool.resolver().resolvePool() ) {
158 MIL << "Failed to resolve pool, skipping " << pi << std::endl;
159 pool.resolver().problems();
165 std::set<sat::Solvable> removedInThisRun;
166 removedInThisRun.insert( slv );
168 for ( const PoolItem & p : pool.byStatus( toBeUninstalledFilter ) ) {
170 //check if that package is removeable
171 if ( p.status().isByUser() //this was set by us, ignore it
172 || (currentSetOfRemovals.find( p.satSolvable() ) != currentSetOfRemovals.end()) //this was marked by a previous removal, ignore them
176 // remember for later we need remove the debugsource and debuginfo packages as well
177 removedInThisRun.insert( p.satSolvable() );
179 MIL << "Package " << p << " was marked by the solver for removal." << std::endl;
181 // 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 )
182 if ( removeList.find( p.satSolvable() ) != removeList.end() )
185 if ( keepList.find( p.satSolvable() ) != keepList.end() ) {
186 MIL << "Package " << p << " is in keep spec, skipping" << pi << std::endl;
192 if ( !str::regex_match( p.name(), what, validRemovals) ) {
193 MIL << "Package " << p << " should not be removed, skipping " << pi << std::endl;
199 MIL << "Successfully marked package: " << pi << " for removal."<<std::endl;
201 //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
202 MIL << "Trying to remove debuginfo for: " << pi <<"."<<std::endl;
203 for ( sat::Solvable solvable : removedInThisRun ) {
205 if ( solvable.arch() == Arch_noarch ||
206 solvable.arch() == Arch_empty )
209 for ( const char * suffix : { "-debugsource", "-debuginfo" } ) {
211 q.addKind( zypp::ResKind::package );
212 q.addDependency( sat::SolvAttr::provides, Capability( solvable.name()+suffix, Rel::EQ, solvable.edition() ) );
213 q.setInstalledOnly();
216 for ( sat::Solvable debugPackage : q ) {
218 if ( debugPackage.arch() != solvable.arch() )
221 MIL << "Found debug package for " << solvable << " : " << debugPackage << std::endl;
222 //if removing the package fails it will not stop us from going on , so no need to check
223 removePackageAndCheck( debugPackage, keepList, removeList );
227 MIL << "Finished removing debuginfo for: " << pi <<"."<<std::endl;
233 * Return true if \a a == \a b or \a a == (\a b minus rebuild counter)
235 bool PurgeKernels::Impl::versionMatch( const Edition &a, const Edition &b )
240 // the build counter should not be considered here, so if there is one we cut it off
241 const str::regex buildCntRegex( "\\.[0-9]+($|\\.g[0-9a-f]{7}$)", str::regex::match_extended );
243 std::string versionStr = b.asString();
245 if ( buildCntRegex.matches( versionStr.data(), matches ) ) {
246 if ( matches.size() >= 2 ) {
247 versionStr.replace( matches.begin(0), (matches.end(0) - matches.begin(0))+1, matches[1] );
248 return a == Edition(versionStr);
255 * Parse the config line keep spec that tells us which kernels should be kept
257 void PurgeKernels::Impl::parseKeepSpec( )
259 //keep spec parse regex, make sure to edit the group offsets if changing this regex
260 const str::regex specRegex( "^(latest|oldest)([+-][0-9]+)?$", str::regex::match_extended );
262 const unsigned tokenGrp = 1; //index of the group matching the token
263 const unsigned modifierGrp = 2; //index of the group matching the offset modifier
266 MIL << "Parsing keep spec: " << _keepSpec << std::endl;
268 std::vector<std::string> words;
269 str::split( _keepSpec, std::back_inserter(words), ",", str::TRIM );
270 if ( words.empty() ) {
271 WAR << "Invalid keep spec: " << _keepSpec << " using default latest,running." << std::endl;
275 _keepRunning = false;
276 _keepLatestOffsets.clear();
277 _keepOldestOffsets.clear();
279 for ( const std::string &word : words ) {
280 if ( word == "running" ) {
284 if ( !str::regex_match( word, what, specRegex ) ) {
285 _keepSpecificEditions.insert( Edition(word) );
289 auto addKeepOff = []( const auto &off, auto &set, const auto &constraint ){
290 const off_t num = off.empty() ? 0 : str::strtonum<off_t>( off );
291 if ( !constraint(num) ) return false;
292 set.insert( static_cast<size_t>(std::abs(num)) );
296 if ( what[tokenGrp] == "oldest" ) {
297 addKeepOff( what[modifierGrp], _keepOldestOffsets, [ &word ]( off_t num ) {
299 WAR << "Ignoring invalid modifier in keep spec: " << word << ", oldest supports only positive modifiers." << std::endl;
305 addKeepOff( what[modifierGrp], _keepLatestOffsets, [ &word ]( off_t num ) {
307 WAR << "Ignoring invalid modifier in keep spec: " << word << ", latest supports only negative modifiers." << std::endl;
318 * Go over the list of available Editions for each flavour/arch combinations, apply the keep spec and mark the
319 * packages that belong to a matching category as to keep
321 * All packages with Arch_noarch will only be matched against the version but NOT the flavour, reasoning for that is
322 * simply that source flavours not necessarily match the binary flavours. Without a translation table that would not be
323 * doable. This is also what the perl script did.
326 void PurgeKernels::Impl::fillKeepList( const GroupMap &installedKernels, std::set<sat::Solvable> &keepList, std::set<sat::Solvable> &removeList ) const
329 const auto markAsKeep = [ &keepList, &removeList ]( sat::Solvable pck ) {
330 MIL << "Marking package " << pck << " as to keep." << std::endl;
331 keepList.insert( pck ) ;
332 removeList.erase( pck );
335 const auto versionPredicate = []( const auto &edition ){
336 return [ &edition ]( const auto &elem ) {
337 return versionMatch( edition, elem.first );
341 for ( const auto &groupInfo : installedKernels ) {
343 MIL << "Starting with group " << groupInfo.first << std::endl;
345 for ( const auto &archMap : groupInfo.second.archToEdMap ) {
347 MIL << "Starting with arch " << archMap.first << std::endl;
349 size_t currOff = 0; //the current "oldest" offset ( runs from map start to end )
350 size_t currROff = archMap.second.size() - 1; // the current "latest" offset ( runs from map end to start )
353 const EditionToSolvableMap &map = archMap.second;
356 && ( ( archMap.first == _kernelArch && groupInfo.second.groupFlavour == _runningKernelFlavour )
357 || groupInfo.second.groupType == GroupInfo::Sources ) ) {
359 MIL << "Matching packages against running kernel "<< _runningKernelEdition << "-" << _runningKernelFlavour << "-" <<_kernelArch << std::endl;
361 auto it = std::find_if( map.begin(), map.end(), versionPredicate( _runningKernelEdition ) );
362 if ( it == map.end() ) {
364 // If we look at Sources we cannot match the flavour but we still want to keep on checking the rest of the keep spec
365 if ( groupInfo.second.groupType != GroupInfo::Sources ) {
366 MIL << "Running kernel "<< _runningKernelEdition << "-" << _runningKernelFlavour << "-" <<_kernelArch << " not installed."<<std::endl;
367 MIL << "NOT removing any packages for flavor "<<_runningKernelFlavour<<"-"<<_kernelArch<<" ."<<std::endl;
369 for ( const auto &kernelMap : map ) {
370 for( sat::Solvable pck : kernelMap.second )
377 // there could be multiple matches here because of rebuild counter, lets try to find the last one
378 MIL << "Found possible running candidate edition: " << it->first << std::endl;
380 for ( nit++ ; nit != map.end() && versionMatch( _runningKernelEdition, nit->first ) ; nit++ ) {
381 MIL << "Found possible more recent running candidate edition: " << nit->first << std::endl;
386 // mark all packages of the running version as keep
387 if ( it != map.end() ) {
388 for( sat::Solvable pck : it->second ) {
394 for ( const auto &kernelMap : map ) {
395 //if we find one of the running offsets in the keepspec, we add the kernel id the the list of packages to keep
396 if ( _keepOldestOffsets.find( currOff ) != _keepOldestOffsets.end() || _keepLatestOffsets.find( currROff ) != _keepLatestOffsets.end() ) {
397 std::for_each( kernelMap.second.begin(), kernelMap.second.end(), markAsKeep );
402 // a kernel package might be explicitely locked by version
403 // We need to go over all package name provides ( provides is named like the package ) and match
404 // them against the specified version to know which ones to keep. (bsc#1176740 bsc#1176192)
405 std::for_each( kernelMap.second.begin(), kernelMap.second.end(), [ & ]( sat::Solvable solv ){
406 for ( Capability prov : solv.provides() ) {
407 if ( prov.detail().name() == solv.name() && _keepSpecificEditions.count( prov.detail().ed() ) ) {
417 PurgeKernels::PurgeKernels()
418 : _pimpl( new Impl() )
423 void PurgeKernels::markObsoleteKernels()
425 MIL << std::endl << "--------------------- Starting to mark obsolete kernels ---------------------"<<std::endl;
427 if ( _pimpl->_keepSpec.empty() ) {
428 WAR << "Keep spec is empty, removing nothing." << std::endl;
432 _pimpl->parseKeepSpec();
434 if ( _pimpl->_keepRunning && !_pimpl->_detectedRunning ) {
435 WAR << "Unable to detect running kernel, but keeping the running kernel was requested. Not removing any packages." << std::endl;
439 auto pool = ResPool::instance();
440 pool.resolver().setForceResolve( true ); // set allow uninstall flag
442 const filter::ByStatus toBeUninstalledFilter( &ResStatus::isToBeUninstalled );
444 // kernel flavour regex
445 const str::regex kernelFlavourRegex("^kernel-(.*)$");
447 // the map of all installed kernel packages, grouped by Flavour -> Arch -> Version -> (List of all packages in that category)
448 // devel and source packages are grouped together
449 GroupMap installedKrnlPackages;
452 // packages that we plan to remove
453 std::set<sat::Solvable> packagesToRemove;
455 const auto addPackageToMap = [&installedKrnlPackages, &packagesToRemove] ( const GroupInfo::GroupType type, const std::string &ident, const std::string &flavour, const sat::Solvable &installedKrnlPck ) {
457 if ( !installedKrnlPackages.count( ident ) )
458 installedKrnlPackages.insert( std::make_pair( ident, GroupInfo(type, flavour) ) );
460 auto &groupInfo = installedKrnlPackages[ ident ];
461 if ( groupInfo.groupType != type || groupInfo.groupFlavour != flavour ) {
462 ERR << "Got inconsistent type and flavour for ident this is a BUG: " << ident << std::endl
463 << "Original Flavour-Type: "<<groupInfo.groupFlavour<<"-"<<groupInfo.groupType << std::endl
464 << "Competing Flavour-Type: "<< flavour << "-" << type << std::endl;
467 const auto currArch = installedKrnlPck.arch();
468 if ( !groupInfo.archToEdMap.count( currArch ) )
469 groupInfo.archToEdMap.insert( std::make_pair( currArch , EditionToSolvableMap {} ) );
471 auto &editionToSolvableMap = groupInfo.archToEdMap[ currArch ];
473 // calculate the "shortest" or most generic edition of all the package name provides
474 // ( the key of the provides is the package name ). This generic edition is used to
475 // group the packages together. This should get us around the issue that uname -r does
476 // not represent the actual rpm package version anymore. ( bsc#1176740 )
477 auto currCount = INT_MAX;
479 for ( Capability prov : installedKrnlPck.provides() ) {
480 if ( prov.detail().name() == installedKrnlPck.name() ) {
481 if ( edToUse == Edition::noedition ) {
482 edToUse = installedKrnlPck.edition();
483 const auto &relStr = edToUse.release();
484 currCount = std::count( relStr.begin(), relStr.end(), '.');
486 const auto &pckEd = prov.detail().ed();
487 const auto &relStr = pckEd.release();
488 if ( const auto pntCnt = std::count( relStr.begin(), relStr.end(), '.'); pntCnt < currCount ) {
496 if ( !editionToSolvableMap.count( edToUse ) )
497 editionToSolvableMap.insert( std::make_pair( edToUse, SolvableList{} ) );
499 editionToSolvableMap[edToUse].push_back( installedKrnlPck );
501 //in the first step we collect all packages in this list, then later we will remove the packages we want to explicitely keep
502 packagesToRemove.insert( installedKrnlPck );
505 // the set of satSolvables that have to be kept always
506 std::set<sat::Solvable> packagesToKeep;
508 //collect the list of installed kernel packages
510 q.addKind( zypp::ResKind::package );
511 q.addAttribute( sat::SolvAttr::provides, "multiversion(kernel)" );
512 q.setInstalledOnly();
515 MIL << "Searching for obsolete multiversion kernel packages." << std::endl;
517 for ( sat::Solvable installedKrnlPck : q ) {
519 MIL << "Found installed multiversion kernel package " << installedKrnlPck << std::endl;
521 if ( installedKrnlPck.provides().matches(Capability("kernel-uname-r")) ) {
522 MIL << "Identified as a kernel package " << std::endl;
524 // we group kernel packages by flavour
526 str::regex_match( installedKrnlPck.name(), what, kernelFlavourRegex );
527 if ( what[1].empty() ) {
528 WAR << "Could not detect flavour for: " << installedKrnlPck << " ...skipping" << std::endl;
532 std::string flavour = what[1];
534 // XXX: No dashes in flavor names
535 const auto dash = flavour.find_first_of('-');
536 if ( dash != std::string::npos ) {
537 flavour = flavour.substr( 0, dash );
540 // 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
541 addPackageToMap( GroupInfo::Kernels, flavour, flavour, installedKrnlPck );
545 // if adapting the groups do not forget to explicitely handle the group when querying the matches
546 const str::regex explicitelyHandled("kernel-syms(-.*)?|kernel(-.*)?-devel");
548 MIL << "Not a kernel package, inspecting more closely " << std::endl;
550 // we directly handle all noarch packages that export multiversion(kernel)
551 if ( installedKrnlPck.arch() == Arch_noarch ) {
553 MIL << "Handling package explicitely due to architecture (noarch)."<< std::endl;
554 addPackageToMap( GroupInfo::Sources, installedKrnlPck.name(), "", installedKrnlPck );
556 } else if ( str::smatch match; str::regex_match( installedKrnlPck.name(), match, explicitelyHandled ) ) {
558 // try to get the flavour from the name
559 // if we have a kernel-syms getting no flavour means we have the "default" one, otherwise we use the flavour
560 // getting no flavour for a kernel(-*)?-devel means we have the kernel-devel package otherwise the flavour specific one
561 // ...yes this is horrible
564 // first group match is a kernel-syms
565 if ( match.size() > 1 && match[1].size() )
566 flav = match[1].substr(1);
567 // second group match is a kernel-flavour-devel
568 else if ( match.size() > 2 && match[2].size() )
569 flav = match[2].substr(1);
570 else if ( installedKrnlPck.name() == "kernel-syms" )
573 MIL << "Handling package explicitely due to name match."<< std::endl;
574 addPackageToMap ( GroupInfo::RelatedBinaries, installedKrnlPck.name(), flav, installedKrnlPck );
576 MIL << "Package not explicitely handled" << std::endl;
582 MIL << "Grouped packages: " << std::endl;
583 std::for_each( installedKrnlPackages.begin(), installedKrnlPackages.end(),[]( const auto &ident ){
584 MIL << "\tGroup ident: "<<ident.first<<std::endl;
585 MIL << "\t Group type: "<<ident.second.groupType<<std::endl;
586 MIL << "\t Group flav: "<<ident.second.groupFlavour<<std::endl;
587 std::for_each( ident.second.archToEdMap.begin(), ident.second.archToEdMap.end(), []( const auto &arch) {
588 MIL << "\t\tArch: "<<arch.first<<std::endl;
589 std::for_each( arch.second.begin(), arch.second.end(), []( const auto &edition) {
590 MIL << "\t\t\tEdition: "<<edition.first<<std::endl;
591 std::for_each( edition.second.begin(), edition.second.end(), []( const auto &packageId) {
592 MIL << "\t\t\t\t "<<sat::Solvable(packageId)<<std::endl;
598 _pimpl->fillKeepList( installedKrnlPackages, packagesToKeep, packagesToRemove );
600 for ( sat::Solvable slv : packagesToRemove )
601 _pimpl->removePackageAndCheck( slv, packagesToKeep, packagesToRemove );
604 void PurgeKernels::setUnameR( const std::string &val )
606 _pimpl->setUnameR( val );
609 std::string PurgeKernels::unameR() const
611 return _pimpl->_uname_r;
614 void PurgeKernels::setKernelArch(const Arch &arch)
616 _pimpl->_kernelArch = arch;
619 Arch PurgeKernels::kernelArch() const
621 return _pimpl->_kernelArch;
624 void PurgeKernels::setKeepSpec( const std::string &val )
626 _pimpl->_keepSpec = val;
629 std::string PurgeKernels::keepSpec() const
631 return _pimpl->_keepSpec;