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