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.topology import NodeType
19 __all__ = ["LXCUtils"]
21 class LXCUtils(object):
24 def __init__(self, container_name='slave'):
26 self._container_name = container_name
28 # Host dir that will be mounted inside LXC
29 self._host_dir = '/tmp/'
30 # Guest dir to mount host dir to
31 self._guest_dir = '/mnt/host'
32 # LXC container env variables
33 self._env_vars = ['LC_ALL="en_US.UTF-8"',
34 'DEBIAN_FRONTEND=noninteractive']
36 def set_node(self, node):
37 """Set node for LXC execution.
39 :param node: Node to execute LXC on.
41 :raises RuntimeError: If Node type is not DUT.
43 if node['type'] != NodeType.DUT:
44 raise RuntimeError('Node type is not DUT.')
47 def set_host_dir(self, node, host_dir):
48 """Set shared dir on parent node for LXC.
50 :param node: Node to control LXC on.
52 :raises RuntimeError: If Node type is not DUT.
54 if node['type'] != NodeType.DUT:
55 raise RuntimeError('Node type is not DUT.')
56 self._host_dir = host_dir
58 def set_guest_dir(self, node, guest_dir):
59 """Set mount dir on LXC.
61 :param node: Node to control LXC on.
62 :param guest_dir: Guest dir for mount.
65 :raises RuntimeError: If Node type is not DUT.
67 if node['type'] != NodeType.DUT:
68 raise RuntimeError('Node type is not DUT.')
69 self._guest_dir = guest_dir
71 def _lxc_checkconfig(self):
72 """Check the current kernel for LXC support.
74 :raises RuntimeError: If failed to check LXC support.
78 ssh.connect(self._node)
80 ret, _, _ = ssh.exec_command_sudo('lxc-checkconfig')
82 raise RuntimeError('Failed to check LXC support.')
84 def _lxc_create(self, distro='ubuntu', release='xenial', arch='amd64'):
85 """Creates a privileged system object where is stored the configuration
86 information and where can be stored user information.
88 :param distro: Linux distribution name.
89 :param release: Linux distribution release.
90 :param arch: Linux distribution architecture.
94 :raises RuntimeError: If failed to create a container.
98 ssh.connect(self._node)
100 ret, _, _ = ssh.exec_command_sudo(
101 'lxc-create -t download --name {0} -- -d {1} -r {2} -a {3}'
102 .format(self._container_name, distro, release, arch), timeout=1800)
104 raise RuntimeError('Failed to create LXC container.')
107 """Queries and shows information about a container.
109 :raises RuntimeError: If failed to get info about a container.
113 ssh.connect(self._node)
115 ret, _, _ = ssh.exec_command_sudo(
116 'lxc-info --name {0}'.format(self._container_name))
118 raise RuntimeError('Failed to get info about LXC container {0}.'
119 .format(self._container_name))
121 def _lxc_start(self):
122 """Start an application inside a container.
124 :raises RuntimeError: If failed to start container.
128 ssh.connect(self._node)
130 ret, _, _ = ssh.exec_command_sudo(
131 'lxc-start --name {0} --daemon'.format(self._container_name))
133 raise RuntimeError('Failed to start LXC container {0}.'
134 .format(self._container_name))
137 """Stop an application inside a container.
139 :raises RuntimeError: If failed to stop container.
143 ssh.connect(self._node)
145 ret, _, _ = ssh.exec_command_sudo(
146 'lxc-stop --name {0}'.format(self._container_name))
148 raise RuntimeError('Failed to stop LXC container {}.'
149 .format(self._container_name))
151 def _lxc_destroy(self):
152 """Destroy a container.
154 :raises RuntimeError: If failed to destroy container.
158 ssh.connect(self._node)
160 ret, _, _ = ssh.exec_command_sudo(
161 'lxc-destroy --force --name {0}'.format(self._container_name))
163 raise RuntimeError('Failed to destroy LXC container {}.'
164 .format(self._container_name))
166 def _lxc_wait(self, state):
167 """Wait for a specific container state.
169 :param state: Specify the container state(s) to wait for.
171 :raises RuntimeError: If failed to wait for state of a container.
175 ssh.connect(self._node)
177 ret, _, _ = ssh.exec_command_sudo(
178 'lxc-wait --name {0} --state "{1}"'
179 .format(self._container_name, state))
181 raise RuntimeError('Failed to wait for "{0}" of LXC container {1}.'
182 .format(state, self._container_name))
184 def _lxc_cgroup(self, state_object, value=''):
185 """Manage the control group associated with a container.
187 :param state_object: Specify the state object name.
188 :param value: Specify the value to assign to the state object. If empty,
189 then action is GET, otherwise is action SET.
190 :type state_object: str
192 :raises RuntimeError: If failed to get/set for state of a container.
196 ssh.connect(self._node)
198 ret, _, _ = ssh.exec_command_sudo(
199 'lxc-cgroup --name {0} {1} {2}'
200 .format(self._container_name, state_object, value))
203 raise RuntimeError('Failed to set {0} of LXC container {1}.'
204 .format(state_object, self._container_name))
206 raise RuntimeError('Failed to get {0} of LXC container {1}.'
207 .format(state_object, self._container_name))
209 def lxc_attach(self, command):
210 """Start a process inside a running container. Runs the specified
211 command inside the container specified by name. The container has to
214 :param command: Command to run inside container.
216 :raises RuntimeError: If container is not running.
217 :raises RuntimeError: If failed to run the command.
219 env_var = '--keep-env {0}'\
220 .format(' '.join('--set-var %s' % var for var in self._env_vars))
223 ssh.connect(self._node)
225 if not self.is_container_running():
226 raise RuntimeError('LXC {0} is not running.'
227 .format(self._container_name))
229 ret, _, _ = ssh.exec_command_lxc(lxc_cmd=command,
230 lxc_name=self._container_name,
231 lxc_params=env_var, timeout=180)
233 raise RuntimeError('Failed to run "{0}" on LXC container {1}.'
234 .format(command, self._container_name))
236 def is_container_present(self):
237 """Check if LXC container is existing on node."""
240 ssh.connect(self._node)
242 ret, _, _ = ssh.exec_command_sudo(
243 'lxc-info --name {0}'.format(self._container_name))
244 return False if int(ret) else True
246 def create_container(self, force_create=True):
247 """Create and start a container.
249 :param force_create: Destroy a container if exists and create.
250 :type force_create: bool
252 if self.is_container_present():
254 self.container_destroy()
258 self._lxc_checkconfig()
259 self._lxc_create(distro='ubuntu', release='xenial', arch='amd64')
260 self.start_container()
262 def start_container(self):
263 """Start a container and wait for running state."""
266 self._lxc_wait('RUNNING')
269 def is_container_running(self):
270 """Check if LXC container is running on node.
272 :raises RuntimeError: If failed to get info about a container.
276 ssh.connect(self._node)
278 ret, stdout, _ = ssh.exec_command_sudo(
279 'lxc-info --state --name {0}'.format(self._container_name))
281 raise RuntimeError('Failed to get info about LXC container {0}.'
282 .format(self._container_name))
284 return True if 'RUNNING' in stdout else False
286 def stop_container(self):
287 """Stop a container and wait for stopped state."""
290 self._lxc_wait('STOPPED|FROZEN')
293 def restart_container(self):
294 """Restart container."""
296 self.stop_container()
297 self.start_container()
299 def container_destroy(self):
300 """Stop and destroy a container."""
304 def container_cpuset_cpus(self, container_cpu):
305 """Set cpuset.cpus control group associated with a container.
307 :param container_cpu: Cpuset.cpus string.
308 :type container_cpu: str
309 :raises RuntimeError: If failed to set cgroup for a container.
313 ssh.connect(self._node)
315 ret, _, _ = ssh.exec_command_sudo('cgset --copy-from / lxc')
317 raise RuntimeError('Failed to copy cgroup settings from root.')
319 self._lxc_cgroup(state_object='cpuset.cpus')
320 self._lxc_cgroup(state_object='cpuset.cpus', value=container_cpu)
321 self._lxc_cgroup(state_object='cpuset.cpus')
323 def mount_host_dir_in_container(self):
324 """Mount shared folder inside container.
326 :raises RuntimeError: If failed to mount host dir in a container.
330 ssh.connect(self._node)
332 self.lxc_attach('mkdir -p {0}'.format(self._guest_dir))
334 mnt_cfg = 'lxc.mount.entry = {0} /var/lib/lxc/{1}/rootfs{2} ' \
335 'none bind 0 0'.format(self._host_dir, self._container_name,
337 ret, _, _ = ssh.exec_command_sudo(
338 "sh -c 'echo \"{0}\" >> /var/lib/lxc/{1}/config'"
339 .format(mnt_cfg, self._container_name))
341 raise RuntimeError('Failed to mount {0} in lxc: {1}'
342 .format(self._host_dir, self._container_name))
344 self.restart_container()
346 def install_vpp_in_container(self, install_dkms=False):
347 """Install vpp inside a container.
349 :param install_dkms: If install dkms package. This will impact install
350 time. Dkms is required for installation of vpp-dpdk-dkms. Default is
352 :type install_dkms: bool
356 ssh.connect(self._node)
358 self.lxc_attach('apt-get update')
360 self.lxc_attach('apt-get install -y dkms && '
361 'dpkg -i --force-all {0}/install_dir/*.deb'
362 .format(self._guest_dir))
364 self.lxc_attach('for i in $(ls -I \"*dkms*\" {0}/install_dir/); '
365 'do dpkg -i --force-all {0}/install_dir/$i; done'
366 .format(self._guest_dir))
367 self.lxc_attach('apt-get -f install -y')
369 def restart_vpp_in_container(self):
370 """Restart vpp service inside a container."""
373 ssh.connect(self._node)
375 self.lxc_attach('service vpp restart')