blob: f435456e7ffff460ae9419a682501837cfd69117 [file] [log] [blame]
Adam Gandelman0f73ff22014-03-13 14:20:43 -07001# lib/nova_plugins/functions-libvirt
2# Common libvirt configuration functions
3
4# Dependencies:
5# ``functions`` file
6# ``STACK_USER`` has to be defined
7
8# Save trace setting
9LV_XTRACE=$(set +o | grep xtrace)
10set +o xtrace
11
12# Defaults
13# -------
14
15# if we should turn on massive libvirt debugging
16DEBUG_LIBVIRT=$(trueorfalse False $DEBUG_LIBVIRT)
17
18# Installs required distro-specific libvirt packages.
19function install_libvirt {
20 if is_ubuntu; then
Adam Gandelmanb0f8beb2014-03-27 00:14:24 -070021 install_package qemu-kvm
Adam Gandelman0f73ff22014-03-13 14:20:43 -070022 install_package libvirt-bin
23 install_package python-libvirt
24 install_package python-guestfs
25 elif is_fedora || is_suse; then
26 install_package kvm
27 install_package libvirt
28 install_package libvirt-python
29 install_package python-libguestfs
30 fi
Ian Wienand3ca91b22014-05-16 14:00:01 +100031
32 # workaround for
33 # https://bugzilla.redhat.com/show_bug.cgi?id=1098376; if we see
34 # the empty Xen proc file then remove the xen/libxl plugin
35 # shared-libraries (yum remove would uninstall libvirt due to
36 # dependencies, so let's avoid that...)
37 if is_fedora && [ -f /proc/xen/capabilities ] && \
38 [ $(stat -c '%s' /proc/xen/capabilities) -eq 0 ]; then
39 sudo rm -f /usr/lib64/libvirt/connection-driver/libvirt_driver_libxl.so
40 sudo rm -f /usr/lib64/libvirt/connection-driver/libvirt_driver_xen.so
41
42 # another bug requires these to be restarted to avoid
43 # potential hang of libvirtd
44 # https://bugzilla.redhat.com/show_bug.cgi?id=1098866
45 sudo service dbus restart
46 sudo service firewalld restart
47 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070048}
49
50# Configures the installed libvirt system so that is accessible by
51# STACK_USER via qemu:///system with management capabilities.
52function configure_libvirt {
53 if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
54 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
55 cat <<EOF | sudo tee -a $QEMU_CONF
56cgroup_device_acl = [
57 "/dev/null", "/dev/full", "/dev/zero",
58 "/dev/random", "/dev/urandom",
59 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
60 "/dev/rtc", "/dev/hpet","/dev/net/tun",
61]
62EOF
63 fi
64
65 if is_ubuntu; then
66 LIBVIRT_DAEMON=libvirt-bin
67 else
68 LIBVIRT_DAEMON=libvirtd
69 fi
70
71 if is_fedora || is_suse; then
72 if is_fedora && [[ $DISTRO =~ (rhel6) || "$os_RELEASE" -le "17" ]]; then
73 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
74[libvirt Management Access]
75Identity=unix-group:$LIBVIRT_GROUP
76Action=org.libvirt.unix.manage
77ResultAny=yes
78ResultInactive=yes
79ResultActive=yes
80EOF
81 elif is_suse && [[ $os_RELEASE = 12.2 || "$os_VENDOR" = "SUSE LINUX" ]]; then
82 # openSUSE < 12.3 or SLE
83 # Work around the fact that polkit-default-privs overrules pklas
84 # with 'unix-group:$group'.
85 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
86[libvirt Management Access]
87Identity=unix-user:$STACK_USER
88Action=org.libvirt.unix.manage
89ResultAny=yes
90ResultInactive=yes
91ResultActive=yes
92EOF
93 else
94 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
95 # virsh -c qemu:///system by creating a policy-kit rule for
96 # stack-user using the new Javascript syntax
97 rules_dir=/etc/polkit-1/rules.d
98 sudo mkdir -p $rules_dir
99 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
100polkit.addRule(function(action, subject) {
101 if (action.id == 'org.libvirt.unix.manage' &&
102 subject.user == '$STACK_USER') {
103 return polkit.Result.YES;
104 }
105});
106EOF
107 unset rules_dir
108 fi
109 fi
110
111 # The user that nova runs as needs to be member of **libvirtd** group otherwise
112 # nova-compute will be unable to use libvirt.
113 if ! getent group $LIBVIRT_GROUP >/dev/null; then
114 sudo groupadd $LIBVIRT_GROUP
115 fi
116 add_user_to_group $STACK_USER $LIBVIRT_GROUP
117
118 # Enable server side traces for libvirtd
119 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
120 local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:event 3:json 3:file 1:util"
121 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
122 if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
123 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
124 fi
125 if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
126 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
127 fi
128 fi
129
130 # libvirt detects various settings on startup, as we potentially changed
131 # the system configuration (modules, filesystems), we need to restart
132 # libvirt to detect those changes.
133 restart_service $LIBVIRT_DAEMON
134}
135
136
137# Restore xtrace
138$LV_XTRACE
139
140# Local variables:
141# mode: shell-script
142# End: