~canonical-platform-qa/ubuntu-qa-tools/practisubunit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/sh
#
# Copyright (C) 2007-2012 Canonical, Ltd.
# Author: Jamie Strandboge <jamie@canonical.com>
# License: GPLv3
#
set -e

# Set some defaults
vm_connect="qemu:///system"
vm_path="$HOME/vms/iso-testing"
vm_image_size="4"
vm_memory="512"
vm_iso_ndisks="1"
vm_iso_vcpus="1"
vm_iso_ostype="linux"
# Default to current release
vm_iso_osvariant="ubuntu"`lsb_release -c | awk '{print $2'} | sed 's/^./\u&/;'`

vm_iso_fully_allocate="no"              # only with qcow2
vm_iso_disk_format="qcow2"              # raw, qcow2, vmdk, etc
vm_iso_disk_cache="writethrough"        # none, writethrough, writeback

uqtvmiso="$HOME/.uqt-vm-iso.conf"
if [ -s "$uqtvmiso" ]; then
    . "$uqtvmiso"
fi

if [ ! -e "$vm_path" ]; then
    echo -n "Creating '$vm_path'... "
    mkdir "$vm_path" || {
        echo "ERROR: Could not create. Does it already exist?"
        exit 1
    }
    echo "done"
fi

if [ -z "$vm_root_size" ]; then
    if [ -n "$vm_image_size" ]; then
        vm_root_size=$((vm_image_size*1024))
    else
        echo "Could not determine disk size"
        exit 1
    fi
fi

# Find out our current directory
if [ -z "$UQT_VM_TOOLS" ]; then
    UQT_VM_TOOLS=$(dirname $(readlink -f $0))
fi

if [ "$USER" = "root" ]; then
    echo "You must not be root. Aborting"
    exit 1
fi

network="network:default"	# network:default give libvirt integration
prefix="test-iso-"
start="1"
num="1"
iso=""

usage() {
    echo "$ vm-iso <iso> -N name"
    echo "$ vm-iso <iso> -n name [<number>]"
    echo "$ vm-iso <iso> [<number> [<start number>]]"
    exit 1
}

if [ -z "$1" ]; then
    usage
    exit 1
fi

iso="$1"
shift
if [ ! -s "$iso" ]; then
    echo "'$iso' doesn't exist"
    exit 1
fi

# TODO: this argument processing is ridiculous. Use getopt.
iters="1"
no_suffix=""
if [ "$1" = "-n" ] || [ "$1" = "-N" ]; then
    if [ -z "$2" ] || [ "$1" = "-N" ] && [ -n "$3" ]; then
       usage
       exit 1
    fi
    if [ "$1" = "-N" ]; then
        no_suffix="yes"
    fi
    prefix="$2"
    shift 2
    if [ -z "$no_suffix" ]  && [ -n "$1" ]; then
        num="$1"
        shift
        end=$((${start}+${num}-1))
        iters=`seq 1 $end`
    fi
elif [ ! -z "$1" ]; then
    num="$1"
    shift
    if [ ! -z "$1" ]; then
        start="$1"
        shift || true
    fi
    end=$((${start}+${num}-1))
    iters=`seq $start $end`
fi

num_disks=1
if [ ! -z "$vm_iso_ndisks" ]; then
    num_disks=$(($vm_iso_ndisks))
fi

if [ -e "$uqtvmiso" ]; then
    echo "Create VM(s) with (using settings from '$uqtvmiso'):"
else
    echo "Create VM(s) with (using defaults):"
fi
if [ "$no_suffix" = "yes" ]; then
    echo "Name: $prefix"
else
    echo "Name prefix: $prefix"
    suffixes=`echo $iters | sed 's/ /,/g'`
    echo "Name suffixes: $suffixes"
fi
echo "Memory: ${vm_memory} (Required: desktop >= 384, server/alternate >= 256)"
echo "VCPUs: ${vm_iso_vcpus}"
echo "Disks: ${num_disks}"
echo "Disk size (per disk): ${vm_root_size}"
echo "Install directory: ${vm_path}"
echo "OS type: ${vm_iso_ostype}/${vm_iso_osvariant}"
echo -n "Continue (y/N)? "
read ans
if [ "$ans" != "y" ] && [ "$ans" != "Y" ]; then
    echo "Aborting"
    exit 0
fi

for i in ${iters} ; do
    name="${prefix}${i}"
    if [ "$no_suffix" = "yes" ]; then
        name="${prefix}"
    fi
    vmdir="${vm_path}/$name"
    mkdir $vmdir || {
        echo "Skipping '$name'. Could not create '$vmdir'. Does it"
        echo "already exist?"
        continue
    }

    disk_size=`echo "scale=2 ; ${vm_root_size} / 1024" | bc`
    disk_args=""
    if [ $num_disks -ge 1 ]; then
        for j in `seq 1 $num_disks` ; do
            disk_args="$disk_args --disk path=${vmdir}/disk${j}.img,size=$disk_size"
        done
    fi

    virt-install --connect=${vm_connect} --name=$name --ram=${vm_memory} --vcpus=${vm_iso_vcpus} $disk_args --accelerate --hvm --cdrom=$iso --os-type=${vm_iso_ostype} --os-variant=${vm_iso_osvariant} --vnc --network=$network &
    if [ "$vm_iso_fully_allocate" = "yes" ]; then
	# wait until the VM comes up before proceeding since there is heavy
	# disk IO creating these disks and if we fire off 5 iso tests at once,
        # we feel great pain
        count=0
        while [ "$count" -lt 30 ]; do
            if vm_exists "$name" && vm_running "$name" ; then
                break
            fi
            count=$(( $count + 1 ))
            sleep 5
        done
    fi
    sleep 5
done