bottlenecks create stack image
[bottlenecks.git] / utils / tools / bottlenecks_image_modify
1 #!/bin/bash
2
3 ##############################################################################
4 # Copyright (c) 2017 Huawei Technologies Co.,Ltd 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 # bottlenecks: this file is copied from yardstick and slightly modified
11 ##############################################################################
12
13 # bottlenecks-img-modify - download and modify a Ubuntu cloud image
14 #
15 # The actual customization is done by a script passed with an absolute path as
16 # the only single argument. The command needs to be invoked as sudo
17 #
18 # Example invocation:
19 # bottlenecks-img-modify /home/opnfv/bottlenecks/utils/tools/ubuntu-server-cloudimg-modify.sh
20 #
21 # Warning: the script will create files by default in:
22 #   /tmp/workspace/bottlenecks
23 # the files will be owned by root!
24 #
25 # TODO: image resize is needed if the base image is too small
26 #
27
28 set -ex
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/bottlenecks"
42 workspace=${WORKSPACE:-"/tmp/workspace/bottlenecks"}
43 host=${HOST:-"cloud-images.ubuntu.com"}
44 release=${RELEASE:-"xenial"}
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}/bottlenecks-image.img"
51 raw_imgfile="${workspace}/bottlenecks-${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     qemu-img convert $filename $raw_imgfile
73     cd -
74 }
75
76 # mount image
77 setup() {
78     # qemu-img resize $raw_imgfile +5GB
79     mkdir -p $mountdir
80
81     loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
82
83     kpartx -av $raw_imgfile
84     # for trouble shooting
85     sleep 2
86     dmsetup ls
87     fdisk -l /dev/${loopdevice:0:5} || true
88     mount /dev/mapper/$loopdevice $mountdir
89     mount -t proc none $mountdir/proc
90
91     cp $cmd $mountdir/$(basename $cmd)
92 }
93
94 # modify image running a script using in a chrooted environment
95 modify() {
96     # resolv.conf does not exist in base image, pass nameserver value from host
97     nameserver_ip=$(grep -m 1 '^nameserver' \
98         /etc/resolv.conf | awk '{ print $2 '})
99
100     # prevent init scripts from running during install
101     echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
102     chmod a+x $mountdir/usr/sbin/policy-rc.d
103
104     chroot $mountdir /$(basename $cmd) $nameserver_ip
105
106     rm -rf $mountdir/usr/sbin/policy-rc.d
107
108     umount -f $mountdir/proc
109     umount $mountdir
110
111     qemu-img convert -c -o compat=0.10 -O qcow2 $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     mount | grep "/mnt/vivid" && umount "/mnt/vivid"
124
125     if [ -f $raw_imgfile ]; then
126         #kpartx -dv $raw_imgfile sometimes failed, we should checked it agein.
127         #if [ -z "$(kpartx -l $raw_imgfile | grep 'loop deleted')" ]; then
128         #    kpartx -dv $raw_imgfile
129         #fi
130         kpartx -dv $raw_imgfile || true
131     fi
132
133     rm -f $raw_imgfile
134     rm -rf $mountdir
135 }
136
137 exitcode=""
138 error_trap()
139 {
140     local rc=$?
141
142     set +e
143
144     if [ -z "$exitcode" ]; then
145         exitcode=$rc
146     fi
147
148     dmesg -T | tail -50
149
150     cleanup
151
152     echo "Image build failed with $exitcode"
153
154     exit $exitcode
155 }
156
157 main() {
158     cleanup
159
160     trap "error_trap" EXIT SIGTERM
161
162     download
163     setup
164     modify
165
166     trap - EXIT SIGTERM
167     cleanup
168
169     echo "the modified image is found here: $imgfile"
170 }
171
172 main