Script to create the guest image
[kvmfornfv.git] / ci / envs / guest-modify.sh
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 # This is copy from yardstick-img-modify on yardstick project. Currently 
13 # yardstick script only ubuntu image, and this one is more for CentOS.
14 # Example invocation:
15 # yardstick-img-modify /home/yardstick/tools/ubuntu-server-cloudimg-modify.sh
16 #
17 # Warning: the script will create files by default in:
18 #   /tmp/workspace/yardstick
19 # the files will be owned by root!
20 #
21 # TODO: image resize is needed if the base image is too small
22 #
23
24 set -e
25 set -x
26
27 die() {
28     echo "error: $1" >&2
29     exit 1
30 }
31
32 usage () {
33     echo "$0 cmd workspace"
34     exit 1
35 }
36
37 test $# -eq 2 || usage
38 test $(id -u) -eq 0 || die "should invoke using sudo"
39
40 ROOTDIR=$(cd $(dirname "$0")/../.. && pwd)
41 cmd=$1
42 test -x $cmd
43 workspace=$2
44 mountdir=`mktemp -d`
45
46 image_url=${IMAGE_URL:-"http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-1510.qcow2"}
47 md5sums_url=${MD5SUMS_URL:-"http://cloud.centos.org/centos/7/images/sha256sum.txt"}
48
49 imgfile="${workspace}/guest.img"
50 raw_imgfile="${workspace}/guest.raw"
51 filename=$(basename $image_url)
52 md5filename=$(basename $md5sums_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 $md5filename # always download the checksum file to a detect stale image
59     wget $md5sums_url
60     test -e $filename || wget -nc $image_url
61     grep "$filename\$" $md5filename |sha256sum -c
62     if [ $? -ne 0 ]; then
63         rm $filename
64         wget -nc $image_url
65         grep $filename $md5filename | md5sum -c
66     fi
67     rm -rf $raw_imgfile
68     qemu-img convert $filename $raw_imgfile
69     cd -
70 }
71
72 # mount image
73 setup() {
74     mkdir -p $mountdir
75
76     loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
77
78     kpartx -a $raw_imgfile
79     # No idea why need this sleep
80     sleep 3
81     mount /dev/mapper/$loopdevice $mountdir
82
83     cp $cmd "$mountdir/"
84 }
85
86 # modify image running a script using in a chrooted environment
87 modify() {
88     # Add the ssh key to the image
89     mkdir -p ${mountdir}/root/.ssh
90     cp ${ROOTDIR}/ci/envs/kvm4nfv_key.pub ${mountdir}/root/.ssh/authorized_keys
91     chmod 700 ${mountdir}/root/.ssh
92     chmod 600 ${mountdir}/root/.ssh/authorized_keys
93     
94
95     umount $mountdir
96
97     qemu-img convert -O qcow2 $raw_imgfile $imgfile
98 }
99
100 # cleanup (umount) the image
101 cleanup() {
102     # designed to be idempotent
103     mount | grep $mountdir && umount $mountdir
104     kpartx -d $raw_imgfile || true
105     rm -f $raw_imgfile
106     rm -rf $mountdir
107 }
108
109 exitcode=""
110 error_trap()
111 {
112     local rc=$?
113
114     set +e
115
116     if [ -z "$exitcode" ]; then
117         exitcode=$rc
118     fi
119
120     cleanup
121
122     echo "Image build failed with $exitcode"
123
124     exit $exitcode
125 }
126
127 main() {
128     cleanup
129
130     trap "error_trap" EXIT SIGTERM
131
132     download
133     setup
134     modify
135
136     trap - EXIT SIGTERM
137     cleanup
138
139     echo "the modified image is found here: $imgfile"
140 }
141
142 main