Merge "Add new scenario NSPerf-RFC3511"
[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 set -e
27 set -x
28
29 die() {
30     echo "error: $1" >&2
31     exit 1
32 }
33
34 test $# -eq 1 -o $# -eq 2 || die "no image specific script as argument"
35 test $(id -u) -eq 0 || die "should invoke using sudo"
36
37 cmd=$1
38 RELEASE=$2
39 test -x $cmd
40 mountdir="/mnt/yardstick"
41 workspace=${WORKSPACE:-"/tmp/workspace/yardstick"}
42 host=${HOST:-"cloud-images.ubuntu.com"}
43 release=${RELEASE:-"xenial"}
44 boot_mode="disk1"
45 if [[ "${YARD_IMG_ARCH}" = "arm64" ]]; then
46     boot_mode="uefi1"
47 fi
48
49 image_path="${release}/current/${release}-server-cloudimg-${YARD_IMG_ARCH}-${boot_mode}.img"
50 image_url=${IMAGE_URL:-"https://${host}/${image_path}"}
51 sha256sums_path="${release}/current/SHA256SUMS"
52 sha256sums_url=${SHA256SUMS_URL:-"https://${host}/${sha256sums_path}"}
53
54 imgfile="${workspace}/yardstick-image.img"
55 raw_imgfile_basename="yardstick-${release}-server.raw"
56 raw_imgfile="${workspace}/${raw_imgfile_basename}"
57 filename=$(basename $image_url)
58
59 apt-get install -y parted
60
61 # download and checksum base image, conditionally if local copy is outdated
62 download() {
63     test -d $workspace || mkdir -p $workspace
64     cd $workspace
65     rm -f SHA256SUMS # always download the checksum file to a detect stale image
66     wget $sha256sums_url
67     test -e $filename || wget -nc --progress=dot:giga $image_url
68     grep $filename SHA256SUMS | sha256sum -c ||
69     if [ $? -ne 0 ]; then
70         rm $filename
71         wget -nc --progress=dot:giga $image_url
72         grep $filename SHA256SUMS | sha256sum -c
73     fi
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     qemu-img convert $filename $raw_imgfile
80     cd -
81 }
82
83 # mount image
84 setup() {
85     # qemu-img resize $raw_imgfile +5GB
86     mkdir -p $mountdir
87
88     loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
89
90     kpartx -av $raw_imgfile
91
92     # for trouble shooting
93     sleep 2
94     dmsetup ls
95     parted -l /dev/${loopdevice:0:5} || true
96     mount /dev/mapper/$loopdevice $mountdir
97     mount -t proc none $mountdir/proc
98
99     cp $cmd $mountdir/$(basename $cmd)
100     if [ "${YARD_IMG_ARCH}" = "arm64" ]; then
101         cp /usr/bin/qemu-aarch64-static $mountdir/usr/bin
102     fi
103 }
104
105 # modify image running a script using in a chrooted environment
106 modify() {
107     # resolv.conf does not exist in base image, pass nameserver value from host
108     nameserver_ip=$(grep -m 1 '^nameserver' \
109         /etc/resolv.conf | awk '{ print $2 '})
110
111     # prevent init scripts from running during install
112     echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
113     chmod a+x $mountdir/usr/sbin/policy-rc.d
114
115     chroot $mountdir /$(basename $cmd) $nameserver_ip
116
117     rm -rf $mountdir/usr/sbin/policy-rc.d
118
119     umount -f $mountdir/proc
120     umount $mountdir
121
122     qemu-img convert -c -o compat=0.10 -O qcow2 $raw_imgfile $imgfile
123
124     if dmsetup table | grep $loopdevice; then
125        dmsetup clear $loopdevice || true
126     fi
127 }
128
129 # cleanup (umount) the image
130 cleanup() {
131     # designed to be idempotent
132     mount | grep $mountdir/proc && umount $mountdir/proc
133     mount | grep $mountdir && umount $mountdir
134     mount | grep "/mnt/${release}" && umount "/mnt/${release}"
135
136     if [ -f "${raw_imgfile}" ]; then
137         #kpartx -dv $raw_imgfile sometimes failed, we should checked it agein.
138         #if [ -z "$(kpartx -l $raw_imgfile | grep 'loop deleted')" ]; then
139         #    kpartx -dv $raw_imgfile
140         #fi
141         kpartx -dv $raw_imgfile || true
142     fi
143
144     rm -f $raw_imgfile
145     rm -rf $mountdir
146 }
147
148 exitcode=""
149 error_trap()
150 {
151     local rc=$?
152
153     set +e
154
155     if [ -z "$exitcode" ]; then
156         exitcode=$rc
157     fi
158
159     dmesg -T | tail -50
160
161     cleanup
162
163     echo "Image build failed with $exitcode"
164
165     exit $exitcode
166 }
167
168 main() {
169     cleanup
170
171     trap "error_trap" EXIT SIGTERM
172
173     download
174     setup
175     modify
176
177     trap - EXIT SIGTERM
178     cleanup
179
180     echo "the modified image is found here: $imgfile"
181 }
182
183 main
184