Fix kvm support in Ubuntu
[tools/build.git] / build-vm-openstack
1 #
2 # Openstack specific functions
3 #
4 ################################################################
5 #
6 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
7 #
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.
11 #
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.
16 #
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
21 #
22 ################################################################
23
24 cloud_volume_attach_openstack() {
25     local VM_SERVER="$1"
26     local VM_VOL_NAME="$2"
27     local VM_VOL_DEV="$3"
28
29     if ! nova volume-attach "$VM_SERVER" "$VM_VOL_NAME" "$VM_VOL_DEV"; then
30         echo "ERROR: nova attach failed. $?" >&2
31         return 3
32     fi
33     while true; do
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
38             return 3
39         fi
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
44                 return 3
45             fi
46         fi
47         sleep 3
48     done
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`
53     fi
54     echo "$VM_VOL_DEV"
55 }
56
57 cloud_volume_detach_openstack() {
58     local VM_SERVER="$1"
59     local VM_VOL_NAME="$2"
60
61     # needed at all?
62     nova volume-show "$VM_VOL_NAME" | grep -q in-use || return 0
63     # umount seems not to be enough
64     sync
65     if ! nova volume-detach "$VM_SERVER" "$VM_VOL_NAME"; then
66         echo "ERROR: nova detach of $VM_VOL_NAME failed." >&2
67         return 3
68     fi
69     while nova volume-show "$VM_VOL_NAME" | grep -q availabe; do
70         sleep 3
71     done
72     return 0
73 }
74
75 vm_verify_options_openstack() {
76     # verify settings
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."
79         cleanup_and_exit 3
80     fi
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."
83         cleanup_and_exit 3
84     fi
85     if test -z "$VM_VOLUME_NAME" ; then
86         echo "ERROR: No worker root VM volume name specified."
87         cleanup_and_exit 3
88     fi
89     if test -z "$VM_VOLUME_SWAP" ; then
90         echo "ERROR: No worker swap VM volume name specified."
91         cleanup_and_exit 3
92     fi
93     if test -z "$VM_SERVER" ; then
94         echo "ERROR: No VM server nod name specified (usually this instance)."
95         cleanup_and_exit 3
96     fi
97
98     # XXX why here?
99     VM_SWAPDEV=/dev/vdb
100     qemu_rootdev=/dev/vda
101 }
102
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
106 }
107
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
111 }
112
113 vm_detach_root_openstack() {
114     cloud_volume_detach_openstack "$VM_SERVER" "$VM_VOLUME_NAME"
115 }
116
117 vm_detach_swap_openstack() {
118     cloud_volume_detach_openstack "$VM_SERVER" "$VM_VOLUME_SWAP"
119 }
120
121 vm_cleanup_openstack() {
122     cloud_volume_detach_openstack "$VM_SERVER" "$VM_VOLUME_NAME"
123     cloud_volume_detach_openstack "$VM_SERVER" "$VM_VOLUME_SWAP"
124 }
125
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"
131 }
132
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"
137             cleanup_and_exit 1
138         fi
139     fi
140 }
141
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"
145 }