-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkpatch.py
348 lines (276 loc) · 12.7 KB
/
kpatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# kpatch patch module packages management plugin for dnf
#
# Copyright (C) 2020 Julien Thierry <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA,
# 02110-1301, USA.
"""
The DNF plugin helps customers to install kpatch-patch packages
when the kernel is upgraded and filter kernel-core packages that
are supported by the kpatch team.
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import configparser
import os.path
import re
from dnfpluginscore import _, logger
import dnf
import dnf.callback
import dnf.cli
import dnf.exceptions
import dnf.transaction
import hawkey
KPATCH_PLUGIN_NAME = "kpatch"
KPATCH_UPDATE_OPT = "autoupdate"
KPATCH_FILTER_OPT = "autofilter"
KERNEL_PKG_NAME = "kernel-core"
# Dnf offers to lookup and read the plugin config file but doesn't provide
# a way to update that file nor to get the name...
def _get_plugin_cfg_file(base_conf):
files = ['%s/%s.conf' % (path, KPATCH_PLUGIN_NAME) for path in base_conf.pluginconfpath]
for file in files:
if os.path.isfile(file):
return file
return None
def _kpp_name_from_kernel_pkg(kernel_pkg):
kernel_release = re.match(r"(.*)\.el.*", kernel_pkg.release).group(1)
kpp_kernel_release = kernel_release.replace(".", "_")
kpp_kernel_version = kernel_pkg.version.replace(".", "_")
return "kpatch-patch-{}-{}".format(kpp_kernel_version, kpp_kernel_release)
def _install_kpp_pkg(dnf_base, kernel_pkg):
kpp_pkg_name = _kpp_name_from_kernel_pkg(kernel_pkg)
kpp_pkgs_query = dnf_base.sack.query().filter(name=kpp_pkg_name,
arch=kernel_pkg.arch)
kpp_sltr = dnf.selector.Selector(dnf_base.sack)
kpp_sltr.set(pkg=kpp_pkgs_query.latest())
dnf_base.goal.install(select=kpp_sltr, optional=not dnf_base.conf.strict)
class KpatchCmd(dnf.cli.Command):
""" Extend DNF with kpatch specific commands """
aliases = ('kpatch',)
summary = _('Toggles automatic installation of kpatch-patch packages')
def __init__(self, cli):
super().__init__(cli)
self.cfg_file = _get_plugin_cfg_file(self.base.conf)
@staticmethod
def set_argparser(parser):
"""
argparse python class
"""
parser.add_argument('action',
metavar="auto-update|manual-update|" \
"auto-filter|no-filter|install|status|" \
"auto|manual"
)
def configure(self):
"""
configure DemandSheet
Collection of demands that different CLI parts have on other parts
"""
demands = self.cli.demands
demands.root_user = True
if self.opts.action in ["auto-update", "install", "status", "auto"]:
demands.resolving = True
demands.sack_activation = True
demands.available_repos = True
else:
demands.resolving = False
demands.sack_activation = False
demands.available_repos = False
def _list_missing_kpp_pkgs(self):
kpps = []
installed_kernels = self.base.sack.query().installed().filter(name=KERNEL_PKG_NAME)
for kernel_pkg in installed_kernels:
kpp_pkg_name = _kpp_name_from_kernel_pkg(kernel_pkg)
installed = self.base.sack.query().installed().filter(name=kpp_pkg_name).run()
if installed:
sub_q = self.base.sack.query().filter(
name=kpp_pkg_name,
release=installed[0].release,
version=installed[0].version
)
kpp_pkgs_query = self.base.sack.query().filter(
name=kpp_pkg_name,
arch=kernel_pkg.arch
).latest().difference(sub_q)
else:
kpp_pkgs_query = self.base.sack.query().filter(
name=kpp_pkg_name,
arch=kernel_pkg.arch
).latest()
for pkg in kpp_pkgs_query:
kpps.append(str(pkg))
return kpps
def _install_missing_kpp_pkgs(self):
installed_kernels = self.base.sack.query().installed().filter(name=KERNEL_PKG_NAME)
for kernel_pkg in installed_kernels:
_install_kpp_pkg(self.base, kernel_pkg)
def _read_conf(self):
if self.cfg_file is None:
logger.warning("Couldn't find configuration file")
return None
try:
parser = configparser.ConfigParser()
parser.read(self.cfg_file)
return parser
except Exception as e:
raise dnf.exceptions.Error(_("Parsing file failed: {}").format(str(e)))
def _update_plugin_cfg(self, option, value):
if self.cfg_file is None:
logger.warning("Couldn't find configuration file")
return
conf = self._read_conf()
if conf is None:
return
if not conf.has_section('main'):
conf.add_section('main')
conf.set('main', option, str(value))
try:
with open(self.cfg_file, 'w', encoding='utf-8') as cfg_stream:
conf.write(cfg_stream)
except Exception as e:
raise dnf.exceptions.Error(_("Failed to update conf file: {}").format(str(e)))
def run(self):
"""
Decision tree, execution based on config
"""
action = self.opts.action
if action in ("auto-update", "auto"):
self._install_missing_kpp_pkgs()
self._update_plugin_cfg(KPATCH_UPDATE_OPT, True)
logger.info(_("Kpatch update setting: {}").format(action))
elif action in ("manual-update", "manual"):
self._update_plugin_cfg(KPATCH_UPDATE_OPT, False)
logger.info(_("Kpatch update setting: {}").format(action))
elif action == "auto-filter":
self._update_plugin_cfg(KPATCH_FILTER_OPT, True)
logger.info(_("Kpatch filter setting: {}").format(action))
elif action == "no-filter":
self._update_plugin_cfg(KPATCH_FILTER_OPT, False)
logger.info(_("Kpatch filter setting: {}").format(action))
elif action == "status":
conf = self._read_conf()
kp_status = "manual-update"
if (conf is not None and conf.has_section('main') and
conf.has_option('main', KPATCH_UPDATE_OPT) and
conf.getboolean('main', KPATCH_UPDATE_OPT)):
kp_status = "auto-update"
logger.info(_("Kpatch update setting: {}").format(kp_status))
kp_status = "no-filter"
if (conf is not None and conf.has_section('main') and
conf.has_option('main', KPATCH_FILTER_OPT) and
conf.getboolean('main', KPATCH_FILTER_OPT)):
kp_status = "auto-filter"
logger.info(_("Kpatch filter setting: {}").format(kp_status))
kpps = self._list_missing_kpp_pkgs()
if kpps:
logger.info(_("Available patches: {}").format(", ".join(kpps)))
elif action == "install":
self._install_missing_kpp_pkgs()
else:
raise dnf.exceptions.Error(_("Invalid argument: {}").format(action))
class KpatchPlugin(dnf.Plugin):
"""
The DNF plugin helps customers to install kpatch-patch packages
when the kernel is upgraded and filter kernel-core packages that
are supported by the kpatch team.
"""
name = KPATCH_PLUGIN_NAME
# list of package names to filter based on kpatch support
kernel_pkg_names = ['kernel', 'kernel-core', 'kernel-modules',
'kernel-modules-core', 'kernel-modules-extra']
kpatch_requirement = ['kernel', 'kernel-uname-r']
def __init__(self, base, cli):
super().__init__(base, cli)
self._commiting = False
self._autoupdate = False
self._autofilter = False
if cli is not None:
cli.register_command(KpatchCmd)
def config(self):
parser = self.read_config(self.base.conf)
try:
self._autoupdate = (parser.has_section('main')
and parser.has_option('main', KPATCH_UPDATE_OPT)
and parser.getboolean('main', KPATCH_UPDATE_OPT))
self._autofilter = (parser.has_section('main')
and parser.has_option('main', KPATCH_FILTER_OPT)
and parser.getboolean('main', KPATCH_FILTER_OPT))
except Exception as e:
logger.warning(_("Parsing file failed: {}").format(str(e)))
def _commit_changes(self):
self._commiting = True
# Get dnf's dependency manager to resolve missing deps for added pkgs
self.base.resolve(self.cli.demands.allow_erasing)
self._commiting = False
def sack(self):
if not self._autofilter:
return
print('Please note, kpatch filter is enabled, only kpatch supported kernels are shown.')
# This query gradually accumulates all kernel packages that should be
# offered to the user (kernels for which exists kpatch-patch-* package
# that requires it). Start with empty query.
kernels_keep = self.base.sack.query().filterm(empty=True)
# pre-filter all available versions of the kernel* packages
kernels_query = self.base.sack.query(flags=hawkey.IGNORE_EXCLUDES)
kernels_query.filterm(name=self.kernel_pkg_names)
# any installed kernel version should not be excluded
kernels_query = kernels_query.available()
# Add to the kernels_keep query all kernel-core package versions that are
# required by any of kpatch-patch-* packages.
kpatch_query = self.base.sack.query(flags=hawkey.IGNORE_EXCLUDES)
kpatch_query.filterm(name__glob="kpatch-patch-*")
for kpatch_pkg in kpatch_query:
for require in kpatch_pkg.requires:
require_parsed = str(require).split(' ')
if len(require_parsed) < 3:
continue
if require_parsed[0] in self.kpatch_requirement:
# get kernel-core package providing "kernel-uname-r = <kpatch_pkg.evra>"
kernel_core = kernels_query.filter(provides=require)
kernel_evr = None
for kernel_core_pkg in kernel_core:
# assume that all such packages have the same evr
kernel_evr = kernel_core_pkg.evr
break
if kernel_evr is not None:
kernels_keep = kernels_keep.union(kernels_query.filter(evr=kernel_evr))
# assume the is only one kernel-uname-r requirement
break
# exclude all kernel-core packages that are not in kernels_keep query
self.base.sack.add_excludes(kernels_query.difference(kernels_keep))
def resolved(self):
# Calling self.base.resolve() will run this callback again
if not self._autoupdate or self._commiting:
return
need_kpp_for = []
explicit_kpp_install = []
for tr_item in self.base.transaction:
# It might not be safe to check tr_item.pkg.name as there might be
# some dnf internal transaction items not linked to any package.
# Check first whether the action is a package related action
if tr_item.action in dnf.transaction.FORWARD_ACTIONS:
if tr_item.pkg.name == KERNEL_PKG_NAME:
need_kpp_for.append(tr_item.pkg)
elif tr_item.pkg.name.startswith("kpatch-patch-"):
explicit_kpp_install.append(tr_item.pkg.name)
# If the user already requested a kpatch-patch package, don't override it
# nor conflict with it
need_kpp_for = [pkg for pkg in need_kpp_for
if _kpp_name_from_kernel_pkg(pkg) not in explicit_kpp_install]
for kernel_pkg in need_kpp_for:
_install_kpp_pkg(self.base, kernel_pkg)
if need_kpp_for:
self._commit_changes()