blob: bed3c093c302589a4cff738ca7328fa3d20846b5 [file] [log] [blame]
Devananda van der Veenf35cf912012-11-12 17:58:38 -08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright (c) 2012 Hewlett-Packard Development Company, L.P.
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18
19# This file provides devstack with the environment and utilities to
20# control nova-compute's baremetal driver.
21# It sets reasonable defaults to run within a single host,
22# using virtual machines in place of physical hardware.
23# However, by changing just a few options, devstack+baremetal can in fact
24# control physical hardware resources on the same network, if you know
25# the MAC address(es) and IPMI credentials.
26#
27# At a minimum, to enable the baremetal driver, you must set these in loclarc:
28# VIRT_DRIVER=baremetal
29# ENABLED_SERVICES="$ENABLED_SERVICES,baremetal"
30#
31#
32# We utilize diskimage-builder to create a ramdisk, and then
33# baremetal driver uses that to push a disk image onto the node(s).
34#
35# Below we define various defaults which control the behavior of the
Tim Miller9a3ba4b2013-03-18 18:08:27 -070036# baremetal compute service, and inform it of the hardware it will control.
Devananda van der Veenf35cf912012-11-12 17:58:38 -080037#
38# Below that, various functions are defined, which are called by devstack
39# in the following order:
40#
41# before nova-cpu starts:
42# - prepare_baremetal_toolchain
43# - configure_baremetal_nova_dirs
44#
45# after nova and glance have started:
46# - build_and_upload_baremetal_deploy_k_and_r $token
47# - create_baremetal_flavor $BM_DEPLOY_KERNEL_ID $BM_DEPLOY_RAMDISK_ID
48# - upload_baremetal_image $url $token
49# - add_baremetal_node <first_mac> <second_mac>
50
51
52# Save trace setting
53XTRACE=$(set +o | grep xtrace)
54set +o xtrace
55
Dean Troyercc6b4432013-04-08 15:38:03 -050056
Devananda van der Veenf35cf912012-11-12 17:58:38 -080057# Sub-driver settings
58# -------------------
59
60# sub-driver to use for kernel deployment
61# - nova.virt.baremetal.pxe.PXE
62# - nova.virt.baremetal.tilera.TILERA
63BM_DRIVER=${BM_DRIVER:-nova.virt.baremetal.pxe.PXE}
64
65# sub-driver to use for remote power management
66# - nova.virt.baremetal.fake.FakePowerManager, for manual power control
Ghe Riverocc404a42013-03-06 11:48:03 +010067# - nova.virt.baremetal.ipmi.IPMI, for remote IPMI
Devananda van der Veenf35cf912012-11-12 17:58:38 -080068# - nova.virt.baremetal.tilera_pdu.Pdu, for TilePro hardware
69BM_POWER_MANAGER=${BM_POWER_MANAGER:-nova.virt.baremetal.fake.FakePowerManager}
70
71
72# These should be customized to your environment and hardware
73# -----------------------------------------------------------
74
Devananda van der Veen7611c892012-11-23 10:54:54 -080075# whether to create a fake environment, eg. for devstack-gate
76BM_USE_FAKE_ENV=`trueorfalse False $BM_USE_FAKE_ENV`
77
78# Extra options to pass to bm_poseur
79# change the bridge name or IP: --bridge br99 --bridge-ip 192.0.2.1
80# change the virtualization type: --engine qemu
81BM_POSEUR_EXTRA_OPTS=${BM_POSEUR_EXTRA_OPTS:-}
82
83# BM_DNSMASQ_IFACE should match FLAT_NETWORK_BRIDGE
84if [ "$BM_USE_FAKE_ENV" ]; then
85 BM_DNSMASQ_IFACE=${BM_DNSMASQ_IFACE:-br99}
86 BM_DNSMASQ_RANGE=${BM_DNSMASQ_RANGE:-192.0.2.32,192.0.2.48}
87else
88 BM_DNSMASQ_IFACE=${BM_DNSMASQ_IFACE:-eth0}
89 # if testing on a physical network,
90 # BM_DNSMASQ_RANGE must be changed to suit your network
91 BM_DNSMASQ_RANGE=${BM_DNSMASQ_RANGE:-}
92fi
Devananda van der Veenf35cf912012-11-12 17:58:38 -080093
Chris Krelle35336282013-02-03 15:48:43 -080094# BM_DNSMASQ_DNS provide dns server to bootstrap clients
95BM_DNSMASQ_DNS=${BM_DNSMASQ_DNS:-}
96
Devananda van der Veenf35cf912012-11-12 17:58:38 -080097# BM_FIRST_MAC *must* be set to the MAC address of the node you will boot.
98# This is passed to dnsmasq along with the kernel/ramdisk to
99# deploy via PXE.
100BM_FIRST_MAC=${BM_FIRST_MAC:-}
101
102# BM_SECOND_MAC is only important if the host has >1 NIC.
103BM_SECOND_MAC=${BM_SECOND_MAC:-}
104
105# Hostname for the baremetal nova-compute node, if not run on this host
106BM_HOSTNAME=${BM_HOSTNAME:-$(hostname -f)}
107
108# BM_PM_* options are only necessary if BM_POWER_MANAGER=...IPMI
109BM_PM_ADDR=${BM_PM_ADDR:-0.0.0.0}
110BM_PM_USER=${BM_PM_USER:-user}
111BM_PM_PASS=${BM_PM_PASS:-pass}
112
113# BM_FLAVOR_* options are arbitrary and not necessarily related to physical
114# hardware capacity. These can be changed if you are testing
115# BaremetalHostManager with multiple nodes and different flavors.
116BM_CPU_ARCH=${BM_CPU_ARCH:-x86_64}
117BM_FLAVOR_CPU=${BM_FLAVOR_CPU:-1}
118BM_FLAVOR_RAM=${BM_FLAVOR_RAM:-1024}
119BM_FLAVOR_ROOT_DISK=${BM_FLAVOR_ROOT_DISK:-10}
120BM_FLAVOR_EPHEMERAL_DISK=${BM_FLAVOR_EPHEMERAL_DISK:-0}
121BM_FLAVOR_SWAP=${BM_FLAVOR_SWAP:-1}
122BM_FLAVOR_NAME=${BM_FLAVOR_NAME:-bm.small}
123BM_FLAVOR_ID=${BM_FLAVOR_ID:-11}
124BM_FLAVOR_ARCH=${BM_FLAVOR_ARCH:-$BM_CPU_ARCH}
125
126
127# Below this, we set some path and filenames.
128# Defaults are probably sufficient.
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800129BM_IMAGE_BUILD_DIR=${BM_IMAGE_BUILD_DIR:-$DEST/diskimage-builder}
Devananda van der Veen7611c892012-11-23 10:54:54 -0800130BM_POSEUR_DIR=${BM_POSEUR_DIR:-$DEST/bm_poseur}
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800131
132BM_HOST_CURRENT_KERNEL=$(uname -r)
133BM_DEPLOY_RAMDISK=${BM_DEPLOY_RAMDISK:-bm-deploy-$BM_HOST_CURRENT_KERNEL-initrd}
134BM_DEPLOY_KERNEL=${BM_DEPLOY_KERNEL:-bm-deploy-$BM_HOST_CURRENT_KERNEL-vmlinuz}
135
136# If you need to add any extra flavors to the deploy ramdisk image
137# eg, specific network drivers, specify them here
138BM_DEPLOY_FLAVOR=${BM_DEPLOY_FLAVOR:-}
139
140# set URL and version for google shell-in-a-box
141BM_SHELL_IN_A_BOX=${BM_SHELL_IN_A_BOX:-http://shellinabox.googlecode.com/files/shellinabox-2.14.tar.gz}
142
143
144# Functions
145# ---------
146
147# Check if baremetal is properly enabled
148# Returns false if VIRT_DRIVER is not baremetal, or if ENABLED_SERVICES
149# does not contain "baremetal"
150function is_baremetal() {
151 if [[ "$ENABLED_SERVICES" =~ 'baremetal' && "$VIRT_DRIVER" = 'baremetal' ]]; then
152 return 0
153 fi
154 return 1
155}
156
157# Install diskimage-builder and shell-in-a-box
158# so that we can build the deployment kernel & ramdisk
159function prepare_baremetal_toolchain() {
160 git_clone $BM_IMAGE_BUILD_REPO $BM_IMAGE_BUILD_DIR $BM_IMAGE_BUILD_BRANCH
Devananda van der Veen7611c892012-11-23 10:54:54 -0800161 git_clone $BM_POSEUR_REPO $BM_POSEUR_DIR $BM_POSEUR_BRANCH
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800162
163 local shellinabox_basename=$(basename $BM_SHELL_IN_A_BOX)
164 if [[ ! -e $DEST/$shellinabox_basename ]]; then
165 cd $DEST
166 wget $BM_SHELL_IN_A_BOX
167 fi
168 if [[ ! -d $DEST/${shellinabox_basename%%.tar.gz} ]]; then
169 cd $DEST
170 tar xzf $shellinabox_basename
171 fi
172 if [[ ! $(which shellinaboxd) ]]; then
173 cd $DEST/${shellinabox_basename%%.tar.gz}
174 ./configure
175 make
176 sudo make install
177 fi
178}
179
Devananda van der Veen7611c892012-11-23 10:54:54 -0800180# set up virtualized environment for devstack-gate testing
181function create_fake_baremetal_env() {
182 local bm_poseur="$BM_POSEUR_DIR/bm_poseur"
183 # TODO(deva): add support for >1 VM
184 sudo $bm_poseur $BM_POSEUR_EXTRA_OPTS create-bridge
185 sudo $bm_poseur $BM_POSEUR_EXTRA_OPTS create-vm
186 BM_FIRST_MAC=$(sudo $bm_poseur get-macs)
187
188 # NOTE: there is currently a limitation in baremetal driver
189 # that requires second MAC even if it is not used.
190 # Passing a fake value allows this to work.
191 # TODO(deva): remove this after driver issue is fixed.
192 BM_SECOND_MAC='12:34:56:78:90:12'
193}
194
195function cleanup_fake_baremetal_env() {
196 local bm_poseur="$BM_POSEUR_DIR/bm_poseur"
197 sudo $bm_poseur $BM_POSEUR_EXTRA_OPTS destroy-vm
198 sudo $bm_poseur $BM_POSEUR_EXTRA_OPTS destroy-bridge
199}
200
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800201# prepare various directories needed by baremetal hypervisor
202function configure_baremetal_nova_dirs() {
203 # ensure /tftpboot is prepared
204 sudo mkdir -p /tftpboot
205 sudo mkdir -p /tftpboot/pxelinux.cfg
206 sudo cp /usr/lib/syslinux/pxelinux.0 /tftpboot/
Émilien Macchib2ef8902013-05-04 00:48:20 +0200207 sudo chown -R $STACK_USER:$LIBVIRT_GROUP /tftpboot
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800208
209 # ensure $NOVA_STATE_PATH/baremetal is prepared
210 sudo mkdir -p $NOVA_STATE_PATH/baremetal
211 sudo mkdir -p $NOVA_STATE_PATH/baremetal/console
212 sudo mkdir -p $NOVA_STATE_PATH/baremetal/dnsmasq
213 sudo touch $NOVA_STATE_PATH/baremetal/dnsmasq/dnsmasq-dhcp.host
Attila Fazekas91b8d132013-01-06 22:40:09 +0100214 sudo chown -R $STACK_USER $NOVA_STATE_PATH/baremetal
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800215
216 # ensure dnsmasq is installed but not running
217 # because baremetal driver will reconfigure and restart this as needed
Devananda van der Veen75eaaf42012-12-28 15:40:21 -0800218 is_package_installed dnsmasq || install_package dnsmasq
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800219 stop_service dnsmasq
220}
221
222# build deploy kernel+ramdisk, then upload them to glance
223# this function sets BM_DEPLOY_KERNEL_ID and BM_DEPLOY_RAMDISK_ID
224function upload_baremetal_deploy() {
225 token=$1
226
227 if [ ! -e $TOP_DIR/files/$BM_DEPLOY_KERNEL -a -e /boot/vmlinuz-$BM_HOST_CURRENT_KERNEL ]; then
228 sudo cp /boot/vmlinuz-$BM_HOST_CURRENT_KERNEL $TOP_DIR/files/$BM_DEPLOY_KERNEL
229 sudo chmod a+r $TOP_DIR/files/$BM_DEPLOY_KERNEL
230 fi
231 if [ ! -e $TOP_DIR/files/$BM_DEPLOY_RAMDISK ]; then
232 $BM_IMAGE_BUILD_DIR/bin/ramdisk-image-create $BM_DEPLOY_FLAVOR deploy \
233 -o $TOP_DIR/files/$BM_DEPLOY_RAMDISK -k $BM_HOST_CURRENT_KERNEL
234 fi
235
236 # load them into glance
237 BM_DEPLOY_KERNEL_ID=$(glance \
238 --os-auth-token $token \
239 --os-image-url http://$GLANCE_HOSTPORT \
240 image-create \
241 --name $BM_DEPLOY_KERNEL \
242 --public --disk-format=aki \
243 < $TOP_DIR/files/$BM_DEPLOY_KERNEL | grep ' id ' | get_field 2)
244 BM_DEPLOY_RAMDISK_ID=$(glance \
245 --os-auth-token $token \
246 --os-image-url http://$GLANCE_HOSTPORT \
247 image-create \
248 --name $BM_DEPLOY_RAMDISK \
249 --public --disk-format=ari \
250 < $TOP_DIR/files/$BM_DEPLOY_RAMDISK | grep ' id ' | get_field 2)
251}
252
253# create a basic baremetal flavor, associated with deploy kernel & ramdisk
254#
255# Usage: create_baremetal_flavor <aki_uuid> <ari_uuid>
256function create_baremetal_flavor() {
257 aki=$1
258 ari=$2
259 nova flavor-create $BM_FLAVOR_NAME $BM_FLAVOR_ID \
260 $BM_FLAVOR_RAM $BM_FLAVOR_ROOT_DISK $BM_FLAVOR_CPU
Devananda van der Veen75eaaf42012-12-28 15:40:21 -0800261 nova flavor-key $BM_FLAVOR_NAME set \
Devananda van der Veen2920b7d2013-03-04 11:47:14 -0800262 "cpu_arch"="$BM_FLAVOR_ARCH" \
263 "baremetal:deploy_kernel_id"="$aki" \
264 "baremetal:deploy_ramdisk_id"="$ari"
265
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800266}
267
268# pull run-time kernel/ramdisk out of disk image and load into glance
269# note that $file is currently expected to be in qcow2 format
270# Sets KERNEL_ID and RAMDISK_ID
271#
272# Usage: extract_and_upload_k_and_r_from_image $token $file
273function extract_and_upload_k_and_r_from_image() {
274 token=$1
275 file=$2
276 image_name=$(basename "$file" ".qcow2")
277
278 # this call returns the file names as "$kernel,$ramdisk"
279 out=$($BM_IMAGE_BUILD_DIR/bin/disk-image-get-kernel \
280 -x -d $TOP_DIR/files -o bm-deploy -i $file)
281 if [ $? -ne 0 ]; then
282 die "Failed to get kernel and ramdisk from $file"
283 fi
284 XTRACE=$(set +o | grep xtrace)
285 set +o xtrace
286 out=$(echo "$out" | tail -1)
287 $XTRACE
288 OUT_KERNEL=${out%%,*}
289 OUT_RAMDISK=${out##*,}
290
291 # load them into glance
292 KERNEL_ID=$(glance \
293 --os-auth-token $token \
294 --os-image-url http://$GLANCE_HOSTPORT \
295 image-create \
296 --name $image_name-kernel \
297 --public --disk-format=aki \
298 < $TOP_DIR/files/$OUT_KERNEL | grep ' id ' | get_field 2)
299 RAMDISK_ID=$(glance \
300 --os-auth-token $token \
301 --os-image-url http://$GLANCE_HOSTPORT \
302 image-create \
303 --name $image_name-initrd \
304 --public --disk-format=ari \
305 < $TOP_DIR/files/$OUT_RAMDISK | grep ' id ' | get_field 2)
306}
307
308
309# Re-implementation of devstack's "upload_image" function
310#
311# Takes the same parameters, but has some peculiarities which made it
312# easier to create a separate method, rather than complicate the logic
313# of the existing function.
314function upload_baremetal_image() {
315 local image_url=$1
316 local token=$2
317
318 # Create a directory for the downloaded image tarballs.
319 mkdir -p $FILES/images
320
321 # Downloads the image (uec ami+aki style), then extracts it.
322 IMAGE_FNAME=`basename "$image_url"`
323 if [[ ! -f $FILES/$IMAGE_FNAME || \
324 "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
325 wget -c $image_url -O $FILES/$IMAGE_FNAME
326 if [[ $? -ne 0 ]]; then
327 echo "Not found: $image_url"
328 return
329 fi
330 fi
331
332 local KERNEL=""
333 local RAMDISK=""
334 local DISK_FORMAT=""
335 local CONTAINER_FORMAT=""
336 case "$IMAGE_FNAME" in
337 *.tar.gz|*.tgz)
338 # Extract ami and aki files
339 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
340 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
341 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
342 xdir="$FILES/images/$IMAGE_NAME"
343 rm -Rf "$xdir";
344 mkdir "$xdir"
345 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
346 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
347 [ -f "$f" ] && echo "$f" && break; done; true)
348 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
349 [ -f "$f" ] && echo "$f" && break; done; true)
350 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
351 [ -f "$f" ] && echo "$f" && break; done; true)
352 if [[ -z "$IMAGE_NAME" ]]; then
353 IMAGE_NAME=$(basename "$IMAGE" ".img")
354 fi
355 DISK_FORMAT=ami
356 CONTAINER_FORMAT=ami
357 ;;
358 *.qcow2)
359 IMAGE="$FILES/${IMAGE_FNAME}"
360 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
361 DISK_FORMAT=qcow2
362 CONTAINER_FORMAT=bare
363 ;;
364 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
365 esac
366
367 if [ "$CONTAINER_FORMAT" = "bare" ]; then
368 extract_and_upload_k_and_r_from_image $token $IMAGE
369 elif [ "$CONTAINER_FORMAT" = "ami" ]; then
370 KERNEL_ID=$(glance \
371 --os-auth-token $token \
372 --os-image-url http://$GLANCE_HOSTPORT \
373 image-create \
374 --name "$IMAGE_NAME-kernel" --public \
375 --container-format aki \
376 --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
377 RAMDISK_ID=$(glance \
378 --os-auth-token $token \
379 --os-image-url http://$GLANCE_HOSTPORT \
380 image-create \
381 --name "$IMAGE_NAME-ramdisk" --public \
382 --container-format ari \
383 --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
384 else
385 # TODO(deva): add support for other image types
386 return
387 fi
388
389 glance \
390 --os-auth-token $token \
391 --os-image-url http://$GLANCE_HOSTPORT \
392 image-create \
393 --name "${IMAGE_NAME%.img}" --public \
394 --container-format $CONTAINER_FORMAT \
395 --disk-format $DISK_FORMAT \
396 ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} \
397 ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
398
Tim Miller9a3ba4b2013-03-18 18:08:27 -0700399 # override DEFAULT_IMAGE_NAME so that tempest can find the image
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800400 # that we just uploaded in glance
401 DEFAULT_IMAGE_NAME="${IMAGE_NAME%.img}"
402}
403
404function clear_baremetal_of_all_nodes() {
Arata Notsu24f79612013-02-13 21:01:18 +0900405 list=$(nova baremetal-node-list | awk -F '| ' 'NR>3 {print $2}' )
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800406 for node in $list
407 do
Arata Notsu24f79612013-02-13 21:01:18 +0900408 nova baremetal-node-delete $node
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800409 done
410}
411
412# inform nova-baremetal about nodes, MACs, etc
413# Defaults to using BM_FIRST_MAC and BM_SECOND_MAC if parameters not specified
414#
415# Usage: add_baremetal_node <first_mac> <second_mac>
416function add_baremetal_node() {
417 mac_1=${1:-$BM_FIRST_MAC}
418 mac_2=${2:-$BM_SECOND_MAC}
419
Arata Notsu24f79612013-02-13 21:01:18 +0900420 id=$(nova baremetal-node-create \
421 --pm_address="$BM_PM_ADDR" \
422 --pm_user="$BM_PM_USER" \
423 --pm_password="$BM_PM_PASS" \
424 "$BM_HOSTNAME" \
425 "$BM_FLAVOR_CPU" \
426 "$BM_FLAVOR_RAM" \
427 "$BM_FLAVOR_ROOT_DISK" \
428 "$mac_1" \
429 | grep ' id ' | get_field 2 )
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800430 [ $? -eq 0 ] || [ "$id" ] || die "Error adding baremetal node"
Arata Notsuc4193b22013-04-08 15:25:30 +0900431 id2=$(nova baremetal-interface-add "$id" "$mac_2" )
Devananda van der Veenf35cf912012-11-12 17:58:38 -0800432 [ $? -eq 0 ] || [ "$id2" ] || die "Error adding interface to barmetal node $id"
433}
434
435
436# Restore xtrace
437$XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400438
439# Local variables:
440# mode: shell-script
441# End: