blob: f82633a40911a6639cb24b0dc9a5b5e8c5929489 [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
36# baremetal compute service, and inform it of the hardware it will contorl.
37#
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
56# Sub-driver settings
57# -------------------
58
59# sub-driver to use for kernel deployment
60# - nova.virt.baremetal.pxe.PXE
61# - nova.virt.baremetal.tilera.TILERA
62BM_DRIVER=${BM_DRIVER:-nova.virt.baremetal.pxe.PXE}
63
64# sub-driver to use for remote power management
65# - nova.virt.baremetal.fake.FakePowerManager, for manual power control
66# - nova.virt.baremetal.ipmi.Ipmi, for remote IPMI
67# - nova.virt.baremetal.tilera_pdu.Pdu, for TilePro hardware
68BM_POWER_MANAGER=${BM_POWER_MANAGER:-nova.virt.baremetal.fake.FakePowerManager}
69
70
71# These should be customized to your environment and hardware
72# -----------------------------------------------------------
73
74# BM_DNSMASQ_* options must be changed to suit your network environment
75BM_DNSMASQ_IFACE=${BM_DNSMASQ_IFACE:-$PUBLIC_INTERFACE}
76BM_DNSMASQ_IFACE=${BM_DNSMASQ_IFACE:-eth0}
77BM_DNSMASQ_RANGE=${BM_DNSMASQ_RANGE:-192.0.2.32,192.0.2.48}
78
79# BM_FIRST_MAC *must* be set to the MAC address of the node you will boot.
80# This is passed to dnsmasq along with the kernel/ramdisk to
81# deploy via PXE.
82BM_FIRST_MAC=${BM_FIRST_MAC:-}
83
84# BM_SECOND_MAC is only important if the host has >1 NIC.
85BM_SECOND_MAC=${BM_SECOND_MAC:-}
86
87# Hostname for the baremetal nova-compute node, if not run on this host
88BM_HOSTNAME=${BM_HOSTNAME:-$(hostname -f)}
89
90# BM_PM_* options are only necessary if BM_POWER_MANAGER=...IPMI
91BM_PM_ADDR=${BM_PM_ADDR:-0.0.0.0}
92BM_PM_USER=${BM_PM_USER:-user}
93BM_PM_PASS=${BM_PM_PASS:-pass}
94
95# BM_FLAVOR_* options are arbitrary and not necessarily related to physical
96# hardware capacity. These can be changed if you are testing
97# BaremetalHostManager with multiple nodes and different flavors.
98BM_CPU_ARCH=${BM_CPU_ARCH:-x86_64}
99BM_FLAVOR_CPU=${BM_FLAVOR_CPU:-1}
100BM_FLAVOR_RAM=${BM_FLAVOR_RAM:-1024}
101BM_FLAVOR_ROOT_DISK=${BM_FLAVOR_ROOT_DISK:-10}
102BM_FLAVOR_EPHEMERAL_DISK=${BM_FLAVOR_EPHEMERAL_DISK:-0}
103BM_FLAVOR_SWAP=${BM_FLAVOR_SWAP:-1}
104BM_FLAVOR_NAME=${BM_FLAVOR_NAME:-bm.small}
105BM_FLAVOR_ID=${BM_FLAVOR_ID:-11}
106BM_FLAVOR_ARCH=${BM_FLAVOR_ARCH:-$BM_CPU_ARCH}
107
108
109# Below this, we set some path and filenames.
110# Defaults are probably sufficient.
111
112BM_IMAGE_BUILD_DIR=${BM_IMAGE_BUILD_DIR:-$DEST/diskimage-builder}
113
114BM_HOST_CURRENT_KERNEL=$(uname -r)
115BM_DEPLOY_RAMDISK=${BM_DEPLOY_RAMDISK:-bm-deploy-$BM_HOST_CURRENT_KERNEL-initrd}
116BM_DEPLOY_KERNEL=${BM_DEPLOY_KERNEL:-bm-deploy-$BM_HOST_CURRENT_KERNEL-vmlinuz}
117
118# If you need to add any extra flavors to the deploy ramdisk image
119# eg, specific network drivers, specify them here
120BM_DEPLOY_FLAVOR=${BM_DEPLOY_FLAVOR:-}
121
122# set URL and version for google shell-in-a-box
123BM_SHELL_IN_A_BOX=${BM_SHELL_IN_A_BOX:-http://shellinabox.googlecode.com/files/shellinabox-2.14.tar.gz}
124
125
126# Functions
127# ---------
128
129# Check if baremetal is properly enabled
130# Returns false if VIRT_DRIVER is not baremetal, or if ENABLED_SERVICES
131# does not contain "baremetal"
132function is_baremetal() {
133 if [[ "$ENABLED_SERVICES" =~ 'baremetal' && "$VIRT_DRIVER" = 'baremetal' ]]; then
134 return 0
135 fi
136 return 1
137}
138
139# Install diskimage-builder and shell-in-a-box
140# so that we can build the deployment kernel & ramdisk
141function prepare_baremetal_toolchain() {
142 git_clone $BM_IMAGE_BUILD_REPO $BM_IMAGE_BUILD_DIR $BM_IMAGE_BUILD_BRANCH
143
144 local shellinabox_basename=$(basename $BM_SHELL_IN_A_BOX)
145 if [[ ! -e $DEST/$shellinabox_basename ]]; then
146 cd $DEST
147 wget $BM_SHELL_IN_A_BOX
148 fi
149 if [[ ! -d $DEST/${shellinabox_basename%%.tar.gz} ]]; then
150 cd $DEST
151 tar xzf $shellinabox_basename
152 fi
153 if [[ ! $(which shellinaboxd) ]]; then
154 cd $DEST/${shellinabox_basename%%.tar.gz}
155 ./configure
156 make
157 sudo make install
158 fi
159}
160
161# prepare various directories needed by baremetal hypervisor
162function configure_baremetal_nova_dirs() {
163 # ensure /tftpboot is prepared
164 sudo mkdir -p /tftpboot
165 sudo mkdir -p /tftpboot/pxelinux.cfg
166 sudo cp /usr/lib/syslinux/pxelinux.0 /tftpboot/
167 sudo chown -R `whoami`:libvirtd /tftpboot
168
169 # ensure $NOVA_STATE_PATH/baremetal is prepared
170 sudo mkdir -p $NOVA_STATE_PATH/baremetal
171 sudo mkdir -p $NOVA_STATE_PATH/baremetal/console
172 sudo mkdir -p $NOVA_STATE_PATH/baremetal/dnsmasq
173 sudo touch $NOVA_STATE_PATH/baremetal/dnsmasq/dnsmasq-dhcp.host
174 sudo chown -R `whoami` $NOVA_STATE_PATH/baremetal
175
176 # ensure dnsmasq is installed but not running
177 # because baremetal driver will reconfigure and restart this as needed
178 if [ ! is_package_installed dnsmasq ]; then
179 install_package dnsmasq
180 fi
181 stop_service dnsmasq
182}
183
184# build deploy kernel+ramdisk, then upload them to glance
185# this function sets BM_DEPLOY_KERNEL_ID and BM_DEPLOY_RAMDISK_ID
186function upload_baremetal_deploy() {
187 token=$1
188
189 if [ ! -e $TOP_DIR/files/$BM_DEPLOY_KERNEL -a -e /boot/vmlinuz-$BM_HOST_CURRENT_KERNEL ]; then
190 sudo cp /boot/vmlinuz-$BM_HOST_CURRENT_KERNEL $TOP_DIR/files/$BM_DEPLOY_KERNEL
191 sudo chmod a+r $TOP_DIR/files/$BM_DEPLOY_KERNEL
192 fi
193 if [ ! -e $TOP_DIR/files/$BM_DEPLOY_RAMDISK ]; then
194 $BM_IMAGE_BUILD_DIR/bin/ramdisk-image-create $BM_DEPLOY_FLAVOR deploy \
195 -o $TOP_DIR/files/$BM_DEPLOY_RAMDISK -k $BM_HOST_CURRENT_KERNEL
196 fi
197
198 # load them into glance
199 BM_DEPLOY_KERNEL_ID=$(glance \
200 --os-auth-token $token \
201 --os-image-url http://$GLANCE_HOSTPORT \
202 image-create \
203 --name $BM_DEPLOY_KERNEL \
204 --public --disk-format=aki \
205 < $TOP_DIR/files/$BM_DEPLOY_KERNEL | grep ' id ' | get_field 2)
206 BM_DEPLOY_RAMDISK_ID=$(glance \
207 --os-auth-token $token \
208 --os-image-url http://$GLANCE_HOSTPORT \
209 image-create \
210 --name $BM_DEPLOY_RAMDISK \
211 --public --disk-format=ari \
212 < $TOP_DIR/files/$BM_DEPLOY_RAMDISK | grep ' id ' | get_field 2)
213}
214
215# create a basic baremetal flavor, associated with deploy kernel & ramdisk
216#
217# Usage: create_baremetal_flavor <aki_uuid> <ari_uuid>
218function create_baremetal_flavor() {
219 aki=$1
220 ari=$2
221 nova flavor-create $BM_FLAVOR_NAME $BM_FLAVOR_ID \
222 $BM_FLAVOR_RAM $BM_FLAVOR_ROOT_DISK $BM_FLAVOR_CPU
223 nova-manage instance_type set_key \
224 --name=$BM_FLAVOR_NAME --key cpu_arch --value $BM_FLAVOR_ARCH
225 nova-manage instance_type set_key \
226 --name=$BM_FLAVOR_NAME --key deploy_kernel_id --value $aki
227 nova-manage instance_type set_key \
228 --name=$BM_FLAVOR_NAME --key deploy_ramdisk_id --value $ari
229}
230
231# pull run-time kernel/ramdisk out of disk image and load into glance
232# note that $file is currently expected to be in qcow2 format
233# Sets KERNEL_ID and RAMDISK_ID
234#
235# Usage: extract_and_upload_k_and_r_from_image $token $file
236function extract_and_upload_k_and_r_from_image() {
237 token=$1
238 file=$2
239 image_name=$(basename "$file" ".qcow2")
240
241 # this call returns the file names as "$kernel,$ramdisk"
242 out=$($BM_IMAGE_BUILD_DIR/bin/disk-image-get-kernel \
243 -x -d $TOP_DIR/files -o bm-deploy -i $file)
244 if [ $? -ne 0 ]; then
245 die "Failed to get kernel and ramdisk from $file"
246 fi
247 XTRACE=$(set +o | grep xtrace)
248 set +o xtrace
249 out=$(echo "$out" | tail -1)
250 $XTRACE
251 OUT_KERNEL=${out%%,*}
252 OUT_RAMDISK=${out##*,}
253
254 # load them into glance
255 KERNEL_ID=$(glance \
256 --os-auth-token $token \
257 --os-image-url http://$GLANCE_HOSTPORT \
258 image-create \
259 --name $image_name-kernel \
260 --public --disk-format=aki \
261 < $TOP_DIR/files/$OUT_KERNEL | grep ' id ' | get_field 2)
262 RAMDISK_ID=$(glance \
263 --os-auth-token $token \
264 --os-image-url http://$GLANCE_HOSTPORT \
265 image-create \
266 --name $image_name-initrd \
267 --public --disk-format=ari \
268 < $TOP_DIR/files/$OUT_RAMDISK | grep ' id ' | get_field 2)
269}
270
271
272# Re-implementation of devstack's "upload_image" function
273#
274# Takes the same parameters, but has some peculiarities which made it
275# easier to create a separate method, rather than complicate the logic
276# of the existing function.
277function upload_baremetal_image() {
278 local image_url=$1
279 local token=$2
280
281 # Create a directory for the downloaded image tarballs.
282 mkdir -p $FILES/images
283
284 # Downloads the image (uec ami+aki style), then extracts it.
285 IMAGE_FNAME=`basename "$image_url"`
286 if [[ ! -f $FILES/$IMAGE_FNAME || \
287 "$(stat -c "%s" $FILES/$IMAGE_FNAME)" = "0" ]]; then
288 wget -c $image_url -O $FILES/$IMAGE_FNAME
289 if [[ $? -ne 0 ]]; then
290 echo "Not found: $image_url"
291 return
292 fi
293 fi
294
295 local KERNEL=""
296 local RAMDISK=""
297 local DISK_FORMAT=""
298 local CONTAINER_FORMAT=""
299 case "$IMAGE_FNAME" in
300 *.tar.gz|*.tgz)
301 # Extract ami and aki files
302 [ "${IMAGE_FNAME%.tar.gz}" != "$IMAGE_FNAME" ] &&
303 IMAGE_NAME="${IMAGE_FNAME%.tar.gz}" ||
304 IMAGE_NAME="${IMAGE_FNAME%.tgz}"
305 xdir="$FILES/images/$IMAGE_NAME"
306 rm -Rf "$xdir";
307 mkdir "$xdir"
308 tar -zxf $FILES/$IMAGE_FNAME -C "$xdir"
309 KERNEL=$(for f in "$xdir/"*-vmlinuz* "$xdir/"aki-*/image; do
310 [ -f "$f" ] && echo "$f" && break; done; true)
311 RAMDISK=$(for f in "$xdir/"*-initrd* "$xdir/"ari-*/image; do
312 [ -f "$f" ] && echo "$f" && break; done; true)
313 IMAGE=$(for f in "$xdir/"*.img "$xdir/"ami-*/image; do
314 [ -f "$f" ] && echo "$f" && break; done; true)
315 if [[ -z "$IMAGE_NAME" ]]; then
316 IMAGE_NAME=$(basename "$IMAGE" ".img")
317 fi
318 DISK_FORMAT=ami
319 CONTAINER_FORMAT=ami
320 ;;
321 *.qcow2)
322 IMAGE="$FILES/${IMAGE_FNAME}"
323 IMAGE_NAME=$(basename "$IMAGE" ".qcow2")
324 DISK_FORMAT=qcow2
325 CONTAINER_FORMAT=bare
326 ;;
327 *) echo "Do not know what to do with $IMAGE_FNAME"; false;;
328 esac
329
330 if [ "$CONTAINER_FORMAT" = "bare" ]; then
331 extract_and_upload_k_and_r_from_image $token $IMAGE
332 elif [ "$CONTAINER_FORMAT" = "ami" ]; then
333 KERNEL_ID=$(glance \
334 --os-auth-token $token \
335 --os-image-url http://$GLANCE_HOSTPORT \
336 image-create \
337 --name "$IMAGE_NAME-kernel" --public \
338 --container-format aki \
339 --disk-format aki < "$KERNEL" | grep ' id ' | get_field 2)
340 RAMDISK_ID=$(glance \
341 --os-auth-token $token \
342 --os-image-url http://$GLANCE_HOSTPORT \
343 image-create \
344 --name "$IMAGE_NAME-ramdisk" --public \
345 --container-format ari \
346 --disk-format ari < "$RAMDISK" | grep ' id ' | get_field 2)
347 else
348 # TODO(deva): add support for other image types
349 return
350 fi
351
352 glance \
353 --os-auth-token $token \
354 --os-image-url http://$GLANCE_HOSTPORT \
355 image-create \
356 --name "${IMAGE_NAME%.img}" --public \
357 --container-format $CONTAINER_FORMAT \
358 --disk-format $DISK_FORMAT \
359 ${KERNEL_ID:+--property kernel_id=$KERNEL_ID} \
360 ${RAMDISK_ID:+--property ramdisk_id=$RAMDISK_ID} < "${IMAGE}"
361
362 # override DEFAULT_IMAGE_NAME so that tempest can find the image
363 # that we just uploaded in glance
364 DEFAULT_IMAGE_NAME="${IMAGE_NAME%.img}"
365}
366
367function clear_baremetal_of_all_nodes() {
368 list=$(nova-baremetal-manage node list | tail -n +2 | awk '{print $1}' )
369 for node in $list
370 do
371 nova-baremetal-manage node delete $node
372 done
373 list=$(nova-baremetal-manage interface list | tail -n +2 | awk '{print $1}' )
374 for iface in $list
375 do
376 nova-baremetal-manage interface delete $iface
377 done
378}
379
380# inform nova-baremetal about nodes, MACs, etc
381# Defaults to using BM_FIRST_MAC and BM_SECOND_MAC if parameters not specified
382#
383# Usage: add_baremetal_node <first_mac> <second_mac>
384function add_baremetal_node() {
385 mac_1=${1:-$BM_FIRST_MAC}
386 mac_2=${2:-$BM_SECOND_MAC}
387
388 id=$(nova-baremetal-manage node create \
389 --host=$BM_HOSTNAME --prov_mac=$mac_1 \
390 --cpus=$BM_FLAVOR_CPU --memory_mb=$BM_FLAVOR_RAM \
391 --local_gb=$BM_FLAVOR_ROOT_DISK --terminal_port=0 \
392 --pm_address=$BM_PM_ADDR --pm_user=$BM_PM_USER --pm_password=$BM_PM_PASS \
393 )
394 [ $? -eq 0 ] || [ "$id" ] || die "Error adding baremetal node"
395 id2=$(nova-baremetal-manage interface create \
396 --node_id=$id --mac_address=$mac_2 --datapath_id=0 --port_no=0 \
397 )
398 [ $? -eq 0 ] || [ "$id2" ] || die "Error adding interface to barmetal node $id"
399}
400
401
402# Restore xtrace
403$XTRACE