| 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 | 
 | 11 | LV_XTRACE=$(set +o | grep xtrace) | 
 | 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 | 
| Hirofumi Ichihara | 508931f | 2015-11-02 17:39:48 +0900 | [diff] [blame] | 26 |         if is_arch "aarch64" && [[ ${DISTRO} == "trusty" ]]; then | 
| Andrew McDermott | bcef63e | 2015-04-01 15:47:36 +0000 | [diff] [blame] | 27 |             install_package qemu-system | 
 | 28 |         else | 
 | 29 |             install_package qemu-kvm | 
 | 30 |             install_package libguestfs0 | 
| Andrew McDermott | bcef63e | 2015-04-01 15:47:36 +0000 | [diff] [blame] | 31 |         fi | 
| Dean Troyer | 4533eee | 2015-02-17 16:25:38 -0600 | [diff] [blame] | 32 |         install_package libvirt-bin libvirt-dev | 
| Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 33 |         pip_install_gr libvirt-python | 
 | 34 |         #pip_install_gr <there-si-no-guestfs-in-pypi> | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 35 |     elif is_fedora || is_suse; then | 
 | 36 |         install_package kvm | 
| Ian Wienand | 614ca26 | 2015-06-26 14:45:04 +1000 | [diff] [blame] | 37 |         # there is a dependency issue with kvm (which is really just a | 
 | 38 |         # wrapper to qemu-system-x86) that leaves some bios files out, | 
 | 39 |         # so install qemu-kvm (which shouldn't strictly be needed, as | 
 | 40 |         # everything has been merged into qemu-system-x86) to bring in | 
 | 41 |         # the right packages. see | 
 | 42 |         # https://bugzilla.redhat.com/show_bug.cgi?id=1235890 | 
 | 43 |         install_package qemu-kvm | 
| Dean Troyer | 4533eee | 2015-02-17 16:25:38 -0600 | [diff] [blame] | 44 |         install_package libvirt libvirt-devel | 
| Sean Dague | 60996b1 | 2015-04-08 09:06:49 -0400 | [diff] [blame] | 45 |         pip_install_gr libvirt-python | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 46 |     fi | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 47 | } | 
 | 48 |  | 
 | 49 | # Configures the installed libvirt system so that is accessible by | 
 | 50 | # STACK_USER via qemu:///system with management capabilities. | 
 | 51 | function configure_libvirt { | 
 | 52 |     if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then | 
 | 53 |         # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces | 
 | 54 |         cat <<EOF | sudo tee -a $QEMU_CONF | 
 | 55 | cgroup_device_acl = [ | 
 | 56 |     "/dev/null", "/dev/full", "/dev/zero", | 
 | 57 |     "/dev/random", "/dev/urandom", | 
 | 58 |     "/dev/ptmx", "/dev/kvm", "/dev/kqemu", | 
 | 59 |     "/dev/rtc", "/dev/hpet","/dev/net/tun", | 
 | 60 | ] | 
 | 61 | EOF | 
 | 62 |     fi | 
 | 63 |  | 
| Gonéri Le Bouder | a214363 | 2014-09-17 11:55:31 +0200 | [diff] [blame] | 64 |     # Since the release of Debian Wheezy the libvirt init script is libvirtd | 
 | 65 |     # and not libvirtd-bin anymore. | 
 | 66 |     if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 67 |         LIBVIRT_DAEMON=libvirt-bin | 
 | 68 |     else | 
 | 69 |         LIBVIRT_DAEMON=libvirtd | 
 | 70 |     fi | 
 | 71 |  | 
 | 72 |     if is_fedora || is_suse; then | 
| Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame] | 73 |         # Starting with fedora 18 and opensuse-12.3 enable stack-user to | 
 | 74 |         # virsh -c qemu:///system by creating a policy-kit rule for | 
 | 75 |         # stack-user using the new Javascript syntax | 
 | 76 |         rules_dir=/etc/polkit-1/rules.d | 
 | 77 |         sudo mkdir -p $rules_dir | 
 | 78 |         cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 79 | polkit.addRule(function(action, subject) { | 
 | 80 |     if (action.id == 'org.libvirt.unix.manage' && | 
 | 81 |         subject.user == '$STACK_USER') { | 
 | 82 |         return polkit.Result.YES; | 
 | 83 |     } | 
 | 84 | }); | 
 | 85 | EOF | 
| Attila Fazekas | 1f316be | 2015-01-26 16:39:57 +0100 | [diff] [blame] | 86 |         unset rules_dir | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 87 |     fi | 
 | 88 |  | 
 | 89 |     # The user that nova runs as needs to be member of **libvirtd** group otherwise | 
 | 90 |     # nova-compute will be unable to use libvirt. | 
 | 91 |     if ! getent group $LIBVIRT_GROUP >/dev/null; then | 
 | 92 |         sudo groupadd $LIBVIRT_GROUP | 
 | 93 |     fi | 
 | 94 |     add_user_to_group $STACK_USER $LIBVIRT_GROUP | 
 | 95 |  | 
 | 96 |     # Enable server side traces for libvirtd | 
 | 97 |     if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then | 
| Daniel P. Berrange | a12f996 | 2014-07-01 13:21:34 +0100 | [diff] [blame] | 98 |         if is_ubuntu; then | 
 | 99 |             # Unexpectedly binary package builds in ubuntu get fully qualified | 
 | 100 |             # source file paths, not relative paths. This screws with the matching | 
 | 101 |             # of '1:libvirt' making everything turn on. So use libvirt.c for now. | 
 | 102 |             # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3 | 
| Kashyap Chamarthy | ae7b4f9 | 2015-03-31 20:49:15 +0200 | [diff] [blame] | 103 |             local log_filters="1:libvirt.c 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util 1:qemu_monitor" | 
| Daniel P. Berrange | a12f996 | 2014-07-01 13:21:34 +0100 | [diff] [blame] | 104 |         else | 
| Kashyap Chamarthy | ae7b4f9 | 2015-03-31 20:49:15 +0200 | [diff] [blame] | 105 |             local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util 1:qemu_monitor" | 
| Daniel P. Berrange | a12f996 | 2014-07-01 13:21:34 +0100 | [diff] [blame] | 106 |         fi | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 107 |         local log_outputs="1:file:/var/log/libvirt/libvirtd.log" | 
 | 108 |         if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then | 
 | 109 |             echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf | 
 | 110 |         fi | 
 | 111 |         if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then | 
 | 112 |             echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf | 
 | 113 |         fi | 
 | 114 |     fi | 
 | 115 |  | 
| Clark Boylan | edd6048 | 2015-03-27 09:19:57 -0700 | [diff] [blame] | 116 |     # Update the libvirt cpu map with a gate64 cpu model. This enables nova | 
 | 117 |     # live migration for 64bit guest OSes on heterogenous cloud "hardware". | 
 | 118 |     if [[ -f /usr/share/libvirt/cpu_map.xml ]] ; then | 
 | 119 |         sudo $TOP_DIR/tools/cpu_map_update.py /usr/share/libvirt/cpu_map.xml | 
 | 120 |     fi | 
 | 121 |  | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 122 |     # libvirt detects various settings on startup, as we potentially changed | 
 | 123 |     # the system configuration (modules, filesystems), we need to restart | 
| Clark Boylan | edd6048 | 2015-03-27 09:19:57 -0700 | [diff] [blame] | 124 |     # libvirt to detect those changes. Use a stop start as otherwise the new | 
 | 125 |     # cpu_map is not loaded properly on some systems (Ubuntu). | 
 | 126 |     stop_service $LIBVIRT_DAEMON | 
 | 127 |     start_service $LIBVIRT_DAEMON | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 128 | } | 
 | 129 |  | 
 | 130 |  | 
 | 131 | # Restore xtrace | 
 | 132 | $LV_XTRACE | 
 | 133 |  | 
 | 134 | # Local variables: | 
 | 135 | # mode: shell-script | 
 | 136 | # End: |