1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """VPP Grub Utility Library."""
18 from vpplib.VPPUtil import VPPUtil
20 __all__ = ['VppGrubUtil']
23 class VppGrubUtil(object):
24 """ VPP Grub Utilities."""
26 def _get_current_cmdline(self):
28 Using /proc/cmdline return the current grub cmdline
30 :returns: The current grub cmdline
34 # Get the memory information using /proc/meminfo
35 cmd = 'sudo cat /proc/cmdline'
36 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
38 raise RuntimeError('{} on node {} {} {}'.
39 format(cmd, self._node['host'],
42 self._current_cmdline = stdout.strip('\n')
44 def _get_default_cmdline(self):
46 Using /etc/default/grub return the default grub cmdline
48 :returns: The default grub cmdline
52 # Get the default grub cmdline
53 rootdir = self._node['rootdir']
54 gfile = self._node['cpu']['grub_config_file']
55 grubcmdline = self._node['cpu']['grubcmdline']
56 cmd = 'cat {}'.format(rootdir + gfile)
57 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
59 raise RuntimeError('{} Executing failed on node {} {}'.
60 format(cmd, self._node['host'], stderr))
62 # Get the Default Linux command line, ignoring commented lines
63 lines = stdout.split('\n')
65 if line == '' or line[0] == '#':
67 ldefault = re.findall(r'{}=.+'.format(grubcmdline), line)
69 self._default_cmdline = ldefault[0]
72 def get_current_cmdline(self):
74 Returns the saved grub cmdline
76 :returns: The saved grub cmdline
79 return self._current_cmdline
81 def get_default_cmdline(self):
83 Returns the default grub cmdline
85 :returns: The default grub cmdline
88 return self._default_cmdline
90 def create_cmdline(self, isolated_cpus):
92 Create the new grub cmdline
94 :param isolated_cpus: The isolated cpu string
95 :type isolated_cpus: string
96 :returns: The command line
99 grubcmdline = self._node['cpu']['grubcmdline']
100 cmdline = self._default_cmdline
101 value = cmdline.split('{}='.format(grubcmdline))[1]
102 value = value.rstrip('"').lstrip('"')
104 iommu = re.findall(r'iommu=\w+', value)
105 pstate = re.findall(r'intel_pstate=\w+', value)
106 # If there is already some iommu commands set, leave them,
108 if iommu == [] and pstate == []:
109 value = '{} intel_pstate=disable'.format(value)
111 # Replace isolcpus with ours
112 isolcpus = re.findall(r'isolcpus=[\w+\-,]+', value)
114 if isolated_cpus != '':
115 value = "{} isolcpus={}".format(value, isolated_cpus)
117 if isolated_cpus != '':
118 value = re.sub(r'isolcpus=[\w+\-,]+',
119 'isolcpus={}'.format(isolated_cpus),
122 value = re.sub(r'isolcpus=[\w+\-,]+', '', value)
124 nohz = re.findall(r'nohz_full=[\w+\-,]+', value)
126 if isolated_cpus != '':
127 value = "{} nohz_full={}".format(value, isolated_cpus)
129 if isolated_cpus != '':
130 value = re.sub(r'nohz_full=[\w+\-,]+',
131 'nohz_full={}'.format(isolated_cpus),
134 value = re.sub(r'nohz_full=[\w+\-,]+', '', value)
136 rcu = re.findall(r'rcu_nocbs=[\w+\-,]+', value)
138 if isolated_cpus != '':
139 value = "{} rcu_nocbs={}".format(value, isolated_cpus)
141 if isolated_cpus != '':
142 value = re.sub(r'rcu_nocbs=[\w+\-,]+',
143 'rcu_nocbs={}'.format(isolated_cpus),
146 value = re.sub(r'rcu_nocbs=[\w+\-,]+', '', value)
148 value = value.lstrip(' ').rstrip(' ')
149 cmdline = '{}="{}"'.format(grubcmdline, value)
152 def apply_cmdline(self, node, isolated_cpus):
154 Apply cmdline to the default grub file
156 :param node: Node dictionary with cpuinfo.
157 :param isolated_cpus: The isolated cpu string
159 :type isolated_cpus: string
160 :return The vpp cmdline
164 vpp_cmdline = self.create_cmdline(isolated_cpus)
165 if vpp_cmdline == '':
169 # Save the original file
170 rootdir = node['rootdir']
171 grubcmdline = node['cpu']['grubcmdline']
172 ofilename = rootdir + node['cpu']['grub_config_file'] + '.orig'
173 filename = rootdir + node['cpu']['grub_config_file']
175 # Write the output file
176 # Does a copy of the original file exist, if not create one
177 (ret, stdout, stderr) = VPPUtil.exec_command('ls {}'.format(ofilename))
179 if stdout.strip('\n') != ofilename:
180 cmd = 'sudo cp {} {}'.format(filename, ofilename)
181 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
183 raise RuntimeError('{} failed on node {} {}'.
184 format(cmd, self._node['host'], stderr))
186 # Get the contents of the current grub config file
187 cmd = 'cat {}'.format(filename)
188 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
190 raise RuntimeError('{} failed on node {} {}'.format(
195 # Write the new contents
196 # Get the Default Linux command line, ignoring commented lines
198 lines = stdout.split('\n')
201 content += line + '\n'
204 content += line + '\n'
207 ldefault = re.findall(r'{}=.+'.format(grubcmdline), line)
209 content += vpp_cmdline + '\n'
211 content += line + '\n'
213 content = content.replace(r"`", r"\`")
214 content = content.rstrip('\n')
215 cmd = "sudo cat > {0} << EOF\n{1}\n".format(filename, content)
216 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
218 raise RuntimeError('{} failed on node {} {}'.format(
225 def __init__(self, node):
226 distro = VPPUtil.get_linux_distro()
227 if distro[0] == 'Ubuntu':
228 node['cpu']['grubcmdline'] = 'GRUB_CMDLINE_LINUX_DEFAULT'
230 node['cpu']['grubcmdline'] = 'GRUB_CMDLINE_LINUX'
233 self._current_cmdline = ""
234 self._default_cmdline = ""
235 self._get_current_cmdline()
236 self._get_default_cmdline()