1 # Copyright (c) 2017 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 """Library to manipulate LXC."""
16 from resources.libraries.python.ssh import SSH
17 from resources.libraries.python.constants import Constants
18 from resources.libraries.python.topology import NodeType
21 __all__ = ["LXCUtils"]
23 class LXCUtils(object):
26 def __init__(self, container_name='slave'):
28 self._container_name = container_name
30 # Host hugepages dir that will be mounted inside LXC
31 self._host_hugepages_dir = '/dev/hugepages'
32 # Host dir that will be mounted inside LXC
33 self._host_dir = '/tmp/'
34 # Guest dir to mount host dir to
35 self._guest_dir = '/mnt/host'
36 # LXC container env variables
37 self._env_vars = ['LC_ALL="en_US.UTF-8"',
38 'DEBIAN_FRONTEND=noninteractive']
40 def set_node(self, node):
41 """Set node for LXC execution.
43 :param node: Node to execute LXC on.
45 :raises RuntimeError: If Node type is not DUT.
47 if node['type'] != NodeType.DUT:
48 raise RuntimeError('Node type is not DUT.')
51 def set_host_dir(self, node, host_dir):
52 """Set shared dir on parent node for LXC.
54 :param node: Node to control LXC on.
56 :raises RuntimeError: If Node type is not DUT.
58 if node['type'] != NodeType.DUT:
59 raise RuntimeError('Node type is not DUT.')
60 self._host_dir = host_dir
62 def set_guest_dir(self, node, guest_dir):
63 """Set mount dir on LXC.
65 :param node: Node to control LXC on.
66 :param guest_dir: Guest dir for mount.
69 :raises RuntimeError: If Node type is not DUT.
71 if node['type'] != NodeType.DUT:
72 raise RuntimeError('Node type is not DUT.')
73 self._guest_dir = guest_dir
75 def _lxc_checkconfig(self):
76 """Check the current kernel for LXC support.
78 :raises RuntimeError: If failed to check LXC support.
82 ssh.connect(self._node)
84 ret, _, _ = ssh.exec_command_sudo('lxc-checkconfig')
86 raise RuntimeError('Failed to check LXC support.')
88 def _lxc_create(self, distro='ubuntu', release='xenial', arch='amd64'):
89 """Creates a privileged system object where is stored the configuration
90 information and where can be stored user information.
92 :param distro: Linux distribution name.
93 :param release: Linux distribution release.
94 :param arch: Linux distribution architecture.
98 :raises RuntimeError: If failed to create a container.
102 ssh.connect(self._node)
104 ret, _, _ = ssh.exec_command_sudo(
105 'lxc-create -t download --name {0} -- -d {1} -r {2} -a {3}'\
106 ' --no-validate'.format(self._container_name, distro, release,
109 raise RuntimeError('Failed to create LXC container.')
112 """Queries and shows information about a container.
114 :raises RuntimeError: If failed to get info about a container.
118 ssh.connect(self._node)
120 ret, _, _ = ssh.exec_command_sudo(
121 'lxc-info --name {0}'.format(self._container_name))
123 raise RuntimeError('Failed to get info about LXC container {0}.'
124 .format(self._container_name))
126 def _lxc_start(self):
127 """Start an application inside a container.
129 :raises RuntimeError: If failed to start container.
133 ssh.connect(self._node)
135 ret, _, _ = ssh.exec_command_sudo(
136 'lxc-start --name {0} --daemon'.format(self._container_name))
138 raise RuntimeError('Failed to start LXC container {0}.'
139 .format(self._container_name))
142 """Stop an application inside a container.
144 :raises RuntimeError: If failed to stop container.
148 ssh.connect(self._node)
150 ret, _, _ = ssh.exec_command_sudo(
151 'lxc-stop --name {0}'.format(self._container_name))
153 raise RuntimeError('Failed to stop LXC container {}.'
154 .format(self._container_name))
156 def _lxc_destroy(self):
157 """Destroy a container.
159 :raises RuntimeError: If failed to destroy container.
163 ssh.connect(self._node)
165 ret, _, _ = ssh.exec_command_sudo(
166 'lxc-destroy --force --name {0}'.format(self._container_name))
168 raise RuntimeError('Failed to destroy LXC container {}.'
169 .format(self._container_name))
171 def _lxc_wait(self, state):
172 """Wait for a specific container state.
174 :param state: Specify the container state(s) to wait for.
176 :raises RuntimeError: If failed to wait for state of a container.
180 ssh.connect(self._node)
182 ret, _, _ = ssh.exec_command_sudo(
183 'lxc-wait --name {0} --state "{1}"'
184 .format(self._container_name, state))
186 raise RuntimeError('Failed to wait for "{0}" of LXC container {1}.'
187 .format(state, self._container_name))
189 def _lxc_cgroup(self, state_object, value=''):
190 """Manage the control group associated with a container.
192 :param state_object: Specify the state object name.
193 :param value: Specify the value to assign to the state object. If empty,
194 then action is GET, otherwise is action SET.
195 :type state_object: str
197 :raises RuntimeError: If failed to get/set for state of a container.
201 ssh.connect(self._node)
203 ret, _, _ = ssh.exec_command_sudo(
204 'lxc-cgroup --name {0} {1} {2}'
205 .format(self._container_name, state_object, value))
208 raise RuntimeError('Failed to set {0} of LXC container {1}.'
209 .format(state_object, self._container_name))
211 raise RuntimeError('Failed to get {0} of LXC container {1}.'
212 .format(state_object, self._container_name))
214 def lxc_attach(self, command):
215 """Start a process inside a running container. Runs the specified
216 command inside the container specified by name. The container has to
219 :param command: Command to run inside container.
221 :raises RuntimeError: If container is not running.
222 :raises RuntimeError: If failed to run the command.
224 env_var = '--keep-env {0}'\
225 .format(' '.join('--set-var %s' % var for var in self._env_vars))
228 ssh.connect(self._node)
230 if not self.is_container_running():
231 raise RuntimeError('LXC {0} is not running.'
232 .format(self._container_name))
234 ret, _, _ = ssh.exec_command_lxc(lxc_cmd=command,
235 lxc_name=self._container_name,
236 lxc_params=env_var, timeout=180)
238 raise RuntimeError('Failed to run "{0}" on LXC container {1}.'
239 .format(command, self._container_name))
241 def is_container_present(self):
242 """Check if LXC container is existing on node."""
245 ssh.connect(self._node)
247 ret, _, _ = ssh.exec_command_sudo(
248 'lxc-info --name {0}'.format(self._container_name))
249 return False if int(ret) else True
251 def create_container(self, force_create=True):
252 """Create and start a container.
254 :param force_create: Destroy a container if exists and create.
255 :type force_create: bool
257 if self.is_container_present():
259 self.destroy_container()
263 self._lxc_checkconfig()
264 self._lxc_create(distro='ubuntu', release='xenial', arch='amd64')
265 self.start_container()
267 def start_container(self):
268 """Start a container and wait for running state."""
271 self._lxc_wait('RUNNING')
274 def is_container_running(self):
275 """Check if LXC container is running on node.
277 :raises RuntimeError: If failed to get info about a container.
281 ssh.connect(self._node)
283 ret, stdout, _ = ssh.exec_command_sudo(
284 'lxc-info --state --name {0}'.format(self._container_name))
286 raise RuntimeError('Failed to get info about LXC container {0}.'
287 .format(self._container_name))
289 return True if 'RUNNING' in stdout else False
291 def stop_container(self):
292 """Stop a container and wait for stopped state."""
295 self._lxc_wait('STOPPED|FROZEN')
298 def restart_container(self):
299 """Restart container."""
301 self.stop_container()
302 self.start_container()
304 def destroy_container(self):
305 """Stop and destroy a container."""
309 def container_cpuset_cpus(self, container_cpu):
310 """Set cpuset.cpus control group associated with a container.
312 :param container_cpu: Cpuset.cpus string.
313 :type container_cpu: str
314 :raises RuntimeError: If failed to set cgroup for a container.
318 ssh.connect(self._node)
320 ret, _, _ = ssh.exec_command_sudo('cgset --copy-from / lxc')
322 raise RuntimeError('Failed to copy cgroup settings from root.')
324 self._lxc_cgroup(state_object='cpuset.cpus')
325 self._lxc_cgroup(state_object='cpuset.cpus', value=container_cpu)
326 self._lxc_cgroup(state_object='cpuset.cpus')
328 def mount_host_dir_in_container(self):
329 """Mount shared folder inside container.
331 :raises RuntimeError: If failed to mount host dir in a container.
335 ssh.connect(self._node)
337 mnt_cfg = 'lxc.mount.entry = {0} /var/lib/lxc/{1}/rootfs{2} ' \
338 'none bind,create=dir 0 0'.format(self._host_dir,
339 self._container_name,
341 ret, _, _ = ssh.exec_command_sudo(
342 "sh -c 'echo \"{0}\" >> /var/lib/lxc/{1}/config'"
343 .format(mnt_cfg, self._container_name))
345 raise RuntimeError('Failed to mount {0} in lxc: {1}'
346 .format(self._host_dir, self._container_name))
348 self.restart_container()
350 def mount_hugepages_in_container(self):
351 """Mount hugepages inside container.
353 :raises RuntimeError: If failed to mount hugepages in a container.
357 ssh.connect(self._node)
359 mnt_cfg = 'lxc.mount.entry = {0} dev/hugepages ' \
360 'none bind,create=dir 0 0'.format(self._host_hugepages_dir)
361 ret, _, _ = ssh.exec_command_sudo(
362 "sh -c 'echo \"{0}\" >> /var/lib/lxc/{1}/config'"
363 .format(mnt_cfg, self._container_name))
365 raise RuntimeError('Failed to mount {0} in lxc: {1}'
366 .format(self._host_hugepages_dir,
367 self._container_name))
369 self.restart_container()
371 def install_vpp_in_container(self, install_dkms=False):
372 """Install vpp inside a container.
374 :param install_dkms: If install dkms package. This will impact install
375 time. Dkms is required for installation of vpp-dpdk-dkms. Default is
377 :type install_dkms: bool
381 ssh.connect(self._node)
383 self.lxc_attach('apt-get update')
385 self.lxc_attach('apt-get install -y dkms && '
386 'dpkg -i --force-all {0}/install_dir/*.deb'
387 .format(self._guest_dir))
389 self.lxc_attach('for i in $(ls -I \"*dkms*\" {0}/install_dir/); '
390 'do dpkg -i --force-all {0}/install_dir/$i; done'
391 .format(self._guest_dir))
392 self.lxc_attach('apt-get -f install -y')
394 def restart_vpp_in_container(self):
395 """Restart vpp service inside a container."""
398 ssh.connect(self._node)
400 self.lxc_attach('service vpp restart')
402 def create_vpp_cfg_in_container(self, vat_template_file, **args):
403 """Create VPP exec config for a container on given node.
405 :param vat_template_file: Template file name of a VAT script.
406 :param args: Parameters for VAT script.
407 :type vat_template_file: str
411 ssh.connect(self._node)
413 vat_file_path = '{}/{}'.format(Constants.RESOURCES_TPL_VAT,
416 with open(vat_file_path, 'r') as template_file:
417 cmd_template = template_file.readlines()
418 for line_tmpl in cmd_template:
419 vat_cmd = line_tmpl.format(**args)
420 ssh.exec_command('echo "{0}" | '
421 'sudo lxc-attach --name {1} -- '
422 '/bin/sh -c "/bin/cat >> /tmp/running.exec"'
423 .format(vat_cmd.replace('\n', ''),
424 self._container_name))