Skip to content

Commit 0f10691

Browse files
author
jovial
authored
Add support for changing libvirt socket and pid path (#8)
Add support for changing libvirt socket and pid path This allows you to use an uncontainerised libvirtd alongside a containerised instance which has bind mounted /var/run/libvirt into the container.
1 parent fd97116 commit 0f10691

File tree

12 files changed

+182
-20
lines changed

12 files changed

+182
-20
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,35 @@ default value is `true`.
3737

3838
`libvirt_host_qemu_emulators`: List of architectures for which to install QEMU
3939
system emulators, e.g. `x86`. The default value is `['x86']` if
40+
4041
`libvirt_host_require_vt` is `false`, otherwise the default value is an empty
4142
list.
43+
4244
`libvirt_host_enable_efi_support`: Whether to enable EFI support. This defaults
4345
to false as extra packages need to be installed.
4446

47+
`libvirt_host_var_prefix`: This determines The directory under /var/run that libvirt
48+
uses to store state, e.g unix domain sockets, as well as the default name of the
49+
PID file. Override this if you have a conflict with the default socket e.g it
50+
could be in use by the nova_libvirt container. Defaults to `""`.
51+
52+
`libvirt_host_socket_dir`: Where the libvirtd socket is created. Defaults to
53+
`/var/run/{{ libvirt_host_var_prefix }}` if `libvirt_host_var_prefix` is set,
54+
otherwise `""`.
55+
56+
`libvirt_host_pid_path`: Path to PID file which prevents multiple instances of
57+
the daemon from spawning. Defaults to `/var/run/{{ libvirt_host_var_prefix }}.pid`
58+
if `libvirt_host_var_prefix` is set, otherwise `""`.
59+
60+
`libvirt_host_libvirtd_args`: Command line arguments passed to libvirtd by the
61+
init system when libvirtd is started - quotes will be added
62+
63+
`libvirt_host_uri`: The libvirt connnection URI. Defaults to
64+
`qemu+unix:///system?socket={{ libvirt_host_socket_dir }}/libvirt-sock` if
65+
`libvirt_host_var_prefix` is set, otherwise `""`. If set to a falsey value,
66+
an explicit connection URI will not be set when calling virsh or any of
67+
the virt_ ansible modules.
68+
4569
Dependencies
4670
------------
4771

defaults/main.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,35 @@ libvirt_host_qemu_emulators: "{{ [] if libvirt_host_require_vt | bool else ['x86
2929
# Whether or not to enable UEFI support. In some cases this requires installing
3030
# extra packages.
3131
libvirt_host_enable_efi_support: false
32+
33+
# This determines The directory under /var/run that libvirt uses to store state,
34+
# e.g unix domain sockets, as well as the default name of the PID file. Override
35+
# this if you have a conflict with the default socket e.g it could be in use by the
36+
# nova_libvirt container
37+
libvirt_host_var_prefix: ""
38+
39+
# Where the Unix Domain sockets are stored
40+
libvirt_host_socket_dir: >-
41+
{%- if libvirt_host_var_prefix -%}
42+
/var/run/{{ libvirt_host_var_prefix }}
43+
{%- endif -%}
44+
45+
# Path to PID file which prevents mulitple instances of the daemon from
46+
# spawning
47+
libvirt_host_pid_path: >-
48+
{%- if libvirt_host_var_prefix -%}
49+
/var/run/{{ libvirt_host_var_prefix }}.pid
50+
{%- endif -%}
51+
52+
# Command line arguments passed to libvirtd by the init system when
53+
# libvirtd is started - quotes will be added
54+
libvirt_host_libvirtd_args: >-
55+
{%- if libvirt_host_pid_path -%}
56+
-p {{ libvirt_host_pid_path }}
57+
{%- endif %}
58+
59+
# The libvirt connnection URI
60+
libvirt_host_uri: >-
61+
{%- if libvirt_host_socket_dir -%}
62+
qemu+unix:///system?socket={{ libvirt_host_socket_dir }}/libvirt-sock
63+
{%- endif %}

handlers/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
3+
- name: restart libvirt
4+
service:
5+
name: libvirtd
6+
state: restarted
7+
become: true

tasks/config.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
# Configure services - runs after the install stage
3+
4+
- name: Set socket directory in libvirtd.conf
5+
lineinfile:
6+
path: /etc/libvirt/libvirtd.conf
7+
insertafter: '^#unix_sock_dir ='
8+
regexp: '^unix_sock_dir ='
9+
line: unix_sock_dir = "{{ libvirt_host_socket_dir }}"
10+
become: true
11+
when: libvirt_host_socket_dir != ""
12+
notify: restart libvirt
13+
14+
- name: Create directory for libvirt socket
15+
file:
16+
state: directory
17+
path: "{{ libvirt_host_socket_dir }}"
18+
owner: root
19+
group: root
20+
mode: 0755
21+
become: true
22+
when: libvirt_host_socket_dir != ""
23+
24+
- name: Process lineinfile rules
25+
lineinfile: "{{ rule.args }}"
26+
become: true
27+
loop: "{{ libvirt_host_lineinfile_extra_rules | default([]) }}"
28+
loop_control:
29+
loop_var: rule
30+
when: rule.condition
31+
notify:
32+
- restart libvirt
33+
34+
- name: Flush handlers
35+
meta: flush_handlers
36+
37+
- name: Ensure the libvirt daemon is started and enabled
38+
service:
39+
name: libvirtd
40+
state: started
41+
enabled: yes
42+
become: True

tasks/install.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
---
2-
3-
- name: gather os specific variables
4-
include_vars: "{{ item }}"
5-
with_first_found:
6-
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
7-
- "{{ ansible_distribution }}.yml"
8-
- "{{ ansible_os_family }}.yml"
9-
tags: vars
10-
112
- name: Install custom yum repositories
123
# Although argument splatting is marked as deprecated:
134
#
@@ -46,10 +37,3 @@
4637
become: True
4738
vars:
4839
package: "qemu-system-{{ item }}"
49-
50-
- name: Ensure the libvirt daemon is started and enabled
51-
service:
52-
name: libvirtd
53-
state: started
54-
enabled: yes
55-
become: True

tasks/main.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
---
2+
- include: prelude.yml
23
- include: validate.yml
34
- include: install.yml
5+
- name: Run post-install stage
6+
include: "{{ post_install_path }}"
7+
with_first_found:
8+
- files:
9+
- post-install-{{ ansible_distribution }}.yml
10+
- post-install-{{ ansible_os_family }}.yml
11+
skip: true
12+
loop_control:
13+
loop_var: post_install_path
14+
- include: config.yml
415
- include: pools.yml
516
- include: networks.yml

tasks/networks.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
name: "{{ item.name }}"
55
command: define
66
xml: "{{ item.xml | default(lookup('template', 'network.xml.j2')) }}"
7+
uri: "{{ libvirt_host_uri | default(omit, true) }}"
78
with_items: "{{ libvirt_host_networks }}"
89
become: True
910

1011
- name: Ensure libvirt networks are active
1112
virt_net:
1213
name: "{{ item.name }}"
1314
state: active
15+
uri: "{{ libvirt_host_uri | default(omit, true) }}"
1416
with_items: "{{ libvirt_host_networks }}"
1517
become: True
1618

1719
- name: Ensure libvirt networks are started on boot
1820
virt_net:
1921
name: "{{ item.name }}"
2022
autostart: yes
23+
uri: "{{ libvirt_host_uri | default(omit, true) }}"
2124
with_items: "{{ libvirt_host_networks }}"
2225
become: True

tasks/pools.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515
name: "{{ item.name }}"
1616
command: define
1717
xml: "{{ item.xml | default(lookup('template', 'pool.xml.j2')) }}"
18+
uri: "{{ libvirt_host_uri | default(omit, true) }}"
1819
with_items: "{{ libvirt_host_pools }}"
1920
become: True
2021

2122
- name: Ensure libvirt storage pools are active
2223
virt_pool:
2324
name: "{{ item.name }}"
2425
state: active
26+
uri: "{{ libvirt_host_uri | default(omit, true) }}"
2527
with_items: "{{ libvirt_host_pools }}"
2628
become: True
2729

2830
- name: Ensure libvirt storage pools are started on boot
2931
virt_pool:
3032
name: "{{ item.name }}"
3133
autostart: yes
34+
uri: "{{ libvirt_host_uri | default(omit, true) }}"
3235
with_items: "{{ libvirt_host_pools }}"
3336
become: True

tasks/post-install-Debian.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
3+
- name: Determine path to libvirt environment file
4+
# On Debian >= 8 and Ubuntu >= 16.04 the libvirt-bin package has been
5+
# split into libvirt-daemon-system and libvirt-clients. They also seem
6+
# to have changed to location to enviroment file. To prevent the need
7+
# to hard code paths for every major version we determine these
8+
# dynamically. This must be done after installing the package.
9+
# You cannot guard the with_first_found with a condition without
10+
# skip being set to true. This is undeseriable so we have to
11+
# put it behind an include (a block doesn't work).
12+
set_fact:
13+
libvirt_host_lineinfile_extra_rules:
14+
- args:
15+
path: "{{ libvirt_env_path }}"
16+
insertafter: '^#libvirtd_opts='
17+
regexp: '^libvirtd_opts='
18+
line: libvirtd_opts="{{ libvirt_host_libvirtd_args }}"
19+
condition: "{{ libvirt_host_libvirtd_args != '' }}"
20+
with_first_found:
21+
- /etc/default/libvirt-bin
22+
- /etc/default/libvirtd
23+
loop_control:
24+
loop_var: libvirt_env_path
25+
tags: vars

tasks/prelude.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
# This file is intended to be included at the beginning of a playbook.
3+
4+
- name: gather os specific variables
5+
include_vars: "{{ item }}"
6+
with_first_found:
7+
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
8+
- "{{ ansible_distribution }}.yml"
9+
- "{{ ansible_os_family }}.yml"
10+
tags: vars

vars/Debian.yml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
---
2-
# List of libvirt package dependencies.
3-
libvirt_host_libvirt_packages_default:
4-
- libvirt-bin
2+
# List of package dependencies common to all Debian distributions
3+
libvirt_host_libvirt_packages_common:
54
- qemu-kvm
65
- python-libvirt
76
- python-lxml
87

8+
# Package that contains the libvirt daemon
9+
libvirt_host_libvirt_packages_libvirt_daemon: >-
10+
{%- if (ansible_distribution == "Ubuntu" and
11+
ansible_distribution_major_version is version_compare('16.04', '<')) or
12+
(ansible_distribution == "Debian" and
13+
ansible_distribution_major_version is version_compare('8', '<')) -%}
14+
libvirt-bin
15+
{%- else -%}
16+
libvirt-daemon-system
17+
{%- endif -%}
18+
919
# Packages that are only necessary if you require EFI support
1020
libvirt_host_packages_efi:
1121
- ovmf
1222

1323
# List of all packages to install
1424
libvirt_host_libvirt_packages: >
15-
{{ libvirt_host_libvirt_packages_default +
25+
{{ libvirt_host_libvirt_packages_common + [libvirt_host_libvirt_packages_libvirt_daemon] +
1626
(libvirt_host_packages_efi if libvirt_host_enable_efi_support else []) | unique
1727
}}
28+
29+
# These are passed to the lineinfile module to customize configuration files
30+
libvirt_host_lineinfile_extra_rules: []

vars/RedHat.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ libvirt_host_custom_yum_repos_efi:
3232

3333
libvirt_host_custom_yum_repos: "{{ libvirt_host_custom_yum_repos_efi if libvirt_host_enable_efi_support else [] | unique }}"
3434

35+
# These are passed to the lineinfile module to customize configuration files
36+
libvirt_host_lineinfile_extra_rules:
37+
- args:
38+
path: /etc/sysconfig/libvirtd
39+
insertafter: '^#LIBVIRTD_ARGS='
40+
regexp: '^LIBVIRTD_ARGS='
41+
line: LIBVIRTD_ARGS="{{ libvirt_host_libvirtd_args }}"
42+
condition: "{{ libvirt_host_libvirtd_args != '' }}"

0 commit comments

Comments
 (0)