blob: d4a07688cd6ac99ece94fd9a2b576cef04e1c8df [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Adam Gandelman0f73ff22014-03-13 14:20:43 -07003# 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
11LV_XTRACE=$(set +o | grep xtrace)
12set +o xtrace
13
14# Defaults
Dean Troyer3324f192014-09-18 09:26:39 -050015# --------
Adam Gandelman0f73ff22014-03-13 14:20:43 -070016
Kashyap Chamarthyd3cfb822015-04-01 11:30:57 +020017# 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.)
21DEBUG_LIBVIRT=$(trueorfalse True DEBUG_LIBVIRT)
Adam Gandelman0f73ff22014-03-13 14:20:43 -070022
23# Installs required distro-specific libvirt packages.
24function install_libvirt {
25 if is_ubuntu; then
Andrew McDermottbcef63e2015-04-01 15:47:36 +000026 if is_arch "aarch64" && [[ ${DISTRO} =~ (trusty|utopic) ]]; then
27 install_package qemu-system
28 else
29 install_package qemu-kvm
30 install_package libguestfs0
31 install_package python-guestfs
32 fi
Dean Troyer4533eee2015-02-17 16:25:38 -060033 install_package libvirt-bin libvirt-dev
34 pip_install libvirt-python
Dean Troyer4533eee2015-02-17 16:25:38 -060035 #pip_install <there-si-no-guestfs-in-pypi>
Adam Gandelman0f73ff22014-03-13 14:20:43 -070036 elif is_fedora || is_suse; then
37 install_package kvm
Dean Troyer4533eee2015-02-17 16:25:38 -060038 install_package libvirt libvirt-devel
39 pip_install libvirt-python
Sean Dague206c5962015-03-30 13:56:11 -040040 install_package python-libguestfs
Adam Gandelman0f73ff22014-03-13 14:20:43 -070041 fi
Ian Wienand3ca91b22014-05-16 14:00:01 +100042
Ian Wienand83afcfe2014-06-12 08:47:50 +100043 # Restart firewalld after install of libvirt to avoid a problem
44 # with polkit, which libvirtd brings in. See
Ian Wienande6d99a92014-05-27 13:58:12 +100045 # https://bugzilla.redhat.com/show_bug.cgi?id=1099031
Ian Wienand83afcfe2014-06-12 08:47:50 +100046
47 # Note there is a difference between F20 rackspace cloud images
48 # and HP images used in the gate; rackspace has firewalld but hp
Attila Fazekas1f316be2015-01-26 16:39:57 +010049 # cloud doesn't.
Dean Troyer85ebb3a2014-08-19 10:54:59 -050050 if is_fedora && is_package_installed firewalld; then
Ian Wienand83afcfe2014-06-12 08:47:50 +100051 sudo service firewalld restart || true
Ian Wienand3ca91b22014-05-16 14:00:01 +100052 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -070053}
54
55# Configures the installed libvirt system so that is accessible by
56# STACK_USER via qemu:///system with management capabilities.
57function configure_libvirt {
58 if is_service_enabled neutron && is_neutron_ovs_base_plugin && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF; then
59 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
60 cat <<EOF | sudo tee -a $QEMU_CONF
61cgroup_device_acl = [
62 "/dev/null", "/dev/full", "/dev/zero",
63 "/dev/random", "/dev/urandom",
64 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
65 "/dev/rtc", "/dev/hpet","/dev/net/tun",
66]
67EOF
68 fi
69
Gonéri Le Boudera2143632014-09-17 11:55:31 +020070 # Since the release of Debian Wheezy the libvirt init script is libvirtd
71 # and not libvirtd-bin anymore.
72 if is_ubuntu && [ ! -f /etc/init.d/libvirtd ]; then
Adam Gandelman0f73ff22014-03-13 14:20:43 -070073 LIBVIRT_DAEMON=libvirt-bin
74 else
75 LIBVIRT_DAEMON=libvirtd
76 fi
77
78 if is_fedora || is_suse; then
Attila Fazekas1f316be2015-01-26 16:39:57 +010079 # Starting with fedora 18 and opensuse-12.3 enable stack-user to
80 # virsh -c qemu:///system by creating a policy-kit rule for
81 # stack-user using the new Javascript syntax
82 rules_dir=/etc/polkit-1/rules.d
83 sudo mkdir -p $rules_dir
84 cat <<EOF | sudo tee $rules_dir/50-libvirt-$STACK_USER.rules
Adam Gandelman0f73ff22014-03-13 14:20:43 -070085polkit.addRule(function(action, subject) {
86 if (action.id == 'org.libvirt.unix.manage' &&
87 subject.user == '$STACK_USER') {
88 return polkit.Result.YES;
89 }
90});
91EOF
Attila Fazekas1f316be2015-01-26 16:39:57 +010092 unset rules_dir
Adam Gandelman0f73ff22014-03-13 14:20:43 -070093 fi
94
95 # The user that nova runs as needs to be member of **libvirtd** group otherwise
96 # nova-compute will be unable to use libvirt.
97 if ! getent group $LIBVIRT_GROUP >/dev/null; then
98 sudo groupadd $LIBVIRT_GROUP
99 fi
100 add_user_to_group $STACK_USER $LIBVIRT_GROUP
101
102 # Enable server side traces for libvirtd
103 if [[ "$DEBUG_LIBVIRT" = "True" ]] ; then
Daniel P. Berrangea12f9962014-07-01 13:21:34 +0100104 if is_ubuntu; then
105 # Unexpectedly binary package builds in ubuntu get fully qualified
106 # source file paths, not relative paths. This screws with the matching
107 # of '1:libvirt' making everything turn on. So use libvirt.c for now.
108 # This will have to be re-visited when Ubuntu ships libvirt >= 1.2.3
Kashyap Chamarthyae7b4f92015-03-31 20:49:15 +0200109 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. Berrangea12f9962014-07-01 13:21:34 +0100110 else
Kashyap Chamarthyae7b4f92015-03-31 20:49:15 +0200111 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. Berrangea12f9962014-07-01 13:21:34 +0100112 fi
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700113 local log_outputs="1:file:/var/log/libvirt/libvirtd.log"
114 if ! grep -q "log_filters=\"$log_filters\"" /etc/libvirt/libvirtd.conf; then
115 echo "log_filters=\"$log_filters\"" | sudo tee -a /etc/libvirt/libvirtd.conf
116 fi
117 if ! grep -q "log_outputs=\"$log_outputs\"" /etc/libvirt/libvirtd.conf; then
118 echo "log_outputs=\"$log_outputs\"" | sudo tee -a /etc/libvirt/libvirtd.conf
119 fi
120 fi
121
Clark Boylanedd60482015-03-27 09:19:57 -0700122 # Update the libvirt cpu map with a gate64 cpu model. This enables nova
123 # live migration for 64bit guest OSes on heterogenous cloud "hardware".
124 if [[ -f /usr/share/libvirt/cpu_map.xml ]] ; then
125 sudo $TOP_DIR/tools/cpu_map_update.py /usr/share/libvirt/cpu_map.xml
126 fi
127
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700128 # libvirt detects various settings on startup, as we potentially changed
129 # the system configuration (modules, filesystems), we need to restart
Clark Boylanedd60482015-03-27 09:19:57 -0700130 # libvirt to detect those changes. Use a stop start as otherwise the new
131 # cpu_map is not loaded properly on some systems (Ubuntu).
132 stop_service $LIBVIRT_DAEMON
133 start_service $LIBVIRT_DAEMON
Adam Gandelman0f73ff22014-03-13 14:20:43 -0700134}
135
136
137# Restore xtrace
138$LV_XTRACE
139
140# Local variables:
141# mode: shell-script
142# End: