3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
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 ##############################################################################
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.
15 # yardstick-img-modify /home/yardstick/tools/ubuntu-server-cloudimg-modify.sh
17 # Warning: the script will create files by default in:
18 # /tmp/workspace/yardstick
19 # the files will be owned by root!
21 # TODO: image resize is needed if the base image is too small
33 echo "$0 cmd workspace"
37 test $# -eq 2 || usage
38 test $(id -u) -eq 0 || die "should invoke using sudo"
40 ROOTDIR=$(cd $(dirname "$0")/../.. && pwd)
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"}
49 imgfile="${workspace}/guest.img"
50 raw_imgfile="${workspace}/guest.raw"
51 filename=$(basename $image_url)
52 md5filename=$(basename $md5sums_url)
54 # download and checksum base image, conditionally if local copy is outdated
56 test -d $workspace || mkdir -p $workspace
58 rm -f $md5filename # always download the checksum file to a detect stale image
60 test -e $filename || wget -nc $image_url
61 grep "$filename\$" $md5filename |sha256sum -c
65 grep $filename $md5filename | md5sum -c
68 qemu-img convert $filename $raw_imgfile
76 loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
78 kpartx -a $raw_imgfile
79 # No idea why need this sleep
81 mount /dev/mapper/$loopdevice $mountdir
86 # modify image running a script using in a chrooted environment
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
97 qemu-img convert -O qcow2 $raw_imgfile $imgfile
100 # cleanup (umount) the image
102 # designed to be idempotent
103 mount | grep $mountdir && umount $mountdir
104 kpartx -d $raw_imgfile || true
116 if [ -z "$exitcode" ]; then
122 echo "Image build failed with $exitcode"
130 trap "error_trap" EXIT SIGTERM
139 echo "the modified image is found here: $imgfile"