blob: c136e352df4a53f239a2aefe08c5e5946161c7c3 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Adam Gandelman0f73ff22014-03-13 14:20:43 -07003# lib/nova_plugins/functions-libvirt
4# Common libvirt configuration functions
5
6# Dependencies:
7# ``functions`` file
8# ``STACK_USER`` has to be defined
9
10# Save trace setting
11LV_XTRACE=$(set +o | grep xtrace)
12set +o xtrace
13
14# Defaults
Dean Troyer3324f192014-09-18 09:26:39 -050015# --------
Adam Gandelman0f73ff22014-03-13 14:20:43 -070016
17# if we should turn on massive libvirt debugging
Sean Dague53753292014-12-04 19:38:15 -050018DEBUG_LIBVIRT=$(trueorfalse False DEBUG_LIBVIRT)
Adam Gandelman0f73ff22014-03-13 14:20:43 -070019
20# Installs required distro-specific libvirt packages.
21function install_libvirt {
22 if is_ubuntu; then
Adam Gandelmanb0f8beb2014-03-27 00:14:24 -070023 install_package qemu-kvm
Adam Gandelman0f73ff22014-03-13 14:20:43 -070024 install_package libvirt-bin
25 install_package python-libvirt
26 install_package python-guestfs
27 elif is_fedora || is_suse; then
28 install_package kvm
29 install_package libvirt
30 install_package libvirt-python
31 install_package python-libguestfs
32 fi
Ian Wienand3ca91b22014-05-16 14:00:01 +100033
Ian Wienand83afcfe2014-06-12 08:47:50 +100034 # Restart firewalld after install of libvirt to avoid a problem
35 # with polkit, which libvirtd brings in. See
Ian Wienande6d99a92014-05-27 13:58:12 +100036 # https://bugzilla.redhat.com/show_bug.cgi?id=1099031
Ian Wienand83afcfe2014-06-12 08:47:50 +100037
38 # Note there is a difference between F20 rackspace cloud images
39 # and HP images used in the gate; rackspace has firewalld but hp
40 # cloud doesn't. RHEL6 doesn't have firewalld either. So we
41 # don't care if it fails.
Dean Troyer85ebb3a2014-08-19 10:54:59 -050042 if is_fedora && is_package_installed firewalld; then
Ian Wienand83afcfe2014-06-12 08:47:50 +100043 sudo service firewalld restart || true
Ian Wienand3ca91b22014-05-16 14:00:01 +100044 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070045}
46
47# Configures the installed libvirt system so that is accessible by
48# STACK_USER via qemu:///system with management capabilities.
49function configure_libvirt {
50 if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
51 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
52 cat <<EOF | sudo tee -a $QEMU_CONF
53cgroup_device_acl = [
54 "/dev/null", "/dev/full", "/dev/zero",
55 "/dev/random", "/dev/urandom",
56 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
57 "/dev/rtc", "/dev/hpet","/dev/net/tun",
58]
59EOF
60 fi
61
Gonéri Le Boudera2143632014-09-17 11:55:31 +020062 # Since the release of Debian Wheezy the libvirt init script is libvirtd
63 # and not libvirtd-bin anymore.
64 if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -070065 LIBVIRT_DAEMON=libvirt-bin
66 else
67 LIBVIRT_DAEMON=libvirtd
68 fi
69
70 if is_fedora || is_suse; then
71 if is_fedora && [[ $DISTRO =~ (rhel6) || "$os_RELEASE" -le "17" ]]; then
72 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
73[libvirt Management Access]
74Identity=unix-group:$LIBVIRT_GROUP
75Action=org.libvirt.unix.manage
76ResultAny=yes
77ResultInactive=yes
78ResultActive=yes
79EOF
80 elif is_suse && [[ $os_RELEASE = 12.2 || "$os_VENDOR" = "SUSE LINUX" ]]; then
81 # openSUSE < 12.3 or SLE
82 # Work around the fact that polkit-default-privs overrules pklas
83 # with 'unix-group:$group'.
84 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
85[libvirt Management Access]
86Identity=unix-user:$STACK_USER
87Action=org.libvirt.unix.manage
88ResultAny=yes
89ResultInactive=yes
90ResultActive=yes
91EOF
92 else
93 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
94 # virsh -c qemu:///system by creating a policy-kit rule for
95 # stack-user using the new Javascript syntax
96 rules_dir=/etc/polkit-1/rules.d
97 sudo mkdir -p $rules_dir
98 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
99polkit.addRule(function(action, subject) {
100 if (action.id == 'org.libvirt.unix.manage' &&
101 subject.user == '$STACK_USER') {
102 return polkit.Result.YES;
103 }
104});
105EOF
106 unset rules_dir
107 fi
108 fi
109
110 # The user that nova runs as needs to be member of **libvirtd** group otherwise
111 # nova-compute will be unable to use libvirt.
112 if ! getent group $LIBVIRT_GROUP >/dev/null; then
113 sudo groupadd $LIBVIRT_GROUP
114 fi
115 add_user_to_group $STACK_USER $LIBVIRT_GROUP
116
117 # Enable server side traces for libvirtd
118 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100119 if is_ubuntu; then
120 # Unexpectedly binary package builds in ubuntu get fully qualified
121 # source file paths, not relative paths. This screws with the matching
122 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
123 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
124 local log_filters="1:libvirt.c 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util"
125 else
126 local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util"
127 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700128 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
129 if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
130 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
131 fi
132 if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
133 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
134 fi
135 fi
136
137 # libvirt detects various settings on startup, as we potentially changed
138 # the system configuration (modules, filesystems), we need to restart
139 # libvirt to detect those changes.
140 restart_service $LIBVIRT_DAEMON
141}
142
143
144# Restore xtrace
145$LV_XTRACE
146
147# Local variables:
148# mode: shell-script
149# End: