This patch is used to add grafana config to opnfv dashboard for
[yardstick.git] / tools / yardstick-img-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-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-modify /home/yardstick/tools/ubuntu-server-cloudimg-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 workspace=${WORKSPACE:-"/tmp/workspace/yardstick"}
42 host=${HOST:-"cloud-images.ubuntu.com"}
43 release=${RELEASE:-"trusty"}
44 image_path="${release}/current/${release}-server-cloudimg-${YARD_IMG_ARCH}-disk1.img"
45 image_url=${IMAGE_URL:-"https://${host}/${image_path}"}
46 md5sums_path="${release}/current/MD5SUMS"
47 md5sums_url=${MD5SUMS_URL:-"https://${host}/${md5sums_path}"}
48
49 imgfile="${workspace}/yardstick-${release}-server.img"
50 raw_imgfile="${workspace}/yardstick-${release}-server.raw"
51 filename=$(basename $image_url)
52
53 # download and checksum base image, conditionally if local copy is outdated
54 download() {
55     test -d $workspace || mkdir -p $workspace
56     cd $workspace
57     rm -f MD5SUMS # always download the checksum file to a detect stale image
58     wget $md5sums_url
59     test -e $filename || wget -nc --progress=dot:giga $image_url
60     grep $filename MD5SUMS | md5sum -c ||
61     if [ $? -ne 0 ]; then
62         rm $filename
63         wget -nc --progress=dot:giga $image_url
64         grep $filename MD5SUMS | md5sum -c
65     fi
66
67     for i in $(seq 0 9); do
68         [ -a /dev/loop$i ] || mknod -m 660 /dev/loop$i b 7 $i
69     done
70
71     if [ $YARD_IMG_ARCH = "arm64" ]; then
72         cd /tmp
73         if [ ! -f /tmp/vivid-server-cloudimg-arm64-kernel-info.txt ]; then
74             wget http://cloud-images.ubuntu.com/vivid/current/vivid-server-cloudimg-arm64-kernel-info.txt
75         fi
76         export VIVID_KERNEL_VERSION=$(cut -d$'\t' -f4 vivid-server-cloudimg-arm64-kernel-info.txt)
77         mkdir -p /tmp/vivid-modules
78         if [ ! -f "/tmp/vivid-server-cloudimg-arm64.tar.gz" ]; then
79             wget $VIVID_IMG_URL
80         fi
81         if [ ! -f "/tmp/vivid-server-cloudimg-arm64.img" ]; then
82             tar zxvf vivid-server-cloudimg-arm64.tar.gz vivid-server-cloudimg-arm64.img
83         fi
84         mkdir -p /mnt/vivid
85         mount /tmp/vivid-server-cloudimg-arm64.img /mnt/vivid
86         cp -r /mnt/vivid/lib/modules/$(echo $VIVID_KERNEL_VERSION | cut -d'-' -f3,4,5) /tmp/vivid-modules
87         umount /mnt/vivid
88         rm /tmp/vivid-server-cloudimg-arm64.img
89         cd $workspace
90     fi
91     qemu-img convert $filename $raw_imgfile
92     cd -
93 }
94
95 # mount image
96 setup() {
97     # qemu-img resize $raw_imgfile +5GB
98     if [ $YARD_IMG_ARCH = "arm64" ]; then
99         echo -e "d\nn\np\n1\n\n\nw" | fdisk $raw_imgfile
100     fi
101     mkdir -p $mountdir
102
103     loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
104
105     kpartx -av $raw_imgfile
106     if [ $YARD_IMG_ARCH = "arm64" ]; then
107         e2fsck -p -f /dev/mapper/$loopdevice
108         resize2fs /dev/mapper/$loopdevice
109     fi
110     # for trouble shooting
111     sleep 2
112     dmsetup ls
113     fdisk -l /dev/${loopdevice:0:5} || true
114     mount /dev/mapper/$loopdevice $mountdir
115     mount -t proc none $mountdir/proc
116
117     if [ $YARD_IMG_ARCH = "arm64" ]; then
118         cp -r /tmp/vivid-modules/$(echo $VIVID_KERNEL_VERSION | cut -d'-' -f3,4,5) "$mountdir/lib/modules"
119         cp $(which "qemu-aarch64-static") "$mountdir/usr/bin"
120     fi
121     cp $cmd $mountdir/$(basename $cmd)
122 }
123
124 # modify image running a script using in a chrooted environment
125 modify() {
126     # resolv.conf does not exist in base image, pass nameserver value from host
127     nameserver_ip=$(grep -m 1 '^nameserver' \
128         /etc/resolv.conf | awk '{ print $2 '})
129
130     # prevent init scripts from running during install
131     echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
132     chmod a+x $mountdir/usr/sbin/policy-rc.d
133
134     chroot $mountdir /$(basename $cmd) $nameserver_ip
135
136     rm -rf $mountdir/usr/sbin/policy-rc.d
137
138     umount -f $mountdir/proc
139     umount $mountdir
140
141     qemu-img convert -c -o compat=0.10 -O qcow2 $raw_imgfile $imgfile
142
143     if dmsetup table | grep $loopdevice; then
144        dmsetup clear $loopdevice || true
145     fi
146 }
147
148 # cleanup (umount) the image
149 cleanup() {
150     # designed to be idempotent
151     mount | grep $mountdir/proc && umount $mountdir/proc
152     mount | grep $mountdir && umount $mountdir
153     mount | grep "/mnt/vivid" && umount "/mnt/vivid"
154     if [ -f $raw_imgfile ]; then
155         kpartx -dv $raw_imgfile || true
156     fi
157     rm -f $raw_imgfile
158     rm -rf $mountdir
159 }
160
161 exitcode=""
162 error_trap()
163 {
164     local rc=$?
165
166     set +e
167
168     if [ -z "$exitcode" ]; then
169         exitcode=$rc
170     fi
171
172     dmesg -T | tail -50
173
174     cleanup
175
176     echo "Image build failed with $exitcode"
177
178     exit $exitcode
179 }
180
181 main() {
182     cleanup
183
184     trap "error_trap" EXIT SIGTERM
185
186     download
187     setup
188     modify
189
190     trap - EXIT SIGTERM
191     cleanup
192
193     echo "the modified image is found here: $imgfile"
194 }
195
196 main
197