blob: d3c4eab0aa670383f3e38dc9ea1720698d978d06 [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
Dean Troyer3324f192014-09-18 09:26:39 -050013# --------
Adam Gandelman0f73ff22014-03-13 14:20:43 -070014
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
Gonéri Le Boudera2143632014-09-17 11:55:31 +020060 # Since the release of Debian Wheezy the libvirt init script is libvirtd
61 # and not libvirtd-bin anymore.
62 if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -070063 LIBVIRT_DAEMON=libvirt-bin
64 else
65 LIBVIRT_DAEMON=libvirtd
66 fi
67
68 if is_fedora || is_suse; then
69 if is_fedora && [[ $DISTRO =~ (rhel6) || "$os_RELEASE" -le "17" ]]; then
70 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
71[libvirt Management Access]
72Identity=unix-group:$LIBVIRT_GROUP
73Action=org.libvirt.unix.manage
74ResultAny=yes
75ResultInactive=yes
76ResultActive=yes
77EOF
78 elif is_suse && [[ $os_RELEASE = 12.2 || "$os_VENDOR" = "SUSE LINUX" ]]; then
79 # openSUSE < 12.3 or SLE
80 # Work around the fact that polkit-default-privs overrules pklas
81 # with 'unix-group:$group'.
82 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
83[libvirt Management Access]
84Identity=unix-user:$STACK_USER
85Action=org.libvirt.unix.manage
86ResultAny=yes
87ResultInactive=yes
88ResultActive=yes
89EOF
90 else
91 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
92 # virsh -c qemu:///system by creating a policy-kit rule for
93 # stack-user using the new Javascript syntax
94 rules_dir=/etc/polkit-1/rules.d
95 sudo mkdir -p $rules_dir
96 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
97polkit.addRule(function(action, subject) {
98 if (action.id == 'org.libvirt.unix.manage' &&
99 subject.user == '$STACK_USER') {
100 return polkit.Result.YES;
101 }
102});
103EOF
104 unset rules_dir
105 fi
106 fi
107
108 # The user that nova runs as needs to be member of **libvirtd** group otherwise
109 # nova-compute will be unable to use libvirt.
110 if ! getent group $LIBVIRT_GROUP >/dev/null; then
111 sudo groupadd $LIBVIRT_GROUP
112 fi
113 add_user_to_group $STACK_USER $LIBVIRT_GROUP
114
115 # Enable server side traces for libvirtd
116 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100117 if is_ubuntu; then
118 # Unexpectedly binary package builds in ubuntu get fully qualified
119 # source file paths, not relative paths. This screws with the matching
120 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
121 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
122 local log_filters="1:libvirt.c 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util"
123 else
124 local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util"
125 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700126 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
127 if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
128 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
129 fi
130 if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
131 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
132 fi
133 fi
134
135 # libvirt detects various settings on startup, as we potentially changed
136 # the system configuration (modules, filesystems), we need to restart
137 # libvirt to detect those changes.
138 restart_service $LIBVIRT_DAEMON
139}
140
141
142# Restore xtrace
143$LV_XTRACE
144
145# Local variables:
146# mode: shell-script
147# End: