blob: 58adde7cd47ec97439b0c17489a12ce4c84275b1 [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
Lee Yarwood169f5de2020-01-06 13:45:33 +000027# Enable the Fedora Virtualization Preview Copr repo that provides the latest
28# rawhide builds of QEMU, Libvirt and other virt tools.
29ENABLE_FEDORA_VIRT_PREVIEW_REPO=$(trueorfalse False ENABLE_FEDORA_VIRT_PREVIEW_REPO)
30
Ian Wienandbfcc7602017-03-29 11:52:06 +110031# Enable coredumps for libvirt
32# Bug: https://bugs.launchpad.net/nova/+bug/1643911
33function _enable_coredump {
Ian Wienand2e667782019-11-20 10:41:34 +110034 local confdir=/etc/systemd/system/libvirtd.service.d
Ian Wienandbfcc7602017-03-29 11:52:06 +110035 local conffile=${confdir}/coredump.conf
36
37 # Create a coredump directory, and instruct the kernel to save to
38 # here
39 sudo mkdir -p /var/core
40 sudo chmod a+wrx /var/core
41 echo '/var/core/core.%e.%p.%h.%t' | \
42 sudo tee /proc/sys/kernel/core_pattern
43
44 # Drop a config file to up the core ulimit
45 sudo mkdir -p ${confdir}
46 sudo tee ${conffile} <<EOF
47[Service]
48LimitCORE=infinity
49EOF
50
51 # Tell systemd to reload the unit (service restarts later after
52 # config anyway)
53 sudo systemctl daemon-reload
54}
55
56
Adam Gandelman0f73ff22014-03-13 14:20:43 -070057# Installs required distro-specific libvirt packages.
58function install_libvirt {
Ian Wienandbfcc7602017-03-29 11:52:06 +110059
Adam Gandelman0f73ff22014-03-13 14:20:43 -070060 if is_ubuntu; then
Ian Wienand2e667782019-11-20 10:41:34 +110061 install_package qemu-system libvirt-clients libvirt-daemon-system libvirt-dev
Kevin Zhao551848d2020-04-27 08:39:37 +080062 if is_arch "aarch64"; then
63 install_package qemu-efi
64 fi
Sean Daguef28e7ef2017-05-07 22:02:10 -040065 # uninstall in case the libvirt version changed
66 pip_uninstall libvirt-python
Sean Dague60996b12015-04-08 09:06:49 -040067 pip_install_gr libvirt-python
68 #pip_install_gr <there-si-no-guestfs-in-pypi>
Adam Gandelman0f73ff22014-03-13 14:20:43 -070069 elif is_fedora || is_suse; then
Ian Wienand0d0b6902017-11-17 10:33:11 +110070
Lee Yarwood169f5de2020-01-06 13:45:33 +000071 # Optionally enable the virt-preview repo when on Fedora
72 if [[ $DISTRO =~ f[0-9][0-9] ]] && [[ ${ENABLE_FEDORA_VIRT_PREVIEW_REPO} == "True" ]]; then
73 # https://copr.fedorainfracloud.org/coprs/g/virtmaint-sig/virt-preview/
74 sudo dnf copr enable -y @virtmaint-sig/virt-preview
75 fi
76
Ian Wienand0d0b6902017-11-17 10:33:11 +110077 # Note that in CentOS/RHEL this needs to come from the RDO
78 # repositories (qemu-kvm-ev ... which provides this package)
79 # as the base system version is too old. We should have
80 # pre-installed these
81 install_package qemu-kvm
82
Dean Troyer4533eee2015-02-17 16:25:38 -060083 install_package libvirt libvirt-devel
Radosław Piliszek96509ea2020-10-21 20:33:08 +020084 if is_arch "x86_64"; then
85 # NOTE(yoctozepto): recent edk2-ovmf on CentOS Stream 8 x86_64 started failing with
86 # "libvirt.libvirtError: internal error: unknown feature amd-sev-es",
87 # so reinstall a known working version until the relevant bugs get fixed:
88 # * https://bugzilla.redhat.com/show_bug.cgi?id=1961558
89 # * https://bugzilla.redhat.com/show_bug.cgi?id=1961562
90 # TODO(yoctozepto): Remove this code when the time is right.
91 if [ "$os_VENDOR" = "CentOSStream" ]; then
92 install_package edk2-ovmf-20200602gitca407c7246bf-4.el8
93 fi
94 elif is_arch "aarch64"; then
Kevin Zhao551848d2020-04-27 08:39:37 +080095 install_package edk2.git-aarch64
96 fi
97
Sean Daguef28e7ef2017-05-07 22:02:10 -040098 pip_uninstall libvirt-python
Sean Dague60996b12015-04-08 09:06:49 -040099 pip_install_gr libvirt-python
Ian Wienandbfcc7602017-03-29 11:52:06 +1100100 fi
Ian Wienand52bb6412017-02-27 15:11:11 +1100101
Ian Wienandbfcc7602017-03-29 11:52:06 +1100102 if [[ $DEBUG_LIBVIRT_COREDUMPS == True ]]; then
103 _enable_coredump
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700104 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700105}
106
107# Configures the installed libvirt system so that is accessible by
108# STACK_USER via qemu:///system with management capabilities.
109function configure_libvirt {
Kevin Bentond1fe0e62017-05-16 22:27:58 -0700110 if is_service_enabled neutron && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700111 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
112 cat <<EOF | sudo tee -a $QEMU_CONF
113cgroup_device_acl = [
114 "/dev/null", "/dev/full", "/dev/zero",
115 "/dev/random", "/dev/urandom",
116 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
117 "/dev/rtc", "/dev/hpet","/dev/net/tun",
Lenny Verkhovsky0a407102016-09-04 12:52:01 +0000118 "/dev/vfio/vfio",
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700119]
120EOF
121 fi
122
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700123 if is_fedora || is_suse; then
Attila Fazekas1f316be2015-01-26 16:39:57 +0100124 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
125 # virsh -c qemu:///system by creating a policy-kit rule for
126 # stack-user using the new Javascript syntax
127 rules_dir=/etc/polkit-1/rules.d
128 sudo mkdir -p $rules_dir
129 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700130polkit.addRule(function(action, subject) {
131 if (action.id == 'org.libvirt.unix.manage' &&
132 subject.user == '$STACK_USER') {
133 return polkit.Result.YES;
134 }
135});
136EOF
Attila Fazekas1f316be2015-01-26 16:39:57 +0100137 unset rules_dir
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700138 fi
139
140 # The user that nova runs as needs to be member of **libvirtd** group otherwise
141 # nova-compute will be unable to use libvirt.
142 if ! getent group $LIBVIRT_GROUP >/dev/null; then
143 sudo groupadd $LIBVIRT_GROUP
144 fi
145 add_user_to_group $STACK_USER $LIBVIRT_GROUP
146
147 # Enable server side traces for libvirtd
148 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100149 if is_ubuntu; then
150 # Unexpectedly binary package builds in ubuntu get fully qualified
151 # source file paths, not relative paths. This screws with the matching
152 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
153 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
Kashyap Chamarthy07dc2bf2016-06-01 12:21:00 +0200154 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 +0100155 else
Kashyap Chamarthy07dc2bf2016-06-01 12:21:00 +0200156 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 +0100157 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700158 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
Mark McLoughlin76cbbe32015-12-07 05:05:04 -0500159 if ! sudo grep -q "^log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700160 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
161 fi
Mark McLoughlin76cbbe32015-12-07 05:05:04 -0500162 if ! sudo grep -q "^log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700163 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
164 fi
165 fi
166
Daniel P. Berrangee9870eb2016-11-10 13:03:32 +0000167 if is_nova_console_proxy_compute_tls_enabled ; then
melanie witt1d378dc2019-10-23 04:20:23 +0000168 echo "vnc_tls = 1" | sudo tee -a $QEMU_CONF
169 echo "vnc_tls_x509_verify = 1" | sudo tee -a $QEMU_CONF
Daniel P. Berrangee9870eb2016-11-10 13:03:32 +0000170
melanie witt1d378dc2019-10-23 04:20:23 +0000171 sudo mkdir -p /etc/pki/libvirt-vnc
172 deploy_int_CA /etc/pki/libvirt-vnc/ca-cert.pem
173 deploy_int_cert /etc/pki/libvirt-vnc/server-cert.pem /etc/pki/libvirt-vnc/server-key.pem
174 # OpenSSL 1.1.0 generates the key file with permissions: 600, by
175 # default and the deploy_int* methods use 'sudo cp' to copy the
176 # files, making them owned by root:root.
177 # Change ownership of everything under /etc/pki/libvirt-vnc to
178 # libvirt-qemu:libvirt-qemu so that libvirt-qemu can read the key
179 # file.
180 sudo chown -R libvirt-qemu:libvirt-qemu /etc/pki/libvirt-vnc
Daniel P. Berrangee9870eb2016-11-10 13:03:32 +0000181 fi
182
Sean Dague214459c2016-06-02 10:29:59 -0400183 # Service needs to be started on redhat/fedora -- do a restart for
184 # sanity after fiddling the config.
Ian Wienand2e667782019-11-20 10:41:34 +1100185 restart_service libvirtd
Janki Chhatbar8f586fb2016-07-18 08:19:25 +0530186
187 # Restart virtlogd companion service to ensure it is running properly
188 # https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/1577455
189 # https://bugzilla.redhat.com/show_bug.cgi?id=1290357
190 # (not all platforms have it; libvirt 1.3+ only, thus the ignore)
191 restart_service virtlogd || true
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700192}
193
194
195# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100196$_XTRACE_NOVA_FN_LIBVIRT
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700197
198# Local variables:
199# mode: shell-script
200# End: