blob: f4288955815b0fe453afc84770d09c6a4aca24c7 [file] [log] [blame]
Dean Troyerbf67c192012-09-21 15:09:37 -05001# lib/nova
2# Functions to control the configuration and operation of the XXXX service
3
4# Dependencies:
5# ``functions`` file
6# ``DEST``, ``DATA_DIR`` must be defined
7# ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
8# ``LIBVIRT_TYPE`` must be defined
9# ``INSTANCE_NAME_PREFIX``, ``VOLUME_NAME_PREFIX`` must be defined
10
11# ``stack.sh`` calls the entry points in this order:
12#
13# install_nova
14# configure_nova
15# init_nova
16# start_nova
17# stop_nova
18# cleanup_nova
19
20# Save trace setting
21XTRACE=$(set +o | grep xtrace)
22set +o xtrace
23
24
25# Defaults
26# --------
27
28# Set up default directories
29NOVA_DIR=$DEST/nova
30NOVACLIENT_DIR=$DEST/python-novaclient
31NOVA_STATE_PATH=${NOVA_STATE_PATH:=$DATA_DIR/nova}
32# INSTANCES_PATH is the previous name for this
33NOVA_INSTANCES_PATH=${NOVA_INSTANCES_PATH:=${INSTANCES_PATH:=$NOVA_STATE_PATH/instances}}
34
35NOVA_CONF_DIR=/etc/nova
36NOVA_CONF=$NOVA_CONF_DIR/nova.conf
37NOVA_API_PASTE_INI=${NOVA_API_PASTE_INI:-$NOVA_CONF_DIR/api-paste.ini}
38
39# Support entry points installation of console scripts
40if [[ -d $NOVA_DIR/bin ]]; then
41 NOVA_BIN_DIR=$NOVA_DIR/bin
42else
43 NOVA_BIN_DIR=/usr/local/bin
44fi
45
46# Set the paths of certain binaries
47if [[ "$os_PACKAGE" = "deb" ]]; then
48 NOVA_ROOTWRAP=/usr/local/bin/nova-rootwrap
49else
50 NOVA_ROOTWRAP=/usr/bin/nova-rootwrap
51fi
52
53# Allow rate limiting to be turned off for testing, like for Tempest
54# NOTE: Set API_RATE_LIMIT="False" to turn OFF rate limiting
55API_RATE_LIMIT=${API_RATE_LIMIT:-"True"}
56
57# Nova supports pluggable schedulers. The default ``FilterScheduler``
58# should work in most cases.
59SCHEDULER=${SCHEDULER:-nova.scheduler.filter_scheduler.FilterScheduler}
60
61QEMU_CONF=/etc/libvirt/qemu.conf
62
63
64# Entry Points
65# ------------
66
67function add_nova_opt {
68 echo "$1" >>$NOVA_CONF
69}
70
71# Helper to clean iptables rules
72function clean_iptables() {
73 # Delete rules
74 sudo iptables -S -v | sed "s/-c [0-9]* [0-9]* //g" | grep "nova" | grep "\-A" | sed "s/-A/-D/g" | awk '{print "sudo iptables",$0}' | bash
75 # Delete nat rules
76 sudo iptables -S -v -t nat | sed "s/-c [0-9]* [0-9]* //g" | grep "nova" | grep "\-A" | sed "s/-A/-D/g" | awk '{print "sudo iptables -t nat",$0}' | bash
77 # Delete chains
78 sudo iptables -S -v | sed "s/-c [0-9]* [0-9]* //g" | grep "nova" | grep "\-N" | sed "s/-N/-X/g" | awk '{print "sudo iptables",$0}' | bash
79 # Delete nat chains
80 sudo iptables -S -v -t nat | sed "s/-c [0-9]* [0-9]* //g" | grep "nova" | grep "\-N" | sed "s/-N/-X/g" | awk '{print "sudo iptables -t nat",$0}' | bash
81}
82
83# cleanup_nova() - Remove residual data files, anything left over from previous
84# runs that a clean run would need to clean up
85function cleanup_nova() {
86 if is_service_enabled n-cpu; then
87 # Clean iptables from previous runs
88 clean_iptables
89
90 # Destroy old instances
91 instances=`sudo virsh list --all | grep $INSTANCE_NAME_PREFIX | sed "s/.*\($INSTANCE_NAME_PREFIX[0-9a-fA-F]*\).*/\1/g"`
92 if [ ! "$instances" = "" ]; then
93 echo $instances | xargs -n1 sudo virsh destroy || true
94 echo $instances | xargs -n1 sudo virsh undefine || true
95 fi
96
97 # Logout and delete iscsi sessions
98 sudo iscsiadm --mode node | grep $VOLUME_NAME_PREFIX | cut -d " " -f2 | xargs sudo iscsiadm --mode node --logout || true
99 sudo iscsiadm --mode node | grep $VOLUME_NAME_PREFIX | cut -d " " -f2 | sudo iscsiadm --mode node --op delete || true
100
101 # Clean out the instances directory.
102 sudo rm -rf $NOVA_INSTANCES_PATH/*
103 fi
104}
105
106# configure_novaclient() - Set config files, create data dirs, etc
107function configure_novaclient() {
108 setup_develop $NOVACLIENT_DIR
109}
110
111# configure_nova_rootwrap() - configure Nova's rootwrap
112function configure_nova_rootwrap() {
113 # Deploy new rootwrap filters files (owned by root).
114 # Wipe any existing rootwrap.d files first
115 if [[ -d $NOVA_CONF_DIR/rootwrap.d ]]; then
116 sudo rm -rf $NOVA_CONF_DIR/rootwrap.d
117 fi
118 # Deploy filters to /etc/nova/rootwrap.d
119 sudo mkdir -m 755 $NOVA_CONF_DIR/rootwrap.d
120 sudo cp $NOVA_DIR/etc/nova/rootwrap.d/*.filters $NOVA_CONF_DIR/rootwrap.d
121 sudo chown -R root:root $NOVA_CONF_DIR/rootwrap.d
122 sudo chmod 644 $NOVA_CONF_DIR/rootwrap.d/*
123 # Set up rootwrap.conf, pointing to /etc/nova/rootwrap.d
124 sudo cp $NOVA_DIR/etc/nova/rootwrap.conf $NOVA_CONF_DIR/
125 sudo sed -e "s:^filters_path=.*$:filters_path=$NOVA_CONF_DIR/rootwrap.d:" -i $NOVA_CONF_DIR/rootwrap.conf
126 sudo chown root:root $NOVA_CONF_DIR/rootwrap.conf
127 sudo chmod 0644 $NOVA_CONF_DIR/rootwrap.conf
128 # Specify rootwrap.conf as first parameter to nova-rootwrap
129 ROOTWRAP_SUDOER_CMD="$NOVA_ROOTWRAP $NOVA_CONF_DIR/rootwrap.conf *"
130
131 # Set up the rootwrap sudoers for nova
132 TEMPFILE=`mktemp`
133 echo "$USER ALL=(root) NOPASSWD: $ROOTWRAP_SUDOER_CMD" >$TEMPFILE
134 chmod 0440 $TEMPFILE
135 sudo chown root:root $TEMPFILE
136 sudo mv $TEMPFILE /etc/sudoers.d/nova-rootwrap
137}
138
139# configure_nova() - Set config files, create data dirs, etc
140function configure_nova() {
141 setup_develop $NOVA_DIR
142
143 # Put config files in ``/etc/nova`` for everyone to find
144 if [[ ! -d $NOVA_CONF_DIR ]]; then
145 sudo mkdir -p $NOVA_CONF_DIR
146 fi
147 sudo chown `whoami` $NOVA_CONF_DIR
148
149 cp -p $NOVA_DIR/etc/nova/policy.json $NOVA_CONF_DIR
150
151 configure_nova_rootwrap
152
153 if is_service_enabled n-api; then
154 # Use the sample http middleware configuration supplied in the
155 # Nova sources. This paste config adds the configuration required
156 # for Nova to validate Keystone tokens.
157
158 # Remove legacy paste config if present
159 rm -f $NOVA_DIR/bin/nova-api-paste.ini
160
161 # Get the sample configuration file in place
162 cp $NOVA_DIR/etc/nova/api-paste.ini $NOVA_CONF_DIR
163
164 # Rewrite the authtoken configuration for our Keystone service.
165 # This is a bit defensive to allow the sample file some variance.
166 sed -e "
167 /^admin_token/i admin_tenant_name = $SERVICE_TENANT_NAME
168 /admin_tenant_name/s/^.*$/admin_tenant_name = $SERVICE_TENANT_NAME/;
169 /admin_user/s/^.*$/admin_user = nova/;
170 /admin_password/s/^.*$/admin_password = $SERVICE_PASSWORD/;
171 s,%SERVICE_TENANT_NAME%,$SERVICE_TENANT_NAME,g;
172 s,%SERVICE_TOKEN%,$SERVICE_TOKEN,g;
173 " -i $NOVA_API_PASTE_INI
174 fi
175
176 if is_service_enabled n-cpu; then
177 # Force IP forwarding on, just on case
178 sudo sysctl -w net.ipv4.ip_forward=1
179
180 # Attempt to load modules: network block device - used to manage qcow images
181 sudo modprobe nbd || true
182
183 # Check for kvm (hardware based virtualization). If unable to initialize
184 # kvm, we drop back to the slower emulation mode (qemu). Note: many systems
185 # come with hardware virtualization disabled in BIOS.
186 if [[ "$LIBVIRT_TYPE" == "kvm" ]]; then
187 sudo modprobe kvm || true
188 if [ ! -e /dev/kvm ]; then
189 echo "WARNING: Switching to QEMU"
190 LIBVIRT_TYPE=qemu
191 if which selinuxenabled 2>&1 > /dev/null && selinuxenabled; then
192 # https://bugzilla.redhat.com/show_bug.cgi?id=753589
193 sudo setsebool virt_use_execmem on
194 fi
195 fi
196 fi
197
198 # Install and configure **LXC** if specified. LXC is another approach to
199 # splitting a system into many smaller parts. LXC uses cgroups and chroot
200 # to simulate multiple systems.
201 if [[ "$LIBVIRT_TYPE" == "lxc" ]]; then
202 if [[ "$os_PACKAGE" = "deb" ]]; then
203 if [[ ! "$DISTRO" > natty ]]; then
204 cgline="none /cgroup cgroup cpuacct,memory,devices,cpu,freezer,blkio 0 0"
205 sudo mkdir -p /cgroup
206 if ! grep -q cgroup /etc/fstab; then
207 echo "$cgline" | sudo tee -a /etc/fstab
208 fi
209 if ! mount -n | grep -q cgroup; then
210 sudo mount /cgroup
211 fi
212 fi
213 fi
214 fi
215
Yoshihiro Kaneko602cf9b2012-07-23 06:27:36 +0000216 if is_service_enabled quantum && is_quantum_ovs_base_plugin "$Q_PLUGIN" && ! sudo grep -q '^cgroup_device_acl' $QEMU_CONF ; then
Dean Troyerbf67c192012-09-21 15:09:37 -0500217 # Add /dev/net/tun to cgroup_device_acls, needed for type=ethernet interfaces
218 cat <<EOF | sudo tee -a $QEMU_CONF
219cgroup_device_acl = [
220 "/dev/null", "/dev/full", "/dev/zero",
221 "/dev/random", "/dev/urandom",
222 "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
223 "/dev/rtc", "/dev/hpet","/dev/net/tun",
224]
225EOF
226 fi
227
228 if [[ "$os_PACKAGE" = "deb" ]]; then
229 LIBVIRT_DAEMON=libvirt-bin
230 else
231 # http://wiki.libvirt.org/page/SSHPolicyKitSetup
232 if ! grep ^libvirtd: /etc/group >/dev/null; then
233 sudo groupadd libvirtd
234 fi
235 sudo bash -c 'cat <<EOF >/etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
236[libvirt Management Access]
237Identity=unix-group:libvirtd
238Action=org.libvirt.unix.manage
239ResultAny=yes
240ResultInactive=yes
241ResultActive=yes
242EOF'
243 LIBVIRT_DAEMON=libvirtd
244 fi
245
246 # The user that nova runs as needs to be member of **libvirtd** group otherwise
247 # nova-compute will be unable to use libvirt.
248 sudo usermod -a -G libvirtd `whoami`
249
250 # libvirt detects various settings on startup, as we potentially changed
251 # the system configuration (modules, filesystems), we need to restart
252 # libvirt to detect those changes.
253 restart_service $LIBVIRT_DAEMON
254
255
256 # Instance Storage
257 # ----------------
258
259 # Nova stores each instance in its own directory.
260 mkdir -p $NOVA_INSTANCES_PATH
261
262 # You can specify a different disk to be mounted and used for backing the
263 # virtual machines. If there is a partition labeled nova-instances we
264 # mount it (ext filesystems can be labeled via e2label).
265 if [ -L /dev/disk/by-label/nova-instances ]; then
266 if ! mount -n | grep -q $NOVA_INSTANCES_PATH; then
267 sudo mount -L nova-instances $NOVA_INSTANCES_PATH
268 sudo chown -R `whoami` $NOVA_INSTANCES_PATH
269 fi
270 fi
271
272 # Clean up old instances
273 cleanup_nova
274 fi
275}
276
277# init_nova() - Initialize databases, etc.
278function init_nova() {
279 # Remove legacy ``nova.conf``
280 rm -f $NOVA_DIR/bin/nova.conf
281
282 # (Re)create ``nova.conf``
Dean Troyer3cf1ffb2012-10-02 11:51:27 -0500283 rm -f $NOVA_CONF
Dean Troyerbf67c192012-09-21 15:09:37 -0500284 add_nova_opt "[DEFAULT]"
285 add_nova_opt "verbose=True"
286 add_nova_opt "auth_strategy=keystone"
287 add_nova_opt "allow_resize_to_same_host=True"
288 add_nova_opt "api_paste_config=$NOVA_API_PASTE_INI"
289 add_nova_opt "rootwrap_config=$NOVA_CONF_DIR/rootwrap.conf"
290 add_nova_opt "compute_scheduler_driver=$SCHEDULER"
Dean Troyer3cf1ffb2012-10-02 11:51:27 -0500291 add_nova_opt "dhcpbridge_flagfile=$NOVA_CONF"
Dean Troyerbf67c192012-09-21 15:09:37 -0500292 add_nova_opt "force_dhcp_release=True"
293 add_nova_opt "fixed_range=$FIXED_RANGE"
294 add_nova_opt "s3_host=$SERVICE_HOST"
295 add_nova_opt "s3_port=$S3_SERVICE_PORT"
296 add_nova_opt "osapi_compute_extension=nova.api.openstack.compute.contrib.standard_extensions"
297 add_nova_opt "my_ip=$HOST_IP"
298 add_nova_opt "sql_connection=$BASE_SQL_CONN/nova?charset=utf8"
299 add_nova_opt "libvirt_type=$LIBVIRT_TYPE"
300 add_nova_opt "libvirt_cpu_mode=none"
301 add_nova_opt "instance_name_template=${INSTANCE_NAME_PREFIX}%08x"
302 add_nova_opt "image_service=nova.image.glance.GlanceImageService"
303
304 if is_service_enabled n-api; then
305 add_nova_opt "enabled_apis=$NOVA_ENABLED_APIS"
306 fi
307 if is_service_enabled n-vol; then
308 add_nova_opt "volume_api_class=nova.volume.api.API"
309 add_nova_opt "volume_group=$VOLUME_GROUP"
310 add_nova_opt "volume_name_template=${VOLUME_NAME_PREFIX}%s"
311 # oneiric no longer supports ietadm
312 add_nova_opt "iscsi_helper=tgtadm"
313 fi
314 if is_service_enabled cinder; then
315 add_nova_opt "volume_api_class=nova.volume.cinder.API"
316 fi
317 if [ -n "$NOVA_STATE_PATH" ]; then
318 add_nova_opt "state_path=$NOVA_STATE_PATH"
Dean Troyer13314452012-10-23 15:09:50 -0500319 add_nova_opt "lock_path=$NOVA_STATE_PATH"
Dean Troyerbf67c192012-09-21 15:09:37 -0500320 fi
321 if [ -n "$NOVA_INSTANCES_PATH" ]; then
322 add_nova_opt "instances_path=$NOVA_INSTANCES_PATH"
323 fi
324 if [ "$MULTI_HOST" != "False" ]; then
325 add_nova_opt "multi_host=True"
326 add_nova_opt "send_arp_for_ha=True"
327 fi
328 if [ "$SYSLOG" != "False" ]; then
329 add_nova_opt "use_syslog=True"
330 fi
331 if [ "$API_RATE_LIMIT" != "True" ]; then
332 add_nova_opt "api_rate_limit=False"
333 fi
334 if [ "$LOG_COLOR" == "True" ] && [ "$SYSLOG" == "False" ]; then
335 # Add color to logging output
336 add_nova_opt "logging_context_format_string=%(asctime)s %(color)s%(levelname)s %(name)s [%(request_id)s %(user_name)s %(project_name)s%(color)s] %(instance)s%(color)s%(message)s"
337 add_nova_opt "logging_default_format_string=%(asctime)s %(color)s%(levelname)s %(name)s [-%(color)s] %(instance)s%(color)s%(message)s"
338 add_nova_opt "logging_debug_format_suffix=from (pid=%(process)d) %(funcName)s %(pathname)s:%(lineno)d"
339 add_nova_opt "logging_exception_prefix=%(color)s%(asctime)s TRACE %(name)s %(instance)s"
340 else
341 # Show user_name and project_name instead of user_id and project_id
342 add_nova_opt "logging_context_format_string=%(asctime)s %(levelname)s %(name)s [%(request_id)s %(user_name)s %(project_name)s] %(instance)s%(message)s"
343 fi
344
345 # Provide some transition from ``EXTRA_FLAGS`` to ``EXTRA_OPTS``
346 if [[ -z "$EXTRA_OPTS" && -n "$EXTRA_FLAGS" ]]; then
347 EXTRA_OPTS=$EXTRA_FLAGS
348 fi
349
350 # Define extra nova conf flags by defining the array ``EXTRA_OPTS``.
351 # For Example: ``EXTRA_OPTS=(foo=true bar=2)``
352 for I in "${EXTRA_OPTS[@]}"; do
353 # Attempt to convert flags to options
354 add_nova_opt ${I//--}
355 done
356
357 # Nova Database
358 # -------------
359
360 # All nova components talk to a central database. We will need to do this step
361 # only once for an entire cluster.
362
363 if is_service_enabled mysql && is_service_enabled nova; then
364 # (Re)create nova database
365 mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e 'DROP DATABASE IF EXISTS nova;'
366
367 # Explicitly use latin1: to avoid lp#829209, nova expects the database to
368 # use latin1 by default, and then upgrades the database to utf8 (see the
369 # 082_essex.py in nova)
370 mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e 'CREATE DATABASE nova CHARACTER SET latin1;'
371
372 # (Re)create nova database
373 $NOVA_BIN_DIR/nova-manage db sync
374 fi
375
376}
377
378# install_novaclient() - Collect source and prepare
379function install_novaclient() {
380 git_clone $NOVACLIENT_REPO $NOVACLIENT_DIR $NOVACLIENT_BRANCH
381}
382
383# install_nova() - Collect source and prepare
384function install_nova() {
385 if is_service_enabled n-cpu; then
386 if [[ "$os_PACKAGE" = "deb" ]]; then
387 LIBVIRT_PKG_NAME=libvirt-bin
388 else
389 LIBVIRT_PKG_NAME=libvirt
390 fi
391 install_package $LIBVIRT_PKG_NAME
392 # Install and configure **LXC** if specified. LXC is another approach to
393 # splitting a system into many smaller parts. LXC uses cgroups and chroot
394 # to simulate multiple systems.
395 if [[ "$LIBVIRT_TYPE" == "lxc" ]]; then
396 if [[ "$os_PACKAGE" = "deb" ]]; then
397 if [[ "$DISTRO" > natty ]]; then
398 install_package cgroup-lite
399 fi
400 else
401 ### FIXME(dtroyer): figure this out
402 echo "RPM-based cgroup not implemented yet"
403 yum_install libcgroup-tools
404 fi
405 fi
406 fi
407
408 git_clone $NOVA_REPO $NOVA_DIR $NOVA_BRANCH
409}
410
411# start_nova() - Start running processes, including screen
412function start_nova() {
413 # The group **libvirtd** is added to the current user in this script.
414 # Use 'sg' to execute nova-compute as a member of the **libvirtd** group.
415 # ``screen_it`` checks ``is_service_enabled``, it is not needed here
416 screen_it n-cpu "cd $NOVA_DIR && sg libvirtd $NOVA_BIN_DIR/nova-compute"
417 screen_it n-crt "cd $NOVA_DIR && $NOVA_BIN_DIR/nova-cert"
418 screen_it n-net "cd $NOVA_DIR && $NOVA_BIN_DIR/nova-network"
419 screen_it n-sch "cd $NOVA_DIR && $NOVA_BIN_DIR/nova-scheduler"
Dean Troyer3cf1ffb2012-10-02 11:51:27 -0500420 screen_it n-novnc "cd $NOVNC_DIR && ./utils/nova-novncproxy --config-file $NOVA_CONF --web ."
421 screen_it n-xvnc "cd $NOVA_DIR && ./bin/nova-xvpvncproxy --config-file $NOVA_CONF"
Dean Troyerbf67c192012-09-21 15:09:37 -0500422 screen_it n-cauth "cd $NOVA_DIR && ./bin/nova-consoleauth"
423}
424
425# stop_nova() - Stop running processes (non-screen)
426function stop_nova() {
427 # Kill the nova screen windows
428 for serv in n-api n-cpu n-crt n-net n-sch n-novnc n-xvnc n-cauth; do
429 screen -S $SCREEN_NAME -p $serv -X kill
430 done
431}
432
433# Restore xtrace
434$XTRACE