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