forked from redhat-performance/tuned
/
plugin_bootloader.py
302 lines (270 loc) · 11.4 KB
/
plugin_bootloader.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
from . import base
from .decorators import *
import tuned.logs
from . import exceptions
from tuned.utils.commands import commands
import tuned.consts as consts
import os
import re
import tempfile
log = tuned.logs.get()
class BootloaderPlugin(base.Plugin):
"""
Plugin for tuning bootloader options.
Currently only grub2 is supported and reboot is required to apply the tunings.
These tunings are unloaded only on profile change followed by reboot.
"""
def __init__(self, *args, **kwargs):
if not os.path.isfile(consts.GRUB2_TUNED_TEMPLATE_PATH):
raise exceptions.NotSupportedPluginException("Required GRUB2 template not found, disabling plugin.")
super(BootloaderPlugin, self).__init__(*args, **kwargs)
self._cmd = commands()
def _instance_init(self, instance):
instance._has_dynamic_tuning = False
instance._has_static_tuning = True
# controls grub2_cfg rewrites in _instance_post_static
self.update_grub2_cfg = False
self._initrd_remove_dir = False
self._initrd_dst_img_val = None
self._cmdline_val = ""
self._initrd_val = ""
self._grub2_cfg_file_name = self._get_grub2_cfg_file()
def _instance_cleanup(self, instance):
pass
@classmethod
def _get_config_options(cls):
return {
"grub2_cfg_file": None,
"initrd_dst_img": None,
"initrd_add_img": None,
"initrd_add_dir": None,
"initrd_remove_dir": None,
"cmdline": None,
}
def _get_effective_options(self, options):
"""Merge provided options with plugin default options and merge all cmdline.* options."""
effective = self._get_config_options().copy()
cmdline_keys = []
for key in options:
if str(key).startswith("cmdline"):
cmdline_keys.append(key)
elif key in effective:
effective[key] = options[key]
else:
log.warn("Unknown option '%s' for plugin '%s'." % (key, self.__class__.__name__))
cmdline_keys.sort()
cmdline = ""
for key in cmdline_keys:
val = options[key]
if val is None or val == "":
continue
op = val[0]
vals = val[1:].strip()
if op == "+" and vals != "":
cmdline += " " + vals
elif op == "-" and vals != "":
for p in vals.split():
regex = re.escape(p)
cmdline = re.sub(r"(\A|\s)" + regex + r"(?=\Z|\s)", r"", cmdline)
else:
cmdline += " " + val
cmdline = cmdline.strip()
if cmdline != "":
effective["cmdline"] = cmdline
return effective
def _get_grub2_cfg_file(self):
for f in consts.GRUB2_CFG_FILES:
if os.path.exists(f):
return f
return None
def _patch_bootcmdline(self, d):
return self._cmd.add_modify_option_in_file(consts.BOOT_CMDLINE_FILE, d)
def _remove_grub2_tuning(self):
if self._grub2_cfg_file_name is None:
log.error("cannot find grub.cfg to patch, you need to regenerate it by hand using grub2-mkconfig")
return
self._patch_bootcmdline({consts.BOOT_CMDLINE_TUNED_VAR : "", consts.BOOT_CMDLINE_INITRD_ADD_VAR : ""})
self._cmd.add_modify_option_in_file(self._grub2_cfg_file_name, {"set\s+" + consts.GRUB2_TUNED_VAR : "", "set\s+" + consts.GRUB2_TUNED_INITRD_VAR : ""}, add = False)
if self._initrd_dst_img_val is not None:
log.info("removing initrd image '%s'" % self._initrd_dst_img_val)
self._cmd.unlink(self._initrd_dst_img_val)
def _instance_unapply_static(self, instance, full_rollback = False):
if full_rollback:
log.info("removing grub2 tuning previously added by Tuned")
self._remove_grub2_tuning()
def _grub2_cfg_unpatch(self, grub2_cfg):
log.debug("unpatching grub.cfg")
cfg = re.sub(r"^\s*set\s+" + consts.GRUB2_TUNED_VAR + "\s*=.*\n", "", grub2_cfg, flags = re.MULTILINE)
grub2_cfg = re.sub(r" *\$" + consts.GRUB2_TUNED_VAR, "", cfg, flags = re.MULTILINE)
cfg = re.sub(r"^\s*set\s+" + consts.GRUB2_TUNED_INITRD_VAR + "\s*=.*\n", "", grub2_cfg, flags = re.MULTILINE)
grub2_cfg = re.sub(r" *\$" + consts.GRUB2_TUNED_INITRD_VAR, "", cfg, flags = re.MULTILINE)
cfg = re.sub(consts.GRUB2_TEMPLATE_HEADER_BEGIN + r"\n", "", grub2_cfg, flags = re.MULTILINE)
return re.sub(consts.GRUB2_TEMPLATE_HEADER_END + r"\n+", "", cfg, flags = re.MULTILINE)
def _grub2_cfg_patch_initial(self, grub2_cfg, d):
log.debug("initial patching of grub.cfg")
s = r"\1\n\n" + consts.GRUB2_TEMPLATE_HEADER_BEGIN + "\n"
for opt in d:
s += r"set " + self._cmd.escape(opt) + "=\"" + self._cmd.escape(d[opt]) + "\"\n"
s += consts.GRUB2_TEMPLATE_HEADER_END + r"\n"
grub2_cfg = re.sub(r"^(\s*###\s+END\s+[^#]+/00_header\s+### *)\n", s, grub2_cfg, flags = re.MULTILINE)
d2 = {"linux" : consts.GRUB2_TUNED_VAR, "initrd" : consts.GRUB2_TUNED_INITRD_VAR}
for i in d2:
# add tuned parameters to all kernels
grub2_cfg = re.sub(r"^(\s*" + i + r"(16|efi)?\s+.*)$", r"\1 $" + d2[i], grub2_cfg, flags = re.MULTILINE)
# remove tuned parameters from rescue kernels
grub2_cfg = re.sub(r"^(\s*" + i + r"(?:16|efi)?\s+\S+rescue.*)\$" + d2[i] + r" *(.*)$", r"\1\2", grub2_cfg, flags = re.MULTILINE)
# fix whitespaces in rescue kernels
grub2_cfg = re.sub(r"^(\s*" + i + r"(?:16|efi)?\s+\S+rescue.*) +$", r"\1", grub2_cfg, flags = re.MULTILINE)
return grub2_cfg
def _grub2_default_env_patch(self):
grub2_default_env = self._cmd.read_file(consts.GRUB2_DEFAULT_ENV_FILE)
if len(grub2_default_env) <= 0:
log.error("error reading '%s'" % consts.GRUB2_DEFAULT_ENV_FILE)
return False
d = {"GRUB_CMDLINE_LINUX_DEFAULT" : consts.GRUB2_TUNED_VAR, "GRUB_INITRD_OVERLAY" : consts.GRUB2_TUNED_INITRD_VAR}
write = False
for i in d:
if re.search(r"^[^#]*\b" + i + r"\s*=.*\\\$" + d[i] + r"\b.*$", grub2_default_env, flags = re.MULTILINE) is None:
write = True
if grub2_default_env[-1] != "\n":
grub2_default_env += "\n"
grub2_default_env += i + "=\"${" + i + ":+$" + i + r" }\$" + d[i] + "\"\n"
if write:
log.debug("patching '%s'" % consts.GRUB2_DEFAULT_ENV_FILE)
self._cmd.write_to_file(consts.GRUB2_DEFAULT_ENV_FILE, grub2_default_env)
return True
def _grub2_cfg_patch(self, d):
log.debug("patching grub.cfg")
if self._grub2_cfg_file_name is None:
log.error("cannot find grub.cfg to patch, you need to regenerate it by hand by grub2-mkconfig")
return False
grub2_cfg = self._cmd.read_file(self._grub2_cfg_file_name)
if len(grub2_cfg) <= 0:
log.error("error patching %s, you need to regenerate it by hand by grub2-mkconfig" % self._grub2_cfg_file_name)
return False
log.debug("adding boot command line parameters to '%s'" % self._grub2_cfg_file_name)
grub2_cfg_new = grub2_cfg
patch_initial = False
for opt in d:
(grub2_cfg_new, nsubs) = re.subn(r"\b(set\s+" + opt + "\s*=).*$", r"\1" + "\"" + d[opt] + "\"", grub2_cfg_new, flags = re.MULTILINE)
if nsubs < 1 or re.search(r"\$" + opt, grub2_cfg, flags = re.MULTILINE) is None:
patch_initial = True
# workaround for rhbz#1442117
if len(re.findall(r"\$" + consts.GRUB2_TUNED_VAR, grub2_cfg, flags = re.MULTILINE)) != \
len(re.findall(r"\$" + consts.GRUB2_TUNED_INITRD_VAR, grub2_cfg, flags = re.MULTILINE)):
patch_initial = True
if patch_initial:
grub2_cfg_new = self._grub2_cfg_patch_initial(self._grub2_cfg_unpatch(grub2_cfg), d)
self._cmd.write_to_file(self._grub2_cfg_file_name, grub2_cfg_new)
self._grub2_default_env_patch()
return True
def _grub2_update(self):
self._grub2_cfg_patch({consts.GRUB2_TUNED_VAR : self._cmdline_val, consts.GRUB2_TUNED_INITRD_VAR : self._initrd_val})
self._patch_bootcmdline({consts.BOOT_CMDLINE_TUNED_VAR : self._cmdline_val, consts.BOOT_CMDLINE_INITRD_ADD_VAR : self._initrd_val})
def _init_initrd_dst_img(self, name):
if self._initrd_dst_img_val is None:
self._initrd_dst_img_val = os.path.join(consts.BOOT_DIR, os.path.basename(name))
def _install_initrd(self, img):
log.info("installing initrd image as '%s'" % self._initrd_dst_img_val)
img_name = os.path.basename(self._initrd_dst_img_val)
if not self._cmd.copy(img, self._initrd_dst_img_val):
return False
self.update_grub2_cfg = True
curr_cmdline = self._cmd.read_file("/proc/cmdline").rstrip()
initrd_grubpath = "/"
lc = len(curr_cmdline)
if lc:
path = re.sub(r"^\s*BOOT_IMAGE=\s*(\S*/).*$", "\\1", curr_cmdline)
if len(path) < lc:
initrd_grubpath = path
self._initrd_val = os.path.join(initrd_grubpath, img_name)
return True
@command_custom("grub2_cfg_file")
def _grub2_cfg_file(self, enabling, value, verify, ignore_missing):
# nothing to verify
if verify:
return None
if enabling and value is not None:
self._grub2_cfg_file_name = str(value)
@command_custom("initrd_dst_img")
def _initrd_dst_img(self, enabling, value, verify, ignore_missing):
# nothing to verify
if verify:
return None
if enabling and value is not None:
self._initrd_dst_img_val = str(value)
if self._initrd_dst_img_val == "":
return False
if self._initrd_dst_img_val[0] != "/":
self._initrd_dst_img_val = os.path.join(consts.BOOT_DIR, self._initrd_dst_img_val)
@command_custom("initrd_remove_dir")
def _initrd_remove_dir(self, enabling, value, verify, ignore_missing):
# nothing to verify
if verify:
return None
if enabling and value is not None:
self._initrd_remove_dir = self._cmd.get_bool(value) == "1"
@command_custom("initrd_add_img", per_device = False, priority = 10)
def _initrd_add_img(self, enabling, value, verify, ignore_missing):
# nothing to verify
if verify:
return None
if enabling and value is not None:
src_img = str(value)
self._init_initrd_dst_img(src_img)
if src_img == "":
return False
if not self._install_initrd(src_img):
return False
@command_custom("initrd_add_dir", per_device = False, priority = 10)
def _initrd_add_dir(self, enabling, value, verify, ignore_missing):
# nothing to verify
if verify:
return None
if enabling and value is not None:
src_dir = str(value)
self._init_initrd_dst_img(src_dir)
if src_dir == "":
return False
if not os.path.isdir(src_dir):
log.error("error: cannot create initrd image, source directory '%s' doesn't exist" % src_dir)
return False
log.info("generating initrd image from directory '%s'" % src_dir)
(fd, tmpfile) = tempfile.mkstemp(prefix = "tuned-bootloader-", suffix = ".tmp")
log.debug("writing initrd image to temporary file '%s'" % tmpfile)
os.close(fd)
(rc, out) = self._cmd.execute("find . | cpio -co > %s" % tmpfile, cwd = src_dir, shell = True)
log.debug("cpio log: %s" % out)
if rc != 0:
log.error("error generating initrd image")
self._cmd.unlink(tmpfile, no_error = True)
return False
self._install_initrd(tmpfile)
self._cmd.unlink(tmpfile)
if self._initrd_remove_dir:
log.info("removing directory '%s'" % src_dir)
self._cmd.rmtree(src_dir)
@command_custom("cmdline", per_device = False, priority = 10)
def _cmdline(self, enabling, value, verify, ignore_missing):
v = self._variables.expand(self._cmd.unquote(value))
if verify:
cmdline = self._cmd.read_file("/proc/cmdline")
if len(cmdline) == 0:
return None
cmdline_set = set(cmdline.split())
value_set = set(v.split())
cmdline_intersect = cmdline_set.intersection(value_set)
if cmdline_intersect == value_set:
log.info(consts.STR_VERIFY_PROFILE_VALUE_OK % ("cmdline", str(value_set)))
return True
else:
log.error(consts.STR_VERIFY_PROFILE_VALUE_FAIL % ("cmdline", str(cmdline_intersect), str(value_set)))
return False
if enabling and value is not None:
log.info("installing additional boot command line parameters to grub2")
self.update_grub2_cfg = True
self._cmdline_val = v
def _instance_post_static(self, instance, enabling):
if enabling and self.update_grub2_cfg:
self._grub2_update()
self.update_grub2_cfg = False