2 # Openstack specific functions
4 ################################################################
6 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License version 2 or 3 as
10 # published by the Free Software Foundation.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program (see the file COPYING); if not, write to the
19 # Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 ################################################################
24 cloud_volume_attach_openstack() {
26 local VM_VOL_NAME="$2"
29 if ! nova volume-attach "$VM_SERVER" "$VM_VOL_NAME" "$VM_VOL_DEV"; then
30 echo "ERROR: nova attach failed. $?" >&2
34 state=`nova volume-show "$VM_VOL_NAME" | sed -n 's,^|[ ]*status[ ]*|[ ]*\([^ ]*\).*,\1,p'`
35 test "$state" = "in-use" && break
36 if test -z "$state" ; then
37 echo "ERROR: unable to find state of volume $VM_VOL_NAME" >&2
40 if test "$state" = available ; then
41 echo "WARNING: volume $VM_VOL_NAME got not attached, retrying" >&2
42 if ! nova volume-attach "$VM_SERVER" "$VM_VOL_NAME" "$VM_VOL_DEV"; then
43 echo "ERROR: nova attach failed. $?" >&2
49 if test ! -e "$VM_VOL_DEV" ; then
50 #GROSS HACK: kernel does not care about the given device name
51 # VM_VOL_DEV="/dev/"`dmesg| sed -n 's,.*\(vd.\): unknown partition tab.*,\1,p' | tail -n 1`
52 VM_VOL_DEV=`ls -1 /dev/vd? | tail -n 1`
57 cloud_volume_detach_openstack() {
59 local VM_VOL_NAME="$2"
62 nova volume-show "$VM_VOL_NAME" | grep -q in-use || return 0
63 # umount seems not to be enough
65 if ! nova volume-detach "$VM_SERVER" "$VM_VOL_NAME"; then
66 echo "ERROR: nova detach of $VM_VOL_NAME failed." >&2
69 while nova volume-show "$VM_VOL_NAME" | grep -q availabe; do
75 vm_verify_options_openstack() {
77 if test -z "$OS_AUTH_URL" ; then
78 echo "ERROR: No openstack environment set. This vm-type works only inside of an openstack VM."
81 if test -z "$OBS_OPENSTACK_KERNEL_IMAGE_ID" ; then
82 echo "ERROR: No image refering to kernel and ramdisk is defined in OBS_OPENSTACK_KERNEL_IMAGE_ID env."
85 if test -z "$VM_VOLUME_NAME" ; then
86 echo "ERROR: No worker root VM volume name specified."
89 if test -z "$VM_VOLUME_SWAP" ; then
90 echo "ERROR: No worker swap VM volume name specified."
93 if test -z "$VM_SERVER" ; then
94 echo "ERROR: No VM server nod name specified (usually this instance)."
100 qemu_rootdev=/dev/vda
103 vm_attach_root_openstack() {
104 VM_IMAGE=`cloud_volume_attach_openstack "$VM_SERVER" "$VM_VOLUME_NAME" "$VM_IMAGE"`
105 test "${VM_IMAGE:0:5}" = "/dev/" || cleanup_and_exit 3
108 vm_attach_swap_openstack() {
109 VM_SWAP=`cloud_volume_attach_openstack "$VM_SERVER" "$VM_VOLUME_SWAP" "$VM_SWAP"`
110 test "${VM_SWAP:0:5}" = /dev/ || cleanup_and_exit 3
113 vm_detach_root_openstack() {
114 cloud_volume_detach_openstack "$VM_SERVER" "$VM_VOLUME_NAME"
117 vm_detach_swap_openstack() {
118 cloud_volume_detach_openstack "$VM_SERVER" "$VM_VOLUME_SWAP"
121 vm_cleanup_openstack() {
122 cloud_volume_detach_openstack "$VM_SERVER" "$VM_VOLUME_NAME"
123 cloud_volume_detach_openstack "$VM_SERVER" "$VM_VOLUME_SWAP"
126 vm_fixup_openstack() {
127 # No way to handle this via init= parameter here....
128 echo "#!/bin/sh" > "$BUILD_ROOT/sbin/init"
129 echo 'exec /.build/build "$@"' >> "$BUILD_ROOT/sbin/init"
130 chmod 0755 "$BUILD_ROOT/sbin/init"
133 vm_kill_openstack() {
134 if nova show "$VM_VOLUME_NAME" >/dev/null 2>&1 ; then
135 if ! nova delete "$VM_VOLUME_NAME" ; then
136 echo "could not kill openstack vm build $VM_VOLUME_NAME"
142 vm_startup_openstack() {
143 nova boot --image $OBS_OPENSTACK_KERNEL_IMAGE_ID --flavor m1.small --block_device_mapping vda=${VM_VOLUME_NAME}::$(( $VMDISK_ROOTSIZE / 1024 )):0 --block_device_mapping vdb=${VM_VOLUME_SWAP}::1:0 --poll "build-$VM_VOLUME_NAME" || cleanup_and_exit 3
144 nova console-log "build-$VM_VOLUME_NAME"