Sean Dague | e263c82 | 2014-12-05 14:25:28 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 3 | # 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 Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 11 | _XTRACE_NOVA_FN_LIBVIRT=$(set +o | grep xtrace) |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 12 | set +o xtrace |
| 13 | |
| 14 | # Defaults |
Dean Troyer | 3324f19 | 2014-09-18 09:26:39 -0500 | [diff] [blame] | 15 | # -------- |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 16 | |
Kashyap Chamarthy | d3cfb82 | 2015-04-01 11:30:57 +0200 | [diff] [blame] | 17 | # 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.) |
| 21 | DEBUG_LIBVIRT=$(trueorfalse True DEBUG_LIBVIRT) |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 22 | |
Ian Wienand | bfcc760 | 2017-03-29 11:52:06 +1100 | [diff] [blame] | 23 | # Try to enable coredumps for libvirt |
| 24 | # Currently fairly specific to OpenStackCI hosts |
| 25 | DEBUG_LIBVIRT_COREDUMPS=$(trueorfalse False DEBUG_LIBVIRT_COREDUMPS) |
| 26 | |
Lee Yarwood | 169f5de | 2020-01-06 13:45:33 +0000 | [diff] [blame] | 27 | # Enable the Fedora Virtualization Preview Copr repo that provides the latest |
| 28 | # rawhide builds of QEMU, Libvirt and other virt tools. |
| 29 | ENABLE_FEDORA_VIRT_PREVIEW_REPO=$(trueorfalse False ENABLE_FEDORA_VIRT_PREVIEW_REPO) |
| 30 | |
Ian Wienand | bfcc760 | 2017-03-29 11:52:06 +1100 | [diff] [blame] | 31 | # Enable coredumps for libvirt |
| 32 | # Bug: https://bugs.launchpad.net/nova/+bug/1643911 |
| 33 | function _enable_coredump { |
Ian Wienand | 2e66778 | 2019-11-20 10:41:34 +1100 | [diff] [blame] | 34 | local confdir=/etc/systemd/system/libvirtd.service.d |
Ian Wienand | bfcc760 | 2017-03-29 11:52:06 +1100 | [diff] [blame] | 35 | 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] |
| 48 | LimitCORE=infinity |
| 49 | EOF |
| 50 | |
| 51 | # Tell systemd to reload the unit (service restarts later after |
| 52 | # config anyway) |
| 53 | sudo systemctl daemon-reload |
| 54 | } |
| 55 | |
| 56 | |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 57 | # Installs required distro-specific libvirt packages. |
| 58 | function install_libvirt { |
Radosław Piliszek | 8b8a4c7 | 2021-08-18 15:00:09 +0000 | [diff] [blame] | 59 | # NOTE(yoctozepto): The common consensus [1] is that libvirt-python should |
| 60 | # be installed from distro packages. However, various projects might be |
| 61 | # trying to ensure it is installed using pip AND use upper-constraints |
| 62 | # with that, causing pip to try to upgrade it and to fail. |
| 63 | # The following line removes libvirt-python from upper-constraints and |
| 64 | # avoids the situation described above. Now only if installed packages |
| 65 | # explicitly depend on a newer (or, in general, incompatible) libvirt-python |
| 66 | # version, will pip try to reinstall it. |
| 67 | # [1] https://review.opendev.org/c/openstack/devstack/+/798514 |
| 68 | $REQUIREMENTS_DIR/.venv/bin/edit-constraints \ |
| 69 | $REQUIREMENTS_DIR/upper-constraints.txt -- libvirt-python |
Ian Wienand | bfcc760 | 2017-03-29 11:52:06 +1100 | [diff] [blame] | 70 | |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 71 | if is_ubuntu; then |
Brian Haley | a132016 | 2023-05-30 13:31:05 -0400 | [diff] [blame] | 72 | install_package qemu-system libvirt-clients libvirt-daemon-system libvirt-dev python3-libvirt systemd-coredump |
Kevin Zhao | 551848d | 2020-04-27 08:39:37 +0800 | [diff] [blame] | 73 | if is_arch "aarch64"; then |
| 74 | install_package qemu-efi |
| 75 | fi |
Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 76 | #pip_install_gr <there-si-no-guestfs-in-pypi> |
Martin Kopec | ec07b34 | 2023-01-24 17:38:45 +0100 | [diff] [blame] | 77 | elif is_fedora; then |
Ian Wienand | 0d0b690 | 2017-11-17 10:33:11 +1100 | [diff] [blame] | 78 | |
Lee Yarwood | 169f5de | 2020-01-06 13:45:33 +0000 | [diff] [blame] | 79 | # Optionally enable the virt-preview repo when on Fedora |
| 80 | if [[ $DISTRO =~ f[0-9][0-9] ]] && [[ ${ENABLE_FEDORA_VIRT_PREVIEW_REPO} == "True" ]]; then |
| 81 | # https://copr.fedorainfracloud.org/coprs/g/virtmaint-sig/virt-preview/ |
| 82 | sudo dnf copr enable -y @virtmaint-sig/virt-preview |
| 83 | fi |
| 84 | |
wangxiyuan | 6440c6d | 2022-08-05 14:18:13 +0800 | [diff] [blame] | 85 | if is_openeuler; then |
| 86 | qemu_package=qemu |
| 87 | else |
| 88 | qemu_package=qemu-kvm |
| 89 | fi |
| 90 | |
Ian Wienand | 0d0b690 | 2017-11-17 10:33:11 +1100 | [diff] [blame] | 91 | # Note that in CentOS/RHEL this needs to come from the RDO |
| 92 | # repositories (qemu-kvm-ev ... which provides this package) |
| 93 | # as the base system version is too old. We should have |
| 94 | # pre-installed these |
wangxiyuan | 6440c6d | 2022-08-05 14:18:13 +0800 | [diff] [blame] | 95 | install_package $qemu_package |
yatinkarel | f5b9596 | 2025-05-02 15:51:01 +0530 | [diff] [blame] | 96 | install_package libvirt libvirt-devel |
| 97 | |
| 98 | if [[ $DISTRO =~ rhel9 ]]; then |
| 99 | pip_install_gr libvirt-python |
| 100 | else |
| 101 | install_package python3-libvirt |
| 102 | fi |
Ian Wienand | 0d0b690 | 2017-11-17 10:33:11 +1100 | [diff] [blame] | 103 | |
Radosław Piliszek | 8083314 | 2021-06-18 12:06:02 +0000 | [diff] [blame] | 104 | if is_arch "aarch64"; then |
Kevin Zhao | 7880ba6 | 2021-03-31 04:58:28 +0000 | [diff] [blame] | 105 | install_package edk2-aarch64 |
Kevin Zhao | 551848d | 2020-04-27 08:39:37 +0800 | [diff] [blame] | 106 | fi |
Ian Wienand | bfcc760 | 2017-03-29 11:52:06 +1100 | [diff] [blame] | 107 | fi |
Ian Wienand | 52bb641 | 2017-02-27 15:11:11 +1100 | [diff] [blame] | 108 | |
Ian Wienand | bfcc760 | 2017-03-29 11:52:06 +1100 | [diff] [blame] | 109 | if [[ $DEBUG_LIBVIRT_COREDUMPS == True ]]; then |
| 110 | _enable_coredump |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 111 | fi |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | # Configures the installed libvirt system so that is accessible by |
| 115 | # STACK_USER via qemu:///system with management capabilities. |
| 116 | function configure_libvirt { |
Kevin Benton | d1fe0e6 | 2017-05-16 22:27:58 -0700 | [diff] [blame] | 117 | if is_service_enabled neutron && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 118 | # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces |
| 119 | cat <<EOF | sudo tee -a $QEMU_CONF |
| 120 | cgroup_device_acl = [ |
| 121 | "/dev/null", "/dev/full", "/dev/zero", |
| 122 | "/dev/random", "/dev/urandom", |
| 123 | "/dev/ptmx", "/dev/kvm", "/dev/kqemu", |
| 124 | "/dev/rtc", "/dev/hpet","/dev/net/tun", |
Lenny Verkhovsky | 0a40710 | 2016-09-04 12:52:01 +0000 | [diff] [blame] | 125 | "/dev/vfio/vfio", |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 126 | ] |
| 127 | EOF |
| 128 | fi |
| 129 | |
Martin Kopec | ec07b34 | 2023-01-24 17:38:45 +0100 | [diff] [blame] | 130 | if is_fedora; then |
| 131 | # Starting with fedora 18 enable stack-user to |
Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame] | 132 | # virsh -c qemu:///system by creating a policy-kit rule for |
| 133 | # stack-user using the new Javascript syntax |
| 134 | rules_dir=/etc/polkit-1/rules.d |
| 135 | sudo mkdir -p $rules_dir |
| 136 | cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 137 | polkit.addRule(function(action, subject) { |
| 138 | if (action.id == 'org.libvirt.unix.manage' && |
| 139 | subject.user == '$STACK_USER') { |
| 140 | return polkit.Result.YES; |
| 141 | } |
| 142 | }); |
| 143 | EOF |
Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame] | 144 | unset rules_dir |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 145 | fi |
| 146 | |
| 147 | # The user that nova runs as needs to be member of **libvirtd** group otherwise |
| 148 | # nova-compute will be unable to use libvirt. |
| 149 | if ! getent group $LIBVIRT_GROUP >/dev/null; then |
| 150 | sudo groupadd $LIBVIRT_GROUP |
| 151 | fi |
| 152 | add_user_to_group $STACK_USER $LIBVIRT_GROUP |
| 153 | |
| 154 | # Enable server side traces for libvirtd |
| 155 | if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then |
Daniel P. Berrange | a12f996 | 2014-07-01 13:21:34 +0100 | [diff] [blame] | 156 | if is_ubuntu; then |
| 157 | # Unexpectedly binary package builds in ubuntu get fully qualified |
| 158 | # source file paths, not relative paths. This screws with the matching |
| 159 | # of '1:libvirt' making everything turn on. So use libvirt.c for now. |
| 160 | # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3 |
Kashyap Chamarthy | 07dc2bf | 2016-06-01 12:21:00 +0200 | [diff] [blame] | 161 | 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. Berrange | a12f996 | 2014-07-01 13:21:34 +0100 | [diff] [blame] | 162 | else |
Kashyap Chamarthy | 07dc2bf | 2016-06-01 12:21:00 +0200 | [diff] [blame] | 163 | 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. Berrange | a12f996 | 2014-07-01 13:21:34 +0100 | [diff] [blame] | 164 | fi |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 165 | local log_outputs="1:file:/var/log/libvirt/libvirtd.log" |
Mark McLoughlin | 76cbbe3 | 2015-12-07 05:05:04 -0500 | [diff] [blame] | 166 | if ! sudo grep -q "^log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 167 | echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf |
| 168 | fi |
Mark McLoughlin | 76cbbe3 | 2015-12-07 05:05:04 -0500 | [diff] [blame] | 169 | if ! sudo grep -q "^log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 170 | echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf |
| 171 | fi |
| 172 | fi |
| 173 | |
Daniel P. Berrange | e9870eb | 2016-11-10 13:03:32 +0000 | [diff] [blame] | 174 | if is_nova_console_proxy_compute_tls_enabled ; then |
melanie witt | 1d378dc | 2019-10-23 04:20:23 +0000 | [diff] [blame] | 175 | echo "vnc_tls = 1" | sudo tee -a $QEMU_CONF |
| 176 | echo "vnc_tls_x509_verify = 1" | sudo tee -a $QEMU_CONF |
Daniel P. Berrange | e9870eb | 2016-11-10 13:03:32 +0000 | [diff] [blame] | 177 | |
melanie witt | 1d378dc | 2019-10-23 04:20:23 +0000 | [diff] [blame] | 178 | sudo mkdir -p /etc/pki/libvirt-vnc |
| 179 | deploy_int_CA /etc/pki/libvirt-vnc/ca-cert.pem |
| 180 | deploy_int_cert /etc/pki/libvirt-vnc/server-cert.pem /etc/pki/libvirt-vnc/server-key.pem |
| 181 | # OpenSSL 1.1.0 generates the key file with permissions: 600, by |
| 182 | # default and the deploy_int* methods use 'sudo cp' to copy the |
| 183 | # files, making them owned by root:root. |
| 184 | # Change ownership of everything under /etc/pki/libvirt-vnc to |
| 185 | # libvirt-qemu:libvirt-qemu so that libvirt-qemu can read the key |
| 186 | # file. |
| 187 | sudo chown -R libvirt-qemu:libvirt-qemu /etc/pki/libvirt-vnc |
Daniel P. Berrange | e9870eb | 2016-11-10 13:03:32 +0000 | [diff] [blame] | 188 | fi |
| 189 | |
Sean Dague | 214459c | 2016-06-02 10:29:59 -0400 | [diff] [blame] | 190 | # Service needs to be started on redhat/fedora -- do a restart for |
| 191 | # sanity after fiddling the config. |
Ian Wienand | 2e66778 | 2019-11-20 10:41:34 +1100 | [diff] [blame] | 192 | restart_service libvirtd |
Janki Chhatbar | 8f586fb | 2016-07-18 08:19:25 +0530 | [diff] [blame] | 193 | |
| 194 | # Restart virtlogd companion service to ensure it is running properly |
| 195 | # https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/1577455 |
| 196 | # https://bugzilla.redhat.com/show_bug.cgi?id=1290357 |
| 197 | # (not all platforms have it; libvirt 1.3+ only, thus the ignore) |
| 198 | restart_service virtlogd || true |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
| 202 | # Restore xtrace |
Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 203 | $_XTRACE_NOVA_FN_LIBVIRT |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 204 | |
| 205 | # Local variables: |
| 206 | # mode: shell-script |
| 207 | # End: |