blob: 4d617e8b5e9f53d1fa6bb4ed7545685ad42750c2 [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
Attila Fazekas1f316be2015-01-26 16:39:57 +010040 # cloud doesn't.
Dean Troyer85ebb3a2014-08-19 10:54:59 -050041 if is_fedora && is_package_installed firewalld; then
Ian Wienand83afcfe2014-06-12 08:47:50 +100042 sudo service firewalld restart || true
Ian Wienand3ca91b22014-05-16 14:00:01 +100043 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070044}
45
46# Configures the installed libvirt system so that is accessible by
47# STACK_USER via qemu:///system with management capabilities.
48function configure_libvirt {
49 if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
50 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
51 cat <<EOF | sudo tee -a $QEMU_CONF
52cgroup_device_acl = [
53 "/dev/null", "/dev/full", "/dev/zero",
54 "/dev/random", "/dev/urandom",
55 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
56 "/dev/rtc", "/dev/hpet","/dev/net/tun",
57]
58EOF
59 fi
60
Gonéri Le Boudera2143632014-09-17 11:55:31 +020061 # Since the release of Debian Wheezy the libvirt init script is libvirtd
62 # and not libvirtd-bin anymore.
63 if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -070064 LIBVIRT_DAEMON=libvirt-bin
65 else
66 LIBVIRT_DAEMON=libvirtd
67 fi
68
69 if is_fedora || is_suse; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010070 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
71 # virsh -c qemu:///system by creating a policy-kit rule for
72 # stack-user using the new Javascript syntax
73 rules_dir=/etc/polkit-1/rules.d
74 sudo mkdir -p $rules_dir
75 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
Adam Gandelman0f73ff22014-03-13 14:20:43 -070076polkit.addRule(function(action, subject) {
77 if (action.id == 'org.libvirt.unix.manage' &&
78 subject.user == '$STACK_USER') {
79 return polkit.Result.YES;
80 }
81});
82EOF
Attila Fazekas1f316be2015-01-26 16:39:57 +010083 unset rules_dir
Adam Gandelman0f73ff22014-03-13 14:20:43 -070084 fi
85
86 # The user that nova runs as needs to be member of **libvirtd** group otherwise
87 # nova-compute will be unable to use libvirt.
88 if ! getent group $LIBVIRT_GROUP >/dev/null; then
89 sudo groupadd $LIBVIRT_GROUP
90 fi
91 add_user_to_group $STACK_USER $LIBVIRT_GROUP
92
93 # Enable server side traces for libvirtd
94 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +010095 if is_ubuntu; then
96 # Unexpectedly binary package builds in ubuntu get fully qualified
97 # source file paths, not relative paths. This screws with the matching
98 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
99 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
100 local log_filters="1:libvirt.c 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util"
101 else
102 local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util"
103 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700104 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: