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 # yardstick-img-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-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
34 test $# -eq 1 -o $# -eq 2 || die "no image specific script as argument"
35 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:-"xenial"}
45 if [[ "${YARD_IMG_ARCH}" = "arm64" ]]; then
49 image_path="${release}/current/${release}-server-cloudimg-${YARD_IMG_ARCH}-${boot_mode}.img"
50 image_url=${IMAGE_URL:-"https://${host}/${image_path}"}
51 md5sums_path="${release}/current/MD5SUMS"
52 md5sums_url=${MD5SUMS_URL:-"https://${host}/${md5sums_path}"}
54 imgfile="${workspace}/yardstick-image.img"
55 raw_imgfile_basename="yardstick-${release}-server.raw"
56 raw_imgfile="${workspace}/${raw_imgfile_basename}"
57 filename=$(basename $image_url)
59 apt-get install -y parted
61 # download and checksum base image, conditionally if local copy is outdated
63 test -d $workspace || mkdir -p $workspace
65 rm -f MD5SUMS # always download the checksum file to a detect stale image
67 test -e $filename || wget -nc --progress=dot:giga $image_url
68 grep $filename MD5SUMS | md5sum -c ||
71 wget -nc --progress=dot:giga $image_url
72 grep $filename MD5SUMS | md5sum -c
75 for i in $(seq 0 9); do
76 [ -a /dev/loop$i ] || mknod -m 660 /dev/loop$i b 7 $i
79 if [[ "${YARD_IMG_ARCH}" = "arm64" && "$release" = "vivid" ]]; then
81 if [ ! -f /tmp/${release}-server-cloudimg-${YARD_IMG_ARCH}-kernel-info.txt ]; then
82 wget http://${host}/${release}/current/${release}-server-cloudimg-${YARD_IMG_ARCH}-kernel-info.txt
84 export CLOUD_KERNEL_VERSION=$(cut -d$'\t' -f4 ${release}-server-cloudimg-${YARD_IMG_ARCH}-kernel-info.txt)
86 mkdir -p /tmp/${release}-modules
87 if [ ! -f "/tmp/${release}-server-cloudimg-${YARD_IMG_ARCH}.tar.gz" ]; then
90 if [ ! -f "/tmp/${release}-server-cloudimg-${YARD_IMG_ARCH}.img" ]; then
91 tar zxvf ${release}-server-cloudimg-${YARD_IMG_ARCH}.tar.gz ${release}-server-cloudimg-${YARD_IMG_ARCH}.img
93 mkdir -p /mnt/${release}
94 mount /tmp/${release}-server-cloudimg-${YARD_IMG_ARCH}.img /mnt/${release}
95 cp -r /mnt/${release}/lib/modules/$(echo $CLOUD_KERNEL_VERSION) /tmp/${release}-modules
96 umount /mnt/${release}
97 rm /tmp/${release}-server-cloudimg-${YARD_IMG_ARCH}.img
100 qemu-img convert $filename $raw_imgfile
106 # qemu-img resize $raw_imgfile +5GB
107 if [ "${YARD_IMG_ARCH}" = "arm64" ]; then
108 echo -e "d\nn\np\n1\n\n\nw" | parted -l $raw_imgfile
112 loopdevice=$(kpartx -l $raw_imgfile | head -1 | cut -f1 -d ' ')
114 kpartx -av $raw_imgfile
116 if [[ "${YARD_IMG_ARCH}" = "arm64" && "$release" = "vivid" ]]; then
117 e2fsck -p -f /dev/mapper/$loopdevice
118 resize2fs /dev/mapper/$loopdevice
120 # for trouble shooting
123 parted -l /dev/${loopdevice:0:5} || true
124 mount /dev/mapper/$loopdevice $mountdir
125 mount -t proc none $mountdir/proc
127 if [[ "${YARD_IMG_ARCH}" = "arm64" && "$release" = "vivid" ]]; then
128 cp -r /tmp/${release}-modules/$(echo $CLOUD_KERNEL_VERSION) "$mountdir/lib/modules"
129 cp $(which "qemu-aarch64-static") "$mountdir/usr/bin"
131 cp $cmd $mountdir/$(basename $cmd)
134 # modify image running a script using in a chrooted environment
136 # resolv.conf does not exist in base image, pass nameserver value from host
137 nameserver_ip=$(grep -m 1 '^nameserver' \
138 /etc/resolv.conf | awk '{ print $2 '})
140 # prevent init scripts from running during install
141 echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
142 chmod a+x $mountdir/usr/sbin/policy-rc.d
144 chroot $mountdir /$(basename $cmd) $nameserver_ip
146 rm -rf $mountdir/usr/sbin/policy-rc.d
148 umount -f $mountdir/proc
151 qemu-img convert -c -o compat=0.10 -O qcow2 $raw_imgfile $imgfile
153 if dmsetup table | grep $loopdevice; then
154 dmsetup clear $loopdevice || true
158 # cleanup (umount) the image
160 # designed to be idempotent
161 mount | grep $mountdir/proc && umount $mountdir/proc
162 mount | grep $mountdir && umount $mountdir
163 mount | grep "/mnt/${release}" && umount "/mnt/${release}"
165 if [ -f "${raw_imgfile}" ]; then
166 #kpartx -dv $raw_imgfile sometimes failed, we should checked it agein.
167 #if [ -z "$(kpartx -l $raw_imgfile | grep 'loop deleted')" ]; then
168 # kpartx -dv $raw_imgfile
170 kpartx -dv $raw_imgfile || true
184 if [ -z "$exitcode" ]; then
192 echo "Image build failed with $exitcode"
200 trap "error_trap" EXIT SIGTERM
209 echo "the modified image is found here: $imgfile"