Wednesday, January 5, 2022

[SOLVED] Listing packages that will be removed by "yum autoremove"?

Issue

Running CentOS 7.6

I am currently uninstalling Java in my kickstart post-install script yum remove java-1.7*. When java is uninstalled, it orphans the python-lxml package which is then removed by a yum autoremove -y as it is now a leaf.

I then use ansible to do a bunch of configuration, which fails in a task that is trying to use "python-lxml".

Is there a way to list all of the packages "yum autoremove" will delete and mark them as do not delete ?


Solution

I ended up looking at the repoquery API and package-cleanup man page. Using package-cleanup I was able to list all the leaves on the system by passing the --leaves and -all flags. I then piped that input into repoquery to figure out which leaves were marked as a dependency. --qf allows me to specify the query format. Looking at the repoquery API I can see various api calls I can make.

querytags = [ 'name', 'version', 'release', 'epoch', 'arch', 'summary',
              'description', 'packager', 'url', 'buildhost', 'sourcerpm',
              'vendor', 'group', 'license', 'buildtime', 'filetime',
              'installedsize', 'archivesize', 'packagesize', 'repoid', 
              'requires', 'provides', 'conflicts', 'obsoletes',
              'weak_requires', 'info_requires',
              'weak_reverse_requires', 'info_reverse_requires', 
              'relativepath', 'hdrstart', 'hdrend', 'id',
              'checksum', 'pkgid', 'committer', 'committime',
              'ui_evr', 'evr', 'ui_nevra', 'ui_envra',
              'ui_from_repo', 'base_package_name', 'size', 'xattr_origin_url',
              'ui_evra', 'ui_nevr', 'na', 'vr', 'vra', 'evr', 'evra',
              'nvr', 'nvra', 'nevr', 'nevra', 'envr', 'envra',

              'repo.<attr of the repo object>',
              'yumdb.<attr of the yumdb object>',
              '<attr of the yum object>'
            ]

Noting nvra (name-ver-rel.arch) and yumdb.<attr> tags I am able to query yumdb for the full name and reason of each leaf passed in.

I can then do basic grep and sed to extract just the package name that I can store in an array.

package-cleanup --leaves -q --all | xargs repoquery --installed --qf '%\{nvra} - %\{yumdb_info.reason}' | grep -- '- dep' | sed "s/\s.*$//"

Using this array I was able to step through and set each item's reason to user so yum autoremove -y will not delete what I intended to be there.

yumdb set reason user $array_item

Or if you want the mother of all bash one liners...

array=$(package-cleanup --leaves -q --all | xargs repoquery --installed --qf '%{nvra} - %{yumdb_info.reason}' | grep -- '- dep' | sed "s/\s.*$//"); a=($array); if [ ${#a[@]} -gt "0" ]; then for i in "${array[@]}"; do yumdb set reason user $i; done; fi


Answered By - wski