diff --git a/playbooks/roles/lxc_container_create/tasks/container_create.yml b/playbooks/roles/lxc_container_create/tasks/container_create.yml index 8e63bae170..89cacc18a7 100644 --- a/playbooks/roles/lxc_container_create/tasks/container_create.yml +++ b/playbooks/roles/lxc_container_create/tasks/container_create.yml @@ -214,6 +214,34 @@ tags: - lxc-container-networks +# Adds post-down and pre-start hooks +- name: Drop veth cleanup script + template: + src: "veth-cleanup.sh.j2" + dest: "/var/lib/lxc/{{ inventory_hostname }}/veth-cleanup.sh" + owner: "root" + group: "root" + mode: "0755" + delegate_to: "{{ physical_host }}" + tags: + - lxc-container-networks + +# This is being defined due to an issue with dangling veth pairs. +# TODO(someone) This should be removed once an upstream patch has +# been submitted to either the kernel or LXC to fix the veth issues. +# Container restart is not happening here because it's not needed. +- name: Defines a pre and post hook script + lineinfile: + dest: "/var/lib/lxc/{{ inventory_hostname }}/config" + line: "{{ item }}" + backup: "true" + with_items: + - "lxc.hook.pre-start = /var/lib/lxc/{{ inventory_hostname }}/veth-cleanup.sh" + - "lxc.hook.post-stop = /var/lib/lxc/{{ inventory_hostname }}/veth-cleanup.sh" + delegate_to: "{{ physical_host }}" + tags: + - lxc-container-networks + # Flush the handlers to ensure the container and networking is online. - meta: flush_handlers diff --git a/playbooks/roles/lxc_container_create/templates/veth-cleanup.sh.j2 b/playbooks/roles/lxc_container_create/templates/veth-cleanup.sh.j2 new file mode 100644 index 0000000000..dc67f36ce6 --- /dev/null +++ b/playbooks/roles/lxc_container_create/templates/veth-cleanup.sh.j2 @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" + +# LXC eth0 is considered special and not managed by the base container_networks +# data structure. This is being added outside of the loop for this reason. +ip link del {{ inventory_hostname[-8:].replace('-', '').replace('_', '') }}_eth0 || true +logger "LXC container {{ inventory_hostname }} removing veth {{ inventory_hostname[-8:].replace('-', '').replace('_', '') }}_eth0" + +# Veth cleanup for items in the container_networks data structure +{% for key, value in container_networks.items() %} +ip link del {{ inventory_hostname[-8:].replace('-', '').replace('_', '') }}_{{ value.interface }} || true +logger "LXC container {{ inventory_hostname }} removing veth {{ inventory_hostname[-8:].replace('-', '').replace('_', '') }}_{{ value.interface }}" + +{% endfor %}