blob: 790227beb5cf3f036858bd9bd26dd1e46d840ec0 [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
Ian Wienand523f4882015-10-13 11:03:03 +110011_XTRACE_NOVA_FN_LIBVIRT=$(set +o | grep xtrace)
Adam Gandelman0f73ff22014-03-13 14:20:43 -070012set +o xtrace
13
14# Defaults
Dean Troyer3324f192014-09-18 09:26:39 -050015# --------
Adam Gandelman0f73ff22014-03-13 14:20:43 -070016
Kashyap Chamarthyd3cfb822015-04-01 11:30:57 +020017# Turn on selective debug log filters for libvirt.
18# (NOTE: Enabling this by default, because the log filters enabled in
19# 'configure_libvirt' function further below are _selective_ and not
20# extremely verbose.)
21DEBUG_LIBVIRT=$(trueorfalse True DEBUG_LIBVIRT)
Adam Gandelman0f73ff22014-03-13 14:20:43 -070022
23# Installs required distro-specific libvirt packages.
24function install_libvirt {
25 if is_ubuntu; then
Kevin Zhaoa80d4092016-08-11 10:41:34 +000026 install_package qemu-system
Dean Troyer4533eee2015-02-17 16:25:38 -060027 install_package libvirt-bin libvirt-dev
Sean Dague60996b12015-04-08 09:06:49 -040028 pip_install_gr libvirt-python
Sean Dague7860f2b2015-11-17 11:59:07 -050029 if [[ "$EBTABLES_RACE_FIX" == "True" ]]; then
30 # Work around for bug #1501558. We can remove this once we
31 # get to a version of Ubuntu that has new enough libvirt.
32 TOP_DIR=$TOP_DIR $TOP_DIR/tools/install_ebtables_workaround.sh
33 fi
Sean Dague60996b12015-04-08 09:06:49 -040034 #pip_install_gr <there-si-no-guestfs-in-pypi>
Adam Gandelman0f73ff22014-03-13 14:20:43 -070035 elif is_fedora || is_suse; then
Alexander Schmidta5ea08b2015-12-02 15:30:44 +000036 # On "KVM for IBM z Systems", kvm does not have its own package
37 if [[ ! ${DISTRO} =~ "kvmibm1" ]]; then
38 install_package kvm
39 fi
Ian Wienand614ca262015-06-26 14:45:04 +100040 # there is a dependency issue with kvm (which is really just a
41 # wrapper to qemu-system-x86) that leaves some bios files out,
42 # so install qemu-kvm (which shouldn't strictly be needed, as
43 # everything has been merged into qemu-system-x86) to bring in
44 # the right packages. see
45 # https://bugzilla.redhat.com/show_bug.cgi?id=1235890
46 install_package qemu-kvm
Dean Troyer4533eee2015-02-17 16:25:38 -060047 install_package libvirt libvirt-devel
Sean Dague60996b12015-04-08 09:06:49 -040048 pip_install_gr libvirt-python
Adam Gandelman0f73ff22014-03-13 14:20:43 -070049 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070050}
51
52# Configures the installed libvirt system so that is accessible by
53# STACK_USER via qemu:///system with management capabilities.
54function configure_libvirt {
55 if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
56 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
57 cat <<EOF | sudo tee -a $QEMU_CONF
58cgroup_device_acl = [
59 "/dev/null", "/dev/full", "/dev/zero",
60 "/dev/random", "/dev/urandom",
61 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
62 "/dev/rtc", "/dev/hpet","/dev/net/tun",
63]
64EOF
65 fi
66
Gonéri Le Boudera2143632014-09-17 11:55:31 +020067 # Since the release of Debian Wheezy the libvirt init script is libvirtd
68 # and not libvirtd-bin anymore.
69 if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -070070 LIBVIRT_DAEMON=libvirt-bin
71 else
72 LIBVIRT_DAEMON=libvirtd
73 fi
74
75 if is_fedora || is_suse; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010076 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
77 # virsh -c qemu:///system by creating a policy-kit rule for
78 # stack-user using the new Javascript syntax
79 rules_dir=/etc/polkit-1/rules.d
80 sudo mkdir -p $rules_dir
81 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
Adam Gandelman0f73ff22014-03-13 14:20:43 -070082polkit.addRule(function(action, subject) {
83 if (action.id == 'org.libvirt.unix.manage' &&
84 subject.user == '$STACK_USER') {
85 return polkit.Result.YES;
86 }
87});
88EOF
Attila Fazekas1f316be2015-01-26 16:39:57 +010089 unset rules_dir
Adam Gandelman0f73ff22014-03-13 14:20:43 -070090 fi
91
92 # The user that nova runs as needs to be member of **libvirtd** group otherwise
93 # nova-compute will be unable to use libvirt.
94 if ! getent group $LIBVIRT_GROUP >/dev/null; then
95 sudo groupadd $LIBVIRT_GROUP
96 fi
97 add_user_to_group $STACK_USER $LIBVIRT_GROUP
98
99 # Enable server side traces for libvirtd
100 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100101 if is_ubuntu; then
102 # Unexpectedly binary package builds in ubuntu get fully qualified
103 # source file paths, not relative paths. This screws with the matching
104 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
105 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
Kashyap Chamarthy07dc2bf2016-06-01 12:21:00 +0200106 local log_filters="1:libvirt.c 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util 1:cpu"
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100107 else
Kashyap Chamarthy07dc2bf2016-06-01 12:21:00 +0200108 local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util 1:cpu"
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100109 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700110 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
Mark McLoughlin76cbbe32015-12-07 05:05:04 -0500111 if ! sudo grep -q "^log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700112 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
113 fi
Mark McLoughlin76cbbe32015-12-07 05:05:04 -0500114 if ! sudo grep -q "^log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700115 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
116 fi
117 fi
118
Sean Dague214459c2016-06-02 10:29:59 -0400119 # Service needs to be started on redhat/fedora -- do a restart for
120 # sanity after fiddling the config.
121 restart_service $LIBVIRT_DAEMON
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700122}
123
124
125# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100126$_XTRACE_NOVA_FN_LIBVIRT
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700127
128# Local variables:
129# mode: shell-script
130# End: