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