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 | |
| 23 | # Installs required distro-specific libvirt packages. |
| 24 | function install_libvirt { |
| 25 | if is_ubuntu; then |
Kevin Zhao | a80d409 | 2016-08-11 10:41:34 +0000 | [diff] [blame] | 26 | install_package qemu-system |
David Rabel | 682e0ab | 2017-03-17 19:19:00 +0100 | [diff] [blame^] | 27 | if [[ ${DISTRO} == "xenial" ]]; then |
| 28 | install_package libvirt-bin libvirt-dev |
| 29 | else |
| 30 | install_package libvirt-clients libvirt-daemon-system libvirt-dev |
| 31 | fi |
Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 32 | pip_install_gr libvirt-python |
Sean Dague | 999dd7e | 2017-02-09 17:56:40 -0500 | [diff] [blame] | 33 | if [[ ${DISTRO} == "trusty" && ${EBTABLES_RACE_FIX} == "True" ]]; then |
Sean Dague | 7860f2b | 2015-11-17 11:59:07 -0500 | [diff] [blame] | 34 | # Work around for bug #1501558. We can remove this once we |
| 35 | # get to a version of Ubuntu that has new enough libvirt. |
| 36 | TOP_DIR=$TOP_DIR $TOP_DIR/tools/install_ebtables_workaround.sh |
| 37 | fi |
Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 38 | #pip_install_gr <there-si-no-guestfs-in-pypi> |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 39 | elif is_fedora || is_suse; then |
Alexander Schmidt | a5ea08b | 2015-12-02 15:30:44 +0000 | [diff] [blame] | 40 | # On "KVM for IBM z Systems", kvm does not have its own package |
Ian Wienand | 52bb641 | 2017-02-27 15:11:11 +1100 | [diff] [blame] | 41 | if [[ ! ${DISTRO} =~ "kvmibm1" && ! ${DISTRO} =~ "rhel7" ]]; then |
Alexander Schmidt | a5ea08b | 2015-12-02 15:30:44 +0000 | [diff] [blame] | 42 | install_package kvm |
| 43 | fi |
Ian Wienand | 52bb641 | 2017-02-27 15:11:11 +1100 | [diff] [blame] | 44 | |
| 45 | if [[ ${DISTRO} =~ "rhel7" ]]; then |
Evgeny Antyshev | 008aa3e | 2017-03-02 11:14:25 +0000 | [diff] [blame] | 46 | # This should install the latest qemu-kvm build, |
| 47 | # which is called qemu-kvm-ev in centos7 |
| 48 | # (as the default OS qemu-kvm package is usually rather old, |
| 49 | # and should be updated by above) |
| 50 | install_package qemu-kvm |
Ian Wienand | 52bb641 | 2017-02-27 15:11:11 +1100 | [diff] [blame] | 51 | fi |
| 52 | |
Dean Troyer | 4533eee | 2015-02-17 16:25:38 -0600 | [diff] [blame] | 53 | install_package libvirt libvirt-devel |
Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 54 | pip_install_gr libvirt-python |
Ian Wienand | 52bb641 | 2017-02-27 15:11:11 +1100 | [diff] [blame] | 55 | |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 56 | fi |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | # Configures the installed libvirt system so that is accessible by |
| 60 | # STACK_USER via qemu:///system with management capabilities. |
| 61 | function configure_libvirt { |
| 62 | if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then |
| 63 | # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces |
| 64 | cat <<EOF | sudo tee -a $QEMU_CONF |
| 65 | cgroup_device_acl = [ |
| 66 | "/dev/null", "/dev/full", "/dev/zero", |
| 67 | "/dev/random", "/dev/urandom", |
| 68 | "/dev/ptmx", "/dev/kvm", "/dev/kqemu", |
| 69 | "/dev/rtc", "/dev/hpet","/dev/net/tun", |
Lenny Verkhovsky | 0a40710 | 2016-09-04 12:52:01 +0000 | [diff] [blame] | 70 | "/dev/vfio/vfio", |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 71 | ] |
| 72 | EOF |
| 73 | fi |
| 74 | |
Gonéri Le Bouder | a214363 | 2014-09-17 11:55:31 +0200 | [diff] [blame] | 75 | # Since the release of Debian Wheezy the libvirt init script is libvirtd |
| 76 | # and not libvirtd-bin anymore. |
| 77 | if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 78 | LIBVIRT_DAEMON=libvirt-bin |
| 79 | else |
| 80 | LIBVIRT_DAEMON=libvirtd |
| 81 | fi |
| 82 | |
| 83 | if is_fedora || is_suse; then |
Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame] | 84 | # Starting with fedora 18 and opensuse-12.3 enable stack-user to |
| 85 | # virsh -c qemu:///system by creating a policy-kit rule for |
| 86 | # stack-user using the new Javascript syntax |
| 87 | rules_dir=/etc/polkit-1/rules.d |
| 88 | sudo mkdir -p $rules_dir |
| 89 | cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 90 | polkit.addRule(function(action, subject) { |
| 91 | if (action.id == 'org.libvirt.unix.manage' && |
| 92 | subject.user == '$STACK_USER') { |
| 93 | return polkit.Result.YES; |
| 94 | } |
| 95 | }); |
| 96 | EOF |
Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame] | 97 | unset rules_dir |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 98 | fi |
| 99 | |
| 100 | # The user that nova runs as needs to be member of **libvirtd** group otherwise |
| 101 | # nova-compute will be unable to use libvirt. |
| 102 | if ! getent group $LIBVIRT_GROUP >/dev/null; then |
| 103 | sudo groupadd $LIBVIRT_GROUP |
| 104 | fi |
| 105 | add_user_to_group $STACK_USER $LIBVIRT_GROUP |
| 106 | |
| 107 | # Enable server side traces for libvirtd |
| 108 | if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then |
Daniel P. Berrange | a12f996 | 2014-07-01 13:21:34 +0100 | [diff] [blame] | 109 | if is_ubuntu; then |
| 110 | # Unexpectedly binary package builds in ubuntu get fully qualified |
| 111 | # source file paths, not relative paths. This screws with the matching |
| 112 | # of '1:libvirt' making everything turn on. So use libvirt.c for now. |
| 113 | # 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] | 114 | 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] | 115 | else |
Kashyap Chamarthy | 07dc2bf | 2016-06-01 12:21:00 +0200 | [diff] [blame] | 116 | 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] | 117 | fi |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 118 | local log_outputs="1:file:/var/log/libvirt/libvirtd.log" |
Mark McLoughlin | 76cbbe3 | 2015-12-07 05:05:04 -0500 | [diff] [blame] | 119 | 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] | 120 | echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf |
| 121 | fi |
Mark McLoughlin | 76cbbe3 | 2015-12-07 05:05:04 -0500 | [diff] [blame] | 122 | 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] | 123 | echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf |
| 124 | fi |
| 125 | fi |
| 126 | |
Sean Dague | 214459c | 2016-06-02 10:29:59 -0400 | [diff] [blame] | 127 | # Service needs to be started on redhat/fedora -- do a restart for |
| 128 | # sanity after fiddling the config. |
| 129 | restart_service $LIBVIRT_DAEMON |
Janki Chhatbar | 8f586fb | 2016-07-18 08:19:25 +0530 | [diff] [blame] | 130 | |
| 131 | # Restart virtlogd companion service to ensure it is running properly |
| 132 | # https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/1577455 |
| 133 | # https://bugzilla.redhat.com/show_bug.cgi?id=1290357 |
| 134 | # (not all platforms have it; libvirt 1.3+ only, thus the ignore) |
| 135 | restart_service virtlogd || true |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | # Restore xtrace |
Ian Wienand | 523f488 | 2015-10-13 11:03:03 +1100 | [diff] [blame] | 140 | $_XTRACE_NOVA_FN_LIBVIRT |
Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 141 | |
| 142 | # Local variables: |
| 143 | # mode: shell-script |
| 144 | # End: |