blob: 05ef605b049372fb0583a6def5fb0c92620c94fd [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
Dean Troyer4533eee2015-02-17 16:25:38 -060024 install_package libvirt-bin libvirt-dev
25 pip_install libvirt-python
26 install_package libguestfs0
Sean Dague206c5962015-03-30 13:56:11 -040027 install_package python-guestfs
Dean Troyer4533eee2015-02-17 16:25:38 -060028 #pip_install <there-si-no-guestfs-in-pypi>
Adam Gandelman0f73ff22014-03-13 14:20:43 -070029 elif is_fedora || is_suse; then
30 install_package kvm
Dean Troyer4533eee2015-02-17 16:25:38 -060031 install_package libvirt libvirt-devel
32 pip_install libvirt-python
Sean Dague206c5962015-03-30 13:56:11 -040033 install_package python-libguestfs
Adam Gandelman0f73ff22014-03-13 14:20:43 -070034 fi
Ian Wienand3ca91b22014-05-16 14:00:01 +100035
Ian Wienand83afcfe2014-06-12 08:47:50 +100036 # Restart firewalld after install of libvirt to avoid a problem
37 # with polkit, which libvirtd brings in. See
Ian Wienande6d99a92014-05-27 13:58:12 +100038 # https://bugzilla.redhat.com/show_bug.cgi?id=1099031
Ian Wienand83afcfe2014-06-12 08:47:50 +100039
40 # Note there is a difference between F20 rackspace cloud images
41 # and HP images used in the gate; rackspace has firewalld but hp
Attila Fazekas1f316be2015-01-26 16:39:57 +010042 # cloud doesn't.
Dean Troyer85ebb3a2014-08-19 10:54:59 -050043 if is_fedora && is_package_installed firewalld; then
Ian Wienand83afcfe2014-06-12 08:47:50 +100044 sudo service firewalld restart || true
Ian Wienand3ca91b22014-05-16 14:00:01 +100045 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070046}
47
48# Configures the installed libvirt system so that is accessible by
49# STACK_USER via qemu:///system with management capabilities.
50function configure_libvirt {
51 if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
52 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
53 cat <<EOF | sudo tee -a $QEMU_CONF
54cgroup_device_acl = [
55 "/dev/null", "/dev/full", "/dev/zero",
56 "/dev/random", "/dev/urandom",
57 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
58 "/dev/rtc", "/dev/hpet","/dev/net/tun",
59]
60EOF
61 fi
62
Gonéri Le Boudera2143632014-09-17 11:55:31 +020063 # Since the release of Debian Wheezy the libvirt init script is libvirtd
64 # and not libvirtd-bin anymore.
65 if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -070066 LIBVIRT_DAEMON=libvirt-bin
67 else
68 LIBVIRT_DAEMON=libvirtd
69 fi
70
71 if is_fedora || is_suse; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010072 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
73 # virsh -c qemu:///system by creating a policy-kit rule for
74 # stack-user using the new Javascript syntax
75 rules_dir=/etc/polkit-1/rules.d
76 sudo mkdir -p $rules_dir
77 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
Adam Gandelman0f73ff22014-03-13 14:20:43 -070078polkit.addRule(function(action, subject) {
79 if (action.id == 'org.libvirt.unix.manage' &&
80 subject.user == '$STACK_USER') {
81 return polkit.Result.YES;
82 }
83});
84EOF
Attila Fazekas1f316be2015-01-26 16:39:57 +010085 unset rules_dir
Adam Gandelman0f73ff22014-03-13 14:20:43 -070086 fi
87
88 # The user that nova runs as needs to be member of **libvirtd** group otherwise
89 # nova-compute will be unable to use libvirt.
90 if ! getent group $LIBVIRT_GROUP >/dev/null; then
91 sudo groupadd $LIBVIRT_GROUP
92 fi
93 add_user_to_group $STACK_USER $LIBVIRT_GROUP
94
95 # Enable server side traces for libvirtd
96 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +010097 if is_ubuntu; then
98 # Unexpectedly binary package builds in ubuntu get fully qualified
99 # source file paths, not relative paths. This screws with the matching
100 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
101 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
Kashyap Chamarthyae7b4f92015-03-31 20:49:15 +0200102 local log_filters="1:libvirt.c 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util 1:qemu_monitor"
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100103 else
Kashyap Chamarthyae7b4f92015-03-31 20:49:15 +0200104 local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util 1:qemu_monitor"
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100105 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700106 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
107 if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
108 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
109 fi
110 if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
111 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
112 fi
113 fi
114
115 # libvirt detects various settings on startup, as we potentially changed
116 # the system configuration (modules, filesystems), we need to restart
117 # libvirt to detect those changes.
118 restart_service $LIBVIRT_DAEMON
119}
120
121
122# Restore xtrace
123$LV_XTRACE
124
125# Local variables:
126# mode: shell-script
127# End: