blob: a6738e22dff74697ead74881d7fad9dffa204314 [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 Wienande6d99a92014-05-27 13:58:12 +100032 # Restart dbus/firewalld after install of libvirt to avoid a
33 # problem with polkit, which libvirtd brings in. See
34 # https://bugzilla.redhat.com/show_bug.cgi?id=1099031
35 if is_fedora; then
Ian Wienand3ca91b22014-05-16 14:00:01 +100036 sudo service dbus restart
37 sudo service firewalld restart
38 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070039}
40
41# Configures the installed libvirt system so that is accessible by
42# STACK_USER via qemu:///system with management capabilities.
43function configure_libvirt {
44 if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
45 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
46 cat <<EOF | sudo tee -a $QEMU_CONF
47cgroup_device_acl = [
48 "/dev/null", "/dev/full", "/dev/zero",
49 "/dev/random", "/dev/urandom",
50 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
51 "/dev/rtc", "/dev/hpet","/dev/net/tun",
52]
53EOF
54 fi
55
56 if is_ubuntu; then
57 LIBVIRT_DAEMON=libvirt-bin
58 else
59 LIBVIRT_DAEMON=libvirtd
60 fi
61
62 if is_fedora || is_suse; then
63 if is_fedora && [[ $DISTRO =~ (rhel6) || "$os_RELEASE" -le "17" ]]; then
64 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
65[libvirt Management Access]
66Identity=unix-group:$LIBVIRT_GROUP
67Action=org.libvirt.unix.manage
68ResultAny=yes
69ResultInactive=yes
70ResultActive=yes
71EOF
72 elif is_suse && [[ $os_RELEASE = 12.2 || "$os_VENDOR" = "SUSE LINUX" ]]; then
73 # openSUSE < 12.3 or SLE
74 # Work around the fact that polkit-default-privs overrules pklas
75 # with 'unix-group:$group'.
76 cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
77[libvirt Management Access]
78Identity=unix-user:$STACK_USER
79Action=org.libvirt.unix.manage
80ResultAny=yes
81ResultInactive=yes
82ResultActive=yes
83EOF
84 else
85 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
86 # virsh -c qemu:///system by creating a policy-kit rule for
87 # stack-user using the new Javascript syntax
88 rules_dir=/etc/polkit-1/rules.d
89 sudo mkdir -p $rules_dir
90 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
91polkit.addRule(function(action, subject) {
92 if (action.id == 'org.libvirt.unix.manage' &&
93 subject.user == '$STACK_USER') {
94 return polkit.Result.YES;
95 }
96});
97EOF
98 unset rules_dir
99 fi
100 fi
101
102 # The user that nova runs as needs to be member of **libvirtd** group otherwise
103 # nova-compute will be unable to use libvirt.
104 if ! getent group $LIBVIRT_GROUP >/dev/null; then
105 sudo groupadd $LIBVIRT_GROUP
106 fi
107 add_user_to_group $STACK_USER $LIBVIRT_GROUP
108
109 # Enable server side traces for libvirtd
110 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
111 local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:event 3:json 3:file 1:util"
112 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
113 if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
114 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
115 fi
116 if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
117 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
118 fi
119 fi
120
121 # libvirt detects various settings on startup, as we potentially changed
122 # the system configuration (modules, filesystems), we need to restart
123 # libvirt to detect those changes.
124 restart_service $LIBVIRT_DAEMON
125}
126
127
128# Restore xtrace
129$LV_XTRACE
130
131# Local variables:
132# mode: shell-script
133# End: