blob: c8527387ab2946a4302bac22efad58290c3c4810 [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
Ian Wienandbfcc7602017-03-29 11:52:06 +110023# Try to enable coredumps for libvirt
24# Currently fairly specific to OpenStackCI hosts
25DEBUG_LIBVIRT_COREDUMPS=$(trueorfalse False DEBUG_LIBVIRT_COREDUMPS)
26
27# Only Xenial is left with libvirt-bin. Everywhere else is libvirtd
28if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then
29 LIBVIRT_DAEMON=libvirt-bin
30else
31 LIBVIRT_DAEMON=libvirtd
32fi
33
34# Enable coredumps for libvirt
35# Bug: https://bugs.launchpad.net/nova/+bug/1643911
36function _enable_coredump {
37 local confdir=/etc/systemd/system/${LIBVIRT_DAEMON}.service.d
38 local conffile=${confdir}/coredump.conf
39
40 # Create a coredump directory, and instruct the kernel to save to
41 # here
42 sudo mkdir -p /var/core
43 sudo chmod a+wrx /var/core
44 echo '/var/core/core.%e.%p.%h.%t' | \
45 sudo tee /proc/sys/kernel/core_pattern
46
47 # Drop a config file to up the core ulimit
48 sudo mkdir -p ${confdir}
49 sudo tee ${conffile} <<EOF
50[Service]
51LimitCORE=infinity
52EOF
53
54 # Tell systemd to reload the unit (service restarts later after
55 # config anyway)
56 sudo systemctl daemon-reload
57}
58
59
Adam Gandelman0f73ff22014-03-13 14:20:43 -070060# Installs required distro-specific libvirt packages.
61function install_libvirt {
Ian Wienandbfcc7602017-03-29 11:52:06 +110062
Adam Gandelman0f73ff22014-03-13 14:20:43 -070063 if is_ubuntu; then
Kevin Zhaoa80d4092016-08-11 10:41:34 +000064 install_package qemu-system
David Rabel682e0ab2017-03-17 19:19:00 +010065 if [[ ${DISTRO} == "xenial" ]]; then
66 install_package libvirt-bin libvirt-dev
67 else
68 install_package libvirt-clients libvirt-daemon-system libvirt-dev
69 fi
Sean Daguef28e7ef2017-05-07 22:02:10 -040070 # uninstall in case the libvirt version changed
71 pip_uninstall libvirt-python
Sean Dague60996b12015-04-08 09:06:49 -040072 pip_install_gr libvirt-python
73 #pip_install_gr <there-si-no-guestfs-in-pypi>
Adam Gandelman0f73ff22014-03-13 14:20:43 -070074 elif is_fedora || is_suse; then
Dean Troyer4533eee2015-02-17 16:25:38 -060075 install_package libvirt libvirt-devel
Sean Daguef28e7ef2017-05-07 22:02:10 -040076 pip_uninstall libvirt-python
Sean Dague60996b12015-04-08 09:06:49 -040077 pip_install_gr libvirt-python
Ian Wienandbfcc7602017-03-29 11:52:06 +110078 fi
Ian Wienand52bb6412017-02-27 15:11:11 +110079
Ian Wienandbfcc7602017-03-29 11:52:06 +110080 if [[ $DEBUG_LIBVIRT_COREDUMPS == True ]]; then
81 _enable_coredump
Adam Gandelman0f73ff22014-03-13 14:20:43 -070082 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070083}
84
85# Configures the installed libvirt system so that is accessible by
86# STACK_USER via qemu:///system with management capabilities.
87function configure_libvirt {
Kevin Bentond1fe0e62017-05-16 22:27:58 -070088 if is_service_enabled neutron && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -070089 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
90 cat <<EOF | sudo tee -a $QEMU_CONF
91cgroup_device_acl = [
92 "/dev/null", "/dev/full", "/dev/zero",
93 "/dev/random", "/dev/urandom",
94 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
95 "/dev/rtc", "/dev/hpet","/dev/net/tun",
Lenny Verkhovsky0a407102016-09-04 12:52:01 +000096 "/dev/vfio/vfio",
Adam Gandelman0f73ff22014-03-13 14:20:43 -070097]
98EOF
99 fi
100
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700101 if is_fedora || is_suse; then
Attila Fazekas1f316be2015-01-26 16:39:57 +0100102 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
103 # virsh -c qemu:///system by creating a policy-kit rule for
104 # stack-user using the new Javascript syntax
105 rules_dir=/etc/polkit-1/rules.d
106 sudo mkdir -p $rules_dir
107 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700108polkit.addRule(function(action, subject) {
109 if (action.id == 'org.libvirt.unix.manage' &&
110 subject.user == '$STACK_USER') {
111 return polkit.Result.YES;
112 }
113});
114EOF
Attila Fazekas1f316be2015-01-26 16:39:57 +0100115 unset rules_dir
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700116 fi
117
118 # The user that nova runs as needs to be member of **libvirtd** group otherwise
119 # nova-compute will be unable to use libvirt.
120 if ! getent group $LIBVIRT_GROUP >/dev/null; then
121 sudo groupadd $LIBVIRT_GROUP
122 fi
123 add_user_to_group $STACK_USER $LIBVIRT_GROUP
124
125 # Enable server side traces for libvirtd
126 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100127 if is_ubuntu; then
128 # Unexpectedly binary package builds in ubuntu get fully qualified
129 # source file paths, not relative paths. This screws with the matching
130 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
131 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
Kashyap Chamarthy07dc2bf2016-06-01 12:21:00 +0200132 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 +0100133 else
Kashyap Chamarthy07dc2bf2016-06-01 12:21:00 +0200134 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 +0100135 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700136 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
Mark McLoughlin76cbbe32015-12-07 05:05:04 -0500137 if ! sudo grep -q "^log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700138 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
139 fi
Mark McLoughlin76cbbe32015-12-07 05:05:04 -0500140 if ! sudo grep -q "^log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700141 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
142 fi
143 fi
144
Sean Dague214459c2016-06-02 10:29:59 -0400145 # Service needs to be started on redhat/fedora -- do a restart for
146 # sanity after fiddling the config.
147 restart_service $LIBVIRT_DAEMON
Janki Chhatbar8f586fb2016-07-18 08:19:25 +0530148
149 # Restart virtlogd companion service to ensure it is running properly
150 # https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/1577455
151 # https://bugzilla.redhat.com/show_bug.cgi?id=1290357
152 # (not all platforms have it; libvirt 1.3+ only, thus the ignore)
153 restart_service virtlogd || true
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700154}
155
156
157# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100158$_XTRACE_NOVA_FN_LIBVIRT
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700159
160# Local variables:
161# mode: shell-script
162# End: