| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 1 | # lib/nova_plugins/functions-libvirt | 
 | 2 | # Common libvirt configuration functions | 
 | 3 |  | 
 | 4 | # Dependencies: | 
 | 5 | # ``functions`` file | 
 | 6 | # ``STACK_USER`` has to be defined | 
 | 7 |  | 
 | 8 | # Save trace setting | 
 | 9 | LV_XTRACE=$(set +o | grep xtrace) | 
 | 10 | set +o xtrace | 
 | 11 |  | 
 | 12 | # Defaults | 
 | 13 | # ------- | 
 | 14 |  | 
 | 15 | # if we should turn on massive libvirt debugging | 
 | 16 | DEBUG_LIBVIRT=$(trueorfalse False $DEBUG_LIBVIRT) | 
 | 17 |  | 
 | 18 | # Installs required distro-specific libvirt packages. | 
 | 19 | function install_libvirt { | 
 | 20 |     if is_ubuntu; then | 
| Adam Gandelman | b0f8beb | 2014-03-27 00:14:24 -0700 | [diff] [blame] | 21 |         install_package qemu-kvm | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 22 |         install_package libvirt-bin | 
 | 23 |         install_package python-libvirt | 
 | 24 |         install_package python-guestfs | 
 | 25 |     elif is_fedora || is_suse; then | 
 | 26 |         install_package kvm | 
 | 27 |         install_package libvirt | 
 | 28 |         install_package libvirt-python | 
 | 29 |         install_package python-libguestfs | 
 | 30 |     fi | 
| Ian Wienand | 3ca91b2 | 2014-05-16 14:00:01 +1000 | [diff] [blame] | 31 |  | 
| Ian Wienand | 83afcfe | 2014-06-12 08:47:50 +1000 | [diff] [blame] | 32 |     # Restart firewalld after install of libvirt to avoid a problem | 
 | 33 |     # with polkit, which libvirtd brings in.  See | 
| Ian Wienand | e6d99a9 | 2014-05-27 13:58:12 +1000 | [diff] [blame] | 34 |     # https://bugzilla.redhat.com/show_bug.cgi?id=1099031 | 
| Ian Wienand | 83afcfe | 2014-06-12 08:47:50 +1000 | [diff] [blame] | 35 |  | 
 | 36 |     # Note there is a difference between F20 rackspace cloud images | 
 | 37 |     # and HP images used in the gate; rackspace has firewalld but hp | 
 | 38 |     # cloud doesn't.  RHEL6 doesn't have firewalld either.  So we | 
 | 39 |     # don't care if it fails. | 
| Dean Troyer | 85ebb3a | 2014-08-19 10:54:59 -0500 | [diff] [blame] | 40 |     if is_fedora && is_package_installed firewalld; then | 
| Ian Wienand | 83afcfe | 2014-06-12 08:47:50 +1000 | [diff] [blame] | 41 |         sudo service firewalld restart || true | 
| Ian Wienand | 3ca91b2 | 2014-05-16 14:00:01 +1000 | [diff] [blame] | 42 |     fi | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 43 | } | 
 | 44 |  | 
 | 45 | # Configures the installed libvirt system so that is accessible by | 
 | 46 | # STACK_USER via qemu:///system with management capabilities. | 
 | 47 | function configure_libvirt { | 
 | 48 |     if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then | 
 | 49 |         # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces | 
 | 50 |         cat <<EOF | sudo tee -a $QEMU_CONF | 
 | 51 | cgroup_device_acl = [ | 
 | 52 |     "/dev/null", "/dev/full", "/dev/zero", | 
 | 53 |     "/dev/random", "/dev/urandom", | 
 | 54 |     "/dev/ptmx", "/dev/kvm", "/dev/kqemu", | 
 | 55 |     "/dev/rtc", "/dev/hpet","/dev/net/tun", | 
 | 56 | ] | 
 | 57 | EOF | 
 | 58 |     fi | 
 | 59 |  | 
| mathieu-rohon | 62ec61c | 2014-08-28 16:40:14 +0200 | [diff] [blame] | 60 |     if [ "$os_VENDOR" = "Ubuntu" ]; then | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 61 |         LIBVIRT_DAEMON=libvirt-bin | 
 | 62 |     else | 
 | 63 |         LIBVIRT_DAEMON=libvirtd | 
 | 64 |     fi | 
 | 65 |  | 
 | 66 |     if is_fedora || is_suse; then | 
 | 67 |         if is_fedora && [[ $DISTRO =~ (rhel6) || "$os_RELEASE" -le "17" ]]; then | 
 | 68 |             cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla | 
 | 69 | [libvirt Management Access] | 
 | 70 | Identity=unix-group:$LIBVIRT_GROUP | 
 | 71 | Action=org.libvirt.unix.manage | 
 | 72 | ResultAny=yes | 
 | 73 | ResultInactive=yes | 
 | 74 | ResultActive=yes | 
 | 75 | EOF | 
 | 76 |         elif is_suse && [[ $os_RELEASE = 12.2 || "$os_VENDOR" = "SUSE LINUX" ]]; then | 
 | 77 |             # openSUSE < 12.3 or SLE | 
 | 78 |             # Work around the fact that polkit-default-privs overrules pklas | 
 | 79 |             # with 'unix-group:$group'. | 
 | 80 |             cat <<EOF | sudo tee /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla | 
 | 81 | [libvirt Management Access] | 
 | 82 | Identity=unix-user:$STACK_USER | 
 | 83 | Action=org.libvirt.unix.manage | 
 | 84 | ResultAny=yes | 
 | 85 | ResultInactive=yes | 
 | 86 | ResultActive=yes | 
 | 87 | EOF | 
 | 88 |         else | 
 | 89 |             # Starting with fedora 18 and opensuse-12.3 enable stack-user to | 
 | 90 |             # virsh -c qemu:///system by creating a policy-kit rule for | 
 | 91 |             # stack-user using the new Javascript syntax | 
 | 92 |             rules_dir=/etc/polkit-1/rules.d | 
 | 93 |             sudo mkdir -p $rules_dir | 
 | 94 |             cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules | 
 | 95 | polkit.addRule(function(action, subject) { | 
 | 96 |     if (action.id == 'org.libvirt.unix.manage' && | 
 | 97 |         subject.user == '$STACK_USER') { | 
 | 98 |         return polkit.Result.YES; | 
 | 99 |     } | 
 | 100 | }); | 
 | 101 | EOF | 
 | 102 |             unset rules_dir | 
 | 103 |         fi | 
 | 104 |     fi | 
 | 105 |  | 
 | 106 |     # The user that nova runs as needs to be member of **libvirtd** group otherwise | 
 | 107 |     # nova-compute will be unable to use libvirt. | 
 | 108 |     if ! getent group $LIBVIRT_GROUP >/dev/null; then | 
 | 109 |         sudo groupadd $LIBVIRT_GROUP | 
 | 110 |     fi | 
 | 111 |     add_user_to_group $STACK_USER $LIBVIRT_GROUP | 
 | 112 |  | 
 | 113 |     # Enable server side traces for libvirtd | 
 | 114 |     if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then | 
| Daniel P. Berrange | a12f996 | 2014-07-01 13:21:34 +0100 | [diff] [blame] | 115 |         if is_ubuntu; then | 
 | 116 |             # Unexpectedly binary package builds in ubuntu get fully qualified | 
 | 117 |             # source file paths, not relative paths. This screws with the matching | 
 | 118 |             # of '1:libvirt' making everything turn on. So use libvirt.c for now. | 
 | 119 |             # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3 | 
 | 120 |             local log_filters="1:libvirt.c 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util" | 
 | 121 |         else | 
 | 122 |             local log_filters="1:libvirt 1:qemu 1:conf 1:security 3:object 3:event 3:json 3:file 1:util" | 
 | 123 |         fi | 
| Adam Gandelman | 0f73ff2 | 2014-03-13 14:20:43 -0700 | [diff] [blame] | 124 |         local log_outputs="1:file:/var/log/libvirt/libvirtd.log" | 
 | 125 |         if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then | 
 | 126 |             echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf | 
 | 127 |         fi | 
 | 128 |         if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then | 
 | 129 |             echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf | 
 | 130 |         fi | 
 | 131 |     fi | 
 | 132 |  | 
 | 133 |     # libvirt detects various settings on startup, as we potentially changed | 
 | 134 |     # the system configuration (modules, filesystems), we need to restart | 
 | 135 |     # libvirt to detect those changes. | 
 | 136 |     restart_service $LIBVIRT_DAEMON | 
 | 137 | } | 
 | 138 |  | 
 | 139 |  | 
 | 140 | # Restore xtrace | 
 | 141 | $LV_XTRACE | 
 | 142 |  | 
 | 143 | # Local variables: | 
 | 144 | # mode: shell-script | 
 | 145 | # End: |