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