3 ##############################################################################
4 # Copyright (c) 2016 Huawei Technologies Co.,Ltd 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 # yardstick-img-lxd-modify - download and modify a Ubuntu cloud image
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
18 # yardstick-img-lxd-modify /home/yardstick/tools/ubuntu-server-cloudimg-modify.sh
20 # Warning: the script will create files by default in:
21 # /tmp/workspace/yardstick
22 # the files will be owned by root!
24 # TODO: image resize is needed if the base image is too small
35 test $# -eq 1 || die "no image specific script as argument"
36 test $(id -u) -eq 0 || die "should invoke using sudo"
40 mountdir="/mnt/yardstick"
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-root.tar.gz"
45 image_url=${IMAGE_URL:-"https://${host}/${image_path}"}
46 md5sums_path="${release}/current/MD5SUMS"
47 md5sums_url=${MD5SUMS_URL:-"https://${host}/${md5sums_path}"}
49 imgfile="${workspace}/yardstick-${release}-server.tar.gz"
50 filename=$(basename $image_url)
52 # download and checksum base image, conditionally if local copy is outdated
54 test -d $workspace || mkdir -p $workspace
56 rm -f MD5SUMS # always download the checksum file to a detect stale image
58 test -e $filename || wget -nc --progress=dot:giga $image_url
59 grep $filename MD5SUMS | md5sum -c ||
62 wget -nc --progress=dot:giga $image_url
63 grep $filename MD5SUMS | md5sum -c
72 cp $cmd $mountdir/$(basename $cmd)
75 tar zxvf $filename -C $mountdir
78 # modify image running a script using in a chrooted environment
80 # resolv.conf does not exist in base image, pass nameserver value from host
81 nameserver_ip=$(grep -m 1 '^nameserver' \
82 /etc/resolv.conf | awk '{ print $2 '})
84 # prevent init scripts from running during install
85 echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
86 chmod a+x $mountdir/usr/sbin/policy-rc.d
88 chroot $mountdir /$(basename $cmd) $nameserver_ip
90 rm -rf $mountdir/usr/sbin/policy-rc.d
92 tar zcvf $(basename $imgfile) $mountdir/
107 if [ -z "$exitcode" ]; then
115 echo "Image build failed with $exitcode"
123 trap "error_trap" EXIT SIGTERM
132 echo "the modified image is found here: $imgfile"