Fix for netperf install in glance image
[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
29 die() {
30     echo "error: $1" >&2
31     exit 1
32 }
33
34 test $# -eq 1 || die "no image specific script as argument"
35 test $(id -u) -eq 0 || die "should invoke using sudo"
36
37 cmd=$1
38 test -x $cmd
39 mountdir="/mnt/yardstick"
40
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-amd64-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 $image_url
60     grep $filename MD5SUMS | md5sum -c ||
61     if [ $? -ne 0 ]; then
62         rm $filename
63         wget -nc $image_url
64         grep $filename MD5SUMS | md5sum -c
65     fi
66     qemu-img convert $filename $raw_imgfile
67     cd -
68 }
69
70 # mount image
71 setup() {
72     mkdir -p $mountdir
73
74     for i in $(seq 0 9); do
75         [ -a /dev/loop$i ] || mknod -m 660 /dev/loop$i b 7 $i
76     done
77
78     loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
79
80     kpartx -a $raw_imgfile
81
82     mount /dev/mapper/$loopdevice $mountdir
83     mount -t proc none $mountdir/proc
84
85     cp $cmd $mountdir/$(basename $cmd)
86 }
87
88 # modify image running a script using in a chrooted environment
89 modify() {
90     # resolv.conf does not exist in base image, pass nameserver value from host
91     nameserver_ip=$(grep -m 1 '^nameserver' \
92         /etc/resolv.conf | awk '{ print $2 '})
93
94     # prevent init scripts from running during install
95     echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
96     chmod a+x $mountdir/usr/sbin/policy-rc.d
97
98     chroot $mountdir /$(basename $cmd) $nameserver_ip
99
100     rm -rf $mountdir/usr/sbin/policy-rc.d
101
102     umount -f $mountdir/proc
103     umount $mountdir
104
105     qemu-img convert -c -o compat=0.10 -O qcow2 $raw_imgfile $imgfile
106 }
107
108 # cleanup (umount) the image
109 cleanup() {
110     # designed to be idempotent
111     mount | grep $mountdir/proc && umount $mountdir/proc
112     mount | grep $mountdir && umount $mountdir
113     if [ -f $raw_imgfile ]; then
114         kpartx -d $raw_imgfile || true
115     fi
116     rm -f $raw_imgfile
117     rm -rf $mountdir
118 }
119
120 set -x
121
122 main() {
123     cleanup
124     download
125     setup
126     modify
127     cleanup
128
129     echo "the modified image is found here: $imgfile"
130 }
131
132 main
133