blob: 258e1a40e60a7af36b3f3a8be9e20df27f4e5e1e [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
Ian Wienand83afcfe2014-06-12 08:47:50 +100032 # Restart firewalld after install of libvirt to avoid a problem
33 # with polkit, which libvirtd brings in. See
Ian Wienande6d99a92014-05-27 13:58:12 +100034 # https://bugzilla.redhat.com/show_bug.cgi?id=1099031
Ian Wienand83afcfe2014-06-12 08:47:50 +100035
36 # Note there is a difference between F20 rackspace cloud images
37 # and HP images used in the gate; rackspace has firewalld but hp
38 # cloud doesn't. RHEL6 doesn't have firewalld either. So we
39 # don't care if it fails.
Dean Troyer85ebb3a2014-08-19 10:54:59 -050040 if is_fedora && is_package_installed firewalld; then
Ian Wienand83afcfe2014-06-12 08:47:50 +100041 sudo service firewalld restart || true
Ian Wienand3ca91b22014-05-16 14:00:01 +100042 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070043}
44
45# Configures the installed libvirt system so that is accessible by
46# STACK_USER via qemu:///system with management capabilities.
47function configure_libvirt {
48 if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
49 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
50 cat <<EOF | sudo tee -a $QEMU_CONF
51cgroup_device_acl = [
52 "/dev/null", "/dev/full", "/dev/zero",
53 "/dev/random", "/dev/urandom",
54 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
55 "/dev/rtc", "/dev/hpet","/dev/net/tun",
56]
57EOF
58 fi
59
60 if is_ubuntu; then
61 LIBVIRT_DAEMON=libvirt-bin
62 else
63 LIBVIRT_DAEMON=libvirtd
64 fi
65
66 if is_fedora || is_suse; then
67 if is_fedora && [[ $DISTRO =~ (rhel6) || "$os_RELEASE" -le "17" ]]; then
68 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
69[libvirt Management Access]
70Identity=unix-group:$LIBVIRT_GROUP
71Action=org.libvirt.unix.manage
72ResultAny=yes
73ResultInactive=yes
74ResultActive=yes
75EOF
76 elif is_suse && [[ $os_RELEASE = 12.2 || "$os_VENDOR" = "SUSE LINUX" ]]; then
77 # openSUSE < 12.3 or SLE
78 # Work around the fact that polkit-default-privs overrules pklas
79 # with 'unix-group:$group'.
80 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
81[libvirt Management Access]
82Identity=unix-user:$STACK_USER
83Action=org.libvirt.unix.manage
84ResultAny=yes
85ResultInactive=yes
86ResultActive=yes
87EOF
88 else
89 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
90 # virsh -c qemu:///system by creating a policy-kit rule for
91 # stack-user using the new Javascript syntax
92 rules_dir=/etc/polkit-1/rules.d
93 sudo mkdir -p $rules_dir
94 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
95polkit.addRule(function(action, subject) {
96 if (action.id == 'org.libvirt.unix.manage' &&
97 subject.user == '$STACK_USER') {
98 return polkit.Result.YES;
99 }
100});
101EOF
102 unset rules_dir
103 fi
104 fi
105
106 # The user that nova runs as needs to be member of **libvirtd** group otherwise
107 # nova-compute will be unable to use libvirt.
108 if ! getent group $LIBVIRT_GROUP >/dev/null; then
109 sudo groupadd $LIBVIRT_GROUP
110 fi
111 add_user_to_group $STACK_USER $LIBVIRT_GROUP
112
113 # Enable server side traces for libvirtd
114 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100115 if is_ubuntu; then
116 # Unexpectedly binary package builds in ubuntu get fully qualified
117 # source file paths, not relative paths. This screws with the matching
118 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
119 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
120 local log_filters="1:libvirt.c 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util"
121 else
122 local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util"
123 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700124 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
125 if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
126 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
127 fi
128 if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
129 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
130 fi
131 fi
132
133 # libvirt detects various settings on startup, as we potentially changed
134 # the system configuration (modules, filesystems), we need to restart
135 # libvirt to detect those changes.
136 restart_service $LIBVIRT_DAEMON
137}
138
139
140# Restore xtrace
141$LV_XTRACE
142
143# Local variables:
144# mode: shell-script
145# End: