This patch is used to add grafana config to opnfv dashboard for
[yardstick.git] / tools / yardstick-img-dpdk-modify
1 #!/bin/bash
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # yardstick-img-dpdk-modify - download and modify a Ubuntu cloud image
13 #
14 # The actual customization is done by a script passed with an absolute path as
15 # the only single argument. The command needs to be invoked as sudo
16 #
17 # Example invocation:
18 # yardstick-img-dpdk-modify /home/yardstick/tools/ubuntu-server-cloudimg-dpdk-modify.sh
19 #
20 # Warning: the script will create files by default in:
21 #   /tmp/workspace/yardstick
22 # the files will be owned by root!
23 #
24 # TODO: image resize is needed if the base image is too small
25 #
26
27 set -e
28 set -x
29
30 die() {
31     echo "error: $1" >&2
32     exit 1
33 }
34
35 test $# -eq 1 || die "no image specific script as argument"
36 test $(id -u) -eq 0 || die "should invoke using sudo"
37
38 cmd=$1
39 test -x $cmd
40 mountdir="/mnt/yardstick"
41
42 workspace=${WORKSPACE:-"/tmp/workspace/yardstick"}
43 host=${HOST:-"cloud-images.ubuntu.com"}
44 release=${RELEASE:-"wily"}
45 image_path="${release}/current/${release}-server-cloudimg-amd64-disk1.img"
46 image_url=${IMAGE_URL:-"https://${host}/${image_path}"}
47 md5sums_path="${release}/current/MD5SUMS"
48 md5sums_url=${MD5SUMS_URL:-"https://${host}/${md5sums_path}"}
49
50 imgfile="${workspace}/yardstick-${release}-server"
51 raw_imgfile="${workspace}/yardstick-${release}-server.raw"
52 filename=$(basename $image_url)
53
54 # download and checksum base image, conditionally if local copy is outdated
55 download() {
56     test -d $workspace || mkdir -p $workspace
57     cd $workspace
58     rm -f MD5SUMS # always download the checksum file to a detect stale image
59     wget $md5sums_url
60     test -e $filename || wget -nc $image_url
61     grep $filename MD5SUMS | md5sum -c ||
62     if [ $? -ne 0 ]; then
63         rm $filename
64         wget -nc $image_url
65         grep $filename MD5SUMS | md5sum -c
66     fi
67     qemu-img convert $filename $raw_imgfile
68     cd -
69 }
70
71 # mount image
72 setup() {
73     mkdir -p $mountdir
74
75     for i in $(seq 0 9); do
76         [ -a /dev/loop$i ] || mknod -m 660 /dev/loop$i b 7 $i
77     done
78
79     loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
80
81     kpartx -a $raw_imgfile
82
83     mount /dev/mapper/$loopdevice $mountdir
84     mount -t proc none $mountdir/proc
85
86     echo $loopdevice
87
88     sudo resize2fs /dev/mapper/$loopdevice
89
90     cp $cmd $mountdir/$(basename $cmd)
91 }
92
93 # modify image running a script using in a chrooted environment
94 modify() {
95     # resolv.conf does not exist in base image, pass nameserver value from host
96     nameserver_ip=$(grep -m 1 '^nameserver' \
97         /etc/resolv.conf | awk '{ print $2 '})
98
99     # prevent init scripts from running during install
100     echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
101     chmod a+x $mountdir/usr/sbin/policy-rc.d
102
103     chroot $mountdir /$(basename $cmd) $nameserver_ip
104
105     rm -rf $mountdir/usr/sbin/policy-rc.d
106
107     umount -f $mountdir/proc
108     umount $mountdir
109
110     qemu-img convert -c -o compat=0.10 -O qcow2 $raw_imgfile $imgfile
111 #    qemu-img convert -O vmdk $raw_imgfile $imgfile
112
113     if dmsetup table | grep $loopdevice; then
114        dmsetup clear $loopdevice || true
115     fi
116 }
117
118 # cleanup (umount) the image
119 cleanup() {
120     # designed to be idempotent
121     mount | grep $mountdir/proc && umount $mountdir/proc
122     mount | grep $mountdir && umount $mountdir
123     if [ -f $raw_imgfile ]; then
124         kpartx -dv $raw_imgfile || true
125     fi
126     rm -f $raw_imgfile
127     rm -rf $mountdir
128 }
129
130 exitcode=""
131 error_trap()
132 {
133     local rc=$?
134
135     set +e
136
137     if [ -z "$exitcode" ]; then
138         exitcode=$rc
139     fi
140
141     cleanup
142
143     echo "Image build failed with $exitcode"
144
145     exit $exitcode
146 }
147
148 main() {
149     cleanup
150
151     trap "error_trap" EXIT SIGTERM
152
153     download
154     setup
155     modify
156     trap - EXIT SIGTERM
157     cleanup
158
159     echo "the modified image is found here: $imgfile"
160 }
161
162 main